Documentation
¶
Overview ¶
Package binding provide some common binder for binding http.Request data to strcut
Auto picks a binder by content type: query data for a body-less method, form values, JSON or XML. A multipart/form-data request binds its form values and its uploaded files, so a struct field of type *multipart.FileHeader (or a slice of them) is filled by the same call. Use Form, Query, Header, JSON, XML or File to bind from one source only.
Index ¶
- Variables
- func Auto(r *http.Request, obj any) (err error)
- func Bind(r *http.Request, obj any) error
- func DecodeFiles(files map[string][]*multipart.FileHeader, ptr any, tagName string) error
- func DecodeMultipart(values map[string][]string, files map[string][]*multipart.FileHeader, ptr any, ...) error
- func DecodeUrlValues(values map[string][]string, ptr any, tagName string) error
- func DecodeValues(values map[string][]string, ptr any, tagName string) error
- func DisableValidator()
- func MustBind(r *http.Request, obj any)
- func Register(name string, b Binder)
- func Remove(names ...string)
- func ResetValidator()
- func Validate(obj any) error
- func ValidateRequest(r *http.Request, obj any) error
- type Binder
- type BinderFunc
- type DataValidator
- type FileBinder
- type FormBinder
- type HeaderBinder
- type JSONBinder
- type QueryBinder
- type RequestValidator
- type XMLBinder
Constants ¶
This section is empty.
Variables ¶
var ( Form = FormBinder{FormTagName} Header = HeaderBinder{HeaderTagName} Query = QueryBinder{QueryTagName} JSON = JSONBinder{} XML = XMLBinder{} // Binders mapping Binders = map[string]Binder{ "xml": XML, "json": JSON, "query": Query, "form": Form, "header": Header, "file": File, } )
build-in data binder
var DefaultMaxMemory int64 = 32 << 20 // 32 MB
DefaultMaxMemory for parse data form
var File = FileBinder{FormTagName}
File is the built-in binder for uploaded files. It shares the "form" tag with Form, so `form:"avatar"` names the field for both.
var FormTagName = "form"
FormTagName for decode form data
var HeaderTagName = "header"
HeaderTagName for binding data
var QueryTagName = "query"
TagName for decode url.Values(form,query) data
Functions ¶
func Auto ¶
Auto bind request data to a ptr value
body, err := ioutil.ReadAll(c.Request().Body)
if err != nil {
c.Logger().Errorf("could not read request body: %v", err)
}
c.Set("request_body", body)
// fix: can not read request body multiple times
c.Request().Body = ioutil.NopCloser(bytes.NewReader(body))
func DecodeFiles ¶ added in v2.1.2
DecodeFiles binds uploaded files into the struct pointer ptr.
It is the file counterpart of DecodeUrlValues: fields without an upload keep their zero value and an upload without a matching field is ignored. A target that does not point to a struct is a no-op, so DecodeMultipart can hand over any target the value decoder accepts.
func DecodeMultipart ¶ added in v2.1.2
func DecodeMultipart( values map[string][]string, files map[string][]*multipart.FileHeader, ptr any, tagName string, ) error
DecodeMultipart binds the form values and the uploaded files of a multipart/form-data request into ptr.
Values and files are decoded first and the validator runs once afterwards, so rules that look at an upload (required, image, mime) see the bound file. Binders that hold the request (Auto, Form.Bind) also hand it to a RequestValidator; this entry point has no request and passes nil.
func DecodeUrlValues ¶
DecodeUrlValues data to struct
func DecodeValues ¶ added in v2.1.2
DecodeValues data to struct, without running the validator.
func ValidateRequest ¶ added in v2.1.2
ValidateRequest validates obj with the request at hand.
A validator that implements RequestValidator receives r, so it can look at the raw request as well as the bound struct. Any other validator is called through Validate, and a nil request always takes that path.
Types ¶
type BinderFunc ¶
BinderFunc bind func, implement the Binder() interface
type DataValidator ¶
DataValidator interface
var Validator DataValidator
Validator validates bound data. It is nil by default; applications can install their own validator adapter when validation is needed.
Implement RequestValidator instead when a rule needs the request itself.
type FileBinder ¶ added in v2.1.2
type FileBinder struct {
TagName string
}
FileBinder binds uploaded files (multipart/form-data) into struct fields.
Field names are resolved like form values: the tag value (first comma part) when present, else the field name. Nested structs use a dotted path ("avatar.path"), embedded structs are flattened and "-" skips a field.
Supported field types are multipart.FileHeader, *multipart.FileHeader and slices or arrays of either. A single-value field receives the first upload for its name, a slice receives all of them.
func (FileBinder) Bind ¶ added in v2.1.2
func (b FileBinder) Bind(r *http.Request, ptr any) error
Bind uploaded files from http.Request into ptr.
It parses the multipart body itself, so it works on a request that no other binder has touched yet.
func (FileBinder) BindValues ¶ added in v2.1.2
func (b FileBinder) BindValues(files map[string][]*multipart.FileHeader, ptr any) error
BindValues binds an already parsed multipart file map into ptr.
type FormBinder ¶
type FormBinder struct {
TagName string
}
FormBinder binding Form/url.Values data to struct
func (FormBinder) Bind ¶
func (b FormBinder) Bind(r *http.Request, ptr any) error
Bind Form data from http.Request
A multipart/form-data request binds its uploaded files as well, see DecodeMultipart.
func (FormBinder) BindValues ¶
func (b FormBinder) BindValues(values url.Values, ptr any) error
BindValues data from url.Values
type HeaderBinder ¶
type HeaderBinder struct {
TagName string
}
HeaderBinder binding URL query data to struct
func (HeaderBinder) Bind ¶
func (b HeaderBinder) Bind(r *http.Request, ptr any) error
Bind Header data binding
func (HeaderBinder) BindValues ¶
func (b HeaderBinder) BindValues(headers map[string][]string, ptr any) error
BindValues data from headers
type JSONBinder ¶
type JSONBinder struct{}
JSONBinder binding JSON data to struct
func (JSONBinder) Bind ¶
func (JSONBinder) Bind(r *http.Request, ptr any) error
Bind JSON data from http.Request
type QueryBinder ¶
type QueryBinder struct {
TagName string
}
QueryBinder binding URL query data to struct
func (QueryBinder) Bind ¶
func (b QueryBinder) Bind(r *http.Request, ptr any) error
Bind Query data binder
func (QueryBinder) BindValues ¶
func (b QueryBinder) BindValues(values url.Values, ptr any) error
BindValues data from url.Values
type RequestValidator ¶ added in v2.1.2
type RequestValidator interface {
DataValidator
ValidateRequest(r *http.Request, obj any) error
}
RequestValidator is an optional extension of DataValidator. When the installed Validator implements it, every binder that holds the request calls ValidateRequest instead of Validate, so a rule can look at the raw request (an uploaded file, for example) and not only at the bound struct.