Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// separate the Base64 string from the security signature.
const SignatureSeparator = "_"

// URLValuesSliceKeySuffix is the character that is used to
// urlValuesSliceKeySuffix is the character that is used to
// specify a suffix for slices parsed by URLValues.
// If the suffix is set to "[i]", then the index of the slice
// is used in place of i
Expand All @@ -29,6 +29,14 @@ const (
URLValuesSliceKeySuffixIndex = "[i]"
)

// URLValuesSliceKeySuffix returns the character that is used to
// specify a suffix for slices parsed by URLValues.
//
// The default suffix is "[]".
func URLValuesSliceKeySuffix() string {
return urlValuesSliceKeySuffix
}

// SetURLValuesSliceKeySuffix sets the character that is used to
// specify a suffix for slices parsed by URLValues.
// If the suffix is set to "[i]", then the index of the slice
Expand Down Expand Up @@ -110,12 +118,25 @@ func cleanUpMapArray(in []Map) []Map {
return result
}

func cleanUpInterfaceMapArray(in []map[any]any) []Map {
if in == nil {
return nil
}
result := make([]Map, len(in))
for i, v := range in {
result[i] = cleanUpInterfaceMap(v)
}
return result
}

func cleanUp(v any) any {
switch v := v.(type) {
case []any:
return cleanUpInterfaceArray(v)
case []map[string]any:
return cleanUpMSIArray(v)
case []map[any]any:
return cleanUpInterfaceMapArray(v)
case map[any]any:
return cleanUpInterfaceMap(v)
case map[string]any:
Expand Down
3 changes: 3 additions & 0 deletions conversions_nil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestConversionJSONPreservesNilContainers(t *testing.T) {
{"nil interface slice", []any(nil), "null"},
{"nil map slice", []objx.Map(nil), "null"},
{"nil string map slice", []map[string]any(nil), "null"},
{"nil interface map slice", []map[any]any(nil), "null"},
{"nil map", objx.Map(nil), "null"},
{"nil interface map", map[any]any(nil), "null"},
{"nil string map", map[string]any(nil), "null"},
Expand All @@ -24,11 +25,13 @@ func TestConversionJSONPreservesNilContainers(t *testing.T) {
{"empty interface slice", []any{}, "[]"},
{"empty map slice", []objx.Map{}, "[]"},
{"empty string map slice", []map[string]any{}, "[]"},
{"empty interface map slice", []map[any]any{}, "[]"},
{"empty map", objx.Map{}, "{}"},
{"empty interface map", map[any]any{}, "{}"},
{"populated interface slice", []any{1}, "[1]"},
{"populated map slice", []objx.Map{{"key": "value"}}, `[{"key":"value"}]`},
{"populated string map slice", []map[string]any{{"key": "value"}}, `[{"key":"value"}]`},
{"populated interface map slice", []map[any]any{{"key": "value"}}, `[{"key":"value"}]`},
{"populated map", objx.Map{"key": "value"}, `{"key":"value"}`},
{"populated interface map", map[any]any{1: "value"}, `{"1":"value"}`},
}
Expand Down
15 changes: 13 additions & 2 deletions conversions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ func TestConversionJSON(t *testing.T) {
"f": []objx.Map{{"g": map[any]any{"h": "i"}}},
"j": []map[string]any{{"k": map[any]any{"l": "m"}}},
"n": []any{objx.Map{"o": "p"}},
"q": []map[any]any{{"r": map[any]any{"s": "t"}}},
},
}

jsonString = `{"a":{"b":{"c":{"d":"e"}},"f":[{"g":{"h":"i"}}],"j":[{"k":{"l":"m"}}],"n":[{"o":"p"}]}}`
jsonString = `{"a":{"b":{"c":{"d":"e"}},"f":[{"g":{"h":"i"}}],"j":[{"k":{"l":"m"}}],"n":[{"o":"p"}],"q":[{"r":{"s":"t"}}]}}`
result, err = i.JSON()
require.NoError(t, err)
assert.Equal(t, jsonString, result)
Expand Down Expand Up @@ -272,15 +273,25 @@ func TestConversionURLValuesNested(t *testing.T) {
}

func TestValidityURLQuerySliceKeySuffix(t *testing.T) {
t.Cleanup(func() { _ = objx.SetURLValuesSliceKeySuffix(objx.URLValuesSliceKeySuffixArray) })
orig := objx.URLValuesSliceKeySuffix()
assert.Equal(t, objx.URLValuesSliceKeySuffixArray, orig)
t.Cleanup(func() { _ = objx.SetURLValuesSliceKeySuffix(orig) })

err := objx.SetURLValuesSliceKeySuffix("")
assert.Nil(t, err)
assert.Equal(t, objx.URLValuesSliceKeySuffixEmpty, objx.URLValuesSliceKeySuffix())

err = objx.SetURLValuesSliceKeySuffix("[]")
assert.Nil(t, err)
assert.Equal(t, objx.URLValuesSliceKeySuffixArray, objx.URLValuesSliceKeySuffix())

err = objx.SetURLValuesSliceKeySuffix("[i]")
assert.Nil(t, err)
assert.Equal(t, objx.URLValuesSliceKeySuffixIndex, objx.URLValuesSliceKeySuffix())

err = objx.SetURLValuesSliceKeySuffix("{}")
assert.Error(t, err)
assert.Equal(t, objx.URLValuesSliceKeySuffixIndex, objx.URLValuesSliceKeySuffix())
}

func getURLQueryMap() objx.Map {
Expand Down
Loading