Documentation ¶
Overview ¶
Package bson implements encoding and decoding of BSON as defined by https://bsonspec.org/spec.html.
Types ¶
The following BSON types are supported:
BSON Go Document/Object *bson.Document or bson.RawDocument Array *bson.Array or bson.RawArray Double float64 String string Binary data bson.Binary ObjectId bson.ObjectID Boolean bool Date time.Time Null bson.NullType Regular Expression bson.Regex 32-bit integer int32 Timestamp bson.Timestamp 64-bit integer int64
Composite types (Document and Array) are passed by pointers. Raw composite type and scalars are passed by values.
Index ¶
- Variables
- func Convert[T types.Type](v T) (any, error)
- func DecodeCString(b []byte) (string, error)
- func EncodeCString(b []byte, v string)
- func FindRaw(b []byte) (int, error)
- func LogMessage(v any) string
- func LogMessageBlock(v any) string
- func SizeCString(s string) int
- type Array
- type Binary
- type BinarySubtype
- type CompositeType
- type Document
- type NullType
- type ObjectID
- type RawArray
- type RawDocument
- type Regex
- type ScalarType
- type Timestamp
- type Type
Constants ¶
This section is empty.
Variables ¶
var ( // ErrDecodeShortInput is returned wrapped by Decode functions if the input bytes slice is too short. ErrDecodeShortInput = bsonproto.ErrDecodeShortInput // ErrDecodeInvalidInput is returned wrapped by Decode functions if the input bytes slice is invalid. ErrDecodeInvalidInput = bsonproto.ErrDecodeInvalidInput )
var Null = bsonproto.Null
Null represents BSON scalar value null.
Functions ¶
func Convert ¶ added in v1.21.0
Convert converts valid types package values to BSON values of that package.
Conversions of composite types may cause errors.
func DecodeCString ¶ added in v1.21.0
DecodeCString decodes cstring value from b.
If there is not enough bytes, DecodeCString will return a wrapped ErrDecodeShortInput. If the input is otherwise invalid, a wrapped ErrDecodeInvalidInput is returned.
func EncodeCString ¶ added in v1.21.0
EncodeCString encodes cstring value v into b.
Slice must be at least len(v)+1 (SizeCString) bytes long; otherwise, EncodeString will panic. Only b[0:len(v)+1] bytes are modified.
func FindRaw ¶ added in v1.21.0
FindRaw finds the first raw BSON document or array in b and returns its length l. It should start from the first byte of b. RawDocument(b[:l] / RawArray(b[:l]) might not be valid. It is the caller's responsibility to check it.
Use RawDocument(b) / RawArray(b) conversion instead if b contains exactly one document/array and no extra bytes.
func LogMessage ¶ added in v1.21.0
LogMessage returns a representation as a string. It may change over time.
func LogMessageBlock ¶ added in v1.21.0
LogMessageBlock is a variant of [RawArray.LogMessage] that never uses a flow style.
func SizeCString ¶ added in v1.21.0
SizeCString returns a size of the encoding of v cstring in bytes.
Types ¶
type Array ¶
type Array struct {
// contains filtered or unexported fields
}
Array represents a BSON array in the (partially) decoded form.
func ConvertArray ¶ added in v1.21.0
ConvertArray converts *types.Array to Array.
func (*Array) Convert ¶ added in v1.21.0
Convert converts Array to *types.Array, decoding raw documents and arrays on the fly.
func (*Array) Encode ¶ added in v1.21.0
Encode encodes BSON array.
TODO https://github.com/FerretDB/FerretDB/issues/3759 This method should accept a slice of bytes, not return it. That would allow to avoid unnecessary allocations.
type BinarySubtype ¶ added in v1.21.0
type BinarySubtype = bsonproto.BinarySubtype
BinarySubtype represents BSON Binary's subtype.
type CompositeType ¶ added in v1.21.0
type CompositeType interface { *Document | *Array | RawDocument | RawArray }
CompositeType represents a BSON composite type (including raw types).
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
Document represents a BSON document a.k.a object in the (partially) decoded form.
It may contain duplicate field names.
func ConvertDocument ¶
ConvertDocument converts *types.Document to Document.
func MakeDocument ¶ added in v1.21.0
MakeDocument creates a new empty Document with the given capacity.
func NewDocument ¶ added in v1.21.0
NewDocument creates a new Document from the given pairs of field names and values.
func (*Document) Convert ¶ added in v1.21.0
Convert converts Document to *types.Document, decoding raw documents and arrays on the fly.
func (*Document) Encode ¶ added in v1.21.0
func (doc *Document) Encode() (RawDocument, error)
Encode encodes BSON document.
TODO https://github.com/FerretDB/FerretDB/issues/3759 This method should accept a slice of bytes, not return it. That would allow to avoid unnecessary allocations.
type RawArray ¶ added in v1.21.0
type RawArray []byte
RawArray represents a single BSON array in the binary encoded form.
It generally references a part of a larger slice, not a copy.
func (RawArray) Convert ¶ added in v1.21.0
Convert converts a single valid BSON array that takes the whole byte slice into *types.Array.
func (RawArray) Decode ¶ added in v1.21.0
Decode decodes a single BSON array that takes the whole byte slice.
Only top-level elements are decoded; nested documents and arrays are converted to RawDocument and RawArray respectively, using raw's subslices without copying.
func (RawArray) DecodeDeep ¶ added in v1.21.0
DecodeDeep decodes a single valid BSON array that takes the whole byte slice.
All nested documents and arrays are decoded recursively.
type RawDocument ¶ added in v1.21.0
type RawDocument []byte
RawDocument represents a singe BSON document a.k.a object in the binary encoded form.
It generally references a part of a larger slice, not a copy.
func (RawDocument) Convert ¶ added in v1.21.0
func (raw RawDocument) Convert() (*types.Document, error)
Convert converts a single valid BSON document that takes the whole byte slice into *types.Document.
func (RawDocument) Decode ¶ added in v1.21.0
func (raw RawDocument) Decode() (*Document, error)
Decode decodes a single BSON document that takes the whole byte slice.
Only top-level fields are decoded; nested documents and arrays are converted to RawDocument and RawArray respectively, using raw's subslices without copying.
func (RawDocument) DecodeDeep ¶ added in v1.21.0
func (raw RawDocument) DecodeDeep() (*Document, error)
DecodeDeep decodes a single valid BSON document that takes the whole byte slice.
All nested documents and arrays are decoded recursively.
func (RawDocument) LogValue ¶ added in v1.21.0
func (raw RawDocument) LogValue() slog.Value
LogValue implements slog.LogValuer interface.
type ScalarType ¶ added in v1.21.0
type ScalarType = bsonproto.ScalarType
ScalarType represents a BSON scalar type.
CString is not included as it is not a real BSON type.
type Type ¶ added in v1.21.0
type Type interface { ScalarType | CompositeType }
Type represents a BSON type.