bind

package
v1.7.2 Latest Latest
Warning

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

Go to latest
Published: May 8, 2021 License: MIT Imports: 21 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"
)

最常见的数据格式的 Content-Type MIME。

Variables

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

这些实现了 IBind 接口,可用于将请求中存在的数据绑定到struct实例。

View Source
var EnableDecoderDisallowUnknownFields = false

EnableDecoderDisallowUnknownFields 用于在JSON Decoder实例上调用DisallowUnknownFields方法。

当目标是结构并且输入包含与目标中任何未忽略的导出字段都不匹配的对象键时,DisallowUnknownFields会导致解码器返回错误。
View Source
var EnableDecoderUseNumber = false

EnableDecoderUseNumber 用于在JSON Decoder实例上调用UseNumber方法。

UseNumber使解码器将数字解组到接口{}中,作为数字而不是float64。

Functions

This section is empty.

Types

type IBind

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

IBind 定义一个绑定接口,用于绑定请求中存在的数据(例如JSON请求正文,查询参数或POST形式), 这个接口的实例将会被 *gin.context的 ShouldBind等方法调用。

func Default

func Default(method, contentType string) IBind

Default 根据HTTP方法和内容类型返回适当的 IBind 实例。

type IBindBody

type IBindBody interface {
	IBind
	BindBody([]byte, interface{}) error
}

IBindBody 从提供的字节而不是req.Body中读取正文。

func DefaultBody

func DefaultBody(name string) IBindBody

type IBindUri

type IBindUri interface {
	Name() string
	BindUri(map[string][]string, interface{}) error
}

IBindUri 从uri中读取参数,实行绑定<params<参数>

type IValidator

type IValidator interface {
	// Validate 可以接收任何类型的数据,即使配置不正确,也永远不会panic。
	//  如果接收到的类型是切片,则应在每个元素上进行验证。
	//  如果接收到的类型不是struct或slice或array,则应跳过任何验证,并且必须返回nil。
	//  如果接收到的类型是 struct/*struct,则应执行验证。
	//  如果该结构体无效或验证本身失败,则应返回描述性错误。
	//  否则必须返回nil。
	Validate(interface{}) error
	// Engine 返回实现了 IStructValidator 接口的验证器。
	Engine() interface{}
}

IValidator 参数验证接口,验证请求的参数。

var Validator IValidator = &gValidValidator{}

Validator 是实现 IStructValidator 接口的默认验证器。

Jump to

Keyboard shortcuts

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