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: allow binding to a nil map #2574

Merged
merged 1 commit into from
Feb 13, 2024
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
3 changes: 3 additions & 0 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ func (b *DefaultBinder) bindData(destination interface{}, data map[string][]stri
if !(isElemSliceOfStrings || isElemString || isElemInterface) {
return nil
}
if val.IsNil() {
val.Set(reflect.MakeMap(typ))
}
for k, v := range data {
if isElemString {
val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v[0]))
Expand Down
47 changes: 47 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,41 @@ 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(nil), 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(nil), dest)
})
}

func TestBindbindData(t *testing.T) {
Expand Down