Documentation
¶
Index ¶
- Constants
- Variables
- type Decoder
- type DecoderFunc
- type DefaultMarshaller
- type Encoder
- type EncoderFunc
- type JSONMarshaller
- type JSONProtoMarshaller
- func (jp *JSONProtoMarshaller) ContentType() string
- func (jp *JSONProtoMarshaller) Marshal(v any) ([]byte, error)
- func (jp *JSONProtoMarshaller) NewDecoder(r io.Reader) Decoder
- func (jp *JSONProtoMarshaller) NewEncoder(w io.Writer) Encoder
- func (jp *JSONProtoMarshaller) Unmarshal(data []byte, v any) error
- type Marshaller
- type ProtoMarshaller
Constants ¶
const ( ContentTypeJSON = "application/json" ContentTypeProtobuf = "application/protobuf" )
http header定义
Variables ¶
var ErrInvalidProtoMessage = errors.New("value is not a proto.Message")
ErrInvalidProtoMessage 当传入的值不是有效的 protobuf 消息时返回此错误
var ErrUnsupportedCodec = errors.New("unsupported codec type")
Functions ¶
This section is empty.
Types ¶
type DecoderFunc ¶
DecoderFunc 是实现 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 创建一个总是返回错误的编码器
type EncoderFunc ¶
EncoderFunc 是实现 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 的编码器
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 的编码器
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 的编码器