mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-24 02:32:17 +08:00
support struct pointer
This commit is contained in:
parent
2282be059b
commit
ae2ec9204a
@ -74,6 +74,18 @@ type FooStructForSliceType struct {
|
|||||||
SliceFoo []int `form:"slice_foo"`
|
SliceFoo []int `form:"slice_foo"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type FooStructForStructType struct {
|
||||||
|
StructFoo struct {
|
||||||
|
Idx int `form:"idx"`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type FooStructForStructPointerType struct {
|
||||||
|
StructPointerFoo *struct {
|
||||||
|
Name string `form:"name"`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type FooStructForSliceMapType struct {
|
type FooStructForSliceMapType struct {
|
||||||
// Unknown type: not support map
|
// Unknown type: not support map
|
||||||
SliceMapFoo []map[string]interface{} `form:"slice_map_foo"`
|
SliceMapFoo []map[string]interface{} `form:"slice_map_foo"`
|
||||||
@ -395,6 +407,22 @@ func TestBindingFormForType(t *testing.T) {
|
|||||||
testFormBindingForType(t, "GET",
|
testFormBindingForType(t, "GET",
|
||||||
"/?ptr_bar=test", "/?bar2=test",
|
"/?ptr_bar=test", "/?bar2=test",
|
||||||
"", "", "Ptr")
|
"", "", "Ptr")
|
||||||
|
|
||||||
|
testFormBindingForType(t, "POST",
|
||||||
|
"/", "/",
|
||||||
|
"idx=123", "id1=1", "Struct")
|
||||||
|
|
||||||
|
testFormBindingForType(t, "GET",
|
||||||
|
"/?idx=123", "/?id1=1",
|
||||||
|
"", "", "Struct")
|
||||||
|
|
||||||
|
testFormBindingForType(t, "POST",
|
||||||
|
"/", "/",
|
||||||
|
"name=thinkerou", "name1=ou", "StructPointer")
|
||||||
|
|
||||||
|
testFormBindingForType(t, "GET",
|
||||||
|
"/?name=thinkerou", "/?name1=ou",
|
||||||
|
"", "", "StructPointer")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBindingQuery(t *testing.T) {
|
func TestBindingQuery(t *testing.T) {
|
||||||
@ -953,6 +981,20 @@ func testFormBindingForType(t *testing.T, method, path, badPath, body, badBody s
|
|||||||
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)
|
||||||
|
case "Struct":
|
||||||
|
obj := FooStructForStructType{}
|
||||||
|
err := b.Bind(req, &obj)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t,
|
||||||
|
struct { Idx int "form:\"idx\"" }(struct { Idx int "form:\"idx\"" }{Idx:123}),
|
||||||
|
obj.StructFoo)
|
||||||
|
case "StructPointer":
|
||||||
|
obj := FooStructForStructPointerType{}
|
||||||
|
err := b.Bind(req, &obj)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t,
|
||||||
|
struct { Name string "form:\"name\"" }(struct { Name string "form:\"name\"" }{Name:"thinkerou"}),
|
||||||
|
*obj.StructPointerFoo)
|
||||||
case "Map":
|
case "Map":
|
||||||
obj := FooStructForMapType{}
|
obj := FooStructForMapType{}
|
||||||
err := b.Bind(req, &obj)
|
err := b.Bind(req, &obj)
|
||||||
|
@ -36,9 +36,16 @@ func mapForm(ptr interface{}, form map[string][]string) error {
|
|||||||
if inputFieldName == "" {
|
if inputFieldName == "" {
|
||||||
inputFieldName = typeField.Name
|
inputFieldName = typeField.Name
|
||||||
|
|
||||||
// if "form" tag is nil, we inspect if the field is a struct.
|
// if "form" tag is nil, we inspect if the field is a struct or struct pointer.
|
||||||
// this would not make sense for JSON parsing but it does for a form
|
// this would not make sense for JSON parsing but it does for a form
|
||||||
// since data is flatten
|
// since data is flatten
|
||||||
|
if structFieldKind == reflect.Ptr {
|
||||||
|
if !structField.Elem().IsValid() {
|
||||||
|
structField.Set(reflect.New(structField.Type().Elem()))
|
||||||
|
}
|
||||||
|
structField = structField.Elem()
|
||||||
|
structFieldKind = structField.Kind()
|
||||||
|
}
|
||||||
if structFieldKind == reflect.Struct {
|
if structFieldKind == reflect.Struct {
|
||||||
err := mapForm(structField.Addr().Interface(), form)
|
err := mapForm(structField.Addr().Interface(), form)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user