xbinding

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2025 License: MIT Imports: 1 Imported by: 4

README

xinding 说明

介绍

xbinding是基于gin框架的binding模块的扩展,主要从项目gin中分离处理binding的部分内容,进行改造处理适配除http以外的其他数据绑定过程。并且兼容http.request的绑定方式。

安装

Requires Go 1.21 or above. Install with go install github.com/zhiyunliu/xbinding@latest.

示例


    type Config struct {
        SpanKind   trace.SpanKind `json:"span_kind" yaml:"span_kind"`
        Provider   string         `json:"provider" yaml:"provider"`
        Propagator string         `json:"propagator" yaml:"propagator"`
    }

    traceCfg := &Config{}

    dataType := "json" //yaml , application/json, application/x-yaml
	codec, err := xbinding.GetCodec(xbinding.WithContentType(dataType))
	if err != nil {
		return nil, fmt.Errorf("xbinding.GetCodec err:%w", err)
	}

	if err = codec.Bind(xbinding.BytesReader(data.Data), traceCfg); err != nil {
		return nil, err
	}

扩展接口--帮助快速类型转换

辅助数据类型定义

type BytesReader []byte
type MapReader map[string][]string
type SMapReader map[string]string
type XMapReader map[string]any
type ReaderWrapper struct {
	Data any
}

扩展实现,实现以下两个接口可自定义实现Codec的注入

type Resolver interface {
	Name() string
	Resolve(opts *Options) (Codec, error)
}

type Codec interface {
	ContentType() string
	Marshal(v interface{}) ([]byte, error)
	Bind(reader Reader, v interface{}) error
}

注入到引擎中


var custom struct{}

func(c custom)	Name() string{return "custom"}
func(c custom) Resolve(opts *Options) (Codec, error){
    return nil,fmt.Errorf("not implement")
}

xbinding.Register(&custom{})

//获取自定义的codec json解析处理
xbinding.GetCodec(xbinding.WithProto("custom"),xbinding.WithContentType("json"))


默认类型支持

类型 描述 是否支持
json json
yaml yaml
xml xml
text text
application/json
text/html
application/xml
text/xml
text/plain
application/x-www-form-urlencoded
multipart/form-data
application/x-protobuf
application/x-msgpack
application/msgpack
application/x-yaml

注意事项

版本变更记录

  • v0.1.0 2024-11-19 初始化
  • v0.1.1 重写绑定reader实现
  • v0.1.2 优化绑定,处理go版本依赖问题

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"
)
View Source
const DefaultProto = "binding"

Variables

View Source
var ErrUnsupportedContentType = fmt.Errorf("xbinding: 不支持的Content-Type")

Functions

func Register

func Register(resolver Resolver)

Types

type BytesReader added in v0.1.1

type BytesReader []byte

func (BytesReader) ReadObject added in v0.1.1

func (b BytesReader) ReadObject() (any, error)

type Codec

type Codec interface {
	ContentType() string
	Marshal(v interface{}) ([]byte, error)
	Bind(reader Reader, v interface{}) error
}

func GetCodec added in v0.1.1

func GetCodec(opts ...Option) (Codec, error)

GetCodec 根据适配器名称及参数返回配置处理器

type MapReader added in v0.1.1

type MapReader map[string][]string

func (MapReader) ReadObject added in v0.1.1

func (b MapReader) ReadObject() (any, error)

type MultipartReqestInfo added in v0.1.1

type MultipartReqestInfo struct {
	Boundary string
	Body     any
}

type Option

type Option func(o *Options)

func WithContentType added in v0.1.1

func WithContentType(contentType string) Option

func WithMethod

func WithMethod(method string) Option

func WithProto

func WithProto(proto string) Option

type Options

type Options struct {
	Proto       string
	Method      string
	ContentType string
}

func NewOptions

func NewOptions(opts ...Option) *Options

type Reader

type Reader interface {
	ReadObject() (any, error)
}

绑定数据的源reader

type ReaderWrapper added in v0.1.1

type ReaderWrapper struct {
	Data any
}

func (*ReaderWrapper) ReadObject added in v0.1.1

func (r *ReaderWrapper) ReadObject() (any, error)

type Resolver

type Resolver interface {
	Name() string
	Resolve(opts *Options) (Codec, error)
}

解析器

type SMapReader added in v0.1.1

type SMapReader map[string]string

func (SMapReader) ReadObject added in v0.1.1

func (b SMapReader) ReadObject() (any, error)

type XMapReader added in v0.1.1

type XMapReader map[string]any

func (XMapReader) ReadObject added in v0.1.1

func (b XMapReader) ReadObject() (any, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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