feat(context): add BindQuery func (#1029)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* feat(context): add BindQuery func, only parse/bind the query string params. * docs(readme): add BindQuery section. * docs(readme): fix import. * docs(readme): separate import
authored by Eason Lin · committed by Javier Provecho Fernandez · parent 74221b8 · Browse files · 6 files·+109 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
6 files+109 added−1 removed
- other:+109 added−1 removed
Provenance
- Commit
c19aa05- Tree
008f4a8- Author
Eason Lin <easonlin404@gmail.com>- Committer
Javier Provecho Fernandez <javiertitan@gmail.com>- Parent
74221b8
6 files changed+109 added−1 removed
modified:binding/binding_test.go+27 added
65 unmodified lines66 "/?foo=bar&bar=foo", "/?bar2=foo",67 "", "")68 }69 70+func TestBindingQuery(t *testing.T) {71+ testQueryBinding(t, "POST",72+ "/?foo=bar&bar=foo", "/",73+ "foo=unused", "bar2=foo")74+}75+76+func TestBindingQuery2(t *testing.T) {77+ testQueryBinding(t, "GET",78+ "/?foo=bar&bar=foo", "/?bar2=foo",79+ "foo=unused", "")80+}81+82 func TestBindingXML(t *testing.T) {83 testBodyBinding(t,84 XML, "xml",85 "/", "/",129 unmodified lines215 err = JSON.Bind(req, &obj)216 assert.Error(t, err)217 }218 219+func testQueryBinding(t *testing.T, method, path, badPath, body, badBody string) {220+ b := Query221+ assert.Equal(t, b.Name(), "query")222+223+ obj := FooBarStruct{}224+ req := requestWithBody(method, path, body)225+ if method == "POST" {226+ req.Header.Add("Content-Type", MIMEPOSTForm)227+ }228+ err := b.Bind(req, &obj)229+ assert.NoError(t, err)230+ assert.Equal(t, obj.Foo, "bar")231+ assert.Equal(t, obj.Bar, "foo")232+}233+234 func testBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {235 assert.Equal(t, b.Name(), name)236 237 obj := FooStruct{}49 unmodified linesmodified:binding/binding.go+1 added
37 unmodified lines38 var (39 JSON = jsonBinding{}40 XML = xmlBinding{}41 Form = formBinding{}42+ Query = queryBinding{}43 FormPost = formPostBinding{}44 FormMultipart = formMultipartBinding{}45 ProtoBuf = protobufBinding{}46 MsgPack = msgpackBinding{}27 unmodified linesadded:binding/query.go+23 added
1+// Copyright 2017 Manu Martinez-Almeida. All rights reserved.2+// Use of this source code is governed by a MIT style3+// license that can be found in the LICENSE file.4+5+package binding6+7+import (8+ "net/http"9+)10+11+type queryBinding struct{}12+13+func (queryBinding) Name() string {14+ return "query"15+}16+17+func (queryBinding) Bind(req *http.Request, obj interface{}) error {18+ values := req.URL.Query()19+ if err := mapForm(obj, values); err != nil {20+ return err21+ }22+ return validate(obj)23+}modified:context_test.go+16 added
1184 unmodified lines1185 assert.Equal(t, obj.Foo, "bar")1186 assert.Equal(t, w.Body.Len(), 0)1187 }1188 1189+func TestContextBindWithQuery(t *testing.T) {1190+ w := httptest.NewRecorder()1191+ c, _ := CreateTestContext(w)1192+1193+ c.Request, _ = http.NewRequest("POST", "/?foo=bar&bar=foo", bytes.NewBufferString("foo=unused"))1194+1195+ var obj struct {1196+ Foo string `form:"foo"`1197+ Bar string `form:"bar"`1198+ }1199+ assert.NoError(t, c.BindQuery(&obj))1200+ assert.Equal(t, "foo", obj.Bar)1201+ assert.Equal(t, "bar", obj.Foo)1202+ assert.Equal(t, 0, w.Body.Len())1203+}1204+1205 func TestContextBadAutoBind(t *testing.T) {1206 w := httptest.NewRecorder()1207 c, _ := CreateTestContext(w)1208 74 unmodified linesmodified:context.go+5 added
465 unmodified lines466 func (c *Context) BindJSON(obj interface{}) error {467 return c.MustBindWith(obj, binding.JSON)468 }469 470+// BindQuery is a shortcut for c.MustBindWith(obj, binding.Query)471+func (c *Context) BindQuery(obj interface{}) error {472+ return c.MustBindWith(obj, binding.Query)473+}474+475 // MustBindWith binds the passed struct pointer using the specified binding476 // engine. It will abort the request with HTTP 400 if any error ocurrs.477 // See the binding package.478 func (c *Context) MustBindWith(obj interface{}, b binding.Binding) (err error) {321 unmodified linesmodified:README.md+37 added-1 removed
458 unmodified lines459 router.Run(":8080")460 }461 ```462 463-### Bind Query String463+### Only Bind Query String464 465+`BindQuery` function only binds the query params and not the post data. See the [detail information](https://github.com/gin-gonic/gin/issues/742#issuecomment-315953017).466+467+```go468+package main469+470+import (471+ "log"472+473+ "github.com/gin-gonic/gin"474+)475+476+type Person struct {477+ Name string `form:"name"`478+ Address string `form:"address"`479+}480+481+func main() {482+ route := gin.Default()483+ route.Any("/testing", startPage)484+ route.Run(":8085")485+}486+487+func startPage(c *gin.Context) {488+ var person Person489+ if c.BindQuery(&person) == nil {490+ log.Println("====== Only Bind By Query String ======")491+ log.Println(person.Name)492+ log.Println(person.Address)493+ }494+ c.String(200, "Success")495+}496+497+```498+499+### Bind Query String or Post Data500+501 See the [detail information](https://github.com/gin-gonic/gin/issues/742#issuecomment-264681292).502 503 ```go504 package main621 unmodified lines