codec

package module
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2026 License: BSD-3-Clause Imports: 5 Imported by: 5 modules, 64 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	// ByteLen is the number of bytes per byte
	ByteLen = 1
	// ShortLen is the number of bytes per short
	ShortLen = 2
	// VersionSize is the number of bytes used for codec version
	VersionSize = ShortLen
	// IntLen is the number of bytes per int
	IntLen = 4
	// LongLen is the number of bytes per long
	LongLen = 8
	// BoolLen is the number of bytes per bool
	BoolLen = 1
	// IPLen is the number of bytes per IP (16 bytes + 2 for port)
	IPLen = 16 + ShortLen
)

Size constants for binary packing

View Source
const DefaultMaxSize = 1024 * 1024

DefaultMaxSize is the default maximum size for codec manager (1MB)

View Source
const MaxStringLen = math.MaxUint16

MaxStringLen is the maximum string length that can be packed

Variables

View Source
var (
	ErrUnsupportedType           = errors.New("unsupported type")
	ErrMaxSliceLenExceeded       = errors.New("max slice length exceeded")
	ErrDoesNotImplementInterface = errors.New("does not implement interface")
	ErrUnexportedField           = errors.New("unexported field")
	ErrMarshalNil                = errors.New("can't marshal nil pointer")
	ErrMarshalZeroLength         = errors.New("can't marshal zero length value")
	ErrUnmarshalNil              = errors.New("can't unmarshal into nil")
	ErrUnmarshalZeroLength       = errors.New("can't unmarshal zero length value")
	ErrCantPackVersion           = errors.New("couldn't pack codec version")
	ErrCantUnpackVersion         = errors.New("couldn't unpack codec version")
	ErrUnknownVersion            = errors.New("unknown codec version")
	ErrDuplicateType             = errors.New("duplicate type registration")
	ErrExtraSpace                = errors.New("trailing buffer space")
)

Common codec errors

View Source
var (
	ErrInsufficientLength = errors.New("packing: insufficient length")
	ErrNegativeLength     = errors.New("packing: negative length")
	ErrBadLength          = errors.New("packing: bad length")
	ErrOverflow           = errors.New("packing: overflow")
)

Common errors

Functions

func StringLen

func StringLen(str string) int

StringLen returns the packed length of a string (2-byte length prefix + string bytes)

Types

type Codec

type Codec interface {
	MarshalInto(interface{}, *wrappers.Packer) error
	UnmarshalFrom(*wrappers.Packer, interface{}) error
	Size(value interface{}) (int, error)
}

Codec marshals and unmarshals

type Errs

type Errs struct {
	Err error
}

Errs collects errors during a series of operations. It stores only the first error encountered.

func (*Errs) Add

func (errs *Errs) Add(errors ...error)

Add records the first non-nil error.

func (*Errs) Errored

func (errs *Errs) Errored() bool

Errored returns true if an error has been recorded.

type GeneralCodec

type GeneralCodec interface {
	Codec
	Registry
}

GeneralCodec combines Codec and Registry interfaces

type Manager

type Manager interface {
	RegisterCodec(version uint16, codec Codec) error
	Marshal(version uint16, source interface{}) ([]byte, error)
	Unmarshal(bytes []byte, dest interface{}) (uint16, error)
	Size(version uint16, value interface{}) (int, error)
}

Manager manages multiple codec versions

func NewDefaultManager

func NewDefaultManager() Manager

NewDefaultManager returns a codec manager with default max size

func NewManager

func NewManager(maxSize uint64) Manager

NewManager returns a new codec manager

type Packer

type Packer struct {
	Bytes  []byte
	Offset int
	Err    error
}

Packer provides methods to pack data into bytes

func NewPacker

func NewPacker(maxSize int) *Packer

NewPacker returns a new Packer with the given max size

func PackerFromBytes

func PackerFromBytes(b []byte) *Packer

PackerFromBytes returns a Packer initialized with the given bytes

func (*Packer) Errored

func (p *Packer) Errored() bool

Errored returns true if there's been an error

func (*Packer) PackBool

func (p *Packer) PackBool(val bool)

PackBool packs a bool

func (*Packer) PackByte

func (p *Packer) PackByte(val byte)

PackByte packs a byte

func (*Packer) PackBytes

func (p *Packer) PackBytes(val []byte)

PackBytes packs a byte slice with length prefix

func (*Packer) PackFixedBytes

func (p *Packer) PackFixedBytes(val []byte)

PackFixedBytes packs a fixed-length byte slice

func (*Packer) PackID

func (p *Packer) PackID(id ids.ID)

PackID packs an ID

func (*Packer) PackInt

func (p *Packer) PackInt(val uint32)

PackInt packs a uint32

func (*Packer) PackLong

func (p *Packer) PackLong(val uint64)

PackLong packs a uint64

func (*Packer) PackShort

func (p *Packer) PackShort(val uint16)

PackShort packs a uint16

func (*Packer) PackStr

func (p *Packer) PackStr(val string)

PackStr packs a string with length prefix

func (*Packer) Remaining

func (p *Packer) Remaining() int

Remaining returns the number of bytes remaining to read

func (*Packer) UnpackBool

func (p *Packer) UnpackBool() bool

UnpackBool unpacks a bool

func (*Packer) UnpackByte

func (p *Packer) UnpackByte() byte

UnpackByte unpacks a byte

func (*Packer) UnpackBytes

func (p *Packer) UnpackBytes() []byte

UnpackBytes unpacks a byte slice with length prefix

func (*Packer) UnpackFixedBytes

func (p *Packer) UnpackFixedBytes(n int) []byte

UnpackFixedBytes unpacks a fixed-length byte slice

func (*Packer) UnpackID

func (p *Packer) UnpackID() ids.ID

UnpackID unpacks an ID

func (*Packer) UnpackInt

func (p *Packer) UnpackInt() uint32

UnpackInt unpacks a uint32

func (*Packer) UnpackLong

func (p *Packer) UnpackLong() uint64

UnpackLong unpacks a uint64

func (*Packer) UnpackShort

func (p *Packer) UnpackShort() uint16

UnpackShort unpacks a uint16

func (*Packer) UnpackStr

func (p *Packer) UnpackStr() string

UnpackStr unpacks a string with length prefix

type Registry

type Registry interface {
	RegisterType(interface{}) error
}

Registry handles type registration for codec

Directories

Path Synopsis
Package codecmock is a generated GoMock package.
Package codecmock is a generated GoMock package.
Package wrappers provides common wrapper types and utilities.
Package wrappers provides common wrapper types and utilities.

Jump to

Keyboard shortcuts

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