diff --git a/binding/binding_test.go b/binding/binding_test.go index ca16a2d0..22be1315 100644 --- a/binding/binding_test.go +++ b/binding/binding_test.go @@ -16,6 +16,11 @@ type FooStruct struct { Foo string `json:"foo" form:"foo" xml:"foo" binding:"required"` } +type FooBarStruct struct { + FooStruct + Bar string `json:"bar" form:"bar" xml:"bar" binding:"required"` +} + func TestBindingDefault(t *testing.T) { assert.Equal(t, Default("GET", ""), Form) assert.Equal(t, Default("GET", MIMEJSON), Form) @@ -40,12 +45,12 @@ func TestBindingJSON(t *testing.T) { func TestBindingForm(t *testing.T) { testFormBinding(t, "POST", "/", "/", - "foo=bar", "bar=foo") + "foo=bar&bar=foo", "bar2=foo") } func TestBindingForm2(t *testing.T) { testFormBinding(t, "GET", - "/?foo=bar", "/?bar=foo", + "/?foo=bar&bar=foo", "/?bar2=foo", "", "") } @@ -60,7 +65,7 @@ func testFormBinding(t *testing.T, method, path, badPath, body, badBody string) b := Form assert.Equal(t, b.Name(), "query") - obj := FooStruct{} + obj := FooBarStruct{} req := requestWithBody(method, path, body) if method == "POST" { req.Header.Add("Content-Type", MIMEPOSTForm) @@ -68,8 +73,9 @@ func testFormBinding(t *testing.T, method, path, badPath, body, badBody string) err := b.Bind(req, &obj) assert.NoError(t, err) assert.Equal(t, obj.Foo, "bar") + assert.Equal(t, obj.Bar, "foo") - obj = FooStruct{} + obj = FooBarStruct{} req = requestWithBody(method, badPath, badBody) err = JSON.Bind(req, &obj) assert.Error(t, err) diff --git a/binding/form_mapping.go b/binding/form_mapping.go index d9aa06cf..02371b88 100644 --- a/binding/form_mapping.go +++ b/binding/form_mapping.go @@ -20,6 +20,15 @@ func mapForm(ptr interface{}, form map[string][]string) error { continue } + // support for embeded fields + if structField.Kind() == reflect.Struct { + err := mapForm(structField.Addr().Interface(), form) + if err != nil { + return err + } + continue + } + inputFieldName := typeField.Tag.Get("form") if inputFieldName == "" { inputFieldName = typeField.Name