encoding

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: May 8, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CodecNameProto           = "proto"
	CodecNameJSON            = "json"
	CodecNameJSONCharsetUTF8 = CodecNameJSON + "; charset=utf-8"
)

Variables

This section is empty.

Functions

func MessageV2Of

func MessageV2Of(v any) proto.Message

func RegisterCodec

func RegisterCodec(codec Codec)

RegisterCodec registers the provided Codec for use with all gRPC 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 Codecs are registered with the same name, the one registered last will take effect.

Types

type Codec

type Codec interface {
	// Marshal returns the wire format of v. The buffers in the returned
	// [mem.BufferSlice] must have at least one reference each, which will be freed
	// by gRPC when they are no longer needed.
	Marshal(v any) (out mem.BufferSlice, err error)
	// Unmarshal parses the wire format into v. Note that data will be freed as soon
	// as this function returns. If the codec wishes to guarantee access to the data
	// after this function, it must take its own reference that it frees when it is
	// no longer needed.
	Unmarshal(data mem.BufferSlice, 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 ReadOnlyCodecs

type ReadOnlyCodecs map[string]Codec

func (ReadOnlyCodecs) Get

func (m ReadOnlyCodecs) Get(name string) Codec

func (ReadOnlyCodecs) Names

func (m ReadOnlyCodecs) Names() []string

func (ReadOnlyCodecs) Protobuf

func (m ReadOnlyCodecs) Protobuf() Codec

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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