README ¶
binding
A powerful fasthttp request parameters binder that supports struct tag expression.
Syntax
The parameter position in HTTP request:
expression | renameable | description |
---|---|---|
path:"$name" or path:"$name,required" |
Yes | URL path parameter |
query:"$name" or query:"$name,required" |
Yes | URL query parameter |
raw_body:"" or raw_body:"required" |
Yes | The raw bytes of body |
form:"$name" or form:"$name,required" |
Yes | The field in body, support:application/x-www-form-urlencoded ,multipart/form-data |
protobuf:"...(raw syntax)" |
No | The field in body, support:application/x-protobuf |
json:"$name" or json:"$name,required" |
No | The field in body, support:application/json |
header:"$name" or header:"$name,required" |
Yes | Header parameter |
cookie:"$name" or cookie:"$name,required" |
Yes | Cookie parameter |
default:"$value" |
Yes | Default parameter |
vd:"...(tagexpr validator syntax)" |
Yes | The tagexpr expression of validator |
NOTE:
"$name"
is variable placeholder- If
"$name"
is empty, use the name of field - If
"$name"
is-
, omit the field - Expression
required
orreq
indicates that the parameter is required default:"$value"
defines the default value for fallback when no binding is successful- If no position is tagged, try bind parameters from the body when the request has body,
otherwise try bind from the URL query - When there are multiple tags or no tags, the order in which to try to bind is:
- path
- form
- query
- cookie
- header
- protobuf
- json
- default
Type Unmarshalor
TimeRFC3339-binding function is registered by default.
Register your own binding function for the specified type, e.g.:
MustRegTypeUnmarshal(reflect.TypeOf(time.Time{}), func(v string, emptyAsZero bool) (reflect.Value, error) {
if v == "" && emptyAsZero {
return reflect.ValueOf(time.Time{}), nil
}
t, err := time.Parse(time.RFC3339, v)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(t), nil
})
Documentation ¶
Index ¶
- func Bind(structPointer interface{}, req *fasthttp.RequestCtx) error
- func BindAndValidate(structPointer interface{}, req *fasthttp.RequestCtx) error
- func MustRegTypeUnmarshal(t reflect.Type, fn func(v string, emptyAsZero bool) (reflect.Value, error))
- func RegTypeUnmarshal(t reflect.Type, fn func(v string, emptyAsZero bool) (reflect.Value, error)) error
- func ResetJSONUnmarshaler(fn JSONUnmarshaler)
- func SetErrorFactory(bindErrFactory, validatingErrFactory func(failField, msg string) error)
- func SetLooseZeroMode(enable bool)
- func Validate(value interface{}) error
- type Binding
- func (b *Binding) Bind(recvPointer interface{}, req *fasthttp.RequestCtx) error
- func (b *Binding) BindAndValidate(recvPointer interface{}, req *fasthttp.RequestCtx) error
- func (b *Binding) SetErrorFactory(bindErrFactory, validatingErrFactory func(failField, msg string) error) *Binding
- func (b *Binding) SetLooseZeroMode(enable bool) *Binding
- func (b *Binding) Validate(value interface{}) error
- type Config
- type Error
- type JSONUnmarshaler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Bind ¶
func Bind(structPointer interface{}, req *fasthttp.RequestCtx) error
Bind binds the request parameters.
func BindAndValidate ¶
func BindAndValidate(structPointer interface{}, req *fasthttp.RequestCtx) error
BindAndValidate binds the request parameters and validates them if needed.
func MustRegTypeUnmarshal ¶
func MustRegTypeUnmarshal(t reflect.Type, fn func(v string, emptyAsZero bool) (reflect.Value, error))
MustRegTypeUnmarshal registers unmarshalor function of type. NOTE:
panic if exist error.
func RegTypeUnmarshal ¶
func RegTypeUnmarshal(t reflect.Type, fn func(v string, emptyAsZero bool) (reflect.Value, error)) error
RegTypeUnmarshal registers unmarshalor function of type.
func ResetJSONUnmarshaler ¶
func ResetJSONUnmarshaler(fn JSONUnmarshaler)
ResetJSONUnmarshaler reset the JSON Unmarshal function. NOTE: verifyingRequired is true if the required tag is supported.
func SetErrorFactory ¶
SetErrorFactory customizes the factory of validation error. NOTE:
If errFactory==nil, the default is used
func SetLooseZeroMode ¶
func SetLooseZeroMode(enable bool)
SetLooseZeroMode if set to true, the empty string request parameter is bound to the zero value of parameter. NOTE:
The default is false; Suitable for these parameter types: query/header/cookie/form .
Types ¶
type Binding ¶
type Binding struct {
// contains filtered or unexported fields
}
Binding binding and verification tool for http request
func Default ¶
func Default() *Binding
Default returns the default binding. NOTE:
path tag name is 'path'; query tag name is 'query'; header tag name is 'header'; cookie tag name is 'cookie'; raw_body tag name is 'raw_body'; form tag name is 'form'; validator tag name is 'vd'; protobuf tag name is 'protobuf'; json tag name is 'json'; LooseZeroMode is false.
func (*Binding) Bind ¶
func (b *Binding) Bind(recvPointer interface{}, req *fasthttp.RequestCtx) error
Bind binds the request parameters.
func (*Binding) BindAndValidate ¶
func (b *Binding) BindAndValidate(recvPointer interface{}, req *fasthttp.RequestCtx) error
BindAndValidate binds the request parameters and validates them if needed.
func (*Binding) SetErrorFactory ¶
func (b *Binding) SetErrorFactory(bindErrFactory, validatingErrFactory func(failField, msg string) error) *Binding
SetErrorFactory customizes the factory of validation error. NOTE:
If errFactory==nil, the default is used
func (*Binding) SetLooseZeroMode ¶
SetLooseZeroMode if set to true, the empty string request parameter is bound to the zero value of parameter. NOTE:
The default is false; Suitable for these parameter types: query/header/cookie/form .
type Config ¶
type Config struct { // LooseZeroMode if set to true, // the empty string request parameter is bound to the zero value of parameter. // NOTE: Suitable for these parameter types: query/header/cookie/form . LooseZeroMode bool // PathParam use 'path' by default when empty PathParam string // Query use 'query' by default when empty Query string // Header use 'header' by default when empty Header string // Cookie use 'cookie' by default when empty Cookie string // RawBody use 'raw' by default when empty RawBody string // FormBody use 'form' by default when empty FormBody string // Validator use 'vd' by default when empty Validator string // contains filtered or unexported fields }
Config the struct tag naming and so on
type JSONUnmarshaler ¶
JSONUnmarshaler is the interface implemented by types that can unmarshal a JSON description of themselves.