binding

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2025 License: MIT, Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Uri    = uriBinding{}
	Query  = queryBinding{}
	Header = headerBinding{}

	CustomBody    = bodyBinding{/* contains filtered or unexported fields */}
	FormPost      = formPostBinding{}
	FormMultipart = formMultipartBinding{}
)

These implement the Binding interface and can be used to bind the data present in the request to struct instances.

View Source
var Tag = "json"

Validator is the default validator which implements the StructValidator interface. It uses https://github.com/go-playground/validator/tree/v8.18.2 under the hood.

Functions

func Bind

func Bind(r *http.Request, obj interface{}) error

func BindBody added in v0.1.9

func BindBody(r *http.Request, obj interface{}) error

func BindHeader added in v0.12.0

func BindHeader(r *http.Request, obj interface{}) error

func BindQuery

func BindQuery(r *http.Request, obj interface{}) error

BindQuery is a shortcut for c.BindWith(obj, binding.Query).

func BindUri

func BindUri(r *http.Request, obj interface{}) error

func BindWith added in v0.12.0

func BindWith(r *http.Request, obj interface{}, b Binding) error

BindWith binds the passed struct pointer using the specified binding engine. It will abort the request with HTTP 400 if any error occurs. See the binding package.

func MapHeader

func MapHeader(ptr interface{}, h map[string][]string) error

func NewReq

func NewReq[REQ any](r *http.Request) (*REQ, error)

func SetByMultipartFormFile

func SetByMultipartFormFile(value reflect.Value, field *reflect.StructField, files []*multipart.FileHeader) (isSet bool, err error)

func SetTag

func SetTag(tag string)

func Validate

func Validate(obj interface{}) error

Types

type Binding

type Binding interface {
	Name() string
	Bind(*http.Request, interface{}) error
}

Binding describes the interface which needs to be implemented for binding the data present in the request such as JSON request body, query parameters or the form POST.

func Body

func Body(contentType string) Binding

func Default

func Default(method string, contentType string) Binding

Default returns the appropriate Binding instance based on the HTTP method and the content type.

type HeaderSource

type HeaderSource map[string][]string

func (HeaderSource) Peek

func (hs HeaderSource) Peek(key string) ([]string, bool)

func (HeaderSource) TrySet

func (hs HeaderSource) TrySet(value reflect.Value, field *reflect.StructField, tagValue string, opt mtos.SetOptions) (isSet bool, err error)

type MultipartSource

type MultipartSource http.Request

func (*MultipartSource) TrySet

func (r *MultipartSource) TrySet(value reflect.Value, field *reflect.StructField, key string, opt mtos.SetOptions) (isSet bool, err error)

TrySet tries to set a value by the multipart request with the binding a form file

type Peek added in v0.12.0

type Peek interface {
	Peek(key string) string
}

type Source added in v0.12.0

type Source interface {
	Uri() Peek
	Query() Peek
	Header() Peek
	BodyBind(io.Reader) error
}

type UriSource

type UriSource http.Request

func (*UriSource) Peek

func (req *UriSource) Peek(key string) ([]string, bool)

func (*UriSource) TrySet

func (req *UriSource) TrySet(value reflect.Value, field *reflect.StructField, tagValue string, opt mtos.SetOptions) (isSet bool, err error)

TrySet tries to set a value by request's form source (like map[string][]string)

Jump to

Keyboard shortcuts

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