builder

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2018 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
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.

AC is a convenience variable provided for access to the ArrayConstructor methods.

func (ArrayConstructor) Array

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

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

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

DBPointer creates a dbpointer element with the given value.

func (ArrayConstructor) DateTime

DateTime creates a datetime element with the given value.

func (ArrayConstructor) Decimal

Decimal creates a decimal element with the given value.

func (ArrayConstructor) Double

Double creates a double element with the given value.

func (ArrayConstructor) Int32

Int32 creates a int32 element with the given value.

func (ArrayConstructor) Int64

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

MaxKey creates a maxkey element with the given value.

func (ArrayConstructor) MinKey

MinKey creates a minkey element with the given value.

func (ArrayConstructor) Null

Null creates a null element with the given value.

func (ArrayConstructor) ObjectID

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

String creates a string element with the given value.

func (ArrayConstructor) SubDocument

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

Timestamp creates a timestamp element with the given value.

func (ArrayConstructor) Undefined

func (ArrayConstructor) Undefined() ArrayElementFunc

Undefined creates a undefined element.

type ArrayElementFunc

type ArrayElementFunc func(pos uint) Elementer

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

type ArrayElementer interface {
	ArrayElement(pos uint) Elementer
}

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

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

type ElementWriter func(start uint, writer []byte) (n int, err error)

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.

Jump to

Keyboard shortcuts

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