bsoncore

package
v0.0.17 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2018 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

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 availble, 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

This section is empty.

Variables

This section is empty.

Functions

func AppendArray

func AppendArray(dst []byte, arr []byte) []byte

AppendArray will append arr to dst and return the extended buffer.

func AppendArrayElement

func AppendArrayElement(dst []byte, key string, arr []byte) []byte

AppendArrayElement will append a BSON array element using key and arr to dst and return the extended buffer.

func AppendArrayElementStart

func AppendArrayElementStart(dst []byte, key string) (index int32, b []byte)

AppendArrayElementStart appends an array element header and then the length bytes for an array, returning the index where the length starts.

func AppendArrayEnd

func AppendArrayEnd(dst []byte, index int32) ([]byte, error)

AppendArrayEnd appends the null byte to an array and calculates the length, inserting that calculated length starting at index.

func AppendArrayStart

func AppendArrayStart(dst []byte) (index int32, b []byte)

AppendArrayStart appends the length bytes to an array and then returns the index of the start of those length bytes.

func AppendBinary

func AppendBinary(dst []byte, subtype byte, b []byte) []byte

AppendBinary will append subtype and b to dst and return the extended buffer.

func AppendBinaryElement

func AppendBinaryElement(dst []byte, key string, subtype byte, b []byte) []byte

AppendBinaryElement will append a BSON binary element using key, subtype, and b to dst and return the extended buffer.

func AppendBoolean

func AppendBoolean(dst []byte, b bool) []byte

AppendBoolean will append b to dst and return the extended buffer.

func AppendBooleanElement

func AppendBooleanElement(dst []byte, key string, b bool) []byte

AppendBooleanElement will append a BSON boolean element using key and b to dst and return the extended buffer.

func AppendCodeWithScope

func AppendCodeWithScope(dst []byte, code string, scope []byte) []byte

AppendCodeWithScope will append code and scope to dst and return the extended buffer.

func AppendCodeWithScopeElement

func AppendCodeWithScopeElement(dst []byte, key, code string, scope []byte) []byte

AppendCodeWithScopeElement will append a BSON code with scope element using key, code, and scope to dst and return the extended buffer.

func AppendDBPointer

func AppendDBPointer(dst []byte, ns string, oid objectid.ObjectID) []byte

AppendDBPointer will append ns and oid to dst and return the extended buffer.

func AppendDBPointerElement

func AppendDBPointerElement(dst []byte, key, ns string, oid objectid.ObjectID) []byte

AppendDBPointerElement will append a BSON DBPointer element using key, ns, and oid to dst and return the extended buffer.

func AppendDateTime

func AppendDateTime(dst []byte, dt int64) []byte

AppendDateTime will append dt to dst and return the extended buffer.

func AppendDateTimeElement

func AppendDateTimeElement(dst []byte, key string, dt int64) []byte

AppendDateTimeElement will append a BSON datetime element using key and dt to dst and return the extended buffer.

func AppendDecimal128

func AppendDecimal128(dst []byte, d128 decimal.Decimal128) []byte

AppendDecimal128 will append d128 to dst and return the extended buffer.

func AppendDecimal128Element

func AppendDecimal128Element(dst []byte, key string, d128 decimal.Decimal128) []byte

AppendDecimal128Element will append a BSON decimal128 element using key and d128 to dst and return the extended buffer.

func AppendDocument

func AppendDocument(dst []byte, doc []byte) []byte

AppendDocument will append doc to dst and return the extended buffer.

func AppendDocumentElement

func AppendDocumentElement(dst []byte, key string, doc []byte) []byte

AppendDocumentElement will append a BSON embeded document element using key and doc to dst and return the extended buffer.

func AppendDocumentElementStart

func AppendDocumentElementStart(dst []byte, key string) (index int32, b []byte)

AppendDocumentElementStart writes a document element header and then reserves the length bytes.

func AppendDocumentEnd

func AppendDocumentEnd(dst []byte, index int32) ([]byte, error)

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

func AppendDocumentStart(dst []byte) (index int32, b []byte)

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.

TODO(skriptble): We really need AppendDocumentStart and AppendDocumentEnd. AppendDocumentStart would handle calling ReserveLength and providing the index of the start of the document. AppendDocumentEnd would handle taking that start index, adding the null byte, calculating the length, and filling in the length at the start of the document.

func AppendDocumentStartInline

func AppendDocumentStartInline(dst []byte, index *int32) []byte

AppendDocumentStartInline functions the same as AppendDocumentStart but takes a pointer to the index int32 which allows this function to be used inline.

func AppendDouble

func AppendDouble(dst []byte, f float64) []byte

AppendDouble will append f to dst and return the extended buffer.

func AppendDoubleElement

func AppendDoubleElement(dst []byte, key string, f float64) []byte

AppendDoubleElement will append a BSON double element using key and f to dst and return the extended buffer.

func AppendHeader

func AppendHeader(dst []byte, t bsontype.Type, key string) []byte

AppendHeader will append Type t and key to dst and return the extended buffer.

func AppendInt32

func AppendInt32(dst []byte, i32 int32) []byte

AppendInt32 will append i32 to dst and return the extended buffer.

func AppendInt32Element

func AppendInt32Element(dst []byte, key string, i32 int32) []byte

AppendInt32Element will append a BSON int32 element using key and i32 to dst and return the extended buffer.

func AppendInt64

func AppendInt64(dst []byte, i64 int64) []byte

AppendInt64 will append i64 to dst and return the extended buffer.

func AppendInt64Element

func AppendInt64Element(dst []byte, key string, i64 int64) []byte

AppendInt64Element will append a BSON int64 element using key and i64 to dst and return the extended buffer.

func AppendJavaScript

func AppendJavaScript(dst []byte, js string) []byte

AppendJavaScript will append js to dst and return the extended buffer.

func AppendJavaScriptElement

func AppendJavaScriptElement(dst []byte, key, js string) []byte

AppendJavaScriptElement will append a BSON JavaScript element using key and js to dst and return the extended buffer.

func AppendKey

func AppendKey(dst []byte, key string) []byte

AppendKey will append key to dst and return the extended buffer.

func AppendMaxKeyElement

func AppendMaxKeyElement(dst []byte, key string) []byte

AppendMaxKeyElement will append a BSON max key element using key to dst and return the extended buffer.

func AppendMinKeyElement

func AppendMinKeyElement(dst []byte, key string) []byte

AppendMinKeyElement will append a BSON min key element using key to dst and return the extended buffer.

func AppendNullElement

func AppendNullElement(dst []byte, key string) []byte

AppendNullElement will append a BSON null element using key to dst and return the extended buffer.

func AppendObjectID

func AppendObjectID(dst []byte, oid objectid.ObjectID) []byte

AppendObjectID will append oid to dst and return the extended buffer.

func AppendObjectIDElement

func AppendObjectIDElement(dst []byte, key string, oid objectid.ObjectID) []byte

AppendObjectIDElement will append a BSON ObjectID element using key and oid to dst and return the extended buffer.

func AppendRegex

func AppendRegex(dst []byte, pattern, options string) []byte

AppendRegex will append pattern and options to dst and return the extended buffer.

func AppendRegexElement

func AppendRegexElement(dst []byte, key, pattern, options string) []byte

AppendRegexElement will append a BSON regex element using key, pattern, and options to dst and return the extended buffer.

func AppendString

func AppendString(dst []byte, s string) []byte

AppendString will append s to dst and return the extended buffer.

func AppendStringElement

func AppendStringElement(dst []byte, key, val string) []byte

AppendStringElement will append a BSON string element using key and val to dst and return the extended buffer.

func AppendSymbol

func AppendSymbol(dst []byte, symbol string) []byte

AppendSymbol will append symbol to dst and return the extended buffer.

func AppendSymbolElement

func AppendSymbolElement(dst []byte, key, symbol string) []byte

AppendSymbolElement will append a BSON symbol element using key and symbol to dst and return the extended buffer.

func AppendTimestamp

func AppendTimestamp(dst []byte, t, i uint32) []byte

AppendTimestamp will append t and i to dst and return the extended buffer.

func AppendTimestampElement

func AppendTimestampElement(dst []byte, key string, t, i uint32) []byte

AppendTimestampElement will append a BSON timestamp element using key, t, and i to dst and return the extended buffer.

func AppendType

func AppendType(dst []byte, t bsontype.Type) []byte

AppendType will append t to dst and return the extended buffer.

func AppendUndefinedElement

func AppendUndefinedElement(dst []byte, key string) []byte

AppendUndefinedElement will append a BSON undefined element using key to dst and return the extended buffer.

func EqualValue

func EqualValue(t1, t2 bsontype.Type, v1, v2 []byte) bool

EqualValue will return true if the two values are equal.

func ReadArray

func ReadArray(src []byte) (arr []byte, rem []byte, ok bool)

ReadArray will read an array from src. If there are not enough bytes it will return false.

func ReadBinary

func ReadBinary(src []byte) (subtype byte, bin []byte, rem []byte, ok bool)

ReadBinary will read a subtype and bin from src. If there are not enough bytes it will return false.

func ReadBoolean

func ReadBoolean(src []byte) (bool, []byte, bool)

ReadBoolean will read a bool from src. If there are not enough bytes it will return false.

func ReadCodeWithScope

func ReadCodeWithScope(src []byte) (code string, scope []byte, rem []byte, ok bool)

ReadCodeWithScope will read code and scope from src. If there are not enough bytes it will return false.

func ReadDBPointer

func ReadDBPointer(src []byte) (ns string, oid objectid.ObjectID, rem []byte, ok bool)

ReadDBPointer will read a ns and oid from src. If there are not enough bytes it will return false.

func ReadDateTime

func ReadDateTime(src []byte) (int64, []byte, bool)

ReadDateTime will read an int64 datetime from src. If there are not enough bytes it will return false.

func ReadDecimal128

func ReadDecimal128(src []byte) (decimal.Decimal128, []byte, bool)

ReadDecimal128 will read a decimal.Decimal128 from src. If there are not enough bytes it will return false.

func ReadDocument

func ReadDocument(src []byte) (doc []byte, rem []byte, ok bool)

ReadDocument will read a document from src. If there are not enough bytes it will return false.

func ReadDouble

func ReadDouble(src []byte) (float64, []byte, bool)

ReadDouble will read a float64 from src. If there are not enough bytes it will return false.

func ReadHeader

func ReadHeader(src []byte) (t bsontype.Type, key string, rem []byte, ok bool)

ReadHeader will return the type byte and the key in src. If both of these values cannot be read, false is returned.

func ReadInt32

func ReadInt32(src []byte) (int32, []byte, bool)

ReadInt32 will read an int32 from src. If there are not enough bytes it will return false.

func ReadInt64

func ReadInt64(src []byte) (int64, []byte, bool)

ReadInt64 will read an int64 from src. If there are not enough bytes it will return false.

func ReadJavaScript

func ReadJavaScript(src []byte) (js string, rem []byte, ok bool)

ReadJavaScript will read a js string from src. If there are not enough bytes it will return false.

func ReadKey

func ReadKey(src []byte) (string, []byte, bool)

ReadKey will return the first key in src. The 0x00 byte will not be present in the returned string. If there are not enough bytes available, false is returned.

func ReadLength

func ReadLength(src []byte) (int32, []byte, bool)

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

func ReadObjectID(src []byte) (objectid.ObjectID, []byte, bool)

ReadObjectID will read an ObjectID from src. If there are not enough bytes it will return false.

func ReadRegex

func ReadRegex(src []byte) (pattern, options string, rem []byte, ok bool)

ReadRegex will read a pattern and options from src. If there are not enough bytes it will return false.

func ReadString

func ReadString(src []byte) (string, []byte, bool)

ReadString will read a string from src. If there are not enough bytes it will return false.

func ReadSymbol

func ReadSymbol(src []byte) (symbol string, rem []byte, ok bool)

ReadSymbol will read a symbol string from src. If there are not enough bytes it will return false.

func ReadTimestamp

func ReadTimestamp(src []byte) (t, i uint32, rem []byte, ok bool)

ReadTimestamp will read t and i from src. If there are not enough bytes it will return false.

func ReadType

func ReadType(src []byte) (bsontype.Type, []byte, bool)

ReadType will return the first byte of the provided []byte as a type. If there is no availble byte, false is returned.

func ReserveLength

func ReserveLength(dst []byte) (int32, []byte)

ReserveLength reserves the space required for length and returns the index where to write the length and the []byte with reserved space.

func UpdateLength

func UpdateLength(dst []byte, index, length int32) []byte

UpdateLength updates the length at index with length and returns the []byte.

Types

type Document

type Document []byte

Document is a raw bytes representation of a BSON document.

type Element

type Element []byte

Element is a raw bytes representation of a BSON element.

type Value

type Value struct {
	Type bsontype.Type
	Data []byte
}

Value represents a BSON value with a type and raw bytes.

func ReadValue

func ReadValue(src []byte, t bsontype.Type) (Value, []byte, bool)

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.

Jump to

Keyboard shortcuts

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