codec

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotAPointer .
	ErrNotAPointer = errors.New("v argument must be a pointer")
)

Functions

func GzipDecode

func GzipDecode(in []byte) ([]byte, error)

GzipDecode 解码

func GzipEncode

func GzipEncode(in []byte) ([]byte, error)

GzipEncode 编码

func Marshal

func Marshal(e Encoding, v any) (data []byte, err error)

Marshal encode data

func RegisterCodec

func RegisterCodec(codec Codec)

RegisterCodec registers the provided Codec for use with all transport clients and servers.

The Codec will be stored and looked up by result of its Name() method, which should match the content-subtype of the encoding handled by the Codec. This is case-insensitive, and is stored and looked up as lowercase. If the result of calling Name() is an empty string, RegisterCodec will panic. See Content-Type on https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests for more details.

NOTE: this function must only be called during initialization time (i.e. in an init() function), and is not thread-safe. If multiple Compressors are registered with the same name, the one registered last will take effect.

func Unmarshal

func Unmarshal(e Encoding, data []byte, v any) (err error)

Unmarshal decode data

Types

type Codec

type Codec interface {
	// Marshal returns the wire format of v.
	Marshal(v any) ([]byte, error)
	// Unmarshal parses the wire format into v.
	Unmarshal(data []byte, v any) error
	// Name returns the name of the Codec implementation. The returned string
	// will be used as part of content type in transmission.  The result must be
	// static; the result cannot change between calls.
	Name() string
}

Codec defines the interface gRPC uses to encode and decode messages. Note that implementations of this interface must be thread safe; a Codec's methods can be called from concurrent goroutines.

func GetCodec

func GetCodec(contentSubtype string) Codec

GetCodec gets a registered Codec by content-subtype, or nil if no Codec is registered for the content-subtype.

The content-subtype is expected to be lowercase.

type Encoding

type Encoding interface {
	Marshal(v any) ([]byte, error)
	Unmarshal(data []byte, v any) error
}

Encoding 编码接口定义

type JSONEncoding

type JSONEncoding struct{}

JSONEncoding json格式

func (JSONEncoding) Marshal

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

Marshal json encode

func (JSONEncoding) Unmarshal

func (j JSONEncoding) Unmarshal(data []byte, value any) error

Unmarshal json decode

type JSONGzipEncoding

type JSONGzipEncoding struct{}

JSONGzipEncoding json and gzip

func (JSONGzipEncoding) Marshal

func (jz JSONGzipEncoding) Marshal(v any) ([]byte, error)

Marshal json encode and gzip

func (JSONGzipEncoding) Unmarshal

func (jz JSONGzipEncoding) Unmarshal(data []byte, value any) error

Unmarshal json encode and gzip

Jump to

Keyboard shortcuts

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