binding

package
v2.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2026 License: MIT Imports: 12 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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

View Source
var DefaultMaxMemory int64 = 32 << 20 // 32 MB

DefaultMaxMemory for parse data form

File is the built-in binder for uploaded files. It shares the "form" tag with Form, so `form:"avatar"` names the field for both.

View Source
var FormTagName = "form"

FormTagName for decode form data

View Source
var HeaderTagName = "header"

HeaderTagName for binding data

View Source
var QueryTagName = "query"

TagName for decode url.Values(form,query) data

Functions

func Auto

func Auto(r *http.Request, obj any) (err error)

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 Bind

func Bind(r *http.Request, obj any) error

Bind auto bind request data to an struct ptr

func DecodeFiles added in v2.1.2

func DecodeFiles(files map[string][]*multipart.FileHeader, ptr any, tagName string) error

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

func DecodeUrlValues(values map[string][]string, ptr any, tagName string) error

DecodeUrlValues data to struct

func DecodeValues added in v2.1.2

func DecodeValues(values map[string][]string, ptr any, tagName string) error

DecodeValues data to struct, without running the validator.

func DisableValidator

func DisableValidator()

DisableValidator for data binding

func MustBind

func MustBind(r *http.Request, obj any)

MustBind auto bind request data to an struct ptr

func Register

func Register(name string, b Binder)

Register new binder with name

func Remove

func Remove(names ...string)

Remove exists binder(s)

func ResetValidator

func ResetValidator()

ResetValidator for the package

func Validate

func Validate(obj any) error

Validate bounded data

func ValidateRequest added in v2.1.2

func ValidateRequest(r *http.Request, obj any) error

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 Binder

type Binder interface {
	Name() string
	Bind(r *http.Request, obj any) error
}

Binder interface

func GetBinder

func GetBinder(name string) Binder

GetBinder get an binder by name

type BinderFunc

type BinderFunc func(r *http.Request, obj any) error

BinderFunc bind func, implement the Binder() interface

func (BinderFunc) Bind

func (fn BinderFunc) Bind(r *http.Request, obj any) error

Bind implements the Binder interface

func (BinderFunc) Name

func (fn BinderFunc) Name() string

Name implements the Binder interface

type DataValidator

type DataValidator interface {
	Validate(i any) error
}

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.

func (FileBinder) Name added in v2.1.2

func (FileBinder) Name() string

Name get name

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

func (FormBinder) Name

func (FormBinder) Name() string

Name get name

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

func (HeaderBinder) Name

func (HeaderBinder) Name() string

Name get name

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

func (JSONBinder) BindBytes

func (JSONBinder) BindBytes(bts []byte, ptr any) error

BindBytes raw JSON data to struct

func (JSONBinder) Name

func (JSONBinder) Name() string

Name get name

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

func (QueryBinder) Name

func (QueryBinder) Name() string

Name get name

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.

type XMLBinder

type XMLBinder struct{}

XMLBinder Xml data binder struct binding-tag default is "xml". eg: `xml:"field"`

func (XMLBinder) Bind

func (XMLBinder) Bind(r *http.Request, obj any) error

Bind XML data binder

func (XMLBinder) BindBytes

func (XMLBinder) BindBytes(bts []byte, ptr any) error

BindBytes raw JSON data to struct

func (XMLBinder) Name

func (XMLBinder) Name() string

Name get name

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL