binding

package
v1.15.0 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2024 License: MIT, BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Index

Constants

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

View Source
var (
	JSON          = jsonBinding{}
	XML           = xmlBinding{}
	Query         = queryBinding{}
	FormPost      = formPostBinding{}
	FormMultipart = formMultipartBinding{}
	Uri           = uriBinding{}
	ProtoBuf      = protobufBinding{}
	MsgPack       = msgpackBinding{}
	YAML          = yamlBinding{}
	Header        = headerBinding{}
)

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

View Source
var EnableDecoderDisallowUnknownFields = false

EnableDecoderDisallowUnknownFields is used to call the DisallowUnknownFields method on the JSON Decoder instance. DisallowUnknownFields causes the Decoder to return an error when the destination is a struct and the input contains object keys which do not match any non-ignored, exported fields in the destination.

View Source
var EnableDecoderUseNumber = false

EnableDecoderUseNumber is used to call the UseNumber method on the JSON Decoder instance. UseNumber causes the Decoder to unmarshal a number into an interface{} as a Number instead of as a float64.

View Source
var Tag = "json"

Functions

func Bind added in v1.5.11

func Bind(c *gin.Context, obj interface{}) error

func BindJSON added in v1.5.11

func BindJSON(c *gin.Context, obj interface{}) error

BindJSON is a shortcut for c.MustBindWith(obj, binding.JSON).

func BindQuery added in v1.5.11

func BindQuery(c *gin.Context, obj interface{}) error

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

func BindXML added in v1.5.11

func BindXML(c *gin.Context, obj interface{}) error

BindXML is a shortcut for c.MustBindWith(obj, binding.BindXML).

func BindYAML added in v1.5.11

func BindYAML(c *gin.Context, obj interface{}) error

BindYAML is a shortcut for c.MustBindWith(obj, binding.YAML).

func DecodeMsgPack

func DecodeMsgPack(r io.Reader, obj interface{}) error

func DecodeProtobuf

func DecodeProtobuf(body []byte, obj interface{}) error

func MustBindWith added in v1.5.11

func MustBindWith(c *gin.Context, obj interface{}, b Binding) error

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 NewReq added in v1.5.11

func NewReq[REQ any](c *gin.Context) (*REQ, error)

func SetTag

func SetTag(tag string)

func ShouldBind added in v1.5.11

func ShouldBind(c *gin.Context, obj interface{}) error

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 ShouldBindBodyWith added in v1.5.11

func ShouldBindBodyWith(c *gin.Context, obj interface{}, bb BindingBody) (err error)

ShouldBindBodyWith is similar with ShouldBindWith, but it stores the request body into the context, and reuse when it is called again.

NOTE: This method reads the body before binding. So you should use ShouldBindWith for better performance if you need to call only once.

func ShouldBindJSON added in v1.5.11

func ShouldBindJSON(c *gin.Context, obj interface{}) error

ShouldBindJSON is a shortcut for c.ShouldBindWith(obj, binding.JSON).

func ShouldBindQuery added in v1.5.11

func ShouldBindQuery(c *gin.Context, obj interface{}) error

ShouldBindQuery is a shortcut for c.ShouldBindWith(obj, binding.Query).

func ShouldBindUri added in v1.5.11

func ShouldBindUri(r *gin.Context, obj interface{}) error

ShouldBindUri binds the passed struct pointer using the specified binding engine.

func ShouldBindWith added in v1.5.11

func ShouldBindWith(c *gin.Context, obj interface{}, b Binding) error

ShouldBindWith binds the passed struct pointer using the specified binding engine. See the binding package.

func ShouldBindXML added in v1.5.11

func ShouldBindXML(c *gin.Context, obj interface{}) error

ShouldBindXML is a shortcut for c.ShouldBindWith(obj, binding.XML).

func ShouldBindYAML added in v1.5.11

func ShouldBindYAML(c *gin.Context, obj interface{}) error

ShouldBindYAML is a shortcut for c.ShouldBindWith(obj, binding.YAML).

func Validate

func Validate(obj interface{}) error

Types

type Binding

type Binding interface {
	Name() string
	Bind(*gin.Context, 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 added in v1.5.11

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 BindingBody

type BindingBody interface {
	Binding
	BindBody([]byte, interface{}) error
}

BindingBody adds BindBody method to Binding. BindBody is similar with GinBind, but it reads the body from supplied bytes instead of req.Body.

Jump to

Keyboard shortcuts

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