Documentation ¶
Overview ¶
Package bsoncore is intended for internal use only. It is made available to facilitate use cases that require access to internal MongoDB driver functionality and state. The API of this package is not stable and there is no backward compatibility guarantee.
WARNING: THIS PACKAGE IS EXPERIMENTAL AND MAY BE MODIFIED OR REMOVED WITHOUT NOTICE! USE WITH EXTREME CAUTION!
Package bsoncore contains functions that can be used to encode and decode BSON elements and values to or from a slice of bytes. These functions are aimed at allowing low level manipulation of BSON and can be used to build a higher level BSON library.
The Read* functions within this package return the values of the element and a boolean indicating if the values are valid. A boolean was used instead of an error because any error that would be returned would be the same: not enough bytes. This library attempts to do no validation, it will only return false if there are not enough bytes for an item to be read. For example, the ReadDocument function checks the length, if that length is larger than the number of bytes available, it will return false, if there are enough bytes, it will return those bytes and true. It is the consumers responsibility to validate those bytes.
The Append* functions within this package will append the type value to the given dst slice. If the slice has enough capacity, it will not grow the slice. The Append*Element functions within this package operate in the same way, but additionally append the BSON type and the key before the value.
Index ¶
- Constants
- Variables
- func AppendArray(dst []byte, arr []byte) []byte
- func AppendArrayElement(dst []byte, key string, arr []byte) []byte
- func AppendArrayElementStart(dst []byte, key string) (index int32, b []byte)
- func AppendArrayEnd(dst []byte, index int32) ([]byte, error)
- func AppendArrayStart(dst []byte) (index int32, b []byte)
- func AppendBinary(dst []byte, subtype byte, b []byte) []byte
- func AppendBinaryElement(dst []byte, key string, subtype byte, b []byte) []byte
- func AppendBoolean(dst []byte, b bool) []byte
- func AppendBooleanElement(dst []byte, key string, b bool) []byte
- func AppendCodeWithScope(dst []byte, code string, scope []byte) []byte
- func AppendCodeWithScopeElement(dst []byte, key, code string, scope []byte) []byte
- func AppendDBPointer(dst []byte, ns string, oid objectID) []byte
- func AppendDBPointerElement(dst []byte, key, ns string, oid objectID) []byte
- func AppendDateTime(dst []byte, dt int64) []byte
- func AppendDateTimeElement(dst []byte, key string, dt int64) []byte
- func AppendDecimal128(dst []byte, high, low uint64) []byte
- func AppendDecimal128Element(dst []byte, key string, high, low uint64) []byte
- func AppendDocument(dst []byte, doc []byte) []byte
- func AppendDocumentElement(dst []byte, key string, doc []byte) []byte
- func AppendDocumentElementStart(dst []byte, key string) (index int32, b []byte)
- func AppendDocumentEnd(dst []byte, index int32) ([]byte, error)
- func AppendDocumentStart(dst []byte) (index int32, b []byte)
- func AppendDocumentStartInline(dst []byte, index *int32) []byte
- func AppendDouble(dst []byte, f float64) []byte
- func AppendDoubleElement(dst []byte, key string, f float64) []byte
- func AppendHeader(dst []byte, t Type, key string) []byte
- func AppendInt32(dst []byte, i32 int32) []byte
- func AppendInt32Element(dst []byte, key string, i32 int32) []byte
- func AppendInt64(dst []byte, i64 int64) []byte
- func AppendInt64Element(dst []byte, key string, i64 int64) []byte
- func AppendJavaScript(dst []byte, js string) []byte
- func AppendJavaScriptElement(dst []byte, key, js string) []byte
- func AppendKey(dst []byte, key string) []byte
- func AppendMaxKeyElement(dst []byte, key string) []byte
- func AppendMinKeyElement(dst []byte, key string) []byte
- func AppendNullElement(dst []byte, key string) []byte
- func AppendObjectID(dst []byte, oid objectID) []byte
- func AppendObjectIDElement(dst []byte, key string, oid objectID) []byte
- func AppendRegex(dst []byte, pattern, options string) []byte
- func AppendRegexElement(dst []byte, key, pattern, options string) []byte
- func AppendString(dst []byte, s string) []byte
- func AppendStringElement(dst []byte, key, val string) []byte
- func AppendSymbol(dst []byte, symbol string) []byte
- func AppendSymbolElement(dst []byte, key, symbol string) []byte
- func AppendTime(dst []byte, t time.Time) []byte
- func AppendTimeElement(dst []byte, key string, t time.Time) []byte
- func AppendTimestamp(dst []byte, t, i uint32) []byte
- func AppendTimestampElement(dst []byte, key string, t, i uint32) []byte
- func AppendType(dst []byte, t Type) []byte
- func AppendUndefinedElement(dst []byte, key string) []byte
- func AppendValueElement(dst []byte, key string, value Value) []byte
- func BuildArray(dst []byte, values ...Value) []byte
- func BuildArrayElement(dst []byte, key string, values ...Value) []byte
- func BuildDocument(dst []byte, elems ...[]byte) []byte
- func BuildDocumentElement(dst []byte, key string, elems ...[]byte) []byte
- func EqualValue(t1, t2 Type, v1, v2 []byte) bool
- func NewArrayLengthError(length, rem int) error
- func NewDocumentLengthError(length, rem int) error
- func ReadBinary(src []byte) (subtype byte, bin []byte, rem []byte, ok bool)
- func ReadBoolean(src []byte) (bool, []byte, bool)
- func ReadCodeWithScope(src []byte) (code string, scope []byte, rem []byte, ok bool)
- func ReadDBPointer(src []byte) (ns string, oid objectID, rem []byte, ok bool)
- func ReadDateTime(src []byte) (int64, []byte, bool)
- func ReadDecimal128(src []byte) (high uint64, low uint64, rem []byte, ok bool)
- func ReadDouble(src []byte) (float64, []byte, bool)
- func ReadHeaderBytes(src []byte) (header []byte, rem []byte, ok bool)
- func ReadInt32(src []byte) (int32, []byte, bool)
- func ReadInt64(src []byte) (int64, []byte, bool)
- func ReadJavaScript(src []byte) (js string, rem []byte, ok bool)
- func ReadKey(src []byte) (string, []byte, bool)
- func ReadKeyBytes(src []byte) ([]byte, []byte, bool)
- func ReadLength(src []byte) (int32, []byte, bool)
- func ReadObjectID(src []byte) (objectID, []byte, bool)
- func ReadRegex(src []byte) (pattern, options string, rem []byte, ok bool)
- func ReadString(src []byte) (string, []byte, bool)
- func ReadSymbol(src []byte) (symbol string, rem []byte, ok bool)
- func ReadTime(src []byte) (time.Time, []byte, bool)
- func ReadTimestamp(src []byte) (t, i uint32, rem []byte, ok bool)
- func ReserveLength(dst []byte) (int32, []byte)
- func UpdateLength(dst []byte, index, length int32) []byte
- type Array
- type ArrayBuilder
- func (a *ArrayBuilder) AppendArray(arr []byte) *ArrayBuilder
- func (a *ArrayBuilder) AppendBinary(subtype byte, b []byte) *ArrayBuilder
- func (a *ArrayBuilder) AppendBoolean(b bool) *ArrayBuilder
- func (a *ArrayBuilder) AppendCodeWithScope(code string, scope Document) *ArrayBuilder
- func (a *ArrayBuilder) AppendDBPointer(ns string, oid objectID) *ArrayBuilder
- func (a *ArrayBuilder) AppendDateTime(dt int64) *ArrayBuilder
- func (a *ArrayBuilder) AppendDecimal128(high, low uint64) *ArrayBuilder
- func (a *ArrayBuilder) AppendDocument(doc []byte) *ArrayBuilder
- func (a *ArrayBuilder) AppendDouble(f float64) *ArrayBuilder
- func (a *ArrayBuilder) AppendInt32(i32 int32) *ArrayBuilder
- func (a *ArrayBuilder) AppendInt64(i64 int64) *ArrayBuilder
- func (a *ArrayBuilder) AppendJavaScript(js string) *ArrayBuilder
- func (a *ArrayBuilder) AppendMaxKey() *ArrayBuilder
- func (a *ArrayBuilder) AppendMinKey() *ArrayBuilder
- func (a *ArrayBuilder) AppendNull() *ArrayBuilder
- func (a *ArrayBuilder) AppendObjectID(oid objectID) *ArrayBuilder
- func (a *ArrayBuilder) AppendRegex(pattern, options string) *ArrayBuilder
- func (a *ArrayBuilder) AppendString(str string) *ArrayBuilder
- func (a *ArrayBuilder) AppendSymbol(symbol string) *ArrayBuilder
- func (a *ArrayBuilder) AppendTimestamp(t, i uint32) *ArrayBuilder
- func (a *ArrayBuilder) AppendUndefined() *ArrayBuilder
- func (a *ArrayBuilder) AppendValue(val Value) *ArrayBuilder
- func (a *ArrayBuilder) Build() Array
- func (a *ArrayBuilder) FinishArray() *ArrayBuilder
- func (a *ArrayBuilder) StartArray() *ArrayBuilder
- type Document
- func (d Document) DebugString() string
- func (d Document) Elements() ([]Element, error)
- func (d Document) Index(index uint) Element
- func (d Document) IndexErr(index uint) (Element, error)
- func (d Document) Lookup(key ...string) Value
- func (d Document) LookupErr(key ...string) (Value, error)
- func (d Document) String() string
- func (d Document) StringN(n int) string
- func (d Document) Validate() error
- func (d Document) Values() ([]Value, error)
- type DocumentBuilder
- func (db *DocumentBuilder) AppendArray(key string, arr []byte) *DocumentBuilder
- func (db *DocumentBuilder) AppendBinary(key string, subtype byte, b []byte) *DocumentBuilder
- func (db *DocumentBuilder) AppendBoolean(key string, b bool) *DocumentBuilder
- func (db *DocumentBuilder) AppendCodeWithScope(key string, code string, scope Document) *DocumentBuilder
- func (db *DocumentBuilder) AppendDBPointer(key string, ns string, oid objectID) *DocumentBuilder
- func (db *DocumentBuilder) AppendDateTime(key string, dt int64) *DocumentBuilder
- func (db *DocumentBuilder) AppendDecimal128(key string, high, low uint64) *DocumentBuilder
- func (db *DocumentBuilder) AppendDocument(key string, doc []byte) *DocumentBuilder
- func (db *DocumentBuilder) AppendDouble(key string, f float64) *DocumentBuilder
- func (db *DocumentBuilder) AppendInt32(key string, i32 int32) *DocumentBuilder
- func (db *DocumentBuilder) AppendInt64(key string, i64 int64) *DocumentBuilder
- func (db *DocumentBuilder) AppendJavaScript(key, js string) *DocumentBuilder
- func (db *DocumentBuilder) AppendMaxKey(key string) *DocumentBuilder
- func (db *DocumentBuilder) AppendMinKey(key string) *DocumentBuilder
- func (db *DocumentBuilder) AppendNull(key string) *DocumentBuilder
- func (db *DocumentBuilder) AppendObjectID(key string, oid objectID) *DocumentBuilder
- func (db *DocumentBuilder) AppendRegex(key, pattern, options string) *DocumentBuilder
- func (db *DocumentBuilder) AppendString(key string, str string) *DocumentBuilder
- func (db *DocumentBuilder) AppendSymbol(key, symbol string) *DocumentBuilder
- func (db *DocumentBuilder) AppendTimestamp(key string, t, i uint32) *DocumentBuilder
- func (db *DocumentBuilder) AppendUndefined(key string) *DocumentBuilder
- func (db *DocumentBuilder) AppendValue(key string, val Value) *DocumentBuilder
- func (db *DocumentBuilder) Build() Document
- func (db *DocumentBuilder) FinishDocument() *DocumentBuilder
- func (db *DocumentBuilder) StartDocument(key string) *DocumentBuilder
- type Element
- func (e Element) CompareKey(key []byte) bool
- func (e Element) DebugString() string
- func (e Element) Key() string
- func (e Element) KeyBytes() []byte
- func (e Element) KeyBytesErr() ([]byte, error)
- func (e Element) KeyErr() (string, error)
- func (e Element) String() string
- func (e Element) StringN(n int) string
- func (e Element) Validate() error
- func (e Element) Value() Value
- func (e Element) ValueErr() (Value, error)
- type ElementTypeError
- type InsufficientBytesError
- type InvalidDepthTraversalError
- type Iterator
- type MalformedElementError
- type Type
- type ValidationError
- type Value
- func (v Value) Array() Array
- func (v Value) ArrayOK() (Array, bool)
- func (v Value) AsInt32() int32
- func (v Value) AsInt32OK() (int32, bool)
- func (v Value) AsInt64() int64
- func (v Value) AsInt64OK() (int64, bool)
- func (v Value) Binary() (subtype byte, data []byte)
- func (v Value) BinaryOK() (subtype byte, data []byte, ok bool)
- func (v Value) Boolean() bool
- func (v Value) BooleanOK() (bool, bool)
- func (v Value) CodeWithScope() (string, Document)
- func (v Value) CodeWithScopeOK() (string, Document, bool)
- func (v Value) DBPointer() (string, objectID)
- func (v Value) DBPointerOK() (string, objectID, bool)
- func (v Value) DateTime() int64
- func (v Value) DateTimeOK() (int64, bool)
- func (v Value) DebugString() string
- func (v Value) Decimal128() (uint64, uint64)
- func (v Value) Decimal128OK() (uint64, uint64, bool)
- func (v Value) Document() Document
- func (v Value) DocumentOK() (Document, bool)
- func (v Value) Double() float64
- func (v Value) DoubleOK() (float64, bool)
- func (v Value) Equal(v2 Value) bool
- func (v Value) Int32() int32
- func (v Value) Int32OK() (int32, bool)
- func (v Value) Int64() int64
- func (v Value) Int64OK() (int64, bool)
- func (v Value) IsNumber() bool
- func (v Value) JavaScript() string
- func (v Value) JavaScriptOK() (string, bool)
- func (v Value) ObjectID() objectID
- func (v Value) ObjectIDOK() (objectID, bool)
- func (v Value) Regex() (pattern, options string)
- func (v Value) RegexOK() (pattern, options string, ok bool)
- func (v Value) String() string
- func (v Value) StringN(n int) string
- func (v Value) StringValue() string
- func (v Value) StringValueOK() (string, bool)
- func (v Value) Symbol() string
- func (v Value) SymbolOK() (string, bool)
- func (v Value) Time() time.Time
- func (v Value) TimeOK() (time.Time, bool)
- func (v Value) Timestamp() (t, i uint32)
- func (v Value) TimestampOK() (t, i uint32, ok bool)
- func (v Value) Validate() error
Examples ¶
Constants ¶
const (
// EmptyDocumentLength is the length of a document that has been started/ended but has no elements.
EmptyDocumentLength = 5
)
Variables ¶
var BuildDocumentFromElements = BuildDocument
BuildDocumentFromElements is an alaias for the BuildDocument function.
var ErrElementNotFound = errors.New("element not found")
ErrElementNotFound indicates that an Element matching a certain condition does not exist.
var ErrEmptyKey = errors.New("empty key provided")
ErrEmptyKey indicates that no key was provided to a Lookup method.
var ErrNilReader = errors.New("nil reader")
ErrNilReader indicates that an operation was attempted on a nil io.Reader.
var ErrOutOfBounds = errors.New("out of bounds")
ErrOutOfBounds indicates that an index provided to access something was invalid.
Functions ¶
func AppendArray ¶
AppendArray will append arr to dst and return the extended buffer.
func AppendArrayElement ¶
AppendArrayElement will append a BSON array element using key and arr to dst and return the extended buffer.
func AppendArrayElementStart ¶
AppendArrayElementStart appends an array element header and then the length bytes for an array, returning the index where the length starts.
func AppendArrayEnd ¶
AppendArrayEnd appends the null byte to an array and calculates the length, inserting that calculated length starting at index.
func AppendArrayStart ¶
AppendArrayStart appends the length bytes to an array and then returns the index of the start of those length bytes.
func AppendBinary ¶
AppendBinary will append subtype and b to dst and return the extended buffer.
func AppendBinaryElement ¶
AppendBinaryElement will append a BSON binary element using key, subtype, and b to dst and return the extended buffer.
func AppendBoolean ¶
AppendBoolean will append b to dst and return the extended buffer.
func AppendBooleanElement ¶
AppendBooleanElement will append a BSON boolean element using key and b to dst and return the extended buffer.
func AppendCodeWithScope ¶
AppendCodeWithScope will append code and scope to dst and return the extended buffer.
func AppendCodeWithScopeElement ¶
AppendCodeWithScopeElement will append a BSON code with scope element using key, code, and scope to dst and return the extended buffer.
func AppendDBPointer ¶
AppendDBPointer will append ns and oid to dst and return the extended buffer.
func AppendDBPointerElement ¶
AppendDBPointerElement will append a BSON DBPointer element using key, ns, and oid to dst and return the extended buffer.
func AppendDateTime ¶
AppendDateTime will append dt to dst and return the extended buffer.
func AppendDateTimeElement ¶
AppendDateTimeElement will append a BSON datetime element using key and dt to dst and return the extended buffer.
func AppendDecimal128 ¶
AppendDecimal128 will append high and low parts of a d128 to dst and return the extended buffer.
func AppendDecimal128Element ¶
AppendDecimal128Element will append high and low parts of a BSON bson.Decimal128 element using key and d128 to dst and return the extended buffer.
func AppendDocument ¶
AppendDocument will append doc to dst and return the extended buffer.
func AppendDocumentElement ¶
AppendDocumentElement will append a BSON embedded document element using key and doc to dst and return the extended buffer.
func AppendDocumentElementStart ¶
AppendDocumentElementStart writes a document element header and then reserves the length bytes.
func AppendDocumentEnd ¶
AppendDocumentEnd writes the null byte for a document and updates the length of the document. The index should be the beginning of the document's length bytes.
func AppendDocumentStart ¶
AppendDocumentStart reserves a document's length and returns the index where the length begins. This index can later be used to write the length of the document.
func AppendDocumentStartInline ¶
AppendDocumentStartInline functions the same as AppendDocumentStart but takes a pointer to the index int32 which allows this function to be used inline.
func AppendDouble ¶
AppendDouble will append f to dst and return the extended buffer.
func AppendDoubleElement ¶
AppendDoubleElement will append a BSON double element using key and f to dst and return the extended buffer.
func AppendHeader ¶
AppendHeader will append Type t and key to dst and return the extended buffer.
func AppendInt32 ¶
AppendInt32 will append i32 to dst and return the extended buffer.
func AppendInt32Element ¶
AppendInt32Element will append a BSON int32 element using key and i32 to dst and return the extended buffer.
func AppendInt64 ¶
AppendInt64 will append i64 to dst and return the extended buffer.
func AppendInt64Element ¶
AppendInt64Element will append a BSON int64 element using key and i64 to dst and return the extended buffer.
func AppendJavaScript ¶
AppendJavaScript will append js to dst and return the extended buffer.
func AppendJavaScriptElement ¶
AppendJavaScriptElement will append a BSON JavaScript element using key and js to dst and return the extended buffer.
func AppendMaxKeyElement ¶
AppendMaxKeyElement will append a BSON max key element using key to dst and return the extended buffer.
func AppendMinKeyElement ¶
AppendMinKeyElement will append a BSON min key element using key to dst and return the extended buffer.
func AppendNullElement ¶
AppendNullElement will append a BSON null element using key to dst and return the extended buffer.
func AppendObjectID ¶
AppendObjectID will append oid to dst and return the extended buffer.
func AppendObjectIDElement ¶
AppendObjectIDElement will append a BSON ObjectID element using key and oid to dst and return the extended buffer.
func AppendRegex ¶
AppendRegex will append pattern and options to dst and return the extended buffer.
func AppendRegexElement ¶
AppendRegexElement will append a BSON regex element using key, pattern, and options to dst and return the extended buffer.
func AppendString ¶
AppendString will append s to dst and return the extended buffer.
func AppendStringElement ¶
AppendStringElement will append a BSON string element using key and val to dst and return the extended buffer.
func AppendSymbol ¶
AppendSymbol will append symbol to dst and return the extended buffer.
func AppendSymbolElement ¶
AppendSymbolElement will append a BSON symbol element using key and symbol to dst and return the extended buffer.
func AppendTime ¶
AppendTime will append time as a BSON DateTime to dst and return the extended buffer.
func AppendTimeElement ¶
AppendTimeElement will append a BSON datetime element using key and dt to dst and return the extended buffer.
func AppendTimestamp ¶
AppendTimestamp will append t and i to dst and return the extended buffer.
func AppendTimestampElement ¶
AppendTimestampElement will append a BSON timestamp element using key, t, and i to dst and return the extended buffer.
func AppendType ¶
AppendType will append t to dst and return the extended buffer.
func AppendUndefinedElement ¶
AppendUndefinedElement will append a BSON undefined element using key to dst and return the extended buffer.
func AppendValueElement ¶
AppendValueElement appends value to dst as an element using key as the element's key.
func BuildArray ¶
BuildArray will append a BSON array to dst built from values.
func BuildArrayElement ¶
BuildArrayElement will create an array element using the provided values.
func BuildDocument ¶
BuildDocument will create a document with the given slice of elements and will append it to dst and return the extended buffer.
func BuildDocumentElement ¶
BuildDocumentElement will append a BSON embedded document element using key and the provided elements and return the extended buffer.
func EqualValue ¶
EqualValue will return true if the two values are equal.
func NewArrayLengthError ¶
NewArrayLengthError creates and returns an error for when the length of an array exceeds the bytes available.
func NewDocumentLengthError ¶
NewDocumentLengthError creates and returns an error for when the length of a document exceeds the bytes available.
func ReadBinary ¶
ReadBinary will read a subtype and bin from src. If there are not enough bytes it will return false.
func ReadBoolean ¶
ReadBoolean will read a bool from src. If there are not enough bytes it will return false.
func ReadCodeWithScope ¶
ReadCodeWithScope will read code and scope from src. If there are not enough bytes it will return false.
func ReadDBPointer ¶
ReadDBPointer will read a ns and oid from src. If there are not enough bytes it will return false.
func ReadDateTime ¶
ReadDateTime will read an int64 datetime from src. If there are not enough bytes it will return false.
func ReadDecimal128 ¶
ReadDecimal128 will read high and low parts of a bson.Decimal128 from src. If there are not enough bytes it will return false.
func ReadDouble ¶
ReadDouble will read a float64 from src. If there are not enough bytes it will return false.
func ReadHeaderBytes ¶
ReadHeaderBytes will read a type and a key from src and the remainder of the bytes are returned as rem. If either the type or key cannot be red, ok will be false.
func ReadInt32 ¶
ReadInt32 will read an int32 from src. If there are not enough bytes it will return false.
func ReadInt64 ¶
ReadInt64 will read an int64 from src. If there are not enough bytes it will return false.
func ReadJavaScript ¶
ReadJavaScript will read a js string from src. If there are not enough bytes it will return false.
func ReadKey ¶
ReadKey will read a key from src. The 0x00 byte will not be present in the returned string. If there are not enough bytes available, false is returned.
func ReadKeyBytes ¶
ReadKeyBytes will read a key from src as bytes. The 0x00 byte will not be present in the returned string. If there are not enough bytes available, false is returned.
func ReadLength ¶
ReadLength reads an int32 length from src and returns the length and the remaining bytes. If there aren't enough bytes to read a valid length, src is returned unomdified and the returned bool will be false.
func ReadObjectID ¶
ReadObjectID will read an ObjectID from src. If there are not enough bytes it will return false.
func ReadRegex ¶
ReadRegex will read a pattern and options from src. If there are not enough bytes it will return false.
func ReadString ¶
ReadString will read a string from src. If there are not enough bytes it will return false.
func ReadSymbol ¶
ReadSymbol will read a symbol string from src. If there are not enough bytes it will return false.
func ReadTime ¶
ReadTime will read an time.Time datetime from src. If there are not enough bytes it will return false.
func ReadTimestamp ¶
ReadTimestamp will read t and i from src. If there are not enough bytes it will return false.
func ReserveLength ¶
ReserveLength reserves the space required for length and returns the index where to write the length and the []byte with reserved space.
func UpdateLength ¶
UpdateLength updates the length at index with length and returns the []byte.
Types ¶
type Array ¶
type Array []byte
Array is a raw bytes representation of a BSON array.
func NewArrayFromReader ¶
NewArrayFromReader reads an array from r. This function will only validate the length is correct and that the array ends with a null byte.
func ReadArray ¶
ReadArray will read an array from src. If there are not enough bytes it will return false.
func (Array) DebugString ¶
DebugString outputs a human readable version of Array. It will attempt to stringify the valid components of the array even if the entire array is not valid.
func (Array) Index ¶
Index searches for and retrieves the value at the given index. This method will panic if the array is invalid or if the index is out of bounds.
func (Array) String ¶
String outputs an ExtendedJSON version of Array. If the Array is not valid, this method returns an empty string.
type ArrayBuilder ¶
type ArrayBuilder struct {
// contains filtered or unexported fields
}
ArrayBuilder builds a bson array
func NewArrayBuilder ¶
func NewArrayBuilder() *ArrayBuilder
NewArrayBuilder creates a new ArrayBuilder
func (*ArrayBuilder) AppendArray ¶
func (a *ArrayBuilder) AppendArray(arr []byte) *ArrayBuilder
AppendArray will append arr to ArrayBuilder.arr
func (*ArrayBuilder) AppendBinary ¶
func (a *ArrayBuilder) AppendBinary(subtype byte, b []byte) *ArrayBuilder
AppendBinary will append a BSON binary element using subtype, and b to a.arr
func (*ArrayBuilder) AppendBoolean ¶
func (a *ArrayBuilder) AppendBoolean(b bool) *ArrayBuilder
AppendBoolean will append a boolean element using b to a.arr
func (*ArrayBuilder) AppendCodeWithScope ¶
func (a *ArrayBuilder) AppendCodeWithScope(code string, scope Document) *ArrayBuilder
AppendCodeWithScope will append code and scope to a.arr
func (*ArrayBuilder) AppendDBPointer ¶
func (a *ArrayBuilder) AppendDBPointer(ns string, oid objectID) *ArrayBuilder
AppendDBPointer will append ns and oid to a.arr
func (*ArrayBuilder) AppendDateTime ¶
func (a *ArrayBuilder) AppendDateTime(dt int64) *ArrayBuilder
AppendDateTime will append datetime element dt to a.arr
func (*ArrayBuilder) AppendDecimal128 ¶
func (a *ArrayBuilder) AppendDecimal128(high, low uint64) *ArrayBuilder
AppendDecimal128 will append d128 to a.arr
func (*ArrayBuilder) AppendDocument ¶
func (a *ArrayBuilder) AppendDocument(doc []byte) *ArrayBuilder
AppendDocument will append doc to ArrayBuilder.arr
func (*ArrayBuilder) AppendDouble ¶
func (a *ArrayBuilder) AppendDouble(f float64) *ArrayBuilder
AppendDouble will append f to ArrayBuilder.doc
func (*ArrayBuilder) AppendInt32 ¶
func (a *ArrayBuilder) AppendInt32(i32 int32) *ArrayBuilder
AppendInt32 will append i32 to ArrayBuilder.arr
func (*ArrayBuilder) AppendInt64 ¶
func (a *ArrayBuilder) AppendInt64(i64 int64) *ArrayBuilder
AppendInt64 will append i64 to a.arr
func (*ArrayBuilder) AppendJavaScript ¶
func (a *ArrayBuilder) AppendJavaScript(js string) *ArrayBuilder
AppendJavaScript will append js to a.arr
func (*ArrayBuilder) AppendMaxKey ¶
func (a *ArrayBuilder) AppendMaxKey() *ArrayBuilder
AppendMaxKey will append a max key element to a.arr
func (*ArrayBuilder) AppendMinKey ¶
func (a *ArrayBuilder) AppendMinKey() *ArrayBuilder
AppendMinKey will append a min key element to a.arr
func (*ArrayBuilder) AppendNull ¶
func (a *ArrayBuilder) AppendNull() *ArrayBuilder
AppendNull will append a null element to a.arr
func (*ArrayBuilder) AppendObjectID ¶
func (a *ArrayBuilder) AppendObjectID(oid objectID) *ArrayBuilder
AppendObjectID will append oid to ArrayBuilder.doc
func (*ArrayBuilder) AppendRegex ¶
func (a *ArrayBuilder) AppendRegex(pattern, options string) *ArrayBuilder
AppendRegex will append pattern and options to a.arr
func (*ArrayBuilder) AppendString ¶
func (a *ArrayBuilder) AppendString(str string) *ArrayBuilder
AppendString will append str to ArrayBuilder.doc
func (*ArrayBuilder) AppendSymbol ¶
func (a *ArrayBuilder) AppendSymbol(symbol string) *ArrayBuilder
AppendSymbol will append symbol to a.arr
func (*ArrayBuilder) AppendTimestamp ¶
func (a *ArrayBuilder) AppendTimestamp(t, i uint32) *ArrayBuilder
AppendTimestamp will append t and i to a.arr
func (*ArrayBuilder) AppendUndefined ¶
func (a *ArrayBuilder) AppendUndefined() *ArrayBuilder
AppendUndefined will append a BSON undefined element using key to a.arr
func (*ArrayBuilder) AppendValue ¶
func (a *ArrayBuilder) AppendValue(val Value) *ArrayBuilder
AppendValue appends a BSON value to the array.
func (*ArrayBuilder) Build ¶
func (a *ArrayBuilder) Build() Array
Build updates the length of the array and index to the beginning of the documents length bytes, then returns the array (bson bytes)
func (*ArrayBuilder) FinishArray ¶
func (a *ArrayBuilder) FinishArray() *ArrayBuilder
FinishArray builds the most recent array created
func (*ArrayBuilder) StartArray ¶
func (a *ArrayBuilder) StartArray() *ArrayBuilder
StartArray starts building an inline Array. After this document is completed, the user must call a.FinishArray
type Document ¶
type Document []byte
Document is a raw bytes representation of a BSON document.
func NewDocumentFromReader ¶
NewDocumentFromReader reads a document from r. This function will only validate the length is correct and that the document ends with a null byte.
func ReadDocument ¶
ReadDocument will read a document from src. If there are not enough bytes it will return false.
func (Document) DebugString ¶
DebugString outputs a human readable version of Document. It will attempt to stringify the valid components of the document even if the entire document is not valid.
func (Document) Elements ¶
Elements returns this document as a slice of elements. The returned slice will contain valid elements. If the document is not valid, the elements up to the invalid point will be returned along with an error.
func (Document) Index ¶
Index searches for and retrieves the element at the given index. This method will panic if the document is invalid or if the index is out of bounds.
func (Document) Lookup ¶
Lookup searches the document, potentially recursively, for the given key. If there are multiple keys provided, this method will recurse down, as long as the top and intermediate nodes are either documents or arrays. If an error occurs or if the value doesn't exist, an empty Value is returned.
func (Document) LookupErr ¶
LookupErr is the same as Lookup, except it returns an error in addition to an empty Value.
func (Document) String ¶
String outputs an ExtendedJSON version of Document. If the document is not valid, this method returns an empty string.
type DocumentBuilder ¶
type DocumentBuilder struct {
// contains filtered or unexported fields
}
DocumentBuilder builds a bson document
func NewDocumentBuilder ¶
func NewDocumentBuilder() *DocumentBuilder
NewDocumentBuilder creates a new DocumentBuilder
func (*DocumentBuilder) AppendArray ¶
func (db *DocumentBuilder) AppendArray(key string, arr []byte) *DocumentBuilder
AppendArray will append a bson array using key and arr to DocumentBuilder.doc
func (*DocumentBuilder) AppendBinary ¶
func (db *DocumentBuilder) AppendBinary(key string, subtype byte, b []byte) *DocumentBuilder
AppendBinary will append a BSON binary element using key, subtype, and b to db.doc
func (*DocumentBuilder) AppendBoolean ¶
func (db *DocumentBuilder) AppendBoolean(key string, b bool) *DocumentBuilder
AppendBoolean will append a boolean element using key and b to db.doc
func (*DocumentBuilder) AppendCodeWithScope ¶
func (db *DocumentBuilder) AppendCodeWithScope(key string, code string, scope Document) *DocumentBuilder
AppendCodeWithScope will append code and scope using key to db.doc
func (*DocumentBuilder) AppendDBPointer ¶
func (db *DocumentBuilder) AppendDBPointer(key string, ns string, oid objectID) *DocumentBuilder
AppendDBPointer will append ns and oid to using key to db.doc
func (*DocumentBuilder) AppendDateTime ¶
func (db *DocumentBuilder) AppendDateTime(key string, dt int64) *DocumentBuilder
AppendDateTime will append a datetime element using key and dt to db.doc
func (*DocumentBuilder) AppendDecimal128 ¶
func (db *DocumentBuilder) AppendDecimal128(key string, high, low uint64) *DocumentBuilder
AppendDecimal128 will append d128 to db.doc using provided key
func (*DocumentBuilder) AppendDocument ¶
func (db *DocumentBuilder) AppendDocument(key string, doc []byte) *DocumentBuilder
AppendDocument will append a bson embedded document element using key and doc to DocumentBuilder.doc
func (*DocumentBuilder) AppendDouble ¶
func (db *DocumentBuilder) AppendDouble(key string, f float64) *DocumentBuilder
AppendDouble will append a double element using key and f to DocumentBuilder.doc
func (*DocumentBuilder) AppendInt32 ¶
func (db *DocumentBuilder) AppendInt32(key string, i32 int32) *DocumentBuilder
AppendInt32 will append an int32 element using key and i32 to DocumentBuilder.doc
func (*DocumentBuilder) AppendInt64 ¶
func (db *DocumentBuilder) AppendInt64(key string, i64 int64) *DocumentBuilder
AppendInt64 will append i64 to dst using key to db.doc
func (*DocumentBuilder) AppendJavaScript ¶
func (db *DocumentBuilder) AppendJavaScript(key, js string) *DocumentBuilder
AppendJavaScript will append js using the provided key to db.doc
func (*DocumentBuilder) AppendMaxKey ¶
func (db *DocumentBuilder) AppendMaxKey(key string) *DocumentBuilder
AppendMaxKey will append a max key element using key to db.doc
func (*DocumentBuilder) AppendMinKey ¶
func (db *DocumentBuilder) AppendMinKey(key string) *DocumentBuilder
AppendMinKey will append a min key element using key to db.doc
func (*DocumentBuilder) AppendNull ¶
func (db *DocumentBuilder) AppendNull(key string) *DocumentBuilder
AppendNull will append a null element using key to db.doc
func (*DocumentBuilder) AppendObjectID ¶
func (db *DocumentBuilder) AppendObjectID(key string, oid objectID) *DocumentBuilder
AppendObjectID will append oid to DocumentBuilder.doc with the given key
func (*DocumentBuilder) AppendRegex ¶
func (db *DocumentBuilder) AppendRegex(key, pattern, options string) *DocumentBuilder
AppendRegex will append pattern and options using key to db.doc
func (*DocumentBuilder) AppendString ¶
func (db *DocumentBuilder) AppendString(key string, str string) *DocumentBuilder
AppendString will append str to DocumentBuilder.doc with the given key
func (*DocumentBuilder) AppendSymbol ¶
func (db *DocumentBuilder) AppendSymbol(key, symbol string) *DocumentBuilder
AppendSymbol will append a BSON symbol element using key and symbol db.doc
func (*DocumentBuilder) AppendTimestamp ¶
func (db *DocumentBuilder) AppendTimestamp(key string, t, i uint32) *DocumentBuilder
AppendTimestamp will append t and i to db.doc using provided key
func (*DocumentBuilder) AppendUndefined ¶
func (db *DocumentBuilder) AppendUndefined(key string) *DocumentBuilder
AppendUndefined will append a BSON undefined element using key to db.doc
func (*DocumentBuilder) AppendValue ¶
func (db *DocumentBuilder) AppendValue(key string, val Value) *DocumentBuilder
AppendValue will append a BSON element with the provided key and value to the document.
func (*DocumentBuilder) Build ¶
func (db *DocumentBuilder) Build() Document
Build updates the length of the document and index to the beginning of the documents length bytes, then returns the document (bson bytes)
func (*DocumentBuilder) FinishDocument ¶
func (db *DocumentBuilder) FinishDocument() *DocumentBuilder
FinishDocument builds the most recent document created
func (*DocumentBuilder) StartDocument ¶
func (db *DocumentBuilder) StartDocument(key string) *DocumentBuilder
StartDocument starts building an inline document element with the provided key After this document is completed, the user must call finishDocument
type Element ¶
type Element []byte
Element is a raw bytes representation of a BSON element.
func ReadElement ¶
ReadElement reads the next full element from src. It returns the element, the remaining bytes in the slice, and a boolean indicating if the read was successful.
func (Element) CompareKey ¶
CompareKey will compare this element's key to key. This method makes it easy to compare keys without needing to allocate a string. The key may be null terminated. If a valid key cannot be read this method will return false.
func (Element) DebugString ¶
DebugString outputs a human readable version of RawElement. It will attempt to stringify the valid components of the element even if the entire element is not valid.
func (Element) Key ¶
Key returns the key for this element. If the element is not valid, this method returns an empty string. If knowing if the element is valid is important, use KeyErr.
func (Element) KeyBytes ¶
KeyBytes returns the key for this element as a []byte. If the element is not valid, this method returns an empty string. If knowing if the element is valid is important, use KeyErr. This method will not include the null byte at the end of the key in the slice of bytes.
func (Element) KeyBytesErr ¶
KeyBytesErr returns the key for this element as a []byte, returning an error if the element is not valid.
func (Element) KeyErr ¶
KeyErr returns the key for this element, returning an error if the element is not valid.
func (Element) String ¶
String implements the fmt.String interface. The output will be in extended JSON format.
func (Element) StringN ¶
StringN implements the fmt.String interface for upto N bytes. The output will be in extended JSON format.
type ElementTypeError ¶
ElementTypeError specifies that a method to obtain a BSON value an incorrect type was called on a bson.Value.
func (ElementTypeError) Error ¶
func (ete ElementTypeError) Error() string
Error implements the error interface.
type InsufficientBytesError ¶
InsufficientBytesError indicates that there were not enough bytes to read the next component.
func NewInsufficientBytesError ¶
func NewInsufficientBytesError(src, rem []byte) InsufficientBytesError
NewInsufficientBytesError creates a new InsufficientBytesError with the given Document and remaining bytes.
func (InsufficientBytesError) Equal ¶
func (ibe InsufficientBytesError) Equal(err2 error) bool
Equal checks that err2 also is an ErrTooSmall.
func (InsufficientBytesError) Error ¶
func (ibe InsufficientBytesError) Error() string
Error implements the error interface.
type InvalidDepthTraversalError ¶
InvalidDepthTraversalError is returned when attempting a recursive Lookup when one component of the path is neither an embedded document nor an array.
func (InvalidDepthTraversalError) Error ¶
func (idte InvalidDepthTraversalError) Error() string
type Iterator ¶
type Iterator struct { List Array // List of BSON values // contains filtered or unexported fields }
Iterator maintains a list of BSON values and keeps track of the current position in relation to its Next() method.
func (*Iterator) Documents ¶
Documents traverses the list as documents and returns them. This method assumes that the underlying list is composed of documents and will return an error otherwise.
type MalformedElementError ¶
type MalformedElementError string
MalformedElementError represents a class of errors that RawElement methods return.
const ErrElementMissingKey MalformedElementError = "element is missing key"
ErrElementMissingKey is returned when a RawElement is missing a key.
const ErrElementMissingType MalformedElementError = "element is missing type"
ErrElementMissingType is returned when a RawElement is missing a type.
func (MalformedElementError) Error ¶
func (mee MalformedElementError) Error() string
type Type ¶
type Type byte
Type represents a BSON type.
const ( TypeDouble Type = 0x01 TypeString Type = 0x02 TypeEmbeddedDocument Type = 0x03 TypeArray Type = 0x04 TypeBinary Type = 0x05 TypeUndefined Type = 0x06 TypeObjectID Type = 0x07 TypeBoolean Type = 0x08 TypeDateTime Type = 0x09 TypeNull Type = 0x0A TypeRegex Type = 0x0B TypeDBPointer Type = 0x0C TypeJavaScript Type = 0x0D TypeSymbol Type = 0x0E TypeCodeWithScope Type = 0x0F TypeInt32 Type = 0x10 TypeTimestamp Type = 0x11 TypeInt64 Type = 0x12 TypeDecimal128 Type = 0x13 TypeMaxKey Type = 0x7F TypeMinKey Type = 0xFF )
BSON element types as described in https://bsonspec.org/spec.html.
func ReadHeader ¶
ReadHeader will read a type byte and a key from src. If both of these values cannot be read, false is returned.
type ValidationError ¶
type ValidationError string
ValidationError is an error type returned when attempting to validate a document or array.
const ErrInvalidLength ValidationError = "document or array length is invalid"
ErrInvalidLength indicates that a length in a binary representation of a BSON document or array is invalid.
const ErrMissingNull ValidationError = "document or array end is missing null byte"
ErrMissingNull is returned when a document or array's last byte is not null.
func (ValidationError) Error ¶
func (ve ValidationError) Error() string
type Value ¶
Value represents a BSON value with a type and raw bytes.
func BuildDocumentValue ¶
BuildDocumentValue creates an Embedded Document value from the given elements.
func ReadValue ¶
ReadValue reads the next value as the provided types and returns a Value, the remaining bytes, and a boolean indicating if the read was successful.
func (Value) Array ¶
Array returns the BSON array the Value represents as an Array. It panics if the value is a BSON type other than array.
func (Value) ArrayOK ¶
ArrayOK is the same as Array, except it returns a boolean instead of panicking.
func (Value) AsInt32 ¶
AsInt32 returns a BSON number as an int32. If the BSON type is not a numeric one, this method will panic.
func (Value) AsInt32OK ¶
AsInt32OK functions the same as AsInt32 but returns a boolean instead of panicking. False indicates an error.
func (Value) AsInt64 ¶
AsInt64 returns a BSON number as an int64. If the BSON type is not a numeric one, this method will panic.
func (Value) AsInt64OK ¶
AsInt64OK functions the same as AsInt64 but returns a boolean instead of panicking. False indicates an error.
func (Value) Binary ¶
Binary returns the BSON binary value the Value represents. It panics if the value is a BSON type other than binary.
func (Value) BinaryOK ¶
BinaryOK is the same as Binary, except it returns a boolean instead of panicking.
func (Value) Boolean ¶
Boolean returns the boolean value the Value represents. It panics if the value is a BSON type other than boolean.
func (Value) BooleanOK ¶
BooleanOK is the same as Boolean, except it returns a boolean instead of panicking.
func (Value) CodeWithScope ¶
CodeWithScope returns the BSON JavaScript code with scope the Value represents. It panics if the value is a BSON type other than JavaScript code with scope.
func (Value) CodeWithScopeOK ¶
CodeWithScopeOK is the same as CodeWithScope, except that it returns a boolean instead of panicking.
func (Value) DBPointer ¶
DBPointer returns the BSON dbpointer value the Value represents. It panics if the value is a BSON type other than DBPointer.
func (Value) DBPointerOK ¶
DBPointerOK is the same as DBPoitner, except that it returns a boolean instead of panicking.
func (Value) DateTime ¶
DateTime returns the BSON datetime value the Value represents as a unix timestamp. It panics if the value is a BSON type other than datetime.
func (Value) DateTimeOK ¶
DateTimeOK is the same as DateTime, except it returns a boolean instead of panicking.
func (Value) DebugString ¶
DebugString outputs a human readable version of Document. It will attempt to stringify the valid components of the document even if the entire document is not valid.
func (Value) Decimal128 ¶
Decimal128 returns the decimal the Value represents. It panics if the value is a BSON type other than decimal.
func (Value) Decimal128OK ¶
Decimal128OK is the same as Decimal128, except that it returns a boolean instead of panicking.
func (Value) Document ¶
Document returns the BSON document the Value represents as a Document. It panics if the value is a BSON type other than document.
func (Value) DocumentOK ¶
DocumentOK is the same as Document, except it returns a boolean instead of panicking.
func (Value) Double ¶
Double returns the float64 value for this element. It panics if e's BSON type is not TypeDouble.
func (Value) Int32 ¶
Int32 returns the int32 the Value represents. It panics if the value is a BSON type other than int32.
func (Value) Int32OK ¶
Int32OK is the same as Int32, except that it returns a boolean instead of panicking.
func (Value) Int64 ¶
Int64 returns the int64 the Value represents. It panics if the value is a BSON type other than int64.
func (Value) Int64OK ¶
Int64OK is the same as Int64, except that it returns a boolean instead of panicking.
func (Value) JavaScript ¶
JavaScript returns the BSON JavaScript code value the Value represents. It panics if the value is a BSON type other than JavaScript code.
func (Value) JavaScriptOK ¶
JavaScriptOK is the same as Javascript, excepti that it returns a boolean instead of panicking.
func (Value) ObjectID ¶
func (v Value) ObjectID() objectID
ObjectID returns the BSON objectid value the Value represents. It panics if the value is a BSON type other than objectid.
func (Value) ObjectIDOK ¶
ObjectIDOK is the same as ObjectID, except it returns a boolean instead of panicking.
func (Value) Regex ¶
Regex returns the BSON regex value the Value represents. It panics if the value is a BSON type other than regex.
func (Value) RegexOK ¶
RegexOK is the same as Regex, except it returns a boolean instead of panicking.
func (Value) String ¶
String implements the fmt.String interface. This method will return values in extended JSON format. If the value is not valid, this returns an empty string
func (Value) StringN ¶
StringN implements the fmt.String interface. This method will return values in extended JSON format that will stringify a value upto N bytes. If the value is not valid, this returns an empty string
func (Value) StringValue ¶
StringValue returns the string balue for this element. It panics if e's BSON type is not TypeString.
NOTE: This method is called StringValue to avoid a collision with the String method which implements the fmt.Stringer interface.
func (Value) StringValueOK ¶
StringValueOK is the same as StringValue, but returns a boolean instead of panicking.
func (Value) Symbol ¶
Symbol returns the BSON symbol value the Value represents. It panics if the value is a BSON type other than symbol.
func (Value) SymbolOK ¶
SymbolOK is the same as Symbol, excepti that it returns a boolean instead of panicking.
func (Value) Time ¶
Time returns the BSON datetime value the Value represents. It panics if the value is a BSON type other than datetime.
func (Value) Timestamp ¶
Timestamp returns the BSON timestamp value the Value represents. It panics if the value is a BSON type other than timestamp.
func (Value) TimestampOK ¶
TimestampOK is the same as Timestamp, except that it returns a boolean instead of panicking.