Conflicts:
	binding/binding.go
This commit is contained in:
Manu Mtz-Almeida 2015-05-24 03:33:21 +02:00
commit 208e1f5569
2 changed files with 19 additions and 4 deletions

View File

@ -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)

View File

@ -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