diff --git a/context/input.go b/context/input.go index fd47c36f..5af8e6c4 100644 --- a/context/input.go +++ b/context/input.go @@ -590,12 +590,15 @@ func (input *BeegoInput) bindStruct(params *url.Values, key string, typ reflect. result := reflect.New(typ).Elem() fieldValues := make(map[string]reflect.Value) for reqKey, val := range *params { - if !strings.HasPrefix(reqKey, key+".") { + var fieldName string + if strings.HasPrefix(reqKey, key+".") { + fieldName = reqKey[len(key)+1:] + } else if strings.HasPrefix(reqKey, key+"[") && reqKey[len(reqKey)-1] == ']' { + fieldName = reqKey[len(key)+1 : len(reqKey)-1] + } else { continue } - fieldName := reqKey[len(key)+1:] - if _, ok := fieldValues[fieldName]; !ok { // Time to bind this field. Get it and make sure we can set it. fieldValue := result.FieldByName(fieldName) diff --git a/context/input_test.go b/context/input_test.go index 8887aec4..e64addba 100644 --- a/context/input_test.go +++ b/context/input_test.go @@ -75,6 +75,24 @@ func TestParse(t *testing.T) { fmt.Println(user) } +func TestParse2(t *testing.T) { + r, _ := http.NewRequest("GET", "/?user[0][Username]=Raph&user[1].Username=Leo&user[0].Password=123456&user[1][Password]=654321", nil) + beegoInput := NewInput() + beegoInput.Context = NewContext() + beegoInput.Context.Reset(httptest.NewRecorder(), r) + beegoInput.ParseFormOrMulitForm(1 << 20) + type User struct { + Username string + Password string + } + var users []User + err := beegoInput.Bind(&users, "user") + fmt.Println(users) + if err != nil || users[0].Username != "Raph" || users[0].Password != "123456" || users[1].Username != "Leo" || users[1].Password != "654321" { + t.Fatal("users info wrong") + } +} + func TestSubDomain(t *testing.T) { r, _ := http.NewRequest("GET", "http://www.example.com/?id=123&isok=true&ft=1.2&ol[0]=1&ol[1]=2&ul[]=str&ul[]=array&user.Name=astaxie", nil) beegoInput := NewInput()