bsonwire

package
v3.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const DefaultMaxBytes int64 = 1 << 20

DefaultMaxBytes is the default maximum BSON document size.

View Source
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

View Source
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 A

type A = bson.A

A is the official driver array type; it is not a top-level BSON document.

type Binary

type Binary = bson.Binary

Binary is the driver binary value including subtype and mutable byte data.

type CodeWithScope

type CodeWithScope = bson.CodeWithScope

CodeWithScope is driver JavaScript source and scope data; Encode/Decode validate scope structure without executing code.

type D

type D = bson.D

D is an ordered driver document; retain field order and explicit duplicate opt-in.

type DBPointer

type DBPointer = bson.DBPointer

DBPointer is the driver legacy database-pointer value.

type DateTime

type DateTime = bson.DateTime

DateTime is the driver signed millisecond Unix-time representation.

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 E

type E = bson.E

E is one key/value element of D.

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 M

type M = bson.M

M is a driver map document with unspecified encoding order.

type MaxKey

type MaxKey = bson.MaxKey

MaxKey is the driver BSON maximum-key marker.

type MinKey

type MinKey = bson.MinKey

MinKey is the driver BSON minimum-key marker.

type ObjectID

type ObjectID = bson.ObjectID

ObjectID is the official driver 12-byte object identifier.

type Raw

type Raw = bson.Raw

Raw is the driver byte-backed document type; alias conversion does not copy its bytes.

type RawArray

type RawArray = bson.RawArray

RawArray is the driver byte-backed array type, not a top-level document.

type RawValue

type RawValue = bson.RawValue

RawValue is a driver type tag and encoded value view; underlying bytes can alias their owner.

type Regex

type Regex = bson.Regex

Regex is the driver pattern/options description, not a compiled regular expression.

type Symbol

type Symbol = bson.Symbol

Symbol is the driver BSON symbol string.

type Timestamp

type Timestamp = bson.Timestamp

Timestamp is the driver BSON timestamp pair, not a DateTime alias.

type Undefined

type Undefined = bson.Undefined

Undefined is the driver BSON undefined marker.

Jump to

Keyboard shortcuts

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