Documentation ¶
Index ¶
- Variables
- type Arr
- type Doc
- func (d Doc) Append(key string, val Val) Doc
- func (d Doc) AppendMarshalBSON(dst []byte) ([]byte, error)
- func (d Doc) Copy() Doc
- func (d Doc) Delete(key string) Doc
- func (d Doc) Equal(id IDoc) bool
- func (d Doc) Lookup(key ...string) Val
- func (d Doc) LookupElement(key ...string) Elem
- func (d Doc) LookupElementErr(key ...string) (Elem, error)
- func (d Doc) LookupErr(key ...string) (Val, error)
- func (d Doc) MarshalBSON() ([]byte, error)
- func (d Doc) MarshalBSONValue() (bsontype.Type, []byte, error)
- func (d Doc) Prepend(key string, val Val) Doc
- func (d Doc) Set(key string, val Val) Doc
- func (d Doc) String() string
- func (d *Doc) UnmarshalBSON(b []byte) error
- type Elem
- type ElementTypeError
- type IDoc
- type KeyNotFound
- type MDoc
- func (d MDoc) AppendMarshalBSON(dst []byte) ([]byte, error)
- func (d MDoc) Copy() MDoc
- func (d MDoc) Equal(id IDoc) bool
- func (d MDoc) Lookup(key ...string) Val
- func (d MDoc) LookupElement(key ...string) Elem
- func (d MDoc) LookupElementErr(key ...string) (Elem, error)
- func (d MDoc) LookupErr(key ...string) (Val, error)
- func (d MDoc) MarshalBSON() ([]byte, error)
- func (d MDoc) MarshalBSONValue() (bsontype.Type, []byte, error)
- func (d MDoc) String() string
- func (d *MDoc) UnmarshalBSON(b []byte) error
- type PrimitiveCodecs
- func (pc PrimitiveCodecs) ArrayDecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
- func (pc PrimitiveCodecs) ArrayEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
- func (pc PrimitiveCodecs) DecodeDocument(dctx bsoncodec.DecodeContext, dr bsonrw.DocumentReader, pdoc *Doc) error
- func (pc PrimitiveCodecs) DocumentDecodeValue(dctx bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
- func (pc PrimitiveCodecs) DocumentEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
- func (pc PrimitiveCodecs) ElementSliceDecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
- func (pc PrimitiveCodecs) ElementSliceEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
- func (pc PrimitiveCodecs) RegisterPrimitiveCodecs(rb *bsoncodec.RegistryBuilder)
- func (pc PrimitiveCodecs) ValueDecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
- func (pc PrimitiveCodecs) ValueEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
- type Val
- func Array(arr Arr) Val
- func Binary(subtype byte, data []byte) Val
- func Boolean(b bool) Val
- func CodeWithScope(code string, scope IDoc) Val
- func DBPointer(ns string, ptr primitive.ObjectID) Val
- func DateTime(dt int64) Val
- func Decimal128(d128 primitive.Decimal128) Val
- func Document(doc IDoc) Val
- func Double(f64 float64) Val
- func Int32(i32 int32) Val
- func Int64(i64 int64) Val
- func JavaScript(js string) Val
- func MaxKey() Val
- func MinKey() Val
- func Null() Val
- func ObjectID(oid primitive.ObjectID) Val
- func Regex(pattern, options string) Val
- func String(str string) Val
- func Symbol(symbol string) Val
- func Time(t time.Time) Val
- func Timestamp(t, i uint32) Val
- func Undefined() Val
- func (v Val) Array() Arr
- func (v Val) ArrayOK() (Arr, bool)
- func (v Val) Binary() (byte, []byte)
- func (v Val) BinaryOK() (byte, []byte, bool)
- func (v Val) Boolean() bool
- func (v Val) BooleanOK() (bool, bool)
- func (v Val) CodeWithScope() (string, Doc)
- func (v Val) CodeWithScopeOK() (string, Doc, bool)
- func (v Val) DBPointer() (string, primitive.ObjectID)
- func (v Val) DBPointerOK() (string, primitive.ObjectID, bool)
- func (v Val) DateTime() int64
- func (v Val) DateTimeOK() (int64, bool)
- func (v Val) Decimal128() primitive.Decimal128
- func (v Val) Decimal128OK() (primitive.Decimal128, bool)
- func (v Val) Document() Doc
- func (v Val) DocumentOK() (Doc, bool)
- func (v Val) Double() float64
- func (v Val) DoubleOK() (float64, bool)
- func (v Val) Equal(v2 Val) bool
- func (v Val) Int32() int32
- func (v Val) Int32OK() (int32, bool)
- func (v Val) Int64() int64
- func (v Val) Int64OK() (int64, bool)
- func (v Val) Interface() interface{}
- func (v Val) IsNumber() bool
- func (v Val) IsZero() bool
- func (v Val) JavaScript() string
- func (v Val) JavaScriptOK() (string, bool)
- func (v Val) MDocument() MDoc
- func (v Val) MDocumentOK() (MDoc, bool)
- func (v Val) MarshalBSONValue() (bsontype.Type, []byte, error)
- func (v Val) MaxKey()
- func (v Val) MaxKeyOK() bool
- func (v Val) MinKey()
- func (v Val) MinKeyOK() bool
- func (v Val) Null()
- func (v Val) NullOK() bool
- func (v Val) ObjectID() primitive.ObjectID
- func (v Val) ObjectIDOK() (primitive.ObjectID, bool)
- func (v Val) Regex() (pattern, options string)
- func (v Val) RegexOK() (pattern, options string, ok bool)
- func (v Val) String() string
- func (v Val) StringValue() string
- func (v Val) StringValueOK() (string, bool)
- func (v Val) Symbol() string
- func (v Val) SymbolOK() (string, bool)
- func (v Val) Time() time.Time
- func (v Val) TimeOK() (time.Time, bool)
- func (v Val) Timestamp() (t, i uint32)
- func (v Val) TimestampOK() (t uint32, i uint32, ok bool)
- func (v Val) Type() bsontype.Type
- func (v Val) Undefined()
- func (v Val) UndefinedOK() bool
- func (v *Val) UnmarshalBSONValue(t bsontype.Type, data []byte) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNilArray = errors.New("array is nil")
ErrNilArray indicates that an operation was attempted on a nil *Array.
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) MarshalBSONValue ¶
MarshalBSONValue implements the bsoncodec.ValueMarshaler interface.
type Doc ¶
type Doc []Elem
Doc is a type safe, concise BSON document representation.
func ReadDoc ¶
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 ¶
Append adds an element to the end of the document, creating it from the key and value provided.
func (Doc) AppendMarshalBSON ¶
AppendMarshalBSON marshals Doc to BSON bytes, appending to dst.
This method will never return an error.
func (Doc) Lookup ¶
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 ¶
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 ¶
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 ¶
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 ¶
MarshalBSON implements the Marshaler interface.
This method will never return an error.
func (Doc) MarshalBSONValue ¶
MarshalBSONValue implements the bsoncodec.ValueMarshaler interface.
This method will never return an error.
func (Doc) Prepend ¶
Prepend adds an element to the beginning of the document, creating it from the key and value provided.
func (Doc) Set ¶
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) UnmarshalBSON ¶
UnmarshalBSON implements the Unmarshaler interface.
type Elem ¶
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.
type ElementTypeError ¶
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 ¶
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 ¶
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 ¶
AppendMarshalBSON marshals Doc to BSON bytes, appending to dst.
This method will never return an error.
func (MDoc) Lookup ¶
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 ¶
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 ¶
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 ¶
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 ¶
MarshalBSON implements the Marshaler interface.
This method will never return an error.
func (MDoc) MarshalBSONValue ¶
MarshalBSONValue implements the bsoncodec.ValueMarshaler interface.
This method will never return an error.
func (*MDoc) UnmarshalBSON ¶
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, val reflect.Value) error
ArrayDecodeValue is the ValueDecoderFunc for *Array.
func (PrimitiveCodecs) ArrayEncodeValue ¶
func (pc PrimitiveCodecs) ArrayEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) 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, val reflect.Value) error
DocumentDecodeValue is the ValueDecoderFunc for *Document.
func (PrimitiveCodecs) DocumentEncodeValue ¶
func (pc PrimitiveCodecs) DocumentEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
DocumentEncodeValue is the ValueEncoderFunc for *Document.
func (PrimitiveCodecs) ElementSliceDecodeValue ¶
func (pc PrimitiveCodecs) ElementSliceDecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
ElementSliceDecodeValue is the ValueDecoderFunc for []*Element.
func (PrimitiveCodecs) ElementSliceEncodeValue ¶
func (pc PrimitiveCodecs) ElementSliceEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
ElementSliceEncodeValue is the ValueEncoderFunc for []*Element.
func (PrimitiveCodecs) RegisterPrimitiveCodecs ¶ added in v0.1.0
func (pc PrimitiveCodecs) RegisterPrimitiveCodecs(rb *bsoncodec.RegistryBuilder)
RegisterPrimitiveCodecs will register the encode and decode methods attached to PrimitiveCodecs with the provided RegistryBuilder. if rb is nil, a new empty RegistryBuilder will be created.
func (PrimitiveCodecs) ValueDecodeValue ¶
func (pc PrimitiveCodecs) ValueDecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
ValueDecodeValue is the ValueDecoderFunc for *Value.
func (PrimitiveCodecs) ValueEncodeValue ¶
func (pc PrimitiveCodecs) ValueEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
ValueEncodeValue is the ValueEncoderFunc for *Value.
type Val ¶
type Val struct {
// contains filtered or unexported fields
}
Val represents a BSON value.
func Array ¶
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 CodeWithScope ¶
CodeWithScope constructs a BSON code with scope Value.
func Decimal128 ¶
func Decimal128(d128 primitive.Decimal128) Val
Decimal128 constructs a BSON decimal128 Value.
func Document ¶
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 (Val) Array ¶
Array returns the BSON array value the Value represents. It panics if the value is a BSON type other than array.
func (Val) ArrayOK ¶
ArrayOK is the same as Array, except it returns a boolean instead of panicking.
func (Val) Binary ¶
Binary returns the BSON binary value the Value represents. It panics if the value is a BSON type other than binary.
func (Val) BinaryOK ¶
BinaryOK is the same as Binary, except it returns a boolean instead of panicking.
func (Val) Boolean ¶
Boolean returns the BSON boolean the Value represents. It panics if the value is a BSON type other than boolean.
func (Val) BooleanOK ¶
BooleanOK is the same as Boolean, except it returns a boolean instead of panicking.
func (Val) CodeWithScope ¶
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 ¶
CodeWithScopeOK is the same as JavascriptWithScope, except that it returns a boolean instead of panicking.
func (Val) DBPointer ¶
DBPointer returns the BSON dbpointer the Value represents. It panics if the value is a BSON type other than dbpointer.
func (Val) DBPointerOK ¶
DBPointerOK is the same as DBPoitner, except that it returns a boolean instead of panicking.
func (Val) DateTime ¶
DateTime returns the BSON datetime the Value represents. It panics if the value is a BSON type other than datetime.
func (Val) DateTimeOK ¶
DateTimeOK is the same as DateTime, except it returns a boolean instead of panicking.
func (Val) Decimal128 ¶
func (v Val) Decimal128() primitive.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() (primitive.Decimal128, bool)
Decimal128OK is the same as Decimal128, except that it returns a boolean instead of panicking.
func (Val) Document ¶
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 ¶
DocumentOK is the same as Document, except it returns a boolean instead of panicking.
func (Val) Double ¶
Double returns the BSON double value the Value represents. It panics if the value is a BSON type other than double.
func (Val) Equal ¶
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 ¶
Int32 returns the BSON int32 the Value represents. It panics if the value is a BSON type other than int32.
func (Val) Int32OK ¶
Int32OK is the same as Int32, except that it returns a boolean instead of panicking.
func (Val) Int64 ¶
Int64 returns the BSON int64 the Value represents. It panics if the value is a BSON type other than int64.
func (Val) Int64OK ¶
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) JavaScript ¶
JavaScript returns the BSON JavaScript the Value represents. It panics if the value is a BSON type other than JavaScript.
func (Val) JavaScriptOK ¶
JavaScriptOK is the same as Javascript, except that it returns a boolean instead of panicking.
func (Val) MDocument ¶
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 ¶
MDocumentOK is the same as Document, except it returns a boolean instead of panicking.
func (Val) MarshalBSONValue ¶
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 ¶
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 ¶
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) ObjectID ¶
ObjectID returns the BSON ObjectID the Value represents. It panics if the value is a BSON type other than ObjectID.
func (Val) ObjectIDOK ¶
ObjectIDOK is the same as ObjectID, except it returns a boolean instead of panicking.
func (Val) Regex ¶
Regex returns the BSON regex the Value represents. It panics if the value is a BSON type other than regex.
func (Val) RegexOK ¶
RegexOK is the same as Regex, except that it returns a boolean instead of panicking.
func (Val) StringValue ¶
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 ¶
StringValueOK is the same as StringValue, but returns a boolean instead of panicking.
func (Val) Symbol ¶
Symbol returns the BSON symbol the Value represents. It panics if the value is a BSON type other than symbol.
func (Val) SymbolOK ¶
SymbolOK is the same as Javascript, except that it returns a boolean instead of panicking.
func (Val) 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) Timestamp ¶
Timestamp returns the BSON timestamp the Value represents. It panics if the value is a BSON type other than timestamp.
func (Val) TimestampOK ¶
TimestampOK is the same as Timestamp, except that it returns a boolean instead of panicking.
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 ¶
UndefinedOK is the same as Undefined, except it returns a boolean instead of panicking.