Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNilInnerCodec = errors.New("cascache/codec: nil inner codec")
var ErrUninitializedCBOR = errors.New("cascache/codec: cbor codec is not initialized")
var ErrUninitializedProtobuf = errors.New("cascache/codec: protobuf codec is not initialized")
Functions ¶
This section is empty.
Types ¶
type Bytes ¶
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.
type CBOR ¶
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 ¶
MustCBOR is like NewCBOR but panics on error. Should not use for prod just handy for package-level variables in tests/examples.
func NewCBOR ¶
NewCBOR constructs a CBOR codec.
- Deterministic is true, uses CoreDetEncOptions (RFC 8949).
- Otherwise uses PreferredUnsortedEncOptions (smaller/faster defaults).
Also sets time encoding to RFC3339Nano.
type Codec ¶
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 ¶
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.
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 ¶
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.
type Protobuf ¶
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 ¶
NewProtobuf constructs a Protobuf codec for the given message type T. Provide a constructor that returns a new instance of T.