Skip to content

Commit

Permalink
binder: allow binding to a nil map
Browse files Browse the repository at this point in the history
  • Loading branch information
georgmu committed Jan 12, 2024
1 parent 60fc2fb commit f638c84
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
3 changes: 3 additions & 0 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ func (b *DefaultBinder) bindData(destination interface{}, data map[string][]stri
// 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 {
if val.IsNil() {
val.Set(reflect.MakeMap(typ))
}
k := typ.Elem().Kind()
isElemInterface := k == reflect.Interface
isElemString := k == reflect.String
Expand Down
48 changes: 48 additions & 0 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,18 @@ func TestDefaultBinder_bindDataToMap(t *testing.T) {
)
})

t.Run("ok, bind to map[string]string with nil map", func(t *testing.T) {
var 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"))
Expand All @@ -459,6 +471,18 @@ func TestDefaultBinder_bindDataToMap(t *testing.T) {
)
})

t.Run("ok, bind to map[string][]string with nil map", func(t *testing.T) {
var 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"))
Expand All @@ -471,18 +495,42 @@ func TestDefaultBinder_bindDataToMap(t *testing.T) {
)
})

t.Run("ok, bind to map[string]interface with nil map", func(t *testing.T) {
var 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 with nil map", func(t *testing.T) {
var 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)
})

t.Run("ok, bind to map[string][]int skips with nil map", func(t *testing.T) {
var 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) {
Expand Down

0 comments on commit f638c84

Please sign in to comment.