Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a756e00

Browse files
authored
Merge pull request revel#1208 from notzippy/wmh_develop
Allow binder to bind struct map (replacement for pr 998)
2 parents 3847e58 + f8ae517 commit a756e00

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

binder.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -432,13 +432,17 @@ func bindMap(params *Params, name string, typ reflect.Type) reflect.Value {
432432
return result
433433
}
434434

435-
for paramName, values := range params.Values {
436-
if !strings.HasPrefix(paramName, name+"[") || paramName[len(paramName)-1] != ']' {
435+
for paramName, _ := range params.Values {
436+
suffix := paramName[len(name)+1:]
437+
fieldName := nextKey(suffix)
438+
if fieldName != "" {
439+
fieldName = fieldName[:len(fieldName)-1]
440+
}
441+
if !strings.HasPrefix(paramName, name+"["+fieldName+"]") {
437442
continue
438443
}
439444

440-
key := paramName[len(name)+1 : len(paramName)-1]
441-
result.SetMapIndex(BindValue(key, keyType), BindValue(values[0], valueType))
445+
result.SetMapIndex(BindValue(fieldName, keyType), Bind(params, name+"["+fieldName+"]", valueType))
442446
}
443447
return result
444448
}

binder_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ var (
8484
"m2[2]": {"bar"},
8585
"m3[a]": {"1"},
8686
"m3[b]": {"2"},
87+
"m4[a].ID": {"1"},
88+
"m4[a].Name": {"foo"},
89+
"m4[b].ID": {"2"},
90+
"m4[b].Name": {"bar"},
8791
"invalidInt": {"xyz"},
8892
"invalidInt2": {""},
8993
"invalidBool": {"xyz"},
@@ -142,6 +146,7 @@ var binderTestCases = map[string]interface{}{
142146
"m": map[string]string{"a": "foo", "b": "bar"},
143147
"m2": map[int]string{1: "foo", 2: "bar"},
144148
"m3": map[string]int{"a": 1, "b": 2},
149+
"m4": map[string]A{"a": A{ID: 1, Name: "foo"}, "b": A{ID: 2, Name: "bar"}},
145150

146151
// TODO: Tests that use TypeBinders
147152

0 commit comments

Comments
 (0)