codec

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNilInnerCodec = errors.New("cascache/codec: nil inner codec")
View Source
var ErrUninitializedCBOR = errors.New("cascache/codec: cbor codec is not initialized")
View Source
var ErrUninitializedProtobuf = errors.New("cascache/codec: protobuf codec is not initialized")

Functions

This section is empty.

Types

type Bytes added in v0.0.3

type Bytes struct{}

Bytes is an identity codec for []byte values. Encode/Decode return the input unchanged. Useful when your value type is already a raw byte slice and you only need cascache's wire framing and validation.

func (Bytes) Decode added in v0.0.3

func (Bytes) Decode(b []byte) ([]byte, error)

func (Bytes) Encode added in v0.0.3

func (Bytes) Encode(b []byte) ([]byte, error)

type CBOR added in v0.0.3

type CBOR[V any] struct {
	// contains filtered or unexported fields
}

CBOR is a Codec that serializes values using fxamacker/cbor. The zero value is NOT ready to use. Construct with NewCBOR or MustCBOR.

Use deterministic=true for canonical encoding (RFC 8949 Core Deterministic) when you need byte-for-byte stable outputs (e.g., hashing/content addressing). Otherwise PreferredUnsortedEncOptions are used (sensible defaults). Time values are encoded as RFC3339Nano for stable, human-readable timestamps.

func MustCBOR added in v0.0.3

func MustCBOR[V any](deterministic bool) CBOR[V]

MustCBOR is like NewCBOR but panics on error. Should not use for prod just handy for package-level variables in tests/examples.

func NewCBOR added in v0.0.3

func NewCBOR[V any](deterministic bool) (CBOR[V], error)

NewCBOR constructs a CBOR codec.

  • Deterministic is true, uses CoreDetEncOptions (RFC 8949).
  • Otherwise uses PreferredUnsortedEncOptions (smaller/faster defaults).

Also sets time encoding to RFC3339Nano.

func (CBOR[V]) Decode added in v0.0.3

func (c CBOR[V]) Decode(b []byte) (V, error)

Decode decodes b into a V using the configured DecMode.

func (CBOR[V]) Encode added in v0.0.3

func (c CBOR[V]) Encode(v V) ([]byte, error)

Encode encodes v as CBOR using the configured EncMode.

type Codec

type Codec[V any] interface {
	Encode(V) ([]byte, error)
	Decode([]byte) (V, error)
}

Codec encodes and decodes a value of type V to and from a byte slice. Implementations should return an error on malformed input. Encode/Decode should be pure (no side effects).

type JSON added in v0.0.3

type JSON[V any] struct{}

JSON is a Codec that serializes values using the standard library's encoding/json. The zero value is ready to use and respects `json` struct tags.

Notes:

  • Interface-typed fields may decode to default concrete types (e.g. numbers to float64) unless you provide custom unmarshaling.
  • Time values use encoding/json defaults.

func (JSON[V]) Decode added in v0.0.3

func (JSON[V]) Decode(b []byte) (V, error)

func (JSON[V]) Encode added in v0.0.3

func (JSON[V]) Encode(v V) ([]byte, error)

type LimitCodec

type LimitCodec[V any] struct {
	// Inner is the underlying codec being wrapped. It must be set.
	Inner Codec[V]
	// MaxDecode is the maximum permitted length (in bytes) of the incoming
	// payload for Decode. If payload length exceeds MaxDecode, Decode returns
	// an error without invoking Inner.
	MaxDecode int
}

LimitCodec wraps another codec to enforce a maximum allowed payload size at Decode time. Encode is forwarded to Inner unchanged. If MaxDecode <= 0, size limiting is disabled.

Typical use: protect against oversized/malicious inputs coming from a shared cache or untrusted source.

func (LimitCodec[V]) Decode

func (c LimitCodec[V]) Decode(b []byte) (V, error)

func (LimitCodec[V]) Encode

func (c LimitCodec[V]) Encode(v V) ([]byte, error)

type Msgpack added in v0.0.3

type Msgpack[V any] struct{}

Msgpack is a Codec that serializes values using vmihailenco/msgpack/v5. The zero value is ready to use.

Msgpack is compact and fast; be mindful of struct tag differences vs JSON. Use `msgpack:"fieldName"` tags if you need explicit control.

func (Msgpack[V]) Decode added in v0.0.3

func (Msgpack[V]) Decode(b []byte) (V, error)

func (Msgpack[V]) Encode added in v0.0.3

func (Msgpack[V]) Encode(v V) ([]byte, error)

type Protobuf added in v0.0.3

type Protobuf[T proto.Message] struct {
	// contains filtered or unexported fields
}

Protobuf is a Codec for protocol buffer messages. Requires a constructor for the concrete message type T so Decode can allocate a new instance.

The zero value is NOT ready to use. Build with NewProtobuf.

Example:

type UserPB = *mypb.User
pbCodec := codec.NewProtobuf(func() UserPB { return &mypb.User{} })

func NewProtobuf added in v0.0.3

func NewProtobuf[T proto.Message](ctor func() T) Protobuf[T]

NewProtobuf constructs a Protobuf codec for the given message type T. Provide a constructor that returns a new instance of T.

func (Protobuf[T]) Decode added in v0.0.3

func (c Protobuf[T]) Decode(b []byte) (T, error)

func (Protobuf[T]) Encode added in v0.0.3

func (c Protobuf[T]) Encode(v T) ([]byte, error)

type String added in v0.0.3

type String struct{}

String is a trivial codec for Go string values. Encode converts to []byte, and Decode converts back to string. By convention this assumes UTF-8 and performs no validation.

func (String) Decode added in v0.0.3

func (String) Decode(b []byte) (string, error)

func (String) Encode added in v0.0.3

func (String) Encode(s string) ([]byte, error)

Jump to

Keyboard shortcuts

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