Documentation ¶
Index ¶
- Constants
- Variables
- func Bind(r *http.Request, obj interface{}) error
- func BindBody(r *http.Request, obj interface{}) error
- func BindQuery(r *http.Request, obj interface{}) error
- func BindUri(r *http.Request, obj interface{}) error
- func MapForm(ptr interface{}, set Setter) error
- func MapFormByTag(ptr interface{}, set Setter, tag string) error
- func MapHeader(ptr interface{}, h map[string][]string) error
- func MappingByPtr(ptr interface{}, setter Setter, tag string) error
- func MustBindWith(r *http.Request, obj interface{}, b Binding) error
- func NewReq[REQ any](r *http.Request) (*REQ, error)
- func RegisterBodyBinding(name string, unmarshaller func(data []byte, obj any) error)
- func RegisterBodyBindingByDecoder(name string, newDecoder func(io.Reader) encoding.Decoder)
- func SetByKV(value reflect.Value, field reflect.StructField, kv Arg, tagValue string, ...) (isSet bool, err error)
- func SetByMultipartFormFile(value reflect.Value, field reflect.StructField, files []*multipart.FileHeader) (isSet bool, err error)
- func SetTag(tag string)
- func ShouldBind(r *http.Request, obj interface{}) error
- func ShouldBindBody(r *http.Request, obj interface{}) error
- func ShouldBindQuery(r *http.Request, obj interface{}) error
- func ShouldBindUri(r *http.Request, obj interface{}) error
- func ShouldBindWith(r *http.Request, obj interface{}, b Binding) error
- func Validate(obj interface{}) error
- type Arg
- type ArgSource
- type Binding
- type BindingBody
- type FormSource
- type HeaderSource
- type KVSource
- type MultipartSource
- type SetOptions
- type Setter
- type UriSource
Constants ¶
const ( MIMEJSON = "application/json" MIMEHTML = "text/html" MIMEXML = "application/xml" MIMEXML2 = "text/xml" MIMEPlain = "text/plain" MIMEPOSTForm = "application/x-www-form-urlencoded" MIMEMultipartPOSTForm = "multipart/form-data" MIMEPROTOBUF = "application/x-protobuf" MIMEMSGPACK = "application/x-msgpack" MIMEMSGPACK2 = "application/msgpack" MIMEYAML = "application/x-yaml" )
Variables ¶
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.
var Tag = "json"
var Validator = validator.DefaultValidator
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 BindUri ¶
MustBindWith binds the passed struct pointer using the specified binding engine. BindUri binds the passed struct pointer using binding.Uri. It will abort the request with HTTP 400 if any error occurs.
func MapFormByTag ¶
func MappingByPtr ¶
func MustBindWith ¶
MustBindWith 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 RegisterBodyBinding ¶ added in v0.1.9
func RegisterBodyBindingByDecoder ¶ added in v0.1.9
func SetByKV ¶
func SetByKV(value reflect.Value, field reflect.StructField, kv Arg, tagValue string, opt SetOptions) (isSet bool, err error)
func SetByMultipartFormFile ¶
func SetByMultipartFormFile(value reflect.Value, field reflect.StructField, files []*multipart.FileHeader) (isSet bool, err error)
func ShouldBind ¶
ShouldBind checks the Content-Type to select a binding engine automatically, Depending the "Content-Type" header different bindings are used:
"application/json" --> JSON binding "application/xml" --> XML binding
otherwise --> returns an error It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input. It decodes the json payload into the struct specified as a pointer. Like c.GinBind() but this method does not set the response status code to 400 and abort if the json is not valid.
func ShouldBindBody ¶ added in v0.1.9
func ShouldBindQuery ¶
ShouldBindQuery is a shortcut for c.ShouldBindWith(obj, binding.Query).
func ShouldBindUri ¶
ShouldBindUri binds the passed struct pointer using the specified binding engine.
func ShouldBindWith ¶
ShouldBindWith binds the passed struct pointer using the specified binding engine. See the binding package.
Types ¶
type Binding ¶
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.
type BindingBody ¶
BindingBody adds BindBody method to Binding. BindBody is similar with GinBind, but it reads the body from supplied bytes instead of req.Body.
type FormSource ¶
func (FormSource) TrySet ¶
func (form FormSource) TrySet(value reflect.Value, field reflect.StructField, tagValue string, opt SetOptions) (isSet bool, err error)
TrySet tries to set a value by request's form source (like map[string][]string)
type HeaderSource ¶
func (HeaderSource) TrySet ¶
func (hs HeaderSource) TrySet(value reflect.Value, field reflect.StructField, tagValue string, opt SetOptions) (isSet bool, err error)
type MultipartSource ¶
func (*MultipartSource) TrySet ¶
func (r *MultipartSource) TrySet(value reflect.Value, field reflect.StructField, key string, opt SetOptions) (isSet bool, err error)
TrySet tries to set a value by the multipart request with the binding a form file
type SetOptions ¶
type SetOptions struct {
// contains filtered or unexported fields
}
type Setter ¶
type Setter interface {
TrySet(value reflect.Value, field reflect.StructField, key string, opt SetOptions) (isSet bool, err error)
}
Setter tries to set value on a walking by fields of a struct