Skip to content
Myr

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

6 files+109 added−1 removed

modified:binding/binding_test.go+27 added
65 unmodified lines
66 "/?foo=bar&bar=foo", "/?bar2=foo",67 "", "")68}6970func TestBindingQuery(t *testing.T) {71 testQueryBinding(t, "POST",72 "/?foo=bar&bar=foo", "/",73 "foo=unused", "bar2=foo")74}7576func TestBindingQuery2(t *testing.T) {77 testQueryBinding(t, "GET",78 "/?foo=bar&bar=foo", "/?bar2=foo",79 "foo=unused", "")80}8182func TestBindingXML(t *testing.T) {83 testBodyBinding(t,84 XML, "xml",85 "/", "/",
129 unmodified lines
215 err = JSON.Bind(req, &obj)216 assert.Error(t, err)217}218219func testQueryBinding(t *testing.T, method, path, badPath, body, badBody string) {220 b := Query221 assert.Equal(t, b.Name(), "query")222223 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}233234func testBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {235 assert.Equal(t, b.Name(), name)236237 obj := FooStruct{}
49 unmodified lines
modified:binding/binding.go+1 added
37 unmodified lines
38var (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 lines
added: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.45package binding67import (8 "net/http"9)1011type queryBinding struct{}1213func (queryBinding) Name() string {14 return "query"15}1617func (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 lines
1185 assert.Equal(t, obj.Foo, "bar")1186 assert.Equal(t, w.Body.Len(), 0)1187}11881189func TestContextBindWithQuery(t *testing.T) {1190 w := httptest.NewRecorder()1191 c, _ := CreateTestContext(w)11921193 c.Request, _ = http.NewRequest("POST", "/?foo=bar&bar=foo", bytes.NewBufferString("foo=unused"))11941195 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}12041205func TestContextBadAutoBind(t *testing.T) {1206 w := httptest.NewRecorder()1207 c, _ := CreateTestContext(w)1208
74 unmodified lines
modified:context.go+5 added
465 unmodified lines
466func (c *Context) BindJSON(obj interface{}) error {467 return c.MustBindWith(obj, binding.JSON)468}469470// BindQuery is a shortcut for c.MustBindWith(obj, binding.Query)471func (c *Context) BindQuery(obj interface{}) error {472 return c.MustBindWith(obj, binding.Query)473}474475// 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.478func (c *Context) MustBindWith(obj interface{}, b binding.Binding) (err error) {
321 unmodified lines
modified:README.md+37 added-1 removed
458 unmodified lines
459 router.Run(":8080")460}461```462463### Bind Query String463### Only Bind Query String464465`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).466467```go468package main469470import (471 "log"472473 "github.com/gin-gonic/gin"474)475476type Person struct {477 Name string `form:"name"`478 Address string `form:"address"`479}480481func main() {482 route := gin.Default()483 route.Any("/testing", startPage)484 route.Run(":8085")485}486487func 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}496497```498499### Bind Query String or Post Data500501See the [detail information](https://github.com/gin-gonic/gin/issues/742#issuecomment-264681292).502503```go504package main
621 unmodified lines