bsonx

package
v0.0.18 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2018 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNilArray = errors.New("array is nil")

ErrNilArray indicates that an operation was attempted on a nil *Array.

View Source
var ErrNilDocument = errors.New("document is nil")

ErrNilDocument indicates that an operation was attempted on a nil *bson.Document.

Functions

This section is empty.

Types

type Arr

type Arr []Val

Arr represents an array in BSON.

func (Arr) Equal

func (a Arr) Equal(a2 Arr) bool

Equal compares this document to another, returning true if they are equal.

func (Arr) MarshalBSONValue

func (a Arr) MarshalBSONValue() (bsontype.Type, []byte, error)

MarshalBSONValue implements the bsoncodec.ValueMarshaler interface.

func (Arr) String

func (a Arr) String() string

String implements the fmt.Stringer interface.

func (*Arr) UnmarshalBSONValue

func (a *Arr) UnmarshalBSONValue(t bsontype.Type, data []byte) error

UnmarshalBSONValue implements the bsoncodec.ValueUnmarshaler interface.

type Doc

type Doc []Elem

Doc is a type safe, concise BSON document representation.

func ReadDoc

func ReadDoc(b []byte) (Doc, error)

ReadDoc will create a Document using the provided slice of bytes. If the slice of bytes is not a valid BSON document, this method will return an error.

func (Doc) Append

func (d Doc) Append(key string, val Val) Doc

Append adds an element to the end of the document, creating it from the key and value provided.

func (Doc) AppendMarshalBSON

func (d Doc) AppendMarshalBSON(dst []byte) ([]byte, error)

AppendMarshalBSON marshals Doc to BSON bytes, appending to dst.

This method will never return an error.

func (Doc) Copy

func (d Doc) Copy() Doc

Copy makes a shallow copy of this document.

func (Doc) Delete

func (d Doc) Delete(key string) Doc

Delete removes the element with key if it exists and returns the updated Doc.

func (Doc) Equal

func (d Doc) Equal(id IDoc) bool

Equal compares this document to another, returning true if they are equal.

func (Doc) Lookup

func (d Doc) Lookup(key ...string) Val

Lookup searches the document and potentially subdocuments or arrays for the provided key. Each key provided to this method represents a layer of depth.

This method will return an empty Value if they key does not exist. To know if they key actually exists, use LookupErr.

func (Doc) LookupElement

func (d Doc) LookupElement(key ...string) Elem

LookupElement searches the document and potentially subdocuments or arrays for the provided key. Each key provided to this method represents a layer of depth.

This method will return an empty Element if they key does not exist. To know if they key actually exists, use LookupElementErr.

func (Doc) LookupElementErr

func (d Doc) LookupElementErr(key ...string) (Elem, error)

LookupElementErr searches the document and potentially subdocuments for the provided key. Each key provided to this method represents a layer of depth.

func (Doc) LookupErr

func (d Doc) LookupErr(key ...string) (Val, error)

LookupErr searches the document and potentially subdocuments or arrays for the provided key. Each key provided to this method represents a layer of depth.

func (Doc) MarshalBSON

func (d Doc) MarshalBSON() ([]byte, error)

MarshalBSON implements the Marshaler interface.

This method will never return an error.

func (Doc) MarshalBSONValue

func (d Doc) MarshalBSONValue() (bsontype.Type, []byte, error)

MarshalBSONValue implements the bsoncodec.ValueMarshaler interface.

This method will never return an error.

func (Doc) Prepend

func (d Doc) Prepend(key string, val Val) Doc

Prepend adds an element to the beginning of the document, creating it from the key and value provided.

func (Doc) Set

func (d Doc) Set(key string, val Val) Doc

Set replaces an element of a document. If an element with a matching key is found, the element will be replaced with the one provided. If the document does not have an element with that key, the element is appended to the document instead.

func (Doc) String

func (d Doc) String() string

String implements the fmt.Stringer interface.

func (*Doc) UnmarshalBSON

func (d *Doc) UnmarshalBSON(b []byte) error

UnmarshalBSON implements the Unmarshaler interface.

type Elem

type Elem struct {
	Key   string
	Value Val
}

Elem represents a BSON element.

NOTE: Element cannot be the value of a map nor a property of a struct without special handling. The default encoders and decoders will not process Element correctly. To do so would require information loss since an Element contains a key, but the keys used when encoding a struct are the struct field names. Instead of using an Element, use a Value as a value in a map or a property of a struct.

func (Elem) Equal

func (e Elem) Equal(e2 Elem) bool

Equal compares e and e2 and returns true if they are equal.

func (Elem) String

func (e Elem) String() string

type ElementTypeError

type ElementTypeError struct {
	Method string
	Type   bsontype.Type
}

ElementTypeError specifies that a method to obtain a BSON value an incorrect type was called on a bson.Value.

TODO: rename this ValueTypeError.

func (ElementTypeError) Error

func (ete ElementTypeError) Error() string

Error implements the error interface.

type IDoc

type IDoc interface {
	// contains filtered or unexported methods
}

IDoc is the interface implemented by Doc and MDoc. It allows either of these types to be provided to the Document function to create a Value.

type KeyNotFound

type KeyNotFound struct {
	Key   []string      // The keys that were searched for.
	Depth uint          // Which key either was not found or was an incorrect type.
	Type  bsontype.Type // The type of the key that was found but was an incorrect type.
}

KeyNotFound is an error type returned from the Lookup methods on Document. This type contains information about which key was not found and if it was actually not found or if a component of the key except the last was not a document nor array.

func (KeyNotFound) Error

func (knf KeyNotFound) Error() string

type MDoc

type MDoc map[string]Val

MDoc is an unordered, type safe, concise BSON document representation. This type should not be used if you require ordering of values or duplicate keys.

func ReadMDoc

func ReadMDoc(b []byte) (MDoc, error)

ReadMDoc will create a Doc using the provided slice of bytes. If the slice of bytes is not a valid BSON document, this method will return an error.

func (MDoc) AppendMarshalBSON

func (d MDoc) AppendMarshalBSON(dst []byte) ([]byte, error)

AppendMarshalBSON marshals Doc to BSON bytes, appending to dst.

This method will never return an error.

func (MDoc) Copy

func (d MDoc) Copy() MDoc

Copy makes a shallow copy of this document.

func (MDoc) Equal

func (d MDoc) Equal(id IDoc) bool

Equal compares this document to another, returning true if they are equal.

func (MDoc) Lookup

func (d MDoc) Lookup(key ...string) Val

Lookup searches the document and potentially subdocuments or arrays for the provided key. Each key provided to this method represents a layer of depth.

This method will return an empty Value if they key does not exist. To know if they key actually exists, use LookupErr.

func (MDoc) LookupElement

func (d MDoc) LookupElement(key ...string) Elem

LookupElement searches the document and potentially subdocuments or arrays for the provided key. Each key provided to this method represents a layer of depth.

This method will return an empty Element if they key does not exist. To know if they key actually exists, use LookupElementErr.

func (MDoc) LookupElementErr

func (d MDoc) LookupElementErr(key ...string) (Elem, error)

LookupElementErr searches the document and potentially subdocuments for the provided key. Each key provided to this method represents a layer of depth.

func (MDoc) LookupErr

func (d MDoc) LookupErr(key ...string) (Val, error)

LookupErr searches the document and potentially subdocuments or arrays for the provided key. Each key provided to this method represents a layer of depth.

func (MDoc) MarshalBSON

func (d MDoc) MarshalBSON() ([]byte, error)

MarshalBSON implements the Marshaler interface.

This method will never return an error.

func (MDoc) MarshalBSONValue

func (d MDoc) MarshalBSONValue() (bsontype.Type, []byte, error)

MarshalBSONValue implements the bsoncodec.ValueMarshaler interface.

This method will never return an error.

func (MDoc) String

func (d MDoc) String() string

String implements the fmt.Stringer interface.

func (*MDoc) UnmarshalBSON

func (d *MDoc) UnmarshalBSON(b []byte) error

UnmarshalBSON implements the Unmarshaler interface.

type PrimitiveCodecs

type PrimitiveCodecs struct{}

PrimitiveCodecs is a namespace for all of the default bsoncodec.Codecs for the primitive types defined in this package.

func (PrimitiveCodecs) ArrayDecodeValue

func (pc PrimitiveCodecs) ArrayDecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, i interface{}) error

ArrayDecodeValue is the ValueDecoderFunc for *Array.

func (PrimitiveCodecs) ArrayEncodeValue

func (pc PrimitiveCodecs) ArrayEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, i interface{}) error

ArrayEncodeValue is the ValueEncoderFunc for *Array.

func (PrimitiveCodecs) DecodeDocument

func (pc PrimitiveCodecs) DecodeDocument(dctx bsoncodec.DecodeContext, dr bsonrw.DocumentReader, pdoc *Doc) error

DecodeDocument haves decoding into a Doc from a bsonrw.DocumentReader.

func (PrimitiveCodecs) DocumentDecodeValue

func (pc PrimitiveCodecs) DocumentDecodeValue(dctx bsoncodec.DecodeContext, vr bsonrw.ValueReader, i interface{}) error

DocumentDecodeValue is the ValueDecoderFunc for *Document.

func (PrimitiveCodecs) DocumentEncodeValue

func (pc PrimitiveCodecs) DocumentEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, i interface{}) error

DocumentEncodeValue is the ValueEncoderFunc for *Document.

func (PrimitiveCodecs) ElementSliceDecodeValue

func (pc PrimitiveCodecs) ElementSliceDecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, i interface{}) error

ElementSliceDecodeValue is the ValueDecoderFunc for []*Element.

func (PrimitiveCodecs) ElementSliceEncodeValue

func (pc PrimitiveCodecs) ElementSliceEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, i interface{}) error

ElementSliceEncodeValue is the ValueEncoderFunc for []*Element.

func (PrimitiveCodecs) ValueDecodeValue

func (pc PrimitiveCodecs) ValueDecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, i interface{}) error

ValueDecodeValue is the ValueDecoderFunc for *Value.

func (PrimitiveCodecs) ValueEncodeValue

func (pc PrimitiveCodecs) ValueEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, i interface{}) error

ValueEncodeValue is the ValueEncoderFunc for *Value.

type Val

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

Val represents a BSON value.

func Array

func Array(arr Arr) Val

Array constructs a Value from arr. If arr is nil, a BSON Null value is returned.

Example
internalVersion := "1234567"

f := func(appName string) Arr {
	arr := make(Arr, 0)
	arr = append(arr,
		Document(Doc{{"name", String("mongo-go-driver")}, {"version", String(internalVersion)}}),
		Document(Doc{{"type", String("darwin")}, {"architecture", String("amd64")}}),
		String("go1.9.2"),
	)
	if appName != "" {
		arr = append(arr, Document(MDoc{"name": String(appName)}))
	}

	return arr
}
_, buf, err := f("hello-world").MarshalBSONValue()
if err != nil {
	fmt.Println(err)
}
fmt.Println(buf)
Output:

[154 0 0 0 3 48 0 52 0 0 0 2 110 97 109 101 0 16 0 0 0 109 111 110 103 111 45 103 111 45 100 114 105 118 101 114 0 2 118 101 114 115 105 111 110 0 8 0 0 0 49 50 51 52 53 54 55 0 0 3 49 0 46 0 0 0 2 116 121 112 101 0 7 0 0 0 100 97 114 119 105 110 0 2 97 114 99 104 105 116 101 99 116 117 114 101 0 6 0 0 0 97 109 100 54 52 0 0 2 50 0 8 0 0 0 103 111 49 46 57 46 50 0 3 51 0 27 0 0 0 2 110 97 109 101 0 12 0 0 0 104 101 108 108 111 45 119 111 114 108 100 0 0 0]

func Binary

func Binary(subtype byte, data []byte) Val

Binary constructs a BSON binary Value.

func Boolean

func Boolean(b bool) Val

Boolean constructs a BSON boolean Value.

func CodeWithScope

func CodeWithScope(code string, scope IDoc) Val

CodeWithScope constructs a BSON code with scope Value.

func DBPointer

func DBPointer(ns string, ptr objectid.ObjectID) Val

DBPointer constructs a BSON dbpointer Value.

func DateTime

func DateTime(dt int64) Val

DateTime constructs a BSON datetime Value.

func Decimal128

func Decimal128(d128 decimal.Decimal128) Val

Decimal128 constructs a BSON decimal128 Value.

func Document

func Document(doc IDoc) Val

Document constructs a Value from the given IDoc. If nil is provided, a BSON Null value will be returned.

Example
internalVersion := "1234567"

f := func(appName string) Doc {
	doc := Doc{
		{"driver", Document(Doc{{"name", String("mongo-go-driver")}, {"version", String(internalVersion)}})},
		{"os", Document(Doc{{"type", String("darwin")}, {"architecture", String("amd64")}})},
		{"platform", String("go1.11.1")},
	}
	if appName != "" {
		doc = append(doc, Elem{"application", Document(MDoc{"name": String(appName)})})
	}

	return doc
}
buf, err := f("hello-world").MarshalBSON()
if err != nil {
	fmt.Println(err)
}
fmt.Println(buf)
Output:

[178 0 0 0 3 100 114 105 118 101 114 0 52 0 0 0 2 110 97 109 101 0 16 0 0 0 109 111 110 103 111 45 103 111 45 100 114 105 118 101 114 0 2 118 101 114 115 105 111 110 0 8 0 0 0 49 50 51 52 53 54 55 0 0 3 111 115 0 46 0 0 0 2 116 121 112 101 0 7 0 0 0 100 97 114 119 105 110 0 2 97 114 99 104 105 116 101 99 116 117 114 101 0 6 0 0 0 97 109 100 54 52 0 0 2 112 108 97 116 102 111 114 109 0 9 0 0 0 103 111 49 46 49 49 46 49 0 3 97 112 112 108 105 99 97 116 105 111 110 0 27 0 0 0 2 110 97 109 101 0 12 0 0 0 104 101 108 108 111 45 119 111 114 108 100 0 0 0]

func Double

func Double(f64 float64) Val

Double constructs a BSON double Value.

func Int32

func Int32(i32 int32) Val

Int32 constructs a BSON int32 Value.

func Int64

func Int64(i64 int64) Val

Int64 constructs a BSON int64 Value.

func JavaScript

func JavaScript(js string) Val

JavaScript constructs a BSON javascript Value.

func MaxKey

func MaxKey() Val

MaxKey constructs a BSON maxkey Value.

func MinKey

func MinKey() Val

MinKey constructs a BSON minkey Value.

func Null

func Null() Val

Null constructs a BSON binary Value.

func ObjectID

func ObjectID(oid objectid.ObjectID) Val

ObjectID constructs a BSON objectid Value.

func Regex

func Regex(pattern, options string) Val

Regex constructs a BSON regex Value.

func String

func String(str string) Val

String constructs a BSON string Value.

func Symbol

func Symbol(symbol string) Val

Symbol constructs a BSON symbol Value.

func Time

func Time(t time.Time) Val

Time constructs a BSON datetime Value.

func Timestamp

func Timestamp(t, i uint32) Val

Timestamp constructs a BSON timestamp Value.

func Undefined

func Undefined() Val

Undefined constructs a BSON binary Value.

func (Val) Array

func (v Val) Array() Arr

Array returns the BSON array value the Value represents. It panics if the value is a BSON type other than array.

func (Val) ArrayOK

func (v Val) ArrayOK() (Arr, bool)

ArrayOK is the same as Array, except it returns a boolean instead of panicking.

func (Val) Binary

func (v Val) Binary() (byte, []byte)

Binary returns the BSON binary value the Value represents. It panics if the value is a BSON type other than binary.

func (Val) BinaryOK

func (v Val) BinaryOK() (byte, []byte, bool)

BinaryOK is the same as Binary, except it returns a boolean instead of panicking.

func (Val) Boolean

func (v Val) Boolean() bool

Boolean returns the BSON boolean the Value represents. It panics if the value is a BSON type other than boolean.

func (Val) BooleanOK

func (v Val) BooleanOK() (bool, bool)

BooleanOK is the same as Boolean, except it returns a boolean instead of panicking.

func (Val) CodeWithScope

func (v Val) CodeWithScope() (string, Doc)

CodeWithScope returns the BSON code with scope value the Value represents. It panics if the value is a BSON type other than code with scope.

func (Val) CodeWithScopeOK

func (v Val) CodeWithScopeOK() (string, Doc, bool)

CodeWithScopeOK is the same as JavascriptWithScope, except that it returns a boolean instead of panicking.

func (Val) DBPointer

func (v Val) DBPointer() (string, objectid.ObjectID)

DBPointer returns the BSON dbpointer the Value represents. It panics if the value is a BSON type other than dbpointer.

func (Val) DBPointerOK

func (v Val) DBPointerOK() (string, objectid.ObjectID, bool)

DBPointerOK is the same as DBPoitner, except that it returns a boolean instead of panicking.

func (Val) DateTime

func (v Val) DateTime() int64

DateTime returns the BSON datetime the Value represents. It panics if the value is a BSON type other than datetime.

func (Val) DateTimeOK

func (v Val) DateTimeOK() (int64, bool)

DateTimeOK is the same as DateTime, except it returns a boolean instead of panicking.

func (Val) Decimal128

func (v Val) Decimal128() decimal.Decimal128

Decimal128 returns the BSON decimal128 value the Value represents. It panics if the value is a BSON type other than decimal128.

func (Val) Decimal128OK

func (v Val) Decimal128OK() (decimal.Decimal128, bool)

Decimal128OK is the same as Decimal128, except that it returns a boolean instead of panicking.

func (Val) Document

func (v Val) Document() Doc

Document returns the BSON embedded document value the Value represents. It panics if the value is a BSON type other than embedded document.

func (Val) DocumentOK

func (v Val) DocumentOK() (Doc, bool)

DocumentOK is the same as Document, except it returns a boolean instead of panicking.

func (Val) Double

func (v Val) Double() float64

Double returns the BSON double value the Value represents. It panics if the value is a BSON type other than double.

func (Val) DoubleOK

func (v Val) DoubleOK() (float64, bool)

DoubleOK is the same as Double, but returns a boolean instead of panicking.

func (Val) Equal

func (v Val) Equal(v2 Val) bool

Equal compares v to v2 and returns true if they are equal. Unknown BSON types are never equal. Two empty values are equal.

func (Val) Int32

func (v Val) Int32() int32

Int32 returns the BSON int32 the Value represents. It panics if the value is a BSON type other than int32.

func (Val) Int32OK

func (v Val) Int32OK() (int32, bool)

Int32OK is the same as Int32, except that it returns a boolean instead of panicking.

func (Val) Int64

func (v Val) Int64() int64

Int64 returns the BSON int64 the Value represents. It panics if the value is a BSON type other than int64.

func (Val) Int64OK

func (v Val) Int64OK() (int64, bool)

Int64OK is the same as Int64, except that it returns a boolean instead of panicking.

func (Val) Interface

func (v Val) Interface() interface{}

Interface returns the Go value of this Value as an empty interface.

This method will return nil if it is empty, otherwise it will return a Go primitive or a primitive.* instance.

func (Val) IsNumber

func (v Val) IsNumber() bool

IsNumber returns true if the type of v is a numberic BSON type.

func (Val) IsZero

func (v Val) IsZero() bool

IsZero returns true if this value is zero or a BSON null.

func (Val) JavaScript

func (v Val) JavaScript() string

JavaScript returns the BSON JavaScript the Value represents. It panics if the value is a BSON type other than JavaScript.

func (Val) JavaScriptOK

func (v Val) JavaScriptOK() (string, bool)

JavaScriptOK is the same as Javascript, except that it returns a boolean instead of panicking.

func (Val) MDocument

func (v Val) MDocument() MDoc

MDocument returns the BSON embedded document value the Value represents. It panics if the value is a BSON type other than embedded document.

func (Val) MDocumentOK

func (v Val) MDocumentOK() (MDoc, bool)

MDocumentOK is the same as Document, except it returns a boolean instead of panicking.

func (Val) MarshalBSONValue

func (v Val) MarshalBSONValue() (bsontype.Type, []byte, error)

MarshalBSONValue implements the bsoncodec.ValueMarshaler interface.

func (Val) MaxKey

func (v Val) MaxKey()

MaxKey returns the BSON maxkey the Value represents. It panics if the value is a BSON type other than binary.

func (Val) MaxKeyOK

func (v Val) MaxKeyOK() bool

MaxKeyOK is the same as MaxKey, except it returns a boolean instead of panicking.

func (Val) MinKey

func (v Val) MinKey()

MinKey returns the BSON minkey the Value represents. It panics if the value is a BSON type other than binary.

func (Val) MinKeyOK

func (v Val) MinKeyOK() bool

MinKeyOK is the same as MinKey, except it returns a boolean instead of panicking.

func (Val) Null

func (v Val) Null()

Null returns the BSON undefined the Value represents. It panics if the value is a BSON type other than binary.

func (Val) NullOK

func (v Val) NullOK() bool

NullOK is the same as Null, except it returns a boolean instead of panicking.

func (Val) ObjectID

func (v Val) ObjectID() objectid.ObjectID

ObjectID returns the BSON ObjectID the Value represents. It panics if the value is a BSON type other than ObjectID.

func (Val) ObjectIDOK

func (v Val) ObjectIDOK() (objectid.ObjectID, bool)

ObjectIDOK is the same as ObjectID, except it returns a boolean instead of panicking.

func (Val) Regex

func (v Val) Regex() (pattern, options string)

Regex returns the BSON regex the Value represents. It panics if the value is a BSON type other than regex.

func (Val) RegexOK

func (v Val) RegexOK() (pattern, options string, ok bool)

RegexOK is the same as Regex, except that it returns a boolean instead of panicking.

func (Val) String

func (v Val) String() string

func (Val) StringValue

func (v Val) StringValue() string

StringValue returns the BSON string the Value represents. It panics if the value is a BSON type other than string.

NOTE: This method is called StringValue to avoid it implementing the fmt.Stringer interface.

func (Val) StringValueOK

func (v Val) StringValueOK() (string, bool)

StringValueOK is the same as StringValue, but returns a boolean instead of panicking.

func (Val) Symbol

func (v Val) Symbol() string

Symbol returns the BSON symbol the Value represents. It panics if the value is a BSON type other than symbol.

func (Val) SymbolOK

func (v Val) SymbolOK() (string, bool)

SymbolOK is the same as Javascript, except that it returns a boolean instead of panicking.

func (Val) Time

func (v Val) Time() time.Time

Time returns the BSON datetime the Value represents as time.Time. It panics if the value is a BSON type other than datetime.

func (Val) TimeOK

func (v Val) TimeOK() (time.Time, bool)

TimeOK is the same as Time, except it returns a boolean instead of panicking.

func (Val) Timestamp

func (v Val) Timestamp() (t, i uint32)

Timestamp returns the BSON timestamp the Value represents. It panics if the value is a BSON type other than timestamp.

func (Val) TimestampOK

func (v Val) TimestampOK() (t uint32, i uint32, ok bool)

TimestampOK is the same as Timestamp, except that it returns a boolean instead of panicking.

func (Val) Type

func (v Val) Type() bsontype.Type

Type returns the BSON type of this value.

func (Val) Undefined

func (v Val) Undefined()

Undefined returns the BSON undefined the Value represents. It panics if the value is a BSON type other than binary.

func (Val) UndefinedOK

func (v Val) UndefinedOK() bool

UndefinedOK is the same as Undefined, except it returns a boolean instead of panicking.

func (*Val) UnmarshalBSONValue

func (v *Val) UnmarshalBSONValue(t bsontype.Type, data []byte) error

UnmarshalBSONValue implements the bsoncodec.ValueUnmarshaler interface.

Directories

Path Synopsis
Package bsoncore contains functions that can be used to encode and decode BSON elements and values to or from a slice of bytes.
Package bsoncore contains functions that can be used to encode and decode BSON elements and values to or from a slice of bytes.

Jump to

Keyboard shortcuts

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