Skip to content

Commit

Permalink
bind: Maintain backwards compatibility for map[string]interface{} bin…
Browse files Browse the repository at this point in the history
…ding
  • Loading branch information
thesaltree committed Jul 9, 2024
1 parent f7d9f51 commit 9622474
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 6 additions & 0 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ func (b *DefaultBinder) bindData(destination interface{}, data map[string][]stri
for k, v := range data {
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))
}
} 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 @@ -498,7 +498,7 @@ func TestDefaultBinder_bindDataToMap(t *testing.T) {
assert.Equal(t,
map[string]interface{}{
"multiple": []string{"1", "2"},
"single": []string{"3"},
"single": "3",
},
dest,
)
Expand All @@ -510,7 +510,7 @@ func TestDefaultBinder_bindDataToMap(t *testing.T) {
assert.Equal(t,
map[string]interface{}{
"multiple": []string{"1", "2"},
"single": []string{"3"},
"single": "3",
},
dest,
)
Expand Down

0 comments on commit 9622474

Please sign in to comment.