bson2

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package bson2 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     *bson2.Document or bson2.RawDocument
Array               *bson2.Array    or bson2.RawArray

Double              float64
String              string
Binary data         bson2.Binary
ObjectId            bson2.ObjectID
Boolean             bool
Date                time.Time
Null                bson2.NullType
Regular Expression  bson2.Regex
32-bit integer      int32
Timestamp           bson2.Timestamp
64-bit integer      int64

Composite types (Document and Array) are passed by pointers. Raw composite type and scalars are passed by values.

Index

Constants

This section is empty.

Variables

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

Null represents BSON scalar value null.

Functions

This section is empty.

Types

type Array

type Array struct {
	// contains filtered or unexported fields
}

Array represents a BSON array in the (partially) decoded form.

func ConvertArray

func ConvertArray(arr *types.Array) (*Array, error)

ConvertArray converts *types.Array to Array.

func (*Array) Convert

func (arr *Array) Convert() (*types.Array, error)

Convert converts Array to *types.Array, decoding raw documents and arrays on the fly.

func (*Array) Encode

func (arr *Array) Encode() (RawArray, error)

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.

func (*Array) LogValue

func (arr *Array) LogValue() slog.Value

LogValue implements slog.LogValuer interface.

type Binary

type Binary = bsonproto.Binary

Binary represents BSON scalar type binary.

type BinarySubtype

type BinarySubtype = bsonproto.BinarySubtype

BinarySubtype represents BSON Binary's subtype.

type CompositeType

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

func ConvertDocument(doc *types.Document) (*Document, error)

ConvertDocument converts *types.Document to Document.

func MakeDocument

func MakeDocument(cap int) *Document

MakeDocument creates a new empty Document with the given capacity.

func NewDocument

func NewDocument(pairs ...any) (*Document, error)

NewDocument creates a new Document from the given pairs of field names and values.

func (*Document) Convert

func (doc *Document) Convert() (*types.Document, error)

Convert converts Document to *types.Document, decoding raw documents and arrays on the fly.

func (*Document) Encode

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.

func (*Document) Get

func (doc *Document) Get(name string) any

Get returns a value of the field with the given name.

It returns nil if the field is not found. If document contains duplicate field names, it returns the first one.

func (*Document) LogValue

func (doc *Document) LogValue() slog.Value

LogValue implements slog.LogValuer interface.

type NullType

type NullType = bsonproto.NullType

NullType represents BSON scalar type null.

type ObjectID

type ObjectID = bsonproto.ObjectID

ObjectID represents BSON scalar type ObjectID.

type RawArray

type RawArray []byte

RawArray represents a BSON array in the binary encoded form.

It generally references a part of a larger slice, not a copy.

func (RawArray) Convert

func (raw RawArray) Convert() (*types.Array, error)

Convert converts a single BSON array that takes the whole raw slice into *types.Array.

func (RawArray) Decode

func (raw RawArray) Decode() (*Array, error)

Decode decodes a single BSON array that takes the whole raw slice.

Only first-level fields are decoded; nested documents and arrays are converted to RawDocument and RawArray respectively, using raw's subslices without copying.

func (RawArray) DecodeDeep

func (raw RawArray) DecodeDeep() (*Array, error)

DecodeDeep decodes a single BSON array that takes the whole raw slice.

All nested documents and arrays are decoded recursively.

func (RawArray) LogValue

func (arr RawArray) LogValue() slog.Value

LogValue implements slog.LogValuer interface.

type RawDocument

type RawDocument []byte

RawDocument represents a 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

func (raw RawDocument) Convert() (*types.Document, error)

Convert converts a single BSON document that takes the whole raw slice into *types.Document.

func (RawDocument) Decode

func (raw RawDocument) Decode() (*Document, error)

Decode decodes a single BSON document that takes the whole raw slice.

Only first-level fields are decoded; nested documents and arrays are converted to RawDocument and RawArray respectively, using raw's subslices without copying.

func (RawDocument) DecodeDeep

func (raw RawDocument) DecodeDeep() (*Document, error)

DecodeDeep decodes a single BSON document that takes the whole raw slice.

All nested documents and arrays are decoded recursively.

func (RawDocument) LogValue

func (doc RawDocument) LogValue() slog.Value

LogValue implements slog.LogValuer interface.

type Regex

type Regex = bsonproto.Regex

Regex represents BSON scalar type regular expression.

type ScalarType

type ScalarType = bsonproto.ScalarType

ScalarType represents a BSON scalar type.

CString is not included as it is not a real BSON type.

type Timestamp

type Timestamp = bsonproto.Timestamp

Timestamp represents BSON scalar type timestamp.

type Type

type Type interface {
	ScalarType | CompositeType
}

Type represents a BSON type.

Jump to

Keyboard shortcuts

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