Documentation
¶
Index ¶
- Constants
- Variables
- func ExtToTimestamp(ext Ext) (time.Time, error)
- func IsTimestamp(ext Ext) bool
- func Marshal(v any) ([]byte, error)
- func MarshalTimestamp(t time.Time) []byte
- func Unmarshal(data []byte, v any) error
- func UnmarshalTimestamp(data []byte) (time.Time, error)
- func UnmarshalWithConfig(data []byte, v any, cfg Config) error
- type Config
- type Decoder
- func (d *Decoder) Decode() (Value, error)
- func (d *Decoder) DecodeAny() (any, error)
- func (d *Decoder) DecodeFloat64Array() ([]float64, error)
- func (d *Decoder) DecodeInt64Array() ([]int64, error)
- func (d *Decoder) DecodeStringArray() ([]string, error)
- func (d *Decoder) DecodeStruct(v any) error
- func (d *Decoder) DecodeTimestamp() (time.Time, error)
- func (d *Decoder) DecodeUint64Array() ([]uint64, error)
- func (d *Decoder) Position() int
- func (d *Decoder) Remaining() int
- func (d *Decoder) Reset(data []byte)
- type Encoder
- func (e *Encoder) Bytes() []byte
- func (e *Encoder) Encode(v any) error
- func (e *Encoder) EncodeArrayHeader(length int)
- func (e *Encoder) EncodeBinary(v []byte)
- func (e *Encoder) EncodeBool(v bool)
- func (e *Encoder) EncodeExt(extType int8, data []byte)
- func (e *Encoder) EncodeFloat32(v float32)
- func (e *Encoder) EncodeFloat64(v float64)
- func (e *Encoder) EncodeFloat64Array(arr []float64)
- func (e *Encoder) EncodeInt(v int64)
- func (e *Encoder) EncodeInt64Array(arr []int64)
- func (e *Encoder) EncodeMapHeader(length int)
- func (e *Encoder) EncodeNil()
- func (e *Encoder) EncodeString(v string)
- func (e *Encoder) EncodeStringArray(arr []string)
- func (e *Encoder) EncodeStringBytes(v []byte)
- func (e *Encoder) EncodeTimestamp(t time.Time)
- func (e *Encoder) EncodeUint(v uint64)
- func (e *Encoder) EncodeUint64Array(arr []uint64)
- func (e *Encoder) EncodeValue(v *Value)
- func (e *Encoder) Len() int
- func (e *Encoder) Reset()
- type Ext
- type KV
- type StructDecoder
- type StructEncoder
- type Type
- type Value
- func (v *Value) AsBool() bool
- func (v *Value) AsBytes() []byte
- func (v *Value) AsFloat64() float64
- func (v *Value) AsInt() int64
- func (v *Value) AsString() string
- func (v *Value) AsUint() uint64
- func (v *Value) Get(key []byte) *Value
- func (v *Value) GetString(key string) *Value
- func (v *Value) Index(i int) *Value
- func (v *Value) IsNil() bool
- func (v *Value) Len() int
Constants ¶
const ( DefaultMaxStringLen = math.MaxInt32 // ~2GB, spec allows 2^32-1 DefaultMaxBinaryLen = math.MaxInt32 // ~2GB, spec allows 2^32-1 DefaultMaxArrayLen = math.MaxInt32 // ~2B elements, spec allows 2^32-1 DefaultMaxMapLen = math.MaxInt32 // ~2B pairs, spec allows 2^32-1 DefaultMaxExtLen = math.MaxInt32 // ~2GB, spec allows 2^32-1 DefaultMaxDepth = 10000 // practical limit for nested structures )
Default limits - aligned with msgpack spec (max 2^32-1) We use MaxInt32 for cross-platform safety (32-bit systems)
const TimestampExtType int8 = -1
TimestampExtType is the extension type for msgpack timestamps.
Variables ¶
var ( // ErrUnexpectedEOF is returned when the input ends before a complete value ErrUnexpectedEOF = errors.New("msgpck: unexpected end of input") // ErrInvalidFormat is returned when an invalid format byte is encountered ErrInvalidFormat = errors.New("msgpck: invalid format byte") // ErrStringTooLong is returned when a string exceeds MaxStringLen ErrStringTooLong = errors.New("msgpck: string exceeds maximum length") // ErrBinaryTooLong is returned when binary data exceeds MaxBinaryLen ErrBinaryTooLong = errors.New("msgpck: binary data exceeds maximum length") // ErrArrayTooLong is returned when an array exceeds MaxArrayLen ErrArrayTooLong = errors.New("msgpck: array exceeds maximum length") // ErrMapTooLong is returned when a map exceeds MaxMapLen ErrMapTooLong = errors.New("msgpck: map exceeds maximum length") // ErrExtTooLong is returned when ext data exceeds MaxExtLen ErrExtTooLong = errors.New("msgpck: ext data exceeds maximum length") // ErrMaxDepthExceeded is returned when nesting exceeds MaxDepth ErrMaxDepthExceeded = errors.New("msgpck: maximum nesting depth exceeded") // ErrTypeMismatch is returned when decoding into an incompatible type ErrTypeMismatch = errors.New("msgpck: type mismatch") // ErrNotPointer is returned when DecodeStruct is called with a non-pointer ErrNotPointer = errors.New("msgpck: decode target must be a pointer") // ErrNotStruct is returned when DecodeStruct is called with a non-struct pointer ErrNotStruct = errors.New("msgpck: decode target must be a pointer to struct") // ErrUnsupportedType is returned when encoding an unsupported Go type ErrUnsupportedType = errors.New("msgpck: unsupported type") )
Functions ¶
func ExtToTimestamp ¶
ExtToTimestamp converts an Ext value to time.Time. Returns ErrTypeMismatch if the extension type is not a timestamp. Returns the time in UTC.
func IsTimestamp ¶
IsTimestamp checks if an Ext value is a timestamp.
func Marshal ¶
Marshal encodes a Go value to msgpack bytes. The returned bytes are a copy and safe to retain.
func MarshalTimestamp ¶
MarshalTimestamp encodes a time.Time to msgpack timestamp bytes. Returns a copy of the encoded bytes (safe to retain). The time is converted to UTC before encoding.
func Unmarshal ¶
Unmarshal decodes msgpack data into v. v must be a pointer to the target value.
Supported target types:
- *struct: decoded from msgpack map
- *map[K]V: decoded from msgpack map
- *[]T: decoded from msgpack array
- *string, *int64, *float64, *bool, *[]byte: decoded from msgpack primitives
- *any: decoded to map[string]any, []any, or primitive types
- *time.Time: decoded from msgpack timestamp extension
func UnmarshalTimestamp ¶
UnmarshalTimestamp decodes msgpack timestamp bytes to time.Time. Returns the time in UTC. If the input has no timezone info, it is treated as UTC.
Types ¶
type Config ¶
type Config struct {
// MaxStringLen is the maximum allowed string length in bytes
MaxStringLen int
// MaxBinaryLen is the maximum allowed binary data length in bytes
MaxBinaryLen int
// MaxArrayLen is the maximum allowed array length (number of elements)
MaxArrayLen int
// MaxMapLen is the maximum allowed map length (number of key-value pairs)
MaxMapLen int
// MaxExtLen is the maximum allowed ext data length in bytes
MaxExtLen int
// MaxDepth is the maximum allowed nesting depth for arrays and maps
MaxDepth int
}
Config controls decoder/encoder behavior and security limits
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a Config with sensible defaults
func (Config) WithMaxArrayLen ¶
WithMaxArrayLen returns a new Config with the specified MaxArrayLen
func (Config) WithMaxBinaryLen ¶
WithMaxBinaryLen returns a new Config with the specified MaxBinaryLen
func (Config) WithMaxDepth ¶
WithMaxDepth returns a new Config with the specified MaxDepth
func (Config) WithMaxExtLen ¶
WithMaxExtLen returns a new Config with the specified MaxExtLen
func (Config) WithMaxMapLen ¶
WithMaxMapLen returns a new Config with the specified MaxMapLen
func (Config) WithMaxStringLen ¶
WithMaxStringLen returns a new Config with the specified MaxStringLen
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder reads MessagePack data from a byte slice. It provides zero-copy decoding where possible.
func NewDecoder ¶
NewDecoder creates a new Decoder for the given data with default config
func NewDecoderWithConfig ¶
NewDecoderWithConfig creates a new Decoder with custom config
func (*Decoder) Decode ¶
Decode decodes a single MessagePack value. Returns a Value with zero-copy references to strings/binary data in the source.
func (*Decoder) DecodeAny ¶
DecodeAny decodes a MessagePack value directly into a Go native type. Returns: nil, bool, int64, uint64, float64, string, []byte, []any, map[string]any This is optimized to avoid intermediate Value structs.
func (*Decoder) DecodeFloat64Array ¶ added in v0.3.0
DecodeFloat64Array decodes an array of float64 values. More efficient than decoding elements individually.
func (*Decoder) DecodeInt64Array ¶ added in v0.3.0
DecodeInt64Array decodes an array of int64 values. More efficient than decoding elements individually.
func (*Decoder) DecodeStringArray ¶ added in v0.3.0
DecodeStringArray decodes an array of strings (with copy). More efficient than decoding elements individually.
func (*Decoder) DecodeStruct ¶
DecodeStruct decodes a msgpack map into a struct. v must be a pointer to a struct.
func (*Decoder) DecodeTimestamp ¶
DecodeTimestamp decodes a msgpack timestamp extension to time.Time. Returns the time in UTC. Returns ErrTypeMismatch if the value is not a timestamp extension.
func (*Decoder) DecodeUint64Array ¶ added in v0.3.0
DecodeUint64Array decodes an array of uint64 values. More efficient than decoding elements individually.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder writes MessagePack data to a byte buffer.
func NewEncoder ¶
NewEncoder creates a new Encoder with the given initial capacity.
func NewEncoderBuffer ¶
NewEncoderBuffer creates an Encoder that writes to an existing buffer. The buffer will be grown as needed.
func (*Encoder) EncodeArrayHeader ¶
EncodeArrayHeader writes the header for an array of the given length. Call this, then encode each element.
func (*Encoder) EncodeBinary ¶
EncodeBinary writes binary data
func (*Encoder) EncodeFloat32 ¶
EncodeFloat32 writes a float32 value
func (*Encoder) EncodeFloat64 ¶
EncodeFloat64 writes a float64 value
func (*Encoder) EncodeFloat64Array ¶ added in v0.3.0
EncodeFloat64Array encodes a slice of float64 values. More efficient than encoding elements individually.
func (*Encoder) EncodeInt64Array ¶ added in v0.3.0
EncodeInt64Array encodes a slice of int64 values. More efficient than encoding elements individually.
func (*Encoder) EncodeMapHeader ¶
EncodeMapHeader writes the header for a map of the given length. Call this, then encode each key-value pair.
func (*Encoder) EncodeString ¶
EncodeString writes a string value (zero-copy using unsafe)
func (*Encoder) EncodeStringArray ¶ added in v0.3.0
EncodeStringArray encodes a slice of strings. More efficient than encoding elements individually.
func (*Encoder) EncodeStringBytes ¶
EncodeStringBytes writes a string from bytes
func (*Encoder) EncodeTimestamp ¶
EncodeTimestamp encodes a time.Time as a msgpack timestamp extension. Uses the most compact format that can represent the timestamp:
- Timestamp 32: seconds only (no nanoseconds), fits in uint32
- Timestamp 64: nanoseconds + seconds fits in 34-bit unsigned
- Timestamp 96: full range with 64-bit signed seconds
The time is converted to UTC before encoding.
func (*Encoder) EncodeUint ¶
EncodeUint writes a uint64 using the most compact format
func (*Encoder) EncodeUint64Array ¶ added in v0.3.0
EncodeUint64Array encodes a slice of uint64 values. More efficient than encoding elements individually.
type KV ¶
KV represents a key-value pair in a map. Key is a []byte pointing into the source buffer for zero-copy.
type StructDecoder ¶
type StructDecoder[T any] struct { // contains filtered or unexported fields }
StructDecoder is a pre-registered decoder for a specific struct type. Create once, reuse many times for best performance.
func GetStructDecoder ¶
func GetStructDecoder[T any](zeroCopy bool) *StructDecoder[T]
GetStructDecoder returns a cached decoder for type T. Creates one if it doesn't exist. If zeroCopy is true, strings point directly into the input buffer and are only valid while the buffer exists.
func (*StructDecoder[T]) Decode ¶
func (sd *StructDecoder[T]) Decode(data []byte, dst *T) error
Decode decodes msgpack data into the struct. This is the fast path - single pass, no intermediate allocations.
func (*StructDecoder[T]) DecodeWith ¶ added in v0.3.0
func (sd *StructDecoder[T]) DecodeWith(d *Decoder, data []byte, dst *T) error
DecodeWith decodes msgpack data using a provided decoder. Use this to avoid pool overhead when you manage your own decoder. The decoder will be reset with the provided data before decoding.
func (*StructDecoder[T]) ZeroCopy ¶
func (sd *StructDecoder[T]) ZeroCopy() *StructDecoder[T]
ZeroCopy returns a zero-copy version of this decoder. Strings will point directly into the input buffer - no allocations! WARNING: Decoded strings are only valid while the input buffer exists. Use this when the input buffer outlives the decoded struct (e.g., from database).
type StructEncoder ¶
type StructEncoder[T any] struct { // contains filtered or unexported fields }
StructEncoder is a pre-registered encoder for a specific struct type. Create once, reuse many times for best performance.
func GetStructEncoder ¶
func GetStructEncoder[T any]() *StructEncoder[T]
GetStructEncoder returns a cached encoder for type T.
func (*StructEncoder[T]) Encode ¶
func (se *StructEncoder[T]) Encode(src *T) ([]byte, error)
Encode encodes the struct to msgpack bytes. The returned bytes are safe to retain. This method is safe for concurrent use. For zero-allocation encoding, use EncodeWith with your own Encoder.
func (*StructEncoder[T]) EncodeWith ¶ added in v0.2.0
func (se *StructEncoder[T]) EncodeWith(e *Encoder, src *T) error
EncodeWith encodes using a provided encoder. Use this to avoid pool overhead when you manage your own encoder. The encoder is NOT reset - call e.Reset() before if needed.
type Value ¶
type Value struct {
Type Type
Bool bool
Int int64
Uint uint64
Float32 float32
Float64 float64
Bytes []byte // string/binary - points into source
Array []Value // array elements
Map []KV // map key-value pairs
Ext Ext // extension data
}
Value represents a decoded MessagePack value. For strings and binary data, Bytes points directly into the source buffer (zero-copy). The caller must not modify the source buffer while Value is in use.
func (*Value) AsFloat64 ¶
AsFloat64 returns the value as float64. Works for float32/64 and int/uint.
func (*Value) Get ¶
Get returns the value for a key in a map (linear search). Returns nil if not found or not a map.
