Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URLQuery with index slice keys #74

Merged
merged 7 commits into from
Jul 2, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
56 changes: 45 additions & 11 deletions conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"net/url"
"strconv"
)

// SignatureSeparator is the character that is used to
Expand All @@ -14,7 +15,10 @@ const SignatureSeparator = "_"

// URLValuesSliceKeySuffix is the character that is used to
// specify a suffic for slices parsed by URLValues.
// If the suffix is set to "[i]", then the index of the slice
// is used in place of i
// Ex: Suffix "[]" would have the form a[]=b&a[]=c
// OR Suffix "[i]" would have the form a[0]=b&a[1]=c
// OR Suffix "" would have the form a=b&a=c
var URLValuesSliceKeySuffix = "[]"

Expand Down Expand Up @@ -106,6 +110,11 @@ func (m Map) URLValues() url.Values {
}

func (m Map) parseURLValues(queryMap Map, vals url.Values, key string) {
useSliceIndex := false
if URLValuesSliceKeySuffix == "[i]" {
useSliceIndex = true
}

for k, v := range queryMap {
val := &Value{data: v}
switch {
Expand All @@ -116,13 +125,21 @@ func (m Map) parseURLValues(queryMap Map, vals url.Values, key string) {
m.parseURLValues(v.(Map), vals, key+"["+k+"]")
}
case val.IsObjxMapSlice():
sliceKey := k + URLValuesSliceKeySuffix
sliceKey := k
if key != "" {
sliceKey = key + "[" + k + "]" + URLValuesSliceKeySuffix
sliceKey = key + "[" + k + "]"
}

for _, sv := range val.MustObjxMapSlice() {
m.parseURLValues(sv, vals, sliceKey)
if useSliceIndex {
for i, sv := range val.MustObjxMapSlice() {
sk := sliceKey + "[" + strconv.FormatInt(int64(i), 10) + "]"
m.parseURLValues(sv, vals, sk)
}
} else {
sliceKey = sliceKey + URLValuesSliceKeySuffix
for _, sv := range val.MustObjxMapSlice() {
m.parseURLValues(sv, vals, sliceKey)
}
}
case val.IsMSI():
if key == "" {
Expand All @@ -131,25 +148,42 @@ func (m Map) parseURLValues(queryMap Map, vals url.Values, key string) {
m.parseURLValues(New(v), vals, key+"["+k+"]")
}
case val.IsMSISlice():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case isn't covered by any testcases. Mind adding some? I think you can reuse the val.IsObjxMapSlice() ones

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to a previous pull request, this part is no longer necessary. Updated the code and added tests.

sliceKey := k + URLValuesSliceKeySuffix
sliceKey := k
if key != "" {
sliceKey = key + "[" + k + "]" + URLValuesSliceKeySuffix
sliceKey = key + "[" + k + "]"
}

for _, sv := range val.MustMSISlice() {
m.parseURLValues(New(sv), vals, sliceKey)
if useSliceIndex {
for i, sv := range val.MustMSISlice() {
sk := sliceKey + "[" + strconv.FormatInt(int64(i), 10) + "]"
m.parseURLValues(New(sv), vals, sk)
}
} else {
sliceKey = sliceKey + URLValuesSliceKeySuffix
for _, sv := range val.MustMSISlice() {
m.parseURLValues(New(sv), vals, sliceKey)
}
}
case val.IsStrSlice(), val.IsBoolSlice(),
val.IsFloat32Slice(), val.IsFloat64Slice(),
val.IsIntSlice(), val.IsInt8Slice(), val.IsInt16Slice(), val.IsInt32Slice(), val.IsInt64Slice(),
val.IsUintSlice(), val.IsUint8Slice(), val.IsUint16Slice(), val.IsUint32Slice(), val.IsUint64Slice():

sliceKey := k + URLValuesSliceKeySuffix
sliceKey := k
if key != "" {
sliceKey = key + "[" + k + "]" + URLValuesSliceKeySuffix
sliceKey = key + "[" + k + "]"
}

if useSliceIndex {
for i, sv := range val.StringSlice() {
sk := sliceKey + "[" + strconv.FormatInt(int64(i), 10) + "]"
vals.Set(sk, sv)
}
} else {
sliceKey = sliceKey + URLValuesSliceKeySuffix
vals[sliceKey] = val.StringSlice()
}

vals[sliceKey] = val.StringSlice()
default:
if key == "" {
vals.Set(k, val.String())
Expand Down
23 changes: 21 additions & 2 deletions conversions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ func TestConversionURLValues(t *testing.T) {
"bools[]": []string{"true", "false"},
"mapSlice[][age]": []string{"40"},
"mapSlice[][height]": []string{"152"},
"msiSlice[][age]": []string{"40"},
"msiSlice[][height]": []string{"152"},
}, u)
}

Expand All @@ -107,7 +109,7 @@ func TestConversionURLQuery(t *testing.T) {
assert.Nil(t, err)
require.NotNil(t, ue)

assert.Equal(t, "abc=123&bools[]=true&bools[]=false&data[age]=30&data[arr][]=1&data[arr][]=2&data[height]=162&mapSlice[][age]=40&mapSlice[][height]=152&name=Mat&stats[]=1&stats[]=2", ue)
assert.Equal(t, "abc=123&bools[]=true&bools[]=false&data[age]=30&data[arr][]=1&data[arr][]=2&data[height]=162&mapSlice[][age]=40&mapSlice[][height]=152&msiSlice[][age]=40&msiSlice[][height]=152&name=Mat&stats[]=1&stats[]=2", ue)
}

func TestConversionURLQueryNoSliceKeySuffix(t *testing.T) {
Expand All @@ -122,7 +124,23 @@ func TestConversionURLQueryNoSliceKeySuffix(t *testing.T) {
assert.Nil(t, err)
require.NotNil(t, ue)

assert.Equal(t, "abc=123&bools=true&bools=false&data[age]=30&data[arr]=1&data[arr]=2&data[height]=162&mapSlice[age]=40&mapSlice[height]=152&name=Mat&stats=1&stats=2", ue)
assert.Equal(t, "abc=123&bools=true&bools=false&data[age]=30&data[arr]=1&data[arr]=2&data[height]=162&mapSlice[age]=40&mapSlice[height]=152&msiSlice[age]=40&msiSlice[height]=152&name=Mat&stats=1&stats=2", ue)
}

func TestConversionURLQueryIndexSliceKeySuffix(t *testing.T) {
m := getURLQueryMap()
m.Set("mapSlice", []objx.Map{{"age": 40, "sex": "male"}, {"height": 152}})
objx.URLValuesSliceKeySuffix = "[i]"
u, err := m.URLQuery()

assert.Nil(t, err)
require.NotNil(t, u)

ue, err := url.QueryUnescape(u)
assert.Nil(t, err)
require.NotNil(t, ue)

assert.Equal(t, "abc=123&bools[0]=true&bools[1]=false&data[age]=30&data[arr][0]=1&data[arr][1]=2&data[height]=162&mapSlice[0][age]=40&mapSlice[0][sex]=male&mapSlice[1][height]=152&msiSlice[0][age]=40&msiSlice[1][height]=152&name=Mat&stats[0]=1&stats[1]=2", ue)
}

func getURLQueryMap() objx.Map {
Expand All @@ -131,6 +149,7 @@ func getURLQueryMap() objx.Map {
"name": "Mat",
"data": objx.Map{"age": 30, "height": 162, "arr": []int{1, 2}},
"mapSlice": []objx.Map{{"age": 40}, {"height": 152}},
"msiSlice": []map[string]interface{}{{"age": 40}, {"height": 152}},
"stats": []string{"1", "2"},
"bools": []bool{true, false},
}
Expand Down