Skip to content
Myr

Now you can parse the inline lowercase start structure (#1893)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Now you can parse the inline lowercase start structure package main import ( "encoding/json" "fmt" "github.com/gin-gonic/gin" ) type appkey struct { Appkey string `json:"appkey" form:"appkey"` } type Query struct { Page int `json:"page" form:"page"` Size int `json:"size" form:"size"` appkey } func main() { router := gin.Default() router.POST("/login", func(c *gin.Context) { var q2 Query if c.ShouldBindQuery(&q2) == nil { c.JSON(200, &q2) } }) router.Run(":8088") } http client: old: curl -X POST "127.0.0.1:8088/login?appkey=china&page=1&size=10" {"page":1,"size":10,"appkey":""} now: curl -X POST "127.0.0.1:8088/login?appkey=china&page=1&size=10" {"page":1,"size":10,"appkey":"china"} * Modify judgment conditions

authored by guonaihong · committed by 田欧 · parent 965d74c · Browse files · 2 files+49 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+49 added−7 removed

  • other:+49 added−7 removed

2 files+49 added−7 removed

modified:binding/binding_test.go+39 added
22 unmodified lines
23 "github.com/stretchr/testify/assert"24 "github.com/ugorji/go/codec"25)2627type appkey struct {28 Appkey string `json:"appkey" form:"appkey"`29}3031type QueryTest struct {32 Page int `json:"page" form:"page"`33 Size int `json:"size" form:"size"`34 appkey35}3637type FooStruct struct {38 Foo string `msgpack:"foo" json:"foo" form:"foo" xml:"foo" binding:"required"`39}40
157 unmodified lines
198 "/?foo=bar&bar=foo", "/?bar2=foo",199 "", "")200}201202func TestBindingFormEmbeddedStruct(t *testing.T) {203 testFormBindingEmbeddedStruct(t, "POST",204 "/", "/",205 "page=1&size=2&appkey=test-appkey", "bar2=foo")206}207208func TestBindingFormEmbeddedStruct2(t *testing.T) {209 testFormBindingEmbeddedStruct(t, "GET",210 "/?page=1&size=2&appkey=test-appkey", "/?bar2=foo",211 "", "")212}213214func TestBindingFormDefaultValue(t *testing.T) {215 testFormBindingDefaultValue(t, "POST",216 "/", "/",217 "foo=bar", "bar2=foo")
491 unmodified lines
709 assert.Equal(t, tag.Name, expectedName)710 assert.Equal(t, tag.S.Age, expectedAge)711}712713func testFormBindingEmbeddedStruct(t *testing.T, method, path, badPath, body, badBody string) {714 b := Form715 assert.Equal(t, "form", b.Name())716717 obj := QueryTest{}718 req := requestWithBody(method, path, body)719 if method == "POST" {720 req.Header.Add("Content-Type", MIMEPOSTForm)721 }722 err := b.Bind(req, &obj)723 assert.NoError(t, err)724 assert.Equal(t, 1, obj.Page)725 assert.Equal(t, 2, obj.Size)726 assert.Equal(t, "test-appkey", obj.Appkey)727728}729730func testFormBinding(t *testing.T, method, path, badPath, body, badBody string) {731 b := Form732 assert.Equal(t, "form", b.Name())733
450 unmodified lines
modified:binding/form_mapping.go+10 added-7 removed
68 unmodified lines
69 }70 return isSetted, nil71 }7273 if vKind != reflect.Struct || !field.Anonymous {73 ok, err := tryToSetValue(value, field, setter, tag)74 if err != nil {75 return false, err74 ok, err := tryToSetValue(value, field, setter, tag)75 if err != nil {76 return false, err77 }78 if ok {79 return true, nil80 }81 }77 if ok {78 return true, nil79 }8283 if vKind == reflect.Struct {84 tValue := value.Type()8586 var isSetted bool87 for i := 0; i < value.NumField(); i++ {86 if !value.Field(i).CanSet() {88 sf := tValue.Field(i)89 if sf.PkgPath != "" && !sf.Anonymous { // unexported90 continue91 }92 ok, err := mapping(value.Field(i), tValue.Field(i), setter, tag)93 if err != nil {
240 unmodified lines