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:
parent
29aab274b3
commit
ea529bbab6
3
bind.go
3
bind.go
@ -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]))
|
||||
|
47
bind_test.go
47
bind_test.go
@ -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"))
|
||||
@ -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"))
|
||||
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user