Documentation ¶
Index ¶
- func Bind(ctx context.Context, v interface{}, data interface{}) (context.Context, error)
- func BindWithValidate(ctx context.Context, v interface{}, data interface{}) (context.Context, error)
- func MethodName() string
- func SliceToString(b []byte) (s string)
- func StringToSlice(s string) (b []byte)
- func TrySet(value reflect.Value, vs []string, opt *DefaultOption) error
- type DefaultOption
- type Execer
- type Gbind
- func (g *Gbind) Bind(ctx context.Context, v interface{}, data interface{}) (context.Context, error)
- func (g *Gbind) BindWithValidate(ctx context.Context, v interface{}, data interface{}) (context.Context, error)
- func (g *Gbind) RegisterBindFunc(name string, fn NewExecer)
- func (g *Gbind) RegisterCustomValidation(tag string, fn validator.Func, callValidationEvenIfNull ...bool) error
- type NewExecer
- type OptApply
- type StructValidator
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Bind ¶
Bind parses the data interface and stores the result in the value pointed to by v. If v is nil or not a pointer, Bind returns an Err.
func BindWithValidate ¶
func BindWithValidate(ctx context.Context, v interface{}, data interface{}) (context.Context, error)
BindWithValidate parses the data interface and stores the result in the value pointed to by v and check whether the data meets the requirements. If v is nil or not a pointer, Bind returns an Err.
func MethodName ¶
func MethodName() string
MethodName get the name of the current executing function
func SliceToString ¶
SliceToString slice to string with out data copy
func StringToSlice ¶
StringToSlice string to slice with out data copy
Types ¶
type DefaultOption ¶
DefaultOption options for the default values
type Execer ¶
type Execer interface { Exec(ctx context.Context, value reflect.Value, data interface{}, opt *DefaultOption) (context.Context, error) Name() string }
Execer A Execer need to implement the methods
type Gbind ¶
type Gbind struct {
// contains filtered or unexported fields
}
Gbind contains the gbind settings and cache
Example ¶
package main import ( "context" "encoding/json" "fmt" "net/http" "net/http/httptest" "net/url" ) type Params struct { API string `gbind:"http.path,default=/api/test"` Appkey string `gbind:"http.query.appkey,default=appkey-default"` Page int `gbind:"http.query.page,default=1"` Size int `gbind:"http.query.size,default=10"` Bduss string `gbind:"http.cookie.BDUSS" validate:"required" err_msg:"please complete the login operation first"` Host string `gbind:"http.header.host,default=www.baidu.com"` Uids []int `gbind:"http.form.uids"` } func Controller(w http.ResponseWriter, r *http.Request) { var requestParams = &Params{} if _, err := BindWithValidate(context.Background(), requestParams, r); err != nil { w.WriteHeader(http.StatusBadRequest) return } bs, _ := json.MarshalIndent(requestParams, "", "\t") w.Write(bs) } func main() { w := httptest.NewRecorder() u, _ := url.Parse("http://gbind.baidu.com/api/test?appkey=abc&page=2") r := &http.Request{ Method: http.MethodPost, Header: map[string][]string{ "Host": {"gbind.baidu.com"}, }, PostForm: url.Values{ "uids": {"1", "2", "3"}, }, URL: u, } r.AddCookie(&http.Cookie{ Name: "BDUSS", Value: "foo-bar-andsoon", }) Controller(w, r) fmt.Println(w.Result().Status) fmt.Println(w.Body.String()) }
Output: 200 OK { "API": "/api/test", "Appkey": "abc", "Page": 2, "Size": 10, "Bduss": "foo-bar-andsoon", "Host": "gbind.baidu.com", "Uids": [ 1, 2, 3 ] }
func (*Gbind) Bind ¶
Bind parses the data interface and stores the result in the value pointed to by v. If v is nil or not a pointer, Bind returns an Err.
func (*Gbind) BindWithValidate ¶
func (g *Gbind) BindWithValidate(ctx context.Context, v interface{}, data interface{}) (context.Context, error)
BindWithValidate parses the data interface and stores the result in the value pointed to by v and check whether the data meets the requirements. If v is nil or not a pointer, Bind returns an Err.
func (*Gbind) RegisterBindFunc ¶
RegisterBindFunc adds a bind Excer with the given name
func (*Gbind) RegisterCustomValidation ¶
func (g *Gbind) RegisterCustomValidation(tag string, fn validator.Func, callValidationEvenIfNull ...bool) error
RegisterCustomValidation adds a validation with the given tag
NOTES: - if the key already exists, the previous validation function will be replaced. - this method is not thread-safe it is intended that these all be registered prior to any validation
type OptApply ¶
type OptApply func(opt *options)
OptApply modify the default option
func WithBindTag ¶
WithBindTag allows you to change the tag name used in structs
func WithDefaultSplitFlag ¶
WithDefaultSplitFlag allows you to change the splitFlag used in structs
func WithErrTag ¶
WithErrTag allows you to change the tag name used in structs
func WithUseNumberForJSON ¶
WithUseNumberForJSON allows you to change the Decoder to unmarshal a number into an interface{} as a Number instead of as a float64
type StructValidator ¶
type StructValidator interface {
ValidateStruct(interface{}) error
}
StructValidator StructValidator
var Validator StructValidator = &defaultValidator{}
Validator Validator