Documentation
¶
Overview ¶
Package bsonwire provides bounded BSON document decoding and encoding.
The package accepts complete BSON documents only; top-level scalar values and arrays are not documents and are rejected. Duplicate keys are rejected recursively by default, including inside embedded documents and arrays. ObjectID, datetime, numeric-width, ordered document, and raw document types are aliases of the official MongoDB driver types. Structs and D values have stable field order; M map order is intentionally not deterministic. BSON is excluded from wire.DetectFormat because arbitrary binary data cannot be identified reliably.
Use Decode/Encode for the pinned BSON 1.1 document grammar. Byte/raw aliases retain driver ownership semantics, not automatic copies. Reader calls buffer one document and never close the reader. Borrowed values, custom codecs and shared targets need caller synchronization; failed targets are indeterminate. No context API, transport, database client, cgo or workers are introduced.
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 A
- type Binary
- type CodeWithScope
- type D
- type DBPointer
- type DateTime
- type Decimal128
- type DecodeOptions
- type E
- type EncodeOptions
- type JavaScript
- type M
- type MaxKey
- type MinKey
- type ObjectID
- type Raw
- type RawArray
- type RawValue
- type Regex
- type Symbol
- type Timestamp
- type Undefined
Constants ¶
const DefaultMaxBytes int64 = 1 << 20
DefaultMaxBytes is the default maximum BSON document size.
const DefaultMaxNestedLevels = 100
DefaultMaxNestedLevels is the maximum raw-validation nesting below the root. Documents, arrays, and CodeWithScope scope documents each add one level. Encoding checks this policy after codec work, not before its allocation.
Variables ¶
var ErrPayloadTooLarge = errors.New("payload exceeds size limit")
ErrPayloadTooLarge identifies BSON documents over the configured byte limit.
Functions ¶
func Decode ¶
func Decode(payload []byte, target any, options DecodeOptions) error
Decode parses exactly one complete BSON document into target.
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 serializes a BSON document. Struct and D ordering is stable; M map ordering follows Go map iteration and is intentionally not guaranteed. Input/custom codecs are borrowed and must remain stable. The output buffer limit does not bound driver buffering; raw depth/duplicate validation follows encoding. Failure returns no payload, including post-encoding validation.
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 CodeWithScope ¶
type CodeWithScope = bson.CodeWithScope
CodeWithScope is driver JavaScript source and scope data; Encode/Decode validate scope structure without executing code.
type Decimal128 ¶
type Decimal128 = bson.Decimal128
Decimal128 is the official driver BSON decimal128 representation.
type DecodeOptions ¶
type DecodeOptions struct {
// MaxBytes caps supplied input bytes; zero selects DefaultMaxBytes; negative values fail.
MaxBytes int64
// AllowDuplicateKeys opts into duplicate document names; structural validation remains
// active, and preservation depends on the driver target.
AllowDuplicateKeys bool
// ObjectIDAsHexString permits the driver to decode ObjectID values into string targets.
ObjectIDAsHexString bool
// AllowTruncatingDoubles permits the driver to truncate fractional double values when
// assigning integer targets.
AllowTruncatingDoubles bool
}
DecodeOptions controls BSON decoding and explicit interoperability choices.
type EncodeOptions ¶
type EncodeOptions struct {
// MaxBytes caps returned encoded bytes; zero selects DefaultMaxBytes; negative values fail.
MaxBytes int64
// AllowDuplicateKeys opts into duplicate document names; structural validation remains
// active, and preservation depends on the driver target.
AllowDuplicateKeys bool
// MinimizeIntegerWidth enables driver integer-width minimization, which changes BSON
// representation.
MinimizeIntegerWidth bool
// UseJSONStructTags uses JSON tags for fields lacking BSON tags, according to the driver.
UseJSONStructTags bool
}
EncodeOptions controls BSON document encoding.
type JavaScript ¶
type JavaScript = bson.JavaScript
JavaScript is BSON JavaScript source data; this package does not execute it.
type RawValue ¶
RawValue is a driver type tag and encoded value view; underlying bytes can alias their owner.