support struct pointer (#1342)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* support struct pointer * add readme
authored by 田欧 · committed by Bo-Yi Wu · parent 2282be0 · Browse files · 3 files·+151 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+151 added−1 removed
- other:+151 added−1 removed
Provenance
- Commit
bd4f73a- Tree
4d3744e- Author
田欧 <thinkerou@gmail.com>- Committer
Bo-Yi Wu <appleboy.tw@gmail.com>- Parent
2282be0
3 files changed+151 added−1 removed
modified:binding/binding_test.go+50 added
72 unmodified lines73 type FooStructForSliceType struct {74 SliceFoo []int `form:"slice_foo"`75 }76 77+type FooStructForStructType struct {78+ StructFoo struct {79+ Idx int `form:"idx"`80+ }81+}82+83+type FooStructForStructPointerType struct {84+ StructPointerFoo *struct {85+ Name string `form:"name"`86+ }87+}88+89 type FooStructForSliceMapType struct {90 // Unknown type: not support map91 SliceMapFoo []map[string]interface{} `form:"slice_map_foo"`92 }313 unmodified lines406 407 testFormBindingForType(t, "GET",408 "/?ptr_bar=test", "/?bar2=test",409 "", "", "Ptr")410+411+ testFormBindingForType(t, "POST",412+ "/", "/",413+ "idx=123", "id1=1", "Struct")414+415+ testFormBindingForType(t, "GET",416+ "/?idx=123", "/?id1=1",417+ "", "", "Struct")418+419+ testFormBindingForType(t, "POST",420+ "/", "/",421+ "name=thinkerou", "name1=ou", "StructPointer")422+423+ testFormBindingForType(t, "GET",424+ "/?name=thinkerou", "/?name1=ou",425+ "", "", "StructPointer")426 }427 428 func TestBindingQuery(t *testing.T) {429 testQueryBinding(t, "POST",550 unmodified lines980 obj = FooStructForSliceType{}981 req = requestWithBody(method, badPath, badBody)982 err = JSON.Bind(req, &obj)983 assert.Error(t, err)984+ case "Struct":985+ obj := FooStructForStructType{}986+ err := b.Bind(req, &obj)987+ assert.NoError(t, err)988+ assert.Equal(t,989+ struct {990+ Idx int "form:\"idx\""991+ }(struct {992+ Idx int "form:\"idx\""993+ }{Idx: 123}),994+ obj.StructFoo)995+ case "StructPointer":996+ obj := FooStructForStructPointerType{}997+ err := b.Bind(req, &obj)998+ assert.NoError(t, err)999+ assert.Equal(t,1000+ struct {1001+ Name string "form:\"name\""1002+ }(struct {1003+ Name string "form:\"name\""1004+ }{Name: "thinkerou"}),1005+ *obj.StructPointerFoo)1006 case "Map":1007 obj := FooStructForMapType{}1008 err := b.Bind(req, &obj)1009 assert.Error(t, err)185 unmodified linesmodified:binding/form_mapping.go+8 added-1 removed
34 unmodified lines35 }36 if inputFieldName == "" {37 inputFieldName = typeField.Name38 39- // if "form" tag is nil, we inspect if the field is a struct.39+ // if "form" tag is nil, we inspect if the field is a struct or struct pointer.40 // this would not make sense for JSON parsing but it does for a form41 // since data is flatten42+ if structFieldKind == reflect.Ptr {43+ if !structField.Elem().IsValid() {44+ structField.Set(reflect.New(structField.Type().Elem()))45+ }46+ structField = structField.Elem()47+ structFieldKind = structField.Kind()48+ }49 if structFieldKind == reflect.Struct {50 err := mapForm(structField.Addr().Interface(), form)51 if err != nil {52 return err157 unmodified linesmodified:README.md+93 added
49 unmodified lines50 - [Support Let's Encrypt](#support-lets-encrypt)51 - [Run multiple service using Gin](#run-multiple-service-using-gin)52 - [Graceful restart or stop](#graceful-restart-or-stop)53 - [Build a single binary with templates](#build-a-single-binary-with-templates)54+ - [Bind form-data request with custom struct](#bind-form-data-request-with-custom-struct)55 - [Testing](#testing)56 - [Users](#users--)57 58 ## Quick start1402 unmodified lines1461 ```1462 1463 See a complete example in the `examples/assets-in-binary` directory.1464 1465+### Bind form-data request with custom struct1466+1467+The follow example using custom struct:1468+1469+```go1470+type StructA struct {1471+ FieldA string `form:"field_a"`1472+}1473+1474+type StructB struct {1475+ NestedStruct StructA1476+ FieldB string `form:"field_b"`1477+}1478+1479+type StructC struct {1480+ NestedStructPointer *StructA1481+ FieldC string `form:"field_c"`1482+}1483+1484+type StructD struct {1485+ NestedAnonyStruct struct {1486+ FieldX string `form:"field_x"`1487+ }1488+ FieldD string `form:"field_d"`1489+}1490+1491+func GetDataB(c *gin.Context) {1492+ var b StructB1493+ c.Bind(&b)1494+ c.JSON(200, gin.H{1495+ "a": b.NestedStruct,1496+ "b": b.FieldB,1497+ })1498+}1499+1500+func GetDataC(c *gin.Context) {1501+ var b StructC1502+ c.Bind(&b)1503+ c.JSON(200, gin.H{1504+ "a": b.NestedStructPointer,1505+ "c": b.FieldC,1506+ })1507+}1508+1509+func GetDataD(c *gin.Context) {1510+ var b StructD1511+ c.Bind(&b)1512+ c.JSON(200, gin.H{1513+ "x": b.NestedAnonyStruct,1514+ "d": b.FieldD,1515+ })1516+}1517+1518+func main() {1519+ r := gin.Default()1520+ r.GET("/getb", GetDataB)1521+ r.GET("/getc", GetDataC)1522+ r.GET("/getd", GetDataD)1523+1524+ r.Run()1525+}1526+```1527+1528+Using the command `curl` command result:1529+1530+```1531+$ curl "http://localhost:8080/getb?field_a=hello&field_b=world"1532+{"a":{"FieldA":"hello"},"b":"world"}1533+$ curl "http://localhost:8080/getc?field_a=hello&field_c=world"1534+{"a":{"FieldA":"hello"},"c":"world"}1535+$ curl "http://localhost:8080/getd?field_x=hello&field_d=world"1536+{"d":"world","x":{"FieldX":"hello"}}1537+```1538+1539+**NOTE**: NOT support the follow style struct:1540+1541+```go1542+type StructX struct {1543+ X struct {} `form:"name_x"` // HERE have form1544+}1545+1546+type StructY struct {1547+ Y StructX `form:"name_y"` // HERE hava form1548+}1549+1550+type StructZ struct {1551+ Z *StructZ `form:"name_z"` // HERE hava form1552+}1553+```1554+1555+In a word, only support nested custom struct which have no `form` now.1556+1557 ## Testing1558 1559 The `net/http/httptest` package is preferable way for HTTP testing.1560 48 unmodified lines