gin/binding/query.go
guonaihong 48e871a76d binding: add ShouldBindQuery2 and BindQuery2
```go
ackage main

import (
	"github.com/gin-gonic/gin"
)

type testQuery struct {
	Size int    `query:"size"`
	Page int    `query:"page"`
	Ak   string `query:"ak"`
}

func main() {
	g := gin.Default()

	g.GET("/query", func(c *gin.Context) {
		q := testQuery{}
		err := c.ShouldBindQuery2(&q)
		if err != nil {
			return
		}
		c.JSON(200, q)
	})

	g.Run()
}

// curl '127.0.0.1:8080/query?size=10&page=20&ak=test'
```
2019-10-14 22:04:27 +08:00

37 lines
773 B
Go

// Copyright 2017 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package binding
import "net/http"
type queryBinding struct{}
func (queryBinding) Name() string {
return "query"
}
func (queryBinding) Bind(req *http.Request, obj interface{}) error {
values := req.URL.Query()
if err := mapForm(obj, values); err != nil {
return err
}
return validate(obj)
}
// Support for query tag binding data
type queryBinding2 struct{}
func (queryBinding2) Name() string {
return "query"
}
func (queryBinding2) Bind(req *http.Request, obj interface{}) error {
if err := mapFormByTag(obj, req.URL.Query(), "query"); err != nil {
return err
}
return validate(obj)
}