codec

package
v0.24.0 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const HeaderContentEncoding = "Content-Encoding"
View Source
const HeaderContentType = "Content-Type"

Variables

View Source
var ErrUnsupportedEncoding = errors.New("unsupported content encoding")

ErrUnsupportedEncoding is returned when a Content-Encoding value is not recognized.

Functions

func MarshalHeader

func MarshalHeader(x any, header Header) ([]byte, error)

MarshalHeader encodes x using the codec specified in the header, then compresses the result if Content-Encoding is set.

func SetContentEncoding added in v0.24.0

func SetContentEncoding(h Header, e ContentEncoding)

SetContentEncoding sets the Content-Encoding header value.

func TypeFromHeaderCopy

func TypeFromHeaderCopy(dst, src Header)

TypeFromHeaderCopy copies the Content-Type from src to dst, defaulting to JSON.

func UnmarshalHeader

func UnmarshalHeader(data []byte, x any, header Header) error

UnmarshalHeader decompresses data if Content-Encoding is set, then decodes it into x using the codec specified in the header.

Types

type Codec

type Codec interface {
	Marshal(any) ([]byte, error)
	Unmarshal([]byte, any) error
	ContentType() ContentType
	SetContentType(Header)
	MatchContentType(Header) bool
}

Codec defines the interface for message serialization/deserialization.

func ForContentType

func ForContentType(c ContentType) (Codec, error)

ForContentType returns the codec for the given content type.

func ForHeader

func ForHeader(h Header) (Codec, error)

ForHeader returns the codec matching the Content-Type header.

type Compressor added in v0.24.0

type Compressor interface {
	Compress([]byte) ([]byte, error)
	Decompress([]byte) ([]byte, error)
	ContentEncoding() ContentEncoding
}

Compressor defines the interface for message compression/decompression.

func ForContentEncoding added in v0.24.0

func ForContentEncoding(e ContentEncoding) (Compressor, error)

ForContentEncoding returns the compressor for the given content encoding.

type ContentEncoding added in v0.24.0

type ContentEncoding uint16

ContentEncoding represents a supported compression algorithm.

const (
	Zstd ContentEncoding = iota + 1
	S2
)

func EncodingFromHeader added in v0.24.0

func EncodingFromHeader(h Header) ContentEncoding

EncodingFromHeader extracts the ContentEncoding from a header. Returns 0 if absent or unrecognized.

func (ContentEncoding) String added in v0.24.0

func (e ContentEncoding) String() string

type ContentType

type ContentType uint16

ContentType represents a supported serialization format.

const (
	JSON ContentType
	YAML
	Msgpack
	Protojson
	Prototext
	Protobin

	Default = JSON
)

func TypeFromHeader

func TypeFromHeader(header Header) ContentType

TypeFromHeader extracts the ContentType from a header, defaulting to JSON.

func (ContentType) String

func (c ContentType) String() string
type Header = textproto.MIMEHeader

Header is a type alias for textproto.MIMEHeader, identical to peanats.Header. Defined here to avoid circular imports between codec and root packages.

Jump to

Keyboard shortcuts

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