binding

package
v0.0.0-...-a0b1be5 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2023 License: MIT Imports: 20 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"
	MIMETOML              = "application/toml"
)

内容类型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{}
	TOML          = tomlBinding{}
)

它们实现了Binding接口,可用于将请求中的数据绑定到struct实例

View Source
var (

	// ErrConvertMapStringSlice不能转换为map[string][]string
	ErrConvertMapStringSlice = errors.New("can not convert to map slices of strings")

	// ErrConvertToMapString不能转换为map[string]string
	ErrConvertToMapString = errors.New("can not convert to map of strings")
)
View Source
var (
	// ErrMultiFileHeader多部分
	// FileHeader无效
	ErrMultiFileHeader = errors.New("unsupported field type for multipart.FileHeader")

	// errmultifileheaderlen无效数组[]*multipart
	// 文件头len无效
	ErrMultiFileHeaderLenInvalid = errors.New("unsupported len of array for []*multipart.FileHeader")
)
View Source
var EnableDecoderDisallowUnknownFields = false

EnableDecoderDisallowUnknownFields用于调用JSON Decoder实例上的DisallowUnknownFields方法 DisallowUnknownFields导致解码器返回一个错误,当目标是一个结构,并且输入包含的对象键与目标中任何非忽略的导出字段不匹配时

View Source
var EnableDecoderUseNumber = false

EnableDecoderUseNumber用于调用JSON Decoder实例上的UseNumber方法 UseNumber导致解码器将一个数字反编组为一个any,作为一个number而不是一个float64

Functions

func MapFormWithTag

func MapFormWithTag(ptr any, form map[string][]string, tag string) error

Types

type Binding

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

绑定描述了需要实现的接口,用于绑定请求中的数据,如JSON请求体、查询参数或表单POST

func Default

func Default(method, contentType string) Binding

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

type BindingBody

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

BindingBody在Binding中添加BindBody方法 BindBody与Bind类似,但它从提供的字节中读取主体,而不是从req.Body中读取主体

type BindingUri

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

BindingUri将BindUri方法添加到Binding中 BindUri与Bind类似,但它读取Params

type SliceValidationError

type SliceValidationError []error

func (SliceValidationError) Error

func (err SliceValidationError) Error() string

Error将SliceValidationError中的所有错误元素连接成一个以\n分隔的字符串

type StructValidator

type StructValidator interface {
	// ValidateStruct可以接收任何类型,即使配置不正确,它也不会panic
	// 如果接收到的类型是slice数组,则应该对每个元素执行验证
	// 如果接收到的类型不是struct或slice|array,则应该跳过任何验证,并且必须返回nil
	// 如果接收到的类型是结构体或指向结构体的指针,则应该执行验证
	// 如果结构无效或验证本身失败,则应返回描述性错误
	// 否则必须返回nil
	ValidateStruct(any) error

	// Engine返回为StructValidator实现提供动力的底层验证器引擎
	Engine() any
}

StructValidator是需要实现的最小接口,以便将其用作确保请求正确性的验证器引擎 Gin为此提供了一个默认实现,使用https://github.com/go-playground/validator/tree/v10.6.1

var Validator StructValidator = &defaultValidator{}

Validator是默认的验证器,它实现了StructValidator接口 它在引擎盖下使用https://github.com/go-playground/validator/tree/v10.6.1

Jump to

Keyboard shortcuts

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