From 0ad4038d9fbcf49cbe360da119352ef99e9b1c29 Mon Sep 17 00:00:00 2001 From: YakunZ Date: Wed, 24 Aug 2016 16:04:22 +0800 Subject: [PATCH] fix#2039 & test --- context/input.go | 9 ++++++--- context/input_test.go | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) 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()