Documentation
¶
Overview ¶
Package msgpackwire provides bounded MessagePack decoding and deterministic encoding.
Decoding accepts exactly one object. Untyped maps require string keys while typed map targets may declare other comparable key types. Integer widths are preserved unless NormalizeNumericWidths is selected. The standard timestamp extension is supported; unknown extension IDs are rejected. A structural preflight rejects truncated objects and impossible collection lengths before target allocation. MessagePack is deliberately excluded from wire.DetectFormat because arbitrary binary bytes cannot be identified reliably.
Raw duplicate keys are rejected recursively by default. Destination-key collisions are also checked for built-in, unregistered scalar and array key projections in maps and unambiguous direct or noinline struct fields. Struct keys and alias, auto-inline, shadowed or interned field routing are not covered by that additional projection check. Destination projection does not execute custom key codecs to predict equality; arbitrary custom codecs and global msgpack.Register overrides remain trusted caller-owned behavior. See docs/security.md for the generic decoding and projection boundaries.
MaxTotalValues admits aggregate structure before generic materialization. MaxKeyComparisonWork reserves conservative raw and projected comparison work and supported expanded array-key preparation before those operations. Zero options retain finite defaults. These quotas do not bound final driver allocations, custom codec objects, elapsed CPU time, or reader cancellation.
Use Decode/Encode for the MessagePack format pinned in specification/manifest.tsv. Reader calls buffer input and never close the reader. Keep input graphs and borrowed callbacks stable; failed destination decoding is not transactional. No transport, context API, cgo or background workers are owned here.
Index ¶
- Constants
- Variables
- func Decode(payload []byte, target any, options DecodeOptions) error
- func DecodeReader(reader io.Reader, target any, options DecodeOptions) error
- func Encode(value any, options EncodeOptions) ([]byte, error)
- func EncodeWriter(writer io.Writer, value any, options EncodeOptions) error
- type DecodeOptions
- type EncodeOptions
Constants ¶
const ( // DefaultMaxNestedLevels bounds recursive arrays and maps. DefaultMaxNestedLevels = 32 // DefaultMaxArrayElements bounds allocation amplification from arrays. DefaultMaxArrayElements = 128 << 10 // DefaultMaxMapPairs bounds allocation amplification from maps. DefaultMaxMapPairs = 64 << 10 // DefaultMaxTotalValues bounds aggregate containers, keys and values. DefaultMaxTotalValues = 256 << 10 // DefaultMaxKeyComparisonWork bounds conservative key-comparison units. DefaultMaxKeyComparisonWork = 8 << 20 )
const DefaultMaxBytes int64 = 1 << 20
DefaultMaxBytes is the default maximum MessagePack object size.
Variables ¶
var ErrPayloadTooLarge = errors.New("payload exceeds size limit")
ErrPayloadTooLarge identifies MessagePack objects over the byte limit.
Functions ¶
func Decode ¶
func Decode(payload []byte, target any, options DecodeOptions) error
Decode borrows payload and parses exactly one object into a non-nil pointer. Raw and supported projected-key refusal precede destination assignment, but failed driver decoding is not transactional. Numeric preflight rejects integer narrowing and floating-point narrowing loss, not all integer-to-float changes; the pinned driver can convert uint64 through int64 for floating destinations. Use explicit integer targets when exact integer representation is required.
func DecodeReader ¶
func DecodeReader(reader io.Reader, target any, options DecodeOptions) error
DecodeReader buffers a borrowed reader through EOF or size refusal before target decoding. Reader is never closed and there is no context API. Target must be a non-nil pointer; codec/strictness failures can leave it mutated. Keep custom codecs bounded and do not reuse a failed target without resetting it.
func Encode ¶
func Encode(value any, options EncodeOptions) ([]byte, error)
Encode borrows a stable acyclic graph and requests the driver's sorted-map encoding. Sorting is shape-dependent, not a universal canonical MessagePack profile. MaxBytes caps retained output, not custom codec work or preflight heap. Errors return no payload; compact/array options change peer representation.
func EncodeWriter ¶
func EncodeWriter(writer io.Writer, value any, options EncodeOptions) error
EncodeWriter completes serialization before touching a borrowed writer, which it never closes. Encoding failure writes nothing; a later write error can leave a prefix visible and matches wire.ErrWrite. It is not an atomic publication API.
Types ¶
type DecodeOptions ¶
type DecodeOptions struct {
// MaxBytes caps supplied input bytes; zero selects DefaultMaxBytes; negative values fail.
MaxBytes int64
// MaxNestedLevels caps recursive array/map levels; zero selects DefaultMaxNestedLevels.
MaxNestedLevels int
// MaxArrayElements caps each declared array length before generic materialization; zero
// selects its finite default.
MaxArrayElements int
// MaxMapPairs caps each declared map length before generic materialization; zero selects
// its finite default.
MaxMapPairs int
// MaxTotalValues counts every container, scalar, key and value once.
// Zero selects DefaultMaxTotalValues; negative values are invalid.
MaxTotalValues int
// MaxKeyComparisonWork bounds conservative examined-byte/value units,
// including supported expanded array-key preparation. Zero selects the
// default; negative values are invalid. This is not a CPU or heap quota.
MaxKeyComparisonWork int64
// AllowDuplicateKeys skips raw/projected duplicate checks and comparison reservations, but
// retains structural and numeric-fit admission.
AllowDuplicateKeys bool
// DisallowUnknownFields requests driver rejection of unknown struct fields.
DisallowUnknownFields bool
// NormalizeNumericWidths selects loose interface decoding, including numeric widening and
// binary-to-string conversion; typed destination policy remains separate.
NormalizeNumericWidths bool
}
DecodeOptions controls MessagePack decoding and integer interoperability.
type EncodeOptions ¶
type EncodeOptions struct {
// MaxBytes caps returned encoded bytes; zero selects DefaultMaxBytes; negative values fail.
MaxBytes int64
// CompactIntegers selects compact driver integer representation instead of retaining
// encoded widths.
CompactIntegers bool
// CompactFloats selects compact driver float representation where supported.
CompactFloats bool
// StructAsArray encodes structs positionally; peers must share the same field order.
StructAsArray bool
}
EncodeOptions controls deterministic MessagePack serialization.