Help us shape the future of Go! Take the 2026 Go Developer Survey

binding

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2023 License: Apache-2.0 Imports: 18 Imported by: 24

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bind

func Bind(req *protocol.Request, obj interface{}, pathParams param.Params) error

Bind binds data from *protocol.Request to obj. NOTE:

obj should be a pointer.

func BindAndValidate

func BindAndValidate(req *protocol.Request, obj interface{}, pathParams param.Params) error

BindAndValidate binds data from *protocol.Request to obj and validates them if needed. NOTE:

obj should be a pointer.

func Validate

func Validate(obj interface{}) error

Validate validates obj with "vd" tag NOTE:

obj should be a pointer.
Validate should be called after Bind.

Types

type BindConfig added in v0.7.0

type BindConfig struct {
	// LooseZeroMode if set to true,
	// the empty string request parameter is bound to the zero value of parameter.
	// NOTE:
	//	The default is false.
	//	Suitable for these parameter types: query/header/cookie/form .
	LooseZeroMode bool
	// DisableDefaultTag is used to add default tags to a field when it has no tag
	// If is false, the field with no tag will be added default tags, for more automated binding. But there may be additional overhead.
	// NOTE:
	// The default is false.
	DisableDefaultTag bool
	// DisableStructFieldResolve is used to generate a separate decoder for a struct.
	// If is false, the 'struct' field will get a single inDecoder.structTypeFieldTextDecoder, and use json.Unmarshal for decode it.
	// It usually used to add json string to query parameter.
	// NOTE:
	// The default is false.
	DisableStructFieldResolve bool
	// 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.
	// NOTE:
	// The default is false.
	// It is used for BindJSON().
	EnableDecoderUseNumber bool
	// 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.
	// NOTE:
	// The default is false.
	// It is used for BindJSON().
	EnableDecoderDisallowUnknownFields bool
	// TypeUnmarshalFuncs registers customized type unmarshaler.
	// NOTE:
	// time.Time is registered by default
	TypeUnmarshalFuncs map[reflect.Type]inDecoder.CustomizeDecodeFunc
	// Validator is used to validate for BindAndValidate()
	Validator StructValidator
}

BindConfig contains options for default bind behavior.

func NewBindConfig added in v0.7.0

func NewBindConfig() *BindConfig

func (*BindConfig) MustRegTypeUnmarshal added in v0.7.0

func (config *BindConfig) MustRegTypeUnmarshal(t reflect.Type, fn func(req *protocol.Request, params param.Params, text string) (reflect.Value, error))

MustRegTypeUnmarshal registers customized type unmarshaler. It will panic if exist error.

func (*BindConfig) RegTypeUnmarshal added in v0.7.0

func (config *BindConfig) RegTypeUnmarshal(t reflect.Type, fn inDecoder.CustomizeDecodeFunc) error

RegTypeUnmarshal registers customized type unmarshaler.

func (*BindConfig) UseStdJSONUnmarshaler added in v0.7.0

func (config *BindConfig) UseStdJSONUnmarshaler()

UseStdJSONUnmarshaler uses encoding/json as json library NOTE:

The current version uses encoding/json by default.
UseStdJSONUnmarshaler will remain in effect once it has been called.

func (*BindConfig) UseThirdPartyJSONUnmarshaler added in v0.7.0

func (config *BindConfig) UseThirdPartyJSONUnmarshaler(fn func(data []byte, v interface{}) error)

UseThirdPartyJSONUnmarshaler uses third-party json library for binding NOTE:

UseThirdPartyJSONUnmarshaler will remain in effect once it has been called.

type Binder added in v0.7.0

type Binder interface {
	Name() string
	Bind(*protocol.Request, interface{}, param.Params) error
	BindAndValidate(*protocol.Request, interface{}, param.Params) error
	BindQuery(*protocol.Request, interface{}) error
	BindHeader(*protocol.Request, interface{}) error
	BindPath(*protocol.Request, interface{}, param.Params) error
	BindForm(*protocol.Request, interface{}) error
	BindJSON(*protocol.Request, interface{}) error
	BindProtobuf(*protocol.Request, interface{}) error
}

func DefaultBinder added in v0.7.0

func DefaultBinder() Binder

func NewDefaultBinder added in v0.7.0

func NewDefaultBinder(config *BindConfig) Binder

type StructValidator added in v0.7.0

type StructValidator interface {
	ValidateStruct(interface{}) error
	Engine() interface{}
	ValidateTag() string
}

func DefaultValidator added in v0.7.0

func DefaultValidator() StructValidator

func NewValidator added in v0.7.0

func NewValidator(config *ValidateConfig) StructValidator

type ValidateConfig added in v0.7.0

type ValidateConfig struct {
	ValidateTag string
	ErrFactory  ValidateErrFactory
}

func NewValidateConfig added in v0.7.0

func NewValidateConfig() *ValidateConfig

func (*ValidateConfig) MustRegValidateFunc added in v0.7.0

func (config *ValidateConfig) MustRegValidateFunc(funcName string, fn func(args ...interface{}) error, force ...bool)

MustRegValidateFunc registers validator function expression. NOTE:

If force=true, allow to cover the existed same funcName.
MustRegValidateFunc will remain in effect once it has been called.

func (*ValidateConfig) SetValidatorErrorFactory added in v0.7.0

func (config *ValidateConfig) SetValidatorErrorFactory(errFactory ValidateErrFactory)

SetValidatorErrorFactory customizes the factory of validation error.

func (*ValidateConfig) SetValidatorTag added in v0.7.0

func (config *ValidateConfig) SetValidatorTag(tag string)

SetValidatorTag customizes the factory of validation error.

type ValidateErrFactory added in v0.7.0

type ValidateErrFactory func(fieldSelector, msg string) error

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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