binder

package
v1.8.1 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: Apache-2.0 Imports: 14 Imported by: 14

Documentation

Index

Constants

View Source
const (
	HeaderAccept      = "Accept"
	HeaderContentType = "Content-Type"
)
View Source
const (
	MIMEJSON     string = "application/json"
	MIMEXML             = "application/xml"
	MIMEXML2            = "text/xml"
	MIMEPOSTForm        = "application/x-www-form-urlencoded"
	MIMEPROTOBUF        = "application/x-protobuf"
	MIMEMSGPACK         = "application/x-msgpack"
	MIMEMSGPACK2        = "application/msgpack"
	MIMEYAML            = "application/x-yaml"
	MIMEBytes    string = "application/bytes"
)

Content-Type MIME of the most common data formats.

Variables

View Source
var Bytes = bytesBinding{
	Binary: binary.BigEndian,
}
View Source
var Form = &formBinding{}
View Source
var Json = jsonBinding{}
View Source
var Protobuf = &protobufBinding{}
View Source
var Xml = &xmlBinding{}

Functions

func ContentTypeFormat added in v1.2.0

func ContentTypeFormat(c string) string

func Decode

func Decode(r io.Reader, i any, t any) error

func Encode

func Encode(w io.Writer, i any, t any) error

func Marshal

func Marshal(i any, t any) ([]byte, error)

func Register

func Register(t string, handle Binder) error

Register 注册 Binder。约定仅在启动阶段单线程调用,参见 binderMap 注释。

func SetMimeType added in v1.2.0

func SetMimeType(id uint8, name string, typ string)

func Unmarshal

func Unmarshal(b []byte, i any, t any) error

Types

type Binder

type Binder interface {
	Id() uint8                   // 1
	Name() string                //JSON
	String() string              //application/json
	Encode(io.Writer, any) error //同Marshal
	Decode(io.Reader, any) error //同Unmarshal
	Marshal(any) ([]byte, error)
	Unmarshal([]byte, any) error
}
var Default Binder = Json

func Get added in v1.1.0

func Get(i any) (h Binder)

Get 获取 string(name/type) uint8(id)

func GetBinder added in v1.6.9

func GetBinder(meta map[string]string, contentType ...string) (b Binder)

func New

func New(t string) (b Binder)

type T added in v1.2.0

type T struct {
	Id   uint8
	Name string
	Type string
}

func Type added in v1.2.0

func Type(i any) (r *T)

Type 按 string(MIME 或 Name) / uint8(Id) 检索注册的 MIME 元数据。 注意:每次调用都做 map 查表。bindings 的 Id()/Name() 内部会调用本函数, 高频路径如 binder.Json.Id() 每次产生一次 map 查找。如果成为瓶颈, 调用方可把结果缓存到本地变量复用。

Jump to

Keyboard shortcuts

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