Skip to content

Commit

Permalink
bind to single string for map[string]interface{}{}
Browse files Browse the repository at this point in the history
  • Loading branch information
thesaltree committed Jul 19, 2024
1 parent 9622474 commit 62fe8cd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
8 changes: 3 additions & 5 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,9 @@ func (b *DefaultBinder) bindData(destination interface{}, data map[string][]stri
if isElemString {
val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v[0]))
} else if isElemInterface {
if len(v) == 1 {
val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v[0]))
} else {
val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v))
}
// To maintain backward compatibility, we always bind to the first string value
// and not the slice of strings when dealing with map[string]interface{}{}
val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v[0]))
} else {
val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v))
}
Expand Down
4 changes: 2 additions & 2 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ func TestDefaultBinder_bindDataToMap(t *testing.T) {
assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param"))
assert.Equal(t,
map[string]interface{}{
"multiple": []string{"1", "2"},
"multiple": "1",
"single": "3",
},
dest,
Expand All @@ -509,7 +509,7 @@ func TestDefaultBinder_bindDataToMap(t *testing.T) {
assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param"))
assert.Equal(t,
map[string]interface{}{
"multiple": []string{"1", "2"},
"multiple": "1",
"single": "3",
},
dest,
Expand Down

0 comments on commit 62fe8cd

Please sign in to comment.