Merge branch 'master' of https://github.com/remerge/gin
Conflicts: binding/binding.go
This commit is contained in:
commit
208e1f5569
@ -16,6 +16,11 @@ type FooStruct struct {
|
|||||||
Foo string `json:"foo" form:"foo" xml:"foo" binding:"required"`
|
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) {
|
func TestBindingDefault(t *testing.T) {
|
||||||
assert.Equal(t, Default("GET", ""), Form)
|
assert.Equal(t, Default("GET", ""), Form)
|
||||||
assert.Equal(t, Default("GET", MIMEJSON), Form)
|
assert.Equal(t, Default("GET", MIMEJSON), Form)
|
||||||
@ -40,12 +45,12 @@ func TestBindingJSON(t *testing.T) {
|
|||||||
func TestBindingForm(t *testing.T) {
|
func TestBindingForm(t *testing.T) {
|
||||||
testFormBinding(t, "POST",
|
testFormBinding(t, "POST",
|
||||||
"/", "/",
|
"/", "/",
|
||||||
"foo=bar", "bar=foo")
|
"foo=bar&bar=foo", "bar2=foo")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBindingForm2(t *testing.T) {
|
func TestBindingForm2(t *testing.T) {
|
||||||
testFormBinding(t, "GET",
|
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
|
b := Form
|
||||||
assert.Equal(t, b.Name(), "query")
|
assert.Equal(t, b.Name(), "query")
|
||||||
|
|
||||||
obj := FooStruct{}
|
obj := FooBarStruct{}
|
||||||
req := requestWithBody(method, path, body)
|
req := requestWithBody(method, path, body)
|
||||||
if method == "POST" {
|
if method == "POST" {
|
||||||
req.Header.Add("Content-Type", MIMEPOSTForm)
|
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)
|
err := b.Bind(req, &obj)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, obj.Foo, "bar")
|
assert.Equal(t, obj.Foo, "bar")
|
||||||
|
assert.Equal(t, obj.Bar, "foo")
|
||||||
|
|
||||||
obj = FooStruct{}
|
obj = FooBarStruct{}
|
||||||
req = requestWithBody(method, badPath, badBody)
|
req = requestWithBody(method, badPath, badBody)
|
||||||
err = JSON.Bind(req, &obj)
|
err = JSON.Bind(req, &obj)
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
|
@ -20,6 +20,15 @@ func mapForm(ptr interface{}, form map[string][]string) error {
|
|||||||
continue
|
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")
|
inputFieldName := typeField.Tag.Get("form")
|
||||||
if inputFieldName == "" {
|
if inputFieldName == "" {
|
||||||
inputFieldName = typeField.Name
|
inputFieldName = typeField.Name
|
||||||
|
Loading…
Reference in New Issue
Block a user