marshal

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ContentTypeJSON     = "application/json"
	ContentTypeProtobuf = "application/protobuf"
)

http header定义

Variables

View Source
var ErrInvalidProtoMessage = errors.New("value is not a proto.Message")

ErrInvalidProtoMessage 当传入的值不是有效的 protobuf 消息时返回此错误

View Source
var ErrUnsupportedCodec = errors.New("unsupported codec type")

Functions

This section is empty.

Types

type Decoder

type Decoder interface {
	// Decode 将数据解码到指定的值中
	Decode(v any) error
}

Decoder 定义了数据解码接口

type DecoderFunc

type DecoderFunc func(v any) error

DecoderFunc 是实现 Decoder 接口的函数类型

func (DecoderFunc) Decode

func (f DecoderFunc) Decode(v any) error

Decode 实现 Decoder 接口

type DefaultMarshaller

type DefaultMarshaller struct{}

DefaultMarshaller 提供了一个简单的 Marshaller 实现,它总是返回不支持的错误

func (*DefaultMarshaller) ContentType

func (d *DefaultMarshaller) ContentType() string

ContentType 返回不支持的编解码类型错误

func (*DefaultMarshaller) Marshal

func (d *DefaultMarshaller) Marshal(v any) ([]byte, error)

Marshal 返回不支持的编解码类型错误

func (*DefaultMarshaller) NewDecoder

func (d *DefaultMarshaller) NewDecoder(r io.Reader) Decoder

NewDecoder 创建一个总是返回错误的解码器

func (*DefaultMarshaller) NewEncoder

func (d *DefaultMarshaller) NewEncoder(w io.Writer) Encoder

NewEncoder 创建一个总是返回错误的编码器

func (*DefaultMarshaller) Unmarshal

func (d *DefaultMarshaller) Unmarshal(data []byte, v any) error

Unmarshal 返回不支持的编解码类型错误

type Encoder

type Encoder interface {
	// Encode 将指定的值编码为数据格式
	Encode(v any) error
}

Encoder 定义了数据编码接口

type EncoderFunc

type EncoderFunc func(v any) error

EncoderFunc 是实现 Encoder 接口的函数类型

func (EncoderFunc) Encode

func (f EncoderFunc) Encode(v any) error

Encode 实现 Encoder 接口

type JSONMarshaller

type JSONMarshaller struct{}

JSONMarshaller 实现了 Marshaller 接口,提供 JSON 格式的序列化和反序列化功能

func (*JSONMarshaller) ContentType

func (p *JSONMarshaller) ContentType() string

ContentType 返回 json 的 MIME 类型

func (*JSONMarshaller) Marshal

func (j *JSONMarshaller) Marshal(v any) ([]byte, error)

Marshal 将给定的值序列化为 JSON 字节切片

func (*JSONMarshaller) NewDecoder

func (j *JSONMarshaller) NewDecoder(r io.Reader) Decoder

NewDecoder 创建一个从 io.Reader 读取并解码 JSON 数据的解码器

func (*JSONMarshaller) NewEncoder

func (j *JSONMarshaller) NewEncoder(w io.Writer) Encoder

NewEncoder 创建一个将数据编码为 JSON 并写入 io.Writer 的编码器

func (*JSONMarshaller) Unmarshal

func (j *JSONMarshaller) Unmarshal(data []byte, v any) error

Unmarshal 将 JSON 字节切片反序列化为给定的值

type JSONProtoMarshaller

type JSONProtoMarshaller struct {
	MarshalOptions   *protojson.MarshalOptions
	UnmarshalOptions *protojson.UnmarshalOptions
}

JSONProtoMarshaller 实现了 Marshaller 接口,提供 JSON 和 Protobuf 之间的转换 将 JSON 输入转换为 Protobuf 消息,并将 Protobuf 消息转换为 JSON 输出

func (*JSONProtoMarshaller) ContentType

func (jp *JSONProtoMarshaller) ContentType() string

ContentType 返回 json 的 MIME 类型

func (*JSONProtoMarshaller) Marshal

func (jp *JSONProtoMarshaller) Marshal(v any) ([]byte, error)

Marshal 将 Protobuf 消息序列化为 JSON 字节切片

func (*JSONProtoMarshaller) NewDecoder

func (jp *JSONProtoMarshaller) NewDecoder(r io.Reader) Decoder

NewDecoder 创建一个从 io.Reader 读取 JSON 并解码为 Protobuf 消息的解码器

func (*JSONProtoMarshaller) NewEncoder

func (jp *JSONProtoMarshaller) NewEncoder(w io.Writer) Encoder

NewEncoder 创建一个将 Protobuf 消息编码为 JSON 并写入 io.Writer 的编码器

func (*JSONProtoMarshaller) Unmarshal

func (jp *JSONProtoMarshaller) Unmarshal(data []byte, v any) error

Unmarshal 将 JSON 字节切片反序列化为 Protobuf 消息

type Marshaller

type Marshaller interface {
	// Marshal 将给定的值序列化为字节切片
	Marshal(v any) ([]byte, error)

	// Unmarshal 将字节切片反序列化为给定的值
	Unmarshal(data []byte, v any) error

	// NewDecoder 创建一个从 io.Reader 读取并解码数据的解码器
	NewDecoder(r io.Reader) Decoder

	// NewEncoder 创建一个将数据编码并写入 io.Writer 的编码器
	NewEncoder(w io.Writer) Encoder

	// ContentType 请求响应的 content-type
	ContentType() string
}

Marshaller 定义了序列化和反序列化数据的接口 实现此接口的类型可以处理特定格式的数据转换(如 JSON、Protobuf 等)

func GetMarshallerByContentType

func GetMarshallerByContentType(contentType string) Marshaller

GetMarshallerByContentType 获取根据 http content-type 获取对应 Marshaller

func NewDefaultMarshaller

func NewDefaultMarshaller() Marshaller

NewDefaultMarshaller 创建一个新的默认 Marshaller

func NewJSONMarshaller

func NewJSONMarshaller() Marshaller

NewJSONMarshaller 创建一个新的 JSON Marshaller

func NewJSONProtoMarshaller

func NewJSONProtoMarshaller() Marshaller

NewJSONProtoMarshaller 创建一个新的 JSON-Protobuf 转换 Marshaller

func NewProtoMarshaller

func NewProtoMarshaller() Marshaller

NewProtoMarshaller 创建一个新的 Protocol Buffers Marshaller

type ProtoMarshaller

type ProtoMarshaller struct{}

ProtoMarshaller 实现了 Marshaller 接口,提供 Protocol Buffers 格式的序列化和反序列化功能

func (*ProtoMarshaller) ContentType

func (p *ProtoMarshaller) ContentType() string

ContentType 返回 Protocol Buffers 的 MIME 类型

func (*ProtoMarshaller) Marshal

func (p *ProtoMarshaller) Marshal(v any) ([]byte, error)

Marshal 将给定的 protobuf 消息序列化为字节切片

func (*ProtoMarshaller) NewDecoder

func (p *ProtoMarshaller) NewDecoder(r io.Reader) Decoder

NewDecoder 创建一个从 io.Reader 读取并解码 protobuf 数据的解码器

func (*ProtoMarshaller) NewEncoder

func (p *ProtoMarshaller) NewEncoder(w io.Writer) Encoder

NewEncoder 创建一个将数据编码为 protobuf 并写入 io.Writer 的编码器

func (*ProtoMarshaller) Unmarshal

func (p *ProtoMarshaller) Unmarshal(data []byte, v any) error

Unmarshal 将字节切片反序列化为给定的 protobuf 消息

Jump to

Keyboard shortcuts

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