Skip to content
Myr

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

3 files+151 added−1 removed

modified:binding/binding_test.go+50 added
72 unmodified lines
73type FooStructForSliceType struct {74 SliceFoo []int `form:"slice_foo"`75}7677type FooStructForStructType struct {78 StructFoo struct {79 Idx int `form:"idx"`80 }81}8283type FooStructForStructPointerType struct {84 StructPointerFoo *struct {85 Name string `form:"name"`86 }87}8889type FooStructForSliceMapType struct {90 // Unknown type: not support map91 SliceMapFoo []map[string]interface{} `form:"slice_map_foo"`92}
313 unmodified lines
406407 testFormBindingForType(t, "GET",408 "/?ptr_bar=test", "/?bar2=test",409 "", "", "Ptr")410411 testFormBindingForType(t, "POST",412 "/", "/",413 "idx=123", "id1=1", "Struct")414415 testFormBindingForType(t, "GET",416 "/?idx=123", "/?id1=1",417 "", "", "Struct")418419 testFormBindingForType(t, "POST",420 "/", "/",421 "name=thinkerou", "name1=ou", "StructPointer")422423 testFormBindingForType(t, "GET",424 "/?name=thinkerou", "/?name1=ou",425 "", "", "StructPointer")426}427428func TestBindingQuery(t *testing.T) {429 testQueryBinding(t, "POST",
550 unmodified lines
980 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 lines
modified:binding/form_mapping.go+8 added-1 removed
34 unmodified lines
35 }36 if inputFieldName == "" {37 inputFieldName = typeField.Name3839 // 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 err
157 unmodified lines
modified:README.md+93 added
49 unmodified lines
50 - [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--)5758## Quick start
1402 unmodified lines
1461```14621463See a complete example in the `examples/assets-in-binary` directory.14641465### Bind form-data request with custom struct14661467The follow example using custom struct:14681469```go1470type StructA struct {1471 FieldA string `form:"field_a"`1472}14731474type StructB struct {1475 NestedStruct StructA1476 FieldB string `form:"field_b"`1477}14781479type StructC struct {1480 NestedStructPointer *StructA1481 FieldC string `form:"field_c"`1482}14831484type StructD struct {1485 NestedAnonyStruct struct {1486 FieldX string `form:"field_x"`1487 }1488 FieldD string `form:"field_d"`1489}14901491func 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}14991500func 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}15081509func 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}15171518func main() {1519 r := gin.Default()1520 r.GET("/getb", GetDataB)1521 r.GET("/getc", GetDataC)1522 r.GET("/getd", GetDataD)15231524 r.Run()1525}1526```15271528Using the command `curl` command result:15291530```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```15381539**NOTE**: NOT support the follow style struct:15401541```go1542type StructX struct {1543 X struct {} `form:"name_x"` // HERE have form1544}15451546type StructY struct {1547 Y StructX `form:"name_y"` // HERE hava form1548}15491550type StructZ struct {1551 Z *StructZ `form:"name_z"` // HERE hava form1552}1553```15541555In a word, only support nested custom struct which have no `form` now.15561557## Testing15581559The `net/http/httptest` package is preferable way for HTTP testing.1560
48 unmodified lines