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

binder: make binding to Map work better with string destinations #2554

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 19 additions & 3 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,26 @@ func (b *DefaultBinder) bindData(destination interface{}, data map[string][]stri
typ := reflect.TypeOf(destination).Elem()
val := reflect.ValueOf(destination).Elem()

// Map
if typ.Kind() == reflect.Map {
// Support binding to limited Map destinations:
// - map[string][]string,
// - map[string]string <-- (binds first value from data slice)
// - map[string]interface{}
// You are better off binding to struct but there are user who want this map feature. Source of data for these cases are:
// params,query,header,form as these sources produce string values, most of the time slice of strings, actually.
if typ.Kind() == reflect.Map && typ.Key().Kind() == reflect.String {
k := typ.Elem().Kind()
isElemInterface := k == reflect.Interface
isElemString := k == reflect.String
isElemSliceOfStrings := k == reflect.Slice && typ.Elem().Elem().Kind() == reflect.String
if !(isElemSliceOfStrings || isElemString || isElemInterface) {
return nil
}
for k, v := range data {
val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v[0]))
if isElemString {
val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v[0]))
} else {
val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v))
}
}
return nil
}
Expand Down
56 changes: 56 additions & 0 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,62 @@ func TestBindUnsupportedMediaType(t *testing.T) {
testBindError(t, strings.NewReader(invalidContent), MIMEApplicationJSON, &json.SyntaxError{})
}

func TestDefaultBinder_bindDataToMap(t *testing.T) {
exampleData := map[string][]string{
"multiple": {"1", "2"},
"single": {"3"},
}

t.Run("ok, bind to map[string]string", func(t *testing.T) {
dest := map[string]string{}
assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param"))
assert.Equal(t,
map[string]string{
"multiple": "1",
"single": "3",
},
dest,
)
})

t.Run("ok, bind to map[string][]string", func(t *testing.T) {
dest := map[string][]string{}
assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param"))
assert.Equal(t,
map[string][]string{
"multiple": {"1", "2"},
"single": {"3"},
},
dest,
)
})

t.Run("ok, bind to map[string]interface", func(t *testing.T) {
dest := map[string]interface{}{}
assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param"))
assert.Equal(t,
map[string]interface{}{
"multiple": []string{"1", "2"},
"single": []string{"3"},
},
dest,
)
})

t.Run("ok, bind to map[string]int skips", func(t *testing.T) {
dest := map[string]int{}
assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param"))
assert.Equal(t, map[string]int{}, dest)
})

t.Run("ok, bind to map[string][]int skips", func(t *testing.T) {
dest := map[string][]int{}
assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param"))
assert.Equal(t, map[string][]int{}, dest)
})

}

func TestBindbindData(t *testing.T) {
ts := new(bindTestStruct)
b := new(DefaultBinder)
Expand Down