Documentation ¶
Index ¶
- Variables
- type ArrayBuilder
- type ArrayConstructor
- func (ArrayConstructor) Array(arr *ArrayBuilder) ArrayElementFunc
- func (ArrayConstructor) ArrayWithElements(elems ...ArrayElementer) ArrayElementFunc
- func (ArrayConstructor) Binary(b []byte) ArrayElementFunc
- func (ArrayConstructor) BinaryWithSubtype(b []byte, btype byte) ArrayElementFunc
- func (ArrayConstructor) Boolean(b bool) ArrayElementFunc
- func (ArrayConstructor) CodeWithScope(code string, scope []byte) ArrayElementFunc
- func (ArrayConstructor) DBPointer(ns string, oid objectid.ObjectID) ArrayElementFunc
- func (ArrayConstructor) DateTime(dt int64) ArrayElementFunc
- func (ArrayConstructor) Decimal(d decimal.Decimal128) ArrayElementFunc
- func (ArrayConstructor) Double(f float64) ArrayElementFunc
- func (ArrayConstructor) Int32(i int32) ArrayElementFunc
- func (ArrayConstructor) Int64(i int64) ArrayElementFunc
- func (ArrayConstructor) JavaScriptCode(code string) ArrayElementFunc
- func (ArrayConstructor) MaxKey() ArrayElementFunc
- func (ArrayConstructor) MinKey() ArrayElementFunc
- func (ArrayConstructor) Null() ArrayElementFunc
- func (ArrayConstructor) ObjectID(oid objectid.ObjectID) ArrayElementFunc
- func (ArrayConstructor) Regex(pattern string, options string) ArrayElementFunc
- func (ArrayConstructor) String(s string) ArrayElementFunc
- func (ArrayConstructor) SubDocument(db *DocumentBuilder) ArrayElementFunc
- func (ArrayConstructor) SubDocumentWithElements(elems ...Elementer) ArrayElementFunc
- func (ArrayConstructor) Symbol(symbol string) ArrayElementFunc
- func (ArrayConstructor) Timestamp(t uint32, i uint32) ArrayElementFunc
- func (ArrayConstructor) Undefined() ArrayElementFunc
- type ArrayElementFunc
- type ArrayElementer
- type Constructor
- func (c Constructor) Array(key string, array *ArrayBuilder) ElementFunc
- func (c Constructor) ArrayWithElements(key string, elems ...ArrayElementer) ElementFunc
- func (c Constructor) Binary(key string, b []byte) ElementFunc
- func (Constructor) BinaryWithSubtype(key string, b []byte, btype byte) ElementFunc
- func (Constructor) Boolean(key string, b bool) ElementFunc
- func (Constructor) CodeWithScope(key string, code string, scope []byte) ElementFunc
- func (Constructor) DBPointer(key string, ns string, oid objectid.ObjectID) ElementFunc
- func (Constructor) DateTime(key string, dt int64) ElementFunc
- func (Constructor) Decimal(key string, d decimal.Decimal128) ElementFunc
- func (Constructor) Double(key string, f float64) ElementFunc
- func (Constructor) Int32(key string, i int32) ElementFunc
- func (Constructor) Int64(key string, i int64) ElementFunc
- func (Constructor) JavaScriptCode(key string, code string) ElementFunc
- func (Constructor) MaxKey(key string) ElementFunc
- func (Constructor) MinKey(key string) ElementFunc
- func (Constructor) Null(key string) ElementFunc
- func (Constructor) ObjectID(key string, oid objectid.ObjectID) ElementFunc
- func (Constructor) Regex(key string, pattern, options string) ElementFunc
- func (Constructor) String(key string, value string) ElementFunc
- func (Constructor) SubDocument(key string, subdoc *DocumentBuilder) Elementer
- func (c Constructor) SubDocumentWithElements(key string, elems ...Elementer) Elementer
- func (Constructor) Symbol(key string, symbol string) ElementFunc
- func (Constructor) Timestamp(key string, t uint32, i uint32) ElementFunc
- func (Constructor) Undefined(key string) ElementFunc
- type DocumentBuilder
- type ElementFunc
- type ElementSizer
- type ElementWriter
- type Elementer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrTooShort = errors.New("builder: The provided slice's length is too short")
ErrTooShort indicates that the slice provided to encode into is not large enough to fit the data.
Functions ¶
This section is empty.
Types ¶
type ArrayBuilder ¶
type ArrayBuilder struct { DocumentBuilder // contains filtered or unexported fields }
ArrayBuilder allows the creation of a BSON document by appending elements and then writing the document. The array can be written multiple times so appending then writing and then appending and writing again is a valid usage pattern.
func (*ArrayBuilder) Append ¶
func (ab *ArrayBuilder) Append(elems ...ArrayElementer) *ArrayBuilder
Append adds the given elements to the BSON array.
type ArrayConstructor ¶
type ArrayConstructor struct{}
ArrayConstructor is used as a namespace for array element constructor functions.
var AC ArrayConstructor
AC is a convenience variable provided for access to the ArrayConstructor methods.
func (ArrayConstructor) Array ¶
func (ArrayConstructor) Array(arr *ArrayBuilder) ArrayElementFunc
Array creates an array element with the given value.
func (ArrayConstructor) ArrayWithElements ¶
func (ArrayConstructor) ArrayWithElements(elems ...ArrayElementer) ArrayElementFunc
ArrayWithElements creates an array element from the given the elements.
func (ArrayConstructor) Binary ¶
func (ArrayConstructor) Binary(b []byte) ArrayElementFunc
Binary creates a binary element with the given value.
func (ArrayConstructor) BinaryWithSubtype ¶
func (ArrayConstructor) BinaryWithSubtype(b []byte, btype byte) ArrayElementFunc
BinaryWithSubtype creates a new binary element with the given data and subtype.
func (ArrayConstructor) Boolean ¶
func (ArrayConstructor) Boolean(b bool) ArrayElementFunc
Boolean creates a boolean element with the given value.
func (ArrayConstructor) CodeWithScope ¶
func (ArrayConstructor) CodeWithScope(code string, scope []byte) ArrayElementFunc
CodeWithScope creates a JavaScript code with scope element with the given value.
func (ArrayConstructor) DBPointer ¶
func (ArrayConstructor) DBPointer(ns string, oid objectid.ObjectID) ArrayElementFunc
DBPointer creates a dbpointer element with the given value.
func (ArrayConstructor) DateTime ¶
func (ArrayConstructor) DateTime(dt int64) ArrayElementFunc
DateTime creates a datetime element with the given value.
func (ArrayConstructor) Decimal ¶
func (ArrayConstructor) Decimal(d decimal.Decimal128) ArrayElementFunc
Decimal creates a decimal element with the given value.
func (ArrayConstructor) Double ¶
func (ArrayConstructor) Double(f float64) ArrayElementFunc
Double creates a double element with the given value.
func (ArrayConstructor) Int32 ¶
func (ArrayConstructor) Int32(i int32) ArrayElementFunc
Int32 creates a int32 element with the given value.
func (ArrayConstructor) Int64 ¶
func (ArrayConstructor) Int64(i int64) ArrayElementFunc
Int64 creates a int64 element with the given value.
func (ArrayConstructor) JavaScriptCode ¶
func (ArrayConstructor) JavaScriptCode(code string) ArrayElementFunc
JavaScriptCode creates a JavaScript code element with the given value.
func (ArrayConstructor) MaxKey ¶
func (ArrayConstructor) MaxKey() ArrayElementFunc
MaxKey creates a maxkey element with the given value.
func (ArrayConstructor) MinKey ¶
func (ArrayConstructor) MinKey() ArrayElementFunc
MinKey creates a minkey element with the given value.
func (ArrayConstructor) Null ¶
func (ArrayConstructor) Null() ArrayElementFunc
Null creates a null element with the given value.
func (ArrayConstructor) ObjectID ¶
func (ArrayConstructor) ObjectID(oid objectid.ObjectID) ArrayElementFunc
ObjectID creates a objectid element with the given value.
func (ArrayConstructor) Regex ¶
func (ArrayConstructor) Regex(pattern string, options string) ArrayElementFunc
Regex creates a regex element with the given value.
func (ArrayConstructor) String ¶
func (ArrayConstructor) String(s string) ArrayElementFunc
String creates a string element with the given value.
func (ArrayConstructor) SubDocument ¶
func (ArrayConstructor) SubDocument(db *DocumentBuilder) ArrayElementFunc
SubDocument creates a subdocument element with the given value.
func (ArrayConstructor) SubDocumentWithElements ¶
func (ArrayConstructor) SubDocumentWithElements(elems ...Elementer) ArrayElementFunc
SubDocumentWithElements creates a subdocument element from the given elements.
func (ArrayConstructor) Symbol ¶
func (ArrayConstructor) Symbol(symbol string) ArrayElementFunc
Symbol creates a symbol element with the given value.
func (ArrayConstructor) Timestamp ¶
func (ArrayConstructor) Timestamp(t uint32, i uint32) ArrayElementFunc
Timestamp creates a timestamp element with the given value.
func (ArrayConstructor) Undefined ¶
func (ArrayConstructor) Undefined() ArrayElementFunc
Undefined creates a undefined element.
type ArrayElementFunc ¶
ArrayElementFunc is a function type used to insert BSON element values into an array using an ArrayBuilder.
func (ArrayElementFunc) ArrayElement ¶
func (aef ArrayElementFunc) ArrayElement(pos uint) Elementer
ArrayElement implements the ArrayElementer interface.
type ArrayElementer ¶
ArrayElementer is the interface implemented by types that can serialize themselves into a BSON array element.
type Constructor ¶
type Constructor struct{}
Constructor is used as a namespace for document element constructor functions.
var C Constructor
C is a convenience variable provided for access to the Constructor methods.
func (Constructor) Array ¶
func (c Constructor) Array(key string, array *ArrayBuilder) ElementFunc
Array creates an array element with the given key and value.
func (Constructor) ArrayWithElements ¶
func (c Constructor) ArrayWithElements(key string, elems ...ArrayElementer) ElementFunc
ArrayWithElements creates an element with the given key. The elements passed as arguments will be used to create a new array as the value.
func (Constructor) Binary ¶
func (c Constructor) Binary(key string, b []byte) ElementFunc
Binary creates a binary element with the given key and value.
func (Constructor) BinaryWithSubtype ¶
func (Constructor) BinaryWithSubtype(key string, b []byte, btype byte) ElementFunc
BinaryWithSubtype creates a binary element with the given key. It will create a new BSON binary value with the given data and subtype.
func (Constructor) Boolean ¶
func (Constructor) Boolean(key string, b bool) ElementFunc
Boolean creates a boolean element with the given key and value.
func (Constructor) CodeWithScope ¶
func (Constructor) CodeWithScope(key string, code string, scope []byte) ElementFunc
CodeWithScope creates a JavaScript code with scope element with the given key and value.
func (Constructor) DBPointer ¶
func (Constructor) DBPointer(key string, ns string, oid objectid.ObjectID) ElementFunc
DBPointer creates a dbpointer element with the given key and value.
func (Constructor) DateTime ¶
func (Constructor) DateTime(key string, dt int64) ElementFunc
DateTime creates a datetime element with the given key and value.
func (Constructor) Decimal ¶
func (Constructor) Decimal(key string, d decimal.Decimal128) ElementFunc
Decimal creates a decimal element with the given key and value.
func (Constructor) Double ¶
func (Constructor) Double(key string, f float64) ElementFunc
Double creates a double element with the given key and value.
func (Constructor) Int32 ¶
func (Constructor) Int32(key string, i int32) ElementFunc
Int32 creates a int32 element with the given key and value.
func (Constructor) Int64 ¶
func (Constructor) Int64(key string, i int64) ElementFunc
Int64 creates a int64 element with the given key and value.
func (Constructor) JavaScriptCode ¶
func (Constructor) JavaScriptCode(key string, code string) ElementFunc
JavaScriptCode creates a JavaScript code element with the given key and value.
func (Constructor) MaxKey ¶
func (Constructor) MaxKey(key string) ElementFunc
MaxKey creates a maxkey element with the given key and value.
func (Constructor) MinKey ¶
func (Constructor) MinKey(key string) ElementFunc
MinKey creates a minkey element with the given key and value.
func (Constructor) Null ¶
func (Constructor) Null(key string) ElementFunc
Null creates a null element with the given key.
func (Constructor) ObjectID ¶
func (Constructor) ObjectID(key string, oid objectid.ObjectID) ElementFunc
ObjectID creates a objectid element with the given key and value.
func (Constructor) Regex ¶
func (Constructor) Regex(key string, pattern, options string) ElementFunc
Regex creates a regex element with the given key and value.
func (Constructor) String ¶
func (Constructor) String(key string, value string) ElementFunc
String creates a string element with the given key and value.
func (Constructor) SubDocument ¶
func (Constructor) SubDocument(key string, subdoc *DocumentBuilder) Elementer
SubDocument creates a subdocument element with the given key and value.
func (Constructor) SubDocumentWithElements ¶
func (c Constructor) SubDocumentWithElements(key string, elems ...Elementer) Elementer
SubDocumentWithElements creates a subdocument element with the given key. The elements passed as arguments will be used to create a new document as the value.
func (Constructor) Symbol ¶
func (Constructor) Symbol(key string, symbol string) ElementFunc
Symbol creates a symbol element with the given key and value.
func (Constructor) Timestamp ¶
func (Constructor) Timestamp(key string, t uint32, i uint32) ElementFunc
Timestamp creates a timestamp element with the given key and value.
func (Constructor) Undefined ¶
func (Constructor) Undefined(key string) ElementFunc
Undefined creates a undefined element with the given key.
type DocumentBuilder ¶
type DocumentBuilder struct { Key string // contains filtered or unexported fields }
DocumentBuilder allows the creation of a BSON document by appending elements and then writing the document. The document can be written multiple times so appending then writing and then appending and writing again is a valid usage pattern.
Example ¶
internalVersion := "1234567" f := func(appName string) *DocumentBuilder { builder := new(DocumentBuilder) builder.init() builder.Append( C.SubDocumentWithElements("driver", C.String("name", "mongo-go-driver"), C.String("version", internalVersion), ), C.SubDocumentWithElements("os", C.String("type", "darwin"), C.String("architecture", "amd64"), ), C.String("platform", "go1.9.2"), ) if appName != "" { builder.Append(C.SubDocumentWithElements("application", C.String("name", appName))) } return builder } db := f("hello-world") buf := make([]byte, db.RequiredBytes()) _, err := f("hello-world").WriteDocument(buf) if err != nil { fmt.Println(err) } fmt.Println(buf)
Output: [177 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 8 0 0 0 103 111 49 46 57 46 50 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 NewDocumentBuilder ¶
func NewDocumentBuilder() *DocumentBuilder
NewDocumentBuilder constructs a new DocumentBuilder.
func (*DocumentBuilder) Append ¶
func (db *DocumentBuilder) Append(elems ...Elementer) *DocumentBuilder
Append adds the given elements to the BSON document.
func (*DocumentBuilder) Element ¶
func (db *DocumentBuilder) Element() (ElementSizer, ElementWriter)
Element implements the Elementer interface.
func (*DocumentBuilder) RequiredBytes ¶
func (db *DocumentBuilder) RequiredBytes() uint
RequiredBytes returns the number of bytes required to write the entire BSON document.
func (*DocumentBuilder) WriteDocument ¶
func (db *DocumentBuilder) WriteDocument(writer []byte) (int64, error)
WriteDocument writes out the document as BSON to the byte slice.
type ElementFunc ¶
type ElementFunc func() (ElementSizer, ElementWriter)
ElementFunc is a function type used to insert BSON element values into a document using a DocumentBuilder.
func (ElementFunc) Element ¶
func (ef ElementFunc) Element() (ElementSizer, ElementWriter)
Element implements the Elementer interface.
type ElementSizer ¶
type ElementSizer func() (size uint)
ElementSizer handles retrieving the size of an element's BSON representation.
type ElementWriter ¶
ElementWriter handles writing an element's BSON representation to a writer.
type Elementer ¶
type Elementer interface {
Element() (ElementSizer, ElementWriter)
}
Elementer is the interface implemented by types that can serialize themselves into a BSON element.