mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 10:20:56 +00:00
commit
7283aead42
@ -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)
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user