1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-22 20:06:21 +02:00

binder: allow binding to a nil map

This commit is contained in:
Georg Müller 2024-02-06 16:18:12 +01:00 committed by Martti T
parent 29aab274b3
commit ea529bbab6
2 changed files with 50 additions and 0 deletions

View File

@ -145,6 +145,9 @@ func (b *DefaultBinder) bindData(destination interface{}, data map[string][]stri
if !(isElemSliceOfStrings || isElemString || isElemInterface) { if !(isElemSliceOfStrings || isElemString || isElemInterface) {
return nil return nil
} }
if val.IsNil() {
val.Set(reflect.MakeMap(typ))
}
for k, v := range data { for k, v := range data {
if isElemString { if isElemString {
val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v[0])) val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v[0]))

View File

@ -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) { t.Run("ok, bind to map[string][]string", func(t *testing.T) {
dest := map[string][]string{} dest := map[string][]string{}
assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param")) assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param"))
@ -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) { t.Run("ok, bind to map[string]interface", func(t *testing.T) {
dest := map[string]interface{}{} dest := map[string]interface{}{}
assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param")) assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param"))
@ -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) { t.Run("ok, bind to map[string]int skips", func(t *testing.T) {
dest := map[string]int{} dest := map[string]int{}
assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param")) assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param"))
assert.Equal(t, map[string]int{}, dest) 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) { t.Run("ok, bind to map[string][]int skips", func(t *testing.T) {
dest := map[string][]int{} dest := map[string][]int{}
assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param")) assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param"))
assert.Equal(t, map[string][]int{}, dest) 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) { func TestBindbindData(t *testing.T) {