codec

package
v0.0.0-...-c1d89c2 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2023 License: BSD-3-Clause Imports: 14 Imported by: 0

README

实现各种编码方式的封装,提供统一的编解码方式

todo:

  • raw
  • binary
  • gob
  • json
  • pb
  • jce
  • thrift
  • ...

Documentation

Index

Constants

This section is empty.

Variables

Functions

func RegisterCodec

func RegisterCodec(t CodecType, c Codec)

自定义注册编码方案

func UnRegisterCodec

func UnRegisterCodec(t CodecType, c Codec)

Types

type BinaryCoder

type BinaryCoder struct {
	// contains filtered or unexported fields
}

二进制协议

func NewBinaryCoder

func NewBinaryCoder() *BinaryCoder

func (*BinaryCoder) Marshal

func (b *BinaryCoder) Marshal(v any) ([]byte, error)

func (*BinaryCoder) MarshalTo

func (b *BinaryCoder) MarshalTo(v any, w io.Writer) error

func (*BinaryCoder) String

func (b *BinaryCoder) String() string

func (*BinaryCoder) Unmarshal

func (b *BinaryCoder) Unmarshal(data []byte, v any) error

func (*BinaryCoder) UnmarshalFrom

func (b *BinaryCoder) UnmarshalFrom(r io.Reader, v any) error

type Codec

type Codec interface {
	Marshal(v any) ([]byte, error)
	MarshalTo(v any, w io.Writer) error
	Unmarshal(data []byte, v any) error
	UnmarshalFrom(r io.Reader, v any) error
	String() string
}

codec 接口

type CodecType

type CodecType string
const (
	CodeTypeNone    CodecType = "code/none"
	CodeTypeBinary  CodecType = "code/binary"
	CodeTypeGob     CodecType = "code/gob"
	CodeTypeJce     CodecType = "code/jce"
	CodeTypeJson    CodecType = "code/json"
	CodeTypePb      CodecType = "code/pb"
	CodeTypeThrift  CodecType = "code/thrift"
	CodeTypeMsgpack CodecType = "code/msgpack"
)

func (CodecType) String

func (t CodecType) String() string

type GobCoder

type GobCoder struct {
}

gob 协议

func NewGobCoder

func NewGobCoder() *GobCoder

func (*GobCoder) Marshal

func (g *GobCoder) Marshal(v any) ([]byte, error)

func (*GobCoder) MarshalTo

func (g *GobCoder) MarshalTo(v any, w io.Writer) error

func (*GobCoder) String

func (g *GobCoder) String() string

func (*GobCoder) Unmarshal

func (g *GobCoder) Unmarshal(data []byte, v any) error

func (*GobCoder) UnmarshalFrom

func (g *GobCoder) UnmarshalFrom(r io.Reader, v any) error

type JceCoder

type JceCoder struct {
}

jce 协议

func NewJceCoder

func NewJceCoder() *JceCoder

func (*JceCoder) Marshal

func (jc *JceCoder) Marshal(v any) ([]byte, error)

func (*JceCoder) MarshalTo

func (jc *JceCoder) MarshalTo(v any, w io.Writer) error

func (*JceCoder) String

func (jc *JceCoder) String() string

func (*JceCoder) Unmarshal

func (jc *JceCoder) Unmarshal(data []byte, v any) error

func (*JceCoder) UnmarshalFrom

func (jc *JceCoder) UnmarshalFrom(r io.Reader, v any) error

type JsonCoder

type JsonCoder struct {
}

json 协议

func NewJsonCoder

func NewJsonCoder() *JsonCoder

func (*JsonCoder) Marshal

func (js *JsonCoder) Marshal(v any) ([]byte, error)

func (*JsonCoder) MarshalTo

func (js *JsonCoder) MarshalTo(v any, w io.Writer) error

func (*JsonCoder) String

func (js *JsonCoder) String() string

func (*JsonCoder) Unmarshal

func (js *JsonCoder) Unmarshal(data []byte, v any) error

func (*JsonCoder) UnmarshalFrom

func (js *JsonCoder) UnmarshalFrom(r io.Reader, v any) error

type MsgpackCoder

type MsgpackCoder struct {
}

msgpack 协议

func NewMsgpackCoder

func NewMsgpackCoder() *MsgpackCoder

func (*MsgpackCoder) Marshal

func (m *MsgpackCoder) Marshal(v any) ([]byte, error)

func (*MsgpackCoder) MarshalTo

func (m *MsgpackCoder) MarshalTo(v any, w io.Writer) (err error)

func (*MsgpackCoder) String

func (m *MsgpackCoder) String() string

func (*MsgpackCoder) Unmarshal

func (m *MsgpackCoder) Unmarshal(data []byte, v any) (err error)

func (*MsgpackCoder) UnmarshalFrom

func (m *MsgpackCoder) UnmarshalFrom(r io.Reader, v any) (err error)

type PbCoder

type PbCoder struct {
}

pb 协议

func NewPbCoder

func NewPbCoder() *PbCoder

func (*PbCoder) Marshal

func (pb *PbCoder) Marshal(v any) ([]byte, error)

func (*PbCoder) MarshalTo

func (pb *PbCoder) MarshalTo(v any, w io.Writer) (err error)

func (*PbCoder) String

func (pb *PbCoder) String() string

func (*PbCoder) Unmarshal

func (pb *PbCoder) Unmarshal(data []byte, v any) error

func (*PbCoder) UnmarshalFrom

func (pb *PbCoder) UnmarshalFrom(r io.Reader, v any) (err error)

type RawCoder

type RawCoder struct {
	// contains filtered or unexported fields
}

裸编码,即默认不编码,只支持原生为 []byte 类型

func NewRawCoder

func NewRawCoder() *RawCoder

func (*RawCoder) Marshal

func (b *RawCoder) Marshal(v any) ([]byte, error)

func (*RawCoder) MarshalTo

func (b *RawCoder) MarshalTo(v any, w io.Writer) (err error)

func (*RawCoder) String

func (b *RawCoder) String() string

func (*RawCoder) Unmarshal

func (b *RawCoder) Unmarshal(data []byte, v any) (err error)

func (*RawCoder) UnmarshalFrom

func (b *RawCoder) UnmarshalFrom(r io.Reader, v any) (err error)

type ThriftCoder

type ThriftCoder struct {
}

thrift 协议

func NewThriftCoder

func NewThriftCoder() *ThriftCoder

func (*ThriftCoder) Marshal

func (t *ThriftCoder) Marshal(v any) ([]byte, error)

func (*ThriftCoder) MarshalTo

func (t *ThriftCoder) MarshalTo(v any, w io.Writer) (err error)

func (*ThriftCoder) String

func (Thrift *ThriftCoder) String() string

func (*ThriftCoder) Unmarshal

func (t *ThriftCoder) Unmarshal(data []byte, v any) error

func (*ThriftCoder) UnmarshalFrom

func (t *ThriftCoder) UnmarshalFrom(r io.Reader, v any) (err error)

Jump to

Keyboard shortcuts

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