bson
import "github.com/kitsunium/sdk/pkg/v1/data/codec/bson"
Package bson is BSON, MongoDB's binary document format, implemented with the standard library alone. Importing it registers the BSON codec with the SDK's codec registry — and no other codec (ADR 0134) — and names the Go types the BSON values Go has no type of its own for decode into:
import "github.com/kitsunium/sdk/pkg/v1/data/codec/bson"
data, err := bson.Marshal(bson.D{{Key: "name", Value: "kitsune"}, {Key: "tails", Value: 9}})
var doc map[string]any
err = bson.Unmarshal(data, &doc) // doc["tails"] is an int32
Everything that dispatches through the registry by format name reads and writes BSON as well: the codec package's Marshal and Unmarshal under Format, config.FSSource, i18n.LoadFS. Importing it beside github.com/kitsunium/sdk/pkg/v1/data/codec is harmless: the format is registered once.
Mapping
The top level is a document: a struct, a map, a D; anything else is refused. Struct fields take the tag bson:"name,omitempty,minsize,truncate,inline" and default to the field name in lower case. int, int8, int16, int32, uint8 and uint16 are written as int32 (int as int64 when it does not fit), int64, uint, uint32 and uint64 as int64, floats as double, []byte as a generic binary, time.Time as a UTC datetime in milliseconds, a nil slice, map or pointer as null, and map keys in sorted order, so the same value always encodes to the same bytes. A type can write and read itself through MarshalBSON() ([]byte, error) and UnmarshalBSON([]byte) error.
Decoding into an interface gives float64, int32, int64, string, bool, nil, A for an array, DateTime, Binary, ObjectID, Decimal128 and the other value types below — and, for a document, a D when decoding into *any, a map[string]any when decoding into a map[string]any. Numbers decode into any numeric field they fit, a double into an integer only when whole unless the field is tagged truncate.
Safety
Unmarshal checks the whole input before it writes anything: one document, at most 10 MiB, every declared length within the bytes there are, every string UTF-8, nested at most 100 levels. A malformed document is refused whole. Marshal refuses what BSON cannot hold — a string that is not UTF-8, a uint64 past the int64 range, a cyclic value — instead of writing something a reader would refuse.
Errors
Failures carry the range 0.3.36.*; match them with errs.HasCode:
CodeMarshalFailed 0.3.36.1 a value with no BSON form, or a top level that is not a document
CodeUnmarshalFailed 0.3.36.2 malformed input, or a value the target cannot hold
CodeSizeExceeded 0.3.36.3 an input past 10 MiB
CodeDepthExceeded 0.3.36.4 nesting past 100 levels, either way
CodeValueInvalid 0.3.36.5 ObjectIDFromHex, ParseDecimal128 and the JSON forms refusing their input
No error message quotes a key or a value from the input.
Index
Constants
const BinaryColumn byte = 7
BinaryColumn is a compressed time-series column.
Initialised with github.com/kitsunium/sdk/internal/service/data/codec/bson.BinaryColumn.
const BinaryEncrypted byte = 6
BinaryEncrypted is a client-side encrypted value.
Initialised with github.com/kitsunium/sdk/internal/service/data/codec/bson.BinaryEncrypted.
const BinaryFunction byte = 1
BinaryFunction marks a function.
Initialised with github.com/kitsunium/sdk/internal/service/data/codec/bson.BinaryFunction.
const BinaryGeneric byte = 0
BinaryGeneric is the default subtype, the one a []byte is written as.
Initialised with github.com/kitsunium/sdk/internal/service/data/codec/bson.BinaryGeneric.
const BinaryMD5 byte = 5
BinaryMD5 is an MD5 digest.
Initialised with github.com/kitsunium/sdk/internal/service/data/codec/bson.BinaryMD5.
const BinaryOld byte = 2
BinaryOld is the deprecated generic subtype, with an inner length.
Initialised with github.com/kitsunium/sdk/internal/service/data/codec/bson.BinaryOld.
const BinarySensitive byte = 8
BinarySensitive marks data a server must not log.
Initialised with github.com/kitsunium/sdk/internal/service/data/codec/bson.BinarySensitive.
const BinaryUUID byte = 4
BinaryUUID is an RFC 9562 UUID.
Initialised with github.com/kitsunium/sdk/internal/service/data/codec/bson.BinaryUUID.
const BinaryUUIDOld byte = 3
BinaryUUIDOld is the deprecated, byte-order-ambiguous UUID subtype.
Initialised with github.com/kitsunium/sdk/internal/service/data/codec/bson.BinaryUUIDOld.
const BinaryUserDefined byte = 128
BinaryUserDefined is the first subtype reserved for applications.
Initialised with github.com/kitsunium/sdk/internal/service/data/codec/bson.BinaryUserDefined.
const BinaryVector byte = 9
BinaryVector is a packed numeric vector.
Initialised with github.com/kitsunium/sdk/internal/service/data/codec/bson.BinaryVector.
const CodeDepthExceeded errs.Code = 205828
CodeDepthExceeded identifies nesting past 100 levels.
Initialised with github.com/kitsunium/sdk/internal/core/data/codec/bson.CodeBSONDepthExceeded.
Code 0.3.36.4
const CodeMarshalFailed errs.Code = 205825
CodeMarshalFailed identifies a value Marshal or Append cannot encode.
Initialised with github.com/kitsunium/sdk/internal/core/data/codec/bson.CodeBSONMarshalFailed.
Code 0.3.36.1
const CodeSizeExceeded errs.Code = 205827
CodeSizeExceeded identifies an Unmarshal input past 10 MiB.
Initialised with github.com/kitsunium/sdk/internal/core/data/codec/bson.CodeBSONSizeExceeded.
Code 0.3.36.3
const CodeUnmarshalFailed errs.Code = 205826
CodeUnmarshalFailed identifies input Unmarshal refuses.
Initialised with github.com/kitsunium/sdk/internal/core/data/codec/bson.CodeBSONUnmarshalFailed.
Code 0.3.36.2
const CodeValueInvalid errs.Code = 205829
CodeValueInvalid identifies a value type's constructor or parser refusing its input.
Initialised with github.com/kitsunium/sdk/internal/core/data/codec/bson.CodeBSONValueInvalid.
Code 0.3.36.5
const Format = "bson"
Format is the name BSON is registered under. It is an untyped constant, so it goes wherever a format name is taken — the codec package's Marshal, config.FSSource's string, i18n.LoadFS's codec.Format — without a conversion.
Variables
var DepthExceeded *errs.Error
DepthExceeded marks nesting past 100 levels.
Initialised with github.com/kitsunium/sdk/internal/core/data/codec/bson.DepthExceeded.
Code 0.3.36.4 · reason BSON_DEPTH_EXCEEDED · public "BSON nesting depth exceeds limit"
var MarshalFailed *errs.Error
MarshalFailed marks a value Marshal or Append cannot encode.
Initialised with github.com/kitsunium/sdk/internal/core/data/codec/bson.MarshalFailed.
Code 0.3.36.1 · reason BSON_MARSHAL_FAILED · public "BSON encoding failed"
var NilObjectID ObjectID
NilObjectID is the zero ObjectID.
var SizeExceeded *errs.Error
SizeExceeded marks an Unmarshal input past 10 MiB.
Initialised with github.com/kitsunium/sdk/internal/core/data/codec/bson.SizeExceeded.
Code 0.3.36.3 · reason BSON_SIZE_EXCEEDED · public "BSON input exceeds size limit"
var UnmarshalFailed *errs.Error
UnmarshalFailed marks input Unmarshal refuses.
Initialised with github.com/kitsunium/sdk/internal/core/data/codec/bson.UnmarshalFailed.
Code 0.3.36.2 · reason BSON_UNMARSHAL_FAILED · public "BSON decoding failed"
var ValueInvalid *errs.Error
ValueInvalid marks a value type's constructor or parser refusing its input.
Initialised with github.com/kitsunium/sdk/internal/core/data/codec/bson.ValueInvalid.
Code 0.3.36.5 · reason BSON_VALUE_INVALID · public "BSON value is invalid"
func Append(dst []byte, v any) ([]byte, error)
Append encodes v and appends the document to dst; a dst with room allocates nothing. On failure dst is returned with its original length.
func Marshal(v any) ([]byte, error)
Marshal encodes v, which must encode as a document, into a slice the caller owns.
func NewDateTimeFromTime(t time.Time) DateTime
NewDateTimeFromTime returns the DateTime t falls in, truncated to the millisecond.
func NewDecimal128(high uint64, low uint64) Decimal128
NewDecimal128 returns the decimal whose BID encoding is high (the first eight bytes) and low (the last eight).
func ObjectIDFromHex(s string) (ObjectID, error)
ObjectIDFromHex parses the 24-digit hexadecimal form of an ObjectID, in either case.
func ParseDecimal128(s string) (Decimal128, error)
ParseDecimal128 parses a decimal's string form — "1.23", "-4E+5", "NaN", "Infinity" — refusing one a decimal128 cannot hold exactly.
func Unmarshal(data []byte, v any) error
Unmarshal decodes one BSON document into v, a non-nil pointer or map. The input is checked whole before v is written.
type A
A is an array.
type A = svcbson.A
A is an alias of github.com/kitsunium/sdk/internal/service/data/codec/bson.A, declared as:
type A[] any
Binary is bytes and the subtype naming what they are.
type Binary = svcbson.Binary
Binary is an alias of github.com/kitsunium/sdk/internal/service/data/codec/bson.Binary, declared as:
type Binary struct {
// Subtype is the BSON binary subtype, BinaryGeneric for opaque bytes.
Subtype byte
// Data is the payload.
Data []byte
}
func (Binary) Equal
func (Binary) Equal(other Binary) bool
Equal reports whether b and other hold the same subtype and bytes.
func (Binary) IsZero
func (Binary) IsZero() bool
IsZero reports whether b is the empty generic binary — what omitempty leaves out.
CodeWithScope is the deprecated JavaScript with its scope document.
type CodeWithScope = svcbson.CodeWithScope
CodeWithScope is an alias of github.com/kitsunium/sdk/internal/service/data/codec/bson.CodeWithScope, declared as:
type CodeWithScope struct {
// Code is the JavaScript.
Code JavaScript
// Scope is the document.
Scope any
}
func (CodeWithScope) String
func (CodeWithScope) String() string
String renders c for a log line or a test failure.
type D
D is a document whose element order matters.
type D = svcbson.D
D is an alias of github.com/kitsunium/sdk/internal/service/data/codec/bson.D, declared as:
type D[] E
DBPointer is the deprecated namespace-and-ObjectID pointer.
type DBPointer = svcbson.DBPointer
DBPointer is an alias of github.com/kitsunium/sdk/internal/service/data/codec/bson.DBPointer, declared as:
type DBPointer struct {
// DB is the namespace the pointer names.
DB string
// Pointer is the identifier within it.
Pointer ObjectID
}
func (DBPointer) Equal
func (DBPointer) Equal(other DBPointer) bool
Equal reports whether p and other name the same namespace and identifier.
func (DBPointer) IsZero
func (DBPointer) IsZero() bool
IsZero reports whether p is the empty pointer — what omitempty leaves out.
func (DBPointer) String
func (DBPointer) String() string
String renders p for a log line or a test failure.
DateTime is milliseconds since the Unix epoch, UTC.
type DateTime = svcbson.DateTime
DateTime is an alias of github.com/kitsunium/sdk/internal/service/data/codec/bson.DateTime, declared as:
type DateTime int64
func (DateTime) MarshalJSON() ([]byte, error)
MarshalJSON writes dt as encoding/json writes the UTC time.Time it denotes.
func (DateTime) Time
func (DateTime) Time() time.Time
Time returns the instant dt denotes, in the local time zone; call UTC on it for the zone BSON stores.
func (*DateTime) UnmarshalJSON(data []byte) error
UnmarshalJSON reads the textual form MarshalJSON writes. A JSON null leaves dt unchanged, as it leaves a time.Time.
Decimal128 is an IEEE 754-2008 128-bit decimal, converted exactly or not at all.
type Decimal128 = svcbson.Decimal128
Decimal128 is an alias of github.com/kitsunium/sdk/internal/service/data/codec/bson.Decimal128, declared as:
type Decimal128 struct{}
func (Decimal128) GetBytes
func (Decimal128) GetBytes() (high uint64, low uint64)
GetBytes returns the two halves NewDecimal128 takes.
func (Decimal128) IsInf
func (Decimal128) IsInf() int
IsInf returns +1 for +Infinity, -1 for -Infinity, and 0 otherwise.
func (Decimal128) IsNaN
func (Decimal128) IsNaN() bool
IsNaN reports whether d is a NaN, quiet or signalling, whatever its sign.
func (Decimal128) IsZero
func (Decimal128) IsZero() bool
IsZero reports whether d is the zero value — all sixteen bytes zero. It is not a numeric test: "0", "0E+3" and "-0" are zeros IsZero does not report.
func (Decimal128) MarshalJSON() ([]byte, error)
MarshalJSON writes d's string form as a JSON string.
func (Decimal128) String
func (Decimal128) String() string
String returns the specification's string form: "NaN", "Infinity", "-Infinity", plain notation when the exponent is not positive and the adjusted exponent is at least -6, scientific notation otherwise.
func (*Decimal128) UnmarshalJSON(data []byte) error
UnmarshalJSON reads a JSON string holding the string form, or the extended-JSON object {"$numberDecimal": "…"}. A JSON null leaves d unchanged.
type E
E is one element of a D.
type E = svcbson.E
E is an alias of github.com/kitsunium/sdk/internal/service/data/codec/bson.E, declared as:
type E struct {
// Key is the element name. It must not contain a NUL byte.
Key string
// Value is any value the codec encodes.
Value any
}
JavaScript is JavaScript code.
type JavaScript = svcbson.JavaScript
JavaScript is an alias of github.com/kitsunium/sdk/internal/service/data/codec/bson.JavaScript, declared as:
type JavaScript string
type M
M is a document whose element order does not; its keys are written sorted.
type M = svcbson.M
M is an alias of github.com/kitsunium/sdk/internal/service/data/codec/bson.M, declared as:
type M map[string]any
MaxKey sorts higher than every other value.
type MaxKey = svcbson.MaxKey
MaxKey is an alias of github.com/kitsunium/sdk/internal/service/data/codec/bson.MaxKey, declared as:
type MaxKey struct{}
MinKey sorts lower than every other value.
type MinKey = svcbson.MinKey
MinKey is an alias of github.com/kitsunium/sdk/internal/service/data/codec/bson.MinKey, declared as:
type MinKey struct{}
Null is an explicit null, for a D.
type Null = svcbson.Null
Null is an alias of github.com/kitsunium/sdk/internal/service/data/codec/bson.Null, declared as:
type Null struct{}
ObjectID is the twelve-byte identifier; its text and JSON forms are 24 hexadecimal digits.
type ObjectID = svcbson.ObjectID
ObjectID is an alias of github.com/kitsunium/sdk/internal/service/data/codec/bson.ObjectID, declared as:
type ObjectID[12] byte
func (ObjectID) Hex
func (ObjectID) Hex() string
Hex returns the 24-digit lowercase hexadecimal form of id.
func (ObjectID) IsZero
func (ObjectID) IsZero() bool
IsZero reports whether id is NilObjectID — what omitempty leaves out.
func (ObjectID) MarshalJSON() ([]byte, error)
MarshalJSON writes the hexadecimal form as a JSON string.
func (ObjectID) MarshalText() ([]byte, error)
MarshalText writes the hexadecimal form, so an ObjectID can key a map that encoding/json writes.
func (ObjectID) String
func (ObjectID) String() string
String renders id as ObjectID("…") for a log line or a test failure.
func (ObjectID) Timestamp() time.Time
Timestamp returns the creation time id carries, to the second, in UTC.
func (*ObjectID) UnmarshalJSON(data []byte) error
UnmarshalJSON reads a JSON string holding the hexadecimal form, or the extended-JSON object {"$oid": "…"}. The empty string reads as NilObjectID, and a JSON null leaves id unchanged.
func (*ObjectID) UnmarshalText(text []byte) error
UnmarshalText reads the hexadecimal form MarshalText writes.
Regex is a pattern and its options.
type Regex = svcbson.Regex
Regex is an alias of github.com/kitsunium/sdk/internal/service/data/codec/bson.Regex, declared as:
type Regex struct {
// Pattern is the expression.
Pattern string
// Options are the single-letter flags, such as "i" or "imx".
Options string
}
func (Regex) Equal
func (Regex) Equal(other Regex) bool
Equal reports whether r and other have the same pattern and options.
func (Regex) IsZero
func (Regex) IsZero() bool
IsZero reports whether r is the empty regex — what omitempty leaves out.
func (Regex) String
func (Regex) String() string
String renders r for a log line or a test failure.
Symbol is the deprecated symbol.
type Symbol = svcbson.Symbol
Symbol is an alias of github.com/kitsunium/sdk/internal/service/data/codec/bson.Symbol, declared as:
type Symbol string
Timestamp is the replication timestamp: seconds and an increment.
type Timestamp = svcbson.Timestamp
Timestamp is an alias of github.com/kitsunium/sdk/internal/service/data/codec/bson.Timestamp, declared as:
type Timestamp struct {
// T is the seconds since the Unix epoch.
T uint32
// I is the increment, ordering operations within the same second.
I uint32
}
func (Timestamp) After
func (Timestamp) After(other Timestamp) bool
After reports whether t is later than other.
func (Timestamp) Before
func (Timestamp) Before(other Timestamp) bool
Before reports whether t is earlier than other.
func (Timestamp) Compare
func (Timestamp) Compare(other Timestamp) int
Compare returns -1 when t is before other, +1 when it is after, 0 when equal.
func (Timestamp) Equal
func (Timestamp) Equal(other Timestamp) bool
Equal reports whether t and other are the same timestamp.
func (Timestamp) IsZero
func (Timestamp) IsZero() bool
IsZero reports whether t is the zero timestamp — what omitempty leaves out.
Undefined is the deprecated undefined value.
type Undefined = svcbson.Undefined
Undefined is an alias of github.com/kitsunium/sdk/internal/service/data/codec/bson.Undefined, declared as:
type Undefined struct{}
Generated from docs/api by tools/genindex.