bsoncodec

package
v0.0.16 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrEOA = errors.New("end of array")

ErrEOA is the error returned when the end of a BSON array has been reached.

View Source
var ErrEOD = errors.New("end of document")

ErrEOD is the error returned when the end of a BSON document has been reached.

View Source
var ErrInvalidJSON = errors.New("invalid JSON input")

ErrInvalidJSON indicates the JSON input is invalid

View Source
var ErrNilType = errors.New("cannot perform an encoder or decoder lookup on <nil>")

ErrNilType is returned when nil is passed to either LookupEncoder or LookupDecoder.

View Source
var ErrNotInterface = errors.New("The provided type is not an interface")

ErrNotInterface is returned when the provided type is not an interface.

Functions

func ConstructElement

func ConstructElement(key string, value interface{}) *bson.Element

ConstructElement will attempt to turn the provided key and value into an Element. For common types, type casting is used, if the type is more complex, such as a map or struct, reflection is used. If the value cannot be converted either by typecasting or through reflection, a null Element is constructed with the key. This method will never return a nil *Element. If an error turning the value into an Element is desired, use the InterfaceErr method.

func ConstructElementErr

func ConstructElementErr(key string, value interface{}) (*bson.Element, error)

ConstructElementErr does the same thing as ConstructElement but returns an error instead of returning a BSON Null element.

func CopyDocument

func CopyDocument(dst ValueWriter, src ValueReader) error

CopyDocument handles copying a document from src to dst.

func Marshal

func Marshal(val interface{}) ([]byte, error)

Marshal returns the BSON encoding of val.

Marshal will use the default registry created by NewRegistry to recursively marshal val into a []byte. Marshal will inspect struct tags and alter the marshaling process accordingly.

func MarshalAppend

func MarshalAppend(dst []byte, val interface{}) ([]byte, error)

MarshalAppend will append the BSON encoding of val to dst. If dst is not large enough to hold the BSON encoding of val, dst will be grown.

func MarshalAppendWithRegistry

func MarshalAppendWithRegistry(r *Registry, dst []byte, val interface{}) ([]byte, error)

MarshalAppendWithRegistry will append the BSON encoding of val to dst using Registry r. If dst is not large enough to hold the BSON encoding of val, dst will be grown.

func MarshalExtJSON added in v0.0.16

func MarshalExtJSON(val interface{}, canonical, escapeHTML bool) ([]byte, error)

MarshalExtJSON returns the extended JSON encoding of val.

func MarshalExtJSONAppend added in v0.0.16

func MarshalExtJSONAppend(dst []byte, val interface{}, canonical, escapeHTML bool) ([]byte, error)

MarshalExtJSONAppend will append the extended JSON encoding of val to dst. If dst is not large enough to hold the extended JSON encoding of val, dst will be grown.

func MarshalExtJSONAppendWithRegistry added in v0.0.16

func MarshalExtJSONAppendWithRegistry(r *Registry, dst []byte, val interface{}, canonical, escapeHTML bool) ([]byte, error)

MarshalExtJSONAppendWithRegistry will append the extended JSON encoding of val to dst using Registry r. If dst is not large enough to hold the BSON encoding of val, dst will be grown.

func MarshalExtJSONWithRegistry added in v0.0.16

func MarshalExtJSONWithRegistry(r *Registry, val interface{}, canonical, escapeHTML bool) ([]byte, error)

MarshalExtJSONWithRegistry returns the extended JSON encoding of val using Registry r.

func MarshalWithRegistry

func MarshalWithRegistry(r *Registry, val interface{}) ([]byte, error)

MarshalWithRegistry returns the BSON encoding of val using Registry r.

func Unmarshal

func Unmarshal(data []byte, val interface{}) error

Unmarshal parses the BSON-encoded data and stores the result in the value pointed to by val. If val is nil or not a pointer, Unmarshal returns InvalidUnmarshalError.

func UnmarshalExtJSON added in v0.0.16

func UnmarshalExtJSON(data []byte, canonical bool, val interface{}) error

UnmarshalExtJSON parses the extended JSON-encoded data and stores the result in the value pointed to by val. If val is nil or not a pointer, Unmarshal returns InvalidUnmarshalError.

func UnmarshalExtJSONWithRegistry added in v0.0.16

func UnmarshalExtJSONWithRegistry(r *Registry, data []byte, canonical bool, val interface{}) error

UnmarshalExtJSONWithRegistry parses the extended JSON-encoded data using Registry r and stores the result in the value pointed to by val. If val is nil or not a pointer, UnmarshalWithRegistry returns InvalidUnmarshalError.

func UnmarshalWithRegistry

func UnmarshalWithRegistry(r *Registry, data []byte, val interface{}) error

UnmarshalWithRegistry parses the BSON-encoded data using Registry r and stores the result in the value pointed to by val. If val is nil or not a pointer, UnmarshalWithRegistry returns InvalidUnmarshalError.

Types

type ArrayReader

type ArrayReader interface {
	ReadValue() (ValueReader, error)
}

ArrayReader is implemented by types that allow reading values from a BSON array.

type ArrayWriter

type ArrayWriter interface {
	WriteArrayElement() (ValueWriter, error)
	WriteArrayEnd() error
}

ArrayWriter is the interface used to create a BSON or BSON adjacent array. Callers must ensure they call WriteArrayEnd when they have finished creating the array.

type BytesReader

type BytesReader interface {
	ReadValueBytes(dst []byte) (bson.Type, []byte, error)
}

BytesReader is a generic interface used to read BSON bytes from a ValueReader. This imterface is meant to be a superset of ValueReader, so that types that implement ValueReader may also implement this interface.

The bytes of the value will be appended to dst.

type BytesWriter

type BytesWriter interface {
	WriteValueBytes(t bson.Type, b []byte) error
}

BytesWriter is the interface used to write BSON bytes to a ValueWriter. This interface is meant to be a superset of ValueWriter, so that types that implement ValueWriter may also implement this interface.

type CodecZeroer

type CodecZeroer interface {
	IsTypeZero(interface{}) bool
}

CodecZeroer is the interface implemented by Codecs that can also determine if a value of the type that would be encoded is zero.

type Copier

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

Copier is a type that allows copying between ValueReaders, ValueWriters, and []byte values.

func NewCopier

func NewCopier(r *Registry) Copier

NewCopier creates a new copier with the given registry. If a nil registry is provided a default registry is used.

func (Copier) AppendDocumentBytes

func (c Copier) AppendDocumentBytes(dst []byte, src ValueReader) ([]byte, error)

AppendDocumentBytes functions the same as CopyDocumentToBytes, but will append the result to dst.

func (Copier) AppendValueBytes

func (c Copier) AppendValueBytes(dst []byte, src ValueReader) (bson.Type, []byte, error)

AppendValueBytes functions the same as CopyValueToBytes, but will append the result to dst.

func (Copier) CopyDocument

func (c Copier) CopyDocument(dst ValueWriter, src ValueReader) error

CopyDocument handles copying one document from the src to the dst.

func (Copier) CopyDocumentFromBytes

func (c Copier) CopyDocumentFromBytes(dst ValueWriter, src []byte) error

CopyDocumentFromBytes copies the values from a BSON document represented as a []byte to a ValueWriter.

func (Copier) CopyDocumentToBytes

func (c Copier) CopyDocumentToBytes(src ValueReader) ([]byte, error)

CopyDocumentToBytes copies an entire document from the ValueReader and returns it as bytes.

func (Copier) CopyValue

func (c Copier) CopyValue(dst ValueWriter, src ValueReader) error

CopyValue will copy a single value from src to dst.

func (Copier) CopyValueFromBytes

func (c Copier) CopyValueFromBytes(dst ValueWriter, t bson.Type, src []byte) error

CopyValueFromBytes will write the value represtend by t and src to dst.

func (Copier) CopyValueToBytes

func (c Copier) CopyValueToBytes(src ValueReader) (bson.Type, []byte, error)

CopyValueToBytes copies a value from src and returns it as a bson.Type and a []byte.

type DecodeContext

type DecodeContext struct {
	*Registry
	Truncate bool
}

DecodeContext is the contextual information required for a Codec to decode a value.

type Decoder

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

A Decoder reads and decodes BSON documents from a stream.

func NewDecoder

func NewDecoder(r *Registry, vr ValueReader) (*Decoder, error)

NewDecoder returns a new decoder that uses Registry reg to read from r.

func (*Decoder) Decode

func (d *Decoder) Decode(val interface{}) error

Decode reads the next BSON document from the stream and decodes it into the value pointed to by val.

The documentation for Unmarshal contains details about of BSON into a Go value.

func (*Decoder) Reset

func (d *Decoder) Reset(vr ValueReader) error

Reset will reset the state of the decoder, using the same *Registry used in the original construction but using r for reading.

func (*Decoder) SetRegistry

func (d *Decoder) SetRegistry(r *Registry) error

SetRegistry replaces the current registry of the decoder with r.

type DefaultValueDecoders

type DefaultValueDecoders struct{}

DefaultValueDecoders is a namespace type for the default ValueDecoders used when creating a registry.

func (DefaultValueDecoders) ArrayDecodeValue

func (dvd DefaultValueDecoders) ArrayDecodeValue(dc DecodeContext, vr ValueReader, i interface{}) error

ArrayDecodeValue is the ValueDecoderFunc for *bson.Array.

func (DefaultValueDecoders) BinaryDecodeValue

func (dvd DefaultValueDecoders) BinaryDecodeValue(dc DecodeContext, vr ValueReader, i interface{}) error

BinaryDecodeValue is the ValueDecoderFunc for bson.Binary.

func (DefaultValueDecoders) BooleanDecodeValue

func (dvd DefaultValueDecoders) BooleanDecodeValue(dctx DecodeContext, vr ValueReader, i interface{}) error

BooleanDecodeValue is the ValueDecoderFunc for bool types.

func (DefaultValueDecoders) ByteSliceDecodeValue

func (dvd DefaultValueDecoders) ByteSliceDecodeValue(dc DecodeContext, vr ValueReader, i interface{}) error

ByteSliceDecodeValue is the ValueDecoderFunc for []byte.

func (DefaultValueDecoders) CodeWithScopeDecodeValue

func (dvd DefaultValueDecoders) CodeWithScopeDecodeValue(dc DecodeContext, vr ValueReader, i interface{}) error

CodeWithScopeDecodeValue is the ValueDecoderFunc for bson.CodeWithScope.

func (DefaultValueDecoders) DBPointerDecodeValue

func (dvd DefaultValueDecoders) DBPointerDecodeValue(dc DecodeContext, vr ValueReader, i interface{}) error

DBPointerDecodeValue is the ValueDecoderFunc for bson.DBPointer.

func (DefaultValueDecoders) DateTimeDecodeValue

func (dvd DefaultValueDecoders) DateTimeDecodeValue(dc DecodeContext, vr ValueReader, i interface{}) error

DateTimeDecodeValue is the ValueDecoderFunc for bson.DateTime.

func (DefaultValueDecoders) Decimal128DecodeValue

func (dvd DefaultValueDecoders) Decimal128DecodeValue(dctx DecodeContext, vr ValueReader, i interface{}) error

Decimal128DecodeValue is the ValueDecoderFunc for decimal.Decimal128.

func (DefaultValueDecoders) DocumentDecodeValue

func (dvd DefaultValueDecoders) DocumentDecodeValue(dctx DecodeContext, vr ValueReader, i interface{}) error

DocumentDecodeValue is the ValueDecoderFunc for *bson.Document.

func (DefaultValueDecoders) ElementSliceDecodeValue

func (dvd DefaultValueDecoders) ElementSliceDecodeValue(dc DecodeContext, vr ValueReader, i interface{}) error

ElementSliceDecodeValue is the ValueDecoderFunc for []*bson.Element.

func (DefaultValueDecoders) EmptyInterfaceDecodeValue

func (dvd DefaultValueDecoders) EmptyInterfaceDecodeValue(dc DecodeContext, vr ValueReader, i interface{}) error

EmptyInterfaceDecodeValue is the ValueDecoderFunc for interface{}.

func (DefaultValueDecoders) FloatDecodeValue

func (dvd DefaultValueDecoders) FloatDecodeValue(ec DecodeContext, vr ValueReader, i interface{}) error

FloatDecodeValue is the ValueDecoderFunc for float types.

func (DefaultValueDecoders) IntDecodeValue

func (dvd DefaultValueDecoders) IntDecodeValue(dc DecodeContext, vr ValueReader, i interface{}) error

IntDecodeValue is the ValueDecoderFunc for bool types.

func (DefaultValueDecoders) JSONNumberDecodeValue

func (dvd DefaultValueDecoders) JSONNumberDecodeValue(dc DecodeContext, vr ValueReader, i interface{}) error

JSONNumberDecodeValue is the ValueDecoderFunc for json.Number.

func (DefaultValueDecoders) MapDecodeValue

func (dvd DefaultValueDecoders) MapDecodeValue(dc DecodeContext, vr ValueReader, i interface{}) error

MapDecodeValue is the ValueDecoderFunc for map[string]* types.

func (DefaultValueDecoders) MaxKeyDecodeValue

func (dvd DefaultValueDecoders) MaxKeyDecodeValue(dc DecodeContext, vr ValueReader, i interface{}) error

MaxKeyDecodeValue is the ValueDecoderFunc for bson.MaxKey.

func (DefaultValueDecoders) MinKeyDecodeValue

func (dvd DefaultValueDecoders) MinKeyDecodeValue(dc DecodeContext, vr ValueReader, i interface{}) error

MinKeyDecodeValue is the ValueDecoderFunc for bson.MinKey.

func (DefaultValueDecoders) NullDecodeValue

func (dvd DefaultValueDecoders) NullDecodeValue(dc DecodeContext, vr ValueReader, i interface{}) error

NullDecodeValue is the ValueDecoderFunc for bson.Null.

func (DefaultValueDecoders) ObjectIDDecodeValue

func (dvd DefaultValueDecoders) ObjectIDDecodeValue(dc DecodeContext, vr ValueReader, i interface{}) error

ObjectIDDecodeValue is the ValueDecoderFunc for objectid.ObjectID.

func (DefaultValueDecoders) ReaderDecodeValue

func (dvd DefaultValueDecoders) ReaderDecodeValue(dc DecodeContext, vr ValueReader, i interface{}) error

ReaderDecodeValue is the ValueDecoderFunc for bson.Reader.

func (DefaultValueDecoders) RegexDecodeValue

func (dvd DefaultValueDecoders) RegexDecodeValue(dc DecodeContext, vr ValueReader, i interface{}) error

RegexDecodeValue is the ValueDecoderFunc for bson.Regex.

func (DefaultValueDecoders) SliceDecodeValue

func (dvd DefaultValueDecoders) SliceDecodeValue(dc DecodeContext, vr ValueReader, i interface{}) error

SliceDecodeValue is the ValueDecoderFunc for []* types.

func (DefaultValueDecoders) StringDecodeValue

func (dvd DefaultValueDecoders) StringDecodeValue(dctx DecodeContext, vr ValueReader, i interface{}) error

StringDecodeValue is the ValueDecoderFunc for string types.

func (DefaultValueDecoders) TimeDecodeValue

func (dvd DefaultValueDecoders) TimeDecodeValue(dc DecodeContext, vr ValueReader, i interface{}) error

TimeDecodeValue is the ValueDecoderFunc for time.Time.

func (DefaultValueDecoders) TimestampDecodeValue

func (dvd DefaultValueDecoders) TimestampDecodeValue(dc DecodeContext, vr ValueReader, i interface{}) error

TimestampDecodeValue is the ValueDecoderFunc for bson.Timestamp.

func (DefaultValueDecoders) URLDecodeValue

func (dvd DefaultValueDecoders) URLDecodeValue(dc DecodeContext, vr ValueReader, i interface{}) error

URLDecodeValue is the ValueDecoderFunc for url.URL.

func (DefaultValueDecoders) UintDecodeValue

func (dvd DefaultValueDecoders) UintDecodeValue(dc DecodeContext, vr ValueReader, i interface{}) error

UintDecodeValue is the ValueDecoderFunc for uint types.

func (DefaultValueDecoders) UndefinedDecodeValue

func (dvd DefaultValueDecoders) UndefinedDecodeValue(dc DecodeContext, vr ValueReader, i interface{}) error

UndefinedDecodeValue is the ValueDecoderFunc for bool types.

func (DefaultValueDecoders) ValueDecodeValue

func (dvd DefaultValueDecoders) ValueDecodeValue(dc DecodeContext, vr ValueReader, i interface{}) error

ValueDecodeValue is the ValueDecoderFunc for *bson.Value.

func (DefaultValueDecoders) ValueUnmarshalerDecodeValue

func (dvd DefaultValueDecoders) ValueUnmarshalerDecodeValue(dc DecodeContext, vr ValueReader, i interface{}) error

ValueUnmarshalerDecodeValue is the ValueDecoderFunc for ValueUnmarshaler implementations.

type DefaultValueEncoders

type DefaultValueEncoders struct{}

DefaultValueEncoders is a namespace type for the default ValueEncoders used when creating a registry.

func (DefaultValueEncoders) ArrayEncodeValue

func (dve DefaultValueEncoders) ArrayEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

ArrayEncodeValue is the ValueEncoderFunc for *bson.Array.

func (DefaultValueEncoders) BinaryEncodeValue

func (dve DefaultValueEncoders) BinaryEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

BinaryEncodeValue is the ValueEncoderFunc for bson.Binary.

func (DefaultValueEncoders) BooleanEncodeValue

func (dve DefaultValueEncoders) BooleanEncodeValue(ectx EncodeContext, vw ValueWriter, i interface{}) error

BooleanEncodeValue is the ValueEncoderFunc for bool types.

func (DefaultValueEncoders) ByteSliceEncodeValue

func (dve DefaultValueEncoders) ByteSliceEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

ByteSliceEncodeValue is the ValueEncoderFunc for []byte.

func (DefaultValueEncoders) CodeWithScopeEncodeValue

func (dve DefaultValueEncoders) CodeWithScopeEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

CodeWithScopeEncodeValue is the ValueEncoderFunc for bson.CodeWithScope.

func (DefaultValueEncoders) DBPointerEncodeValue

func (dve DefaultValueEncoders) DBPointerEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

DBPointerEncodeValue is the ValueEncoderFunc for bson.DBPointer.

func (DefaultValueEncoders) DateTimeEncodeValue

func (dve DefaultValueEncoders) DateTimeEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

DateTimeEncodeValue is the ValueEncoderFunc for bson.DateTime.

func (DefaultValueEncoders) Decimal128EncodeValue

func (dve DefaultValueEncoders) Decimal128EncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

Decimal128EncodeValue is the ValueEncoderFunc for decimal.Decimal128.

func (DefaultValueEncoders) DocumentEncodeValue

func (dve DefaultValueEncoders) DocumentEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

DocumentEncodeValue is the ValueEncoderFunc for *bson.Document.

func (DefaultValueEncoders) ElementSliceEncodeValue

func (dve DefaultValueEncoders) ElementSliceEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

ElementSliceEncodeValue is the ValueEncoderFunc for []*bson.Element.

func (DefaultValueEncoders) EmptyInterfaceEncodeValue

func (dve DefaultValueEncoders) EmptyInterfaceEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

EmptyInterfaceEncodeValue is the ValueEncoderFunc for interface{}.

func (DefaultValueEncoders) FloatEncodeValue

func (dve DefaultValueEncoders) FloatEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

FloatEncodeValue is the ValueEncoderFunc for float types.

func (DefaultValueEncoders) IntEncodeValue

func (dve DefaultValueEncoders) IntEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

IntEncodeValue is the ValueEncoderFunc for int types.

func (DefaultValueEncoders) JSONNumberEncodeValue

func (dve DefaultValueEncoders) JSONNumberEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

JSONNumberEncodeValue is the ValueEncoderFunc for json.Number.

func (DefaultValueEncoders) MapEncodeValue

func (dve DefaultValueEncoders) MapEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

MapEncodeValue is the ValueEncoderFunc for map[string]* types.

func (DefaultValueEncoders) MaxKeyEncodeValue

func (dve DefaultValueEncoders) MaxKeyEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

MaxKeyEncodeValue is the ValueEncoderFunc for bson.MaxKey.

func (DefaultValueEncoders) MinKeyEncodeValue

func (dve DefaultValueEncoders) MinKeyEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

MinKeyEncodeValue is the ValueEncoderFunc for bson.MinKey.

func (DefaultValueEncoders) NullEncodeValue

func (dve DefaultValueEncoders) NullEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

NullEncodeValue is the ValueEncoderFunc for bson.Null.

func (DefaultValueEncoders) ObjectIDEncodeValue

func (dve DefaultValueEncoders) ObjectIDEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

ObjectIDEncodeValue is the ValueEncoderFunc for objectid.ObjectID.

func (DefaultValueEncoders) ReaderEncodeValue

func (dve DefaultValueEncoders) ReaderEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

ReaderEncodeValue is the ValueEncoderFunc for bson.Reader.

func (DefaultValueEncoders) RegexEncodeValue

func (dve DefaultValueEncoders) RegexEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

RegexEncodeValue is the ValueEncoderFunc for bson.Regex.

func (DefaultValueEncoders) SliceEncodeValue

func (dve DefaultValueEncoders) SliceEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

SliceEncodeValue is the ValueEncoderFunc for []* types.

func (DefaultValueEncoders) StringEncodeValue

func (dve DefaultValueEncoders) StringEncodeValue(ectx EncodeContext, vw ValueWriter, i interface{}) error

StringEncodeValue is the ValueEncoderFunc for string types.

func (DefaultValueEncoders) TimeEncodeValue

func (dve DefaultValueEncoders) TimeEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

TimeEncodeValue is the ValueEncoderFunc for time.TIme.

func (DefaultValueEncoders) TimestampEncodeValue

func (dve DefaultValueEncoders) TimestampEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

TimestampEncodeValue is the ValueEncoderFunc for bson.Timestamp.

func (DefaultValueEncoders) URLEncodeValue

func (dve DefaultValueEncoders) URLEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

URLEncodeValue is the ValueEncoderFunc for url.URL.

func (DefaultValueEncoders) UintEncodeValue

func (dve DefaultValueEncoders) UintEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

UintEncodeValue is the ValueEncoderFunc for uint types.

func (DefaultValueEncoders) UndefinedEncodeValue

func (dve DefaultValueEncoders) UndefinedEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

UndefinedEncodeValue is the ValueEncoderFunc for bson.Undefined.

func (DefaultValueEncoders) ValueEncodeValue

func (dve DefaultValueEncoders) ValueEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

ValueEncodeValue is the ValueEncoderFunc for *bson.Value.

func (DefaultValueEncoders) ValueMarshalerEncodeValue

func (dve DefaultValueEncoders) ValueMarshalerEncodeValue(ec EncodeContext, vw ValueWriter, i interface{}) error

ValueMarshalerEncodeValue is the ValueEncoderFunc for ValueMarshaler implementations.

type DocumentReader

type DocumentReader interface {
	ReadElement() (string, ValueReader, error)
}

DocumentReader is implemented by types that allow reading elements from a BSON document.

type DocumentWriter

type DocumentWriter interface {
	WriteDocumentElement(string) (ValueWriter, error)
	WriteDocumentEnd() error
}

DocumentWriter is the interface used to create a BSON or BSON adjacent document. Callers must ensure they call WriteDocumentEnd when they have finished creating the document.

type EncodeContext

type EncodeContext struct {
	*Registry
	MinSize bool
}

EncodeContext is the contextual information required for a Codec to encode a value.

type Encoder

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

An Encoder writes a serialization format to an output stream.

func NewEncoder

func NewEncoder(r *Registry, vw ValueWriter) (*Encoder, error)

NewEncoder returns a new encoder that uses Registry r to write to w.

func (*Encoder) Encode

func (e *Encoder) Encode(val interface{}) error

Encode writes the BSON encoding of val to the stream.

The documentation for Marshal contains details about the conversion of Go values to BSON.

func (*Encoder) Reset

func (e *Encoder) Reset(vw ValueWriter) error

Reset will reset the state of the encoder, using the same *Registry used in the original construction but using vw.

func (*Encoder) SetRegistry

func (e *Encoder) SetRegistry(r *Registry) error

SetRegistry replaces the current registry of the encoder with r.

type ErrNoDecoder

type ErrNoDecoder struct {
	Type reflect.Type
}

ErrNoDecoder is returned when there wasn't a decoder available for a type.

func (ErrNoDecoder) Error

func (end ErrNoDecoder) Error() string

type ErrNoEncoder

type ErrNoEncoder struct {
	Type reflect.Type
}

ErrNoEncoder is returned when there wasn't an encoder available for a type.

func (ErrNoEncoder) Error

func (ene ErrNoEncoder) Error() string

type Marshaler

type Marshaler interface {
	MarshalBSON() ([]byte, error)
}

Marshaler is an interface implemented by types that can marshal themselves into a BSON document represented as bytes. The bytes returned must be a valid BSON document if the error is nil.

type Registry

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

A Registry is used to store and retrieve codecs for types and interfaces. This type is the main typed passed around and Encoders and Decoders are constructed from it.

func (*Registry) LookupDecoder

func (r *Registry) LookupDecoder(t reflect.Type) (ValueDecoder, error)

LookupDecoder will inspect the registry for a decoder that satisfies the type provided. A decoder registered for a specific type will take precedence over a decoder registered for an interface the type satisfies, which takes precedence over a decoder for the reflect.Kind of the value. If no decoder can be found, an error is returned.

func (*Registry) LookupEncoder

func (r *Registry) LookupEncoder(t reflect.Type) (ValueEncoder, error)

LookupEncoder will inspect the registry for an encoder that satisfies the type provided. An encoder registered for a specific type will take precedence over an encoder registered for an interface the type satisfies, which takes precedence over an encoder for the reflect.Kind of the value. If no encoder can be found, an error is returned.

type RegistryBuilder

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

A RegistryBuilder is used to build a Registry. This type is not goroutine safe.

func NewEmptyRegistryBuilder

func NewEmptyRegistryBuilder() *RegistryBuilder

NewEmptyRegistryBuilder creates a new RegistryBuilder with no default kind Codecs.

func NewRegistryBuilder

func NewRegistryBuilder() *RegistryBuilder

NewRegistryBuilder creates a new RegistryBuilder.

func (*RegistryBuilder) Build

func (rb *RegistryBuilder) Build() *Registry

Build creates a Registry from the current state of this RegistryBuilder.

func (*RegistryBuilder) RegisterCodec

func (rb *RegistryBuilder) RegisterCodec(t reflect.Type, codec ValueCodec) *RegistryBuilder

RegisterCodec will register the provided ValueCodec for the provided type.

func (*RegistryBuilder) RegisterDecoder

func (rb *RegistryBuilder) RegisterDecoder(t reflect.Type, dec ValueDecoder) *RegistryBuilder

RegisterDecoder will register the provided ValueDecoder to the provided type.

func (*RegistryBuilder) RegisterDefaultDecoder

func (rb *RegistryBuilder) RegisterDefaultDecoder(kind reflect.Kind, dec ValueDecoder) *RegistryBuilder

RegisterDefaultDecoder will register the provided ValueDecoder to the provided kind.

func (*RegistryBuilder) RegisterDefaultEncoder

func (rb *RegistryBuilder) RegisterDefaultEncoder(kind reflect.Kind, enc ValueEncoder) *RegistryBuilder

RegisterDefaultEncoder will registr the provided ValueEncoder to the provided kind.

func (*RegistryBuilder) RegisterEncoder

func (rb *RegistryBuilder) RegisterEncoder(t reflect.Type, enc ValueEncoder) *RegistryBuilder

RegisterEncoder will register the provided ValueEncoder to the provided type.

type StructCodec

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

StructCodec is the Codec used for struct values.

func NewStructCodec

func NewStructCodec(p StructTagParser) (*StructCodec, error)

NewStructCodec returns a StructCodec that uses p for struct tag parsing.

func (*StructCodec) DecodeValue

func (sc *StructCodec) DecodeValue(r DecodeContext, vr ValueReader, i interface{}) error

DecodeValue implements the Codec interface.

func (*StructCodec) EncodeValue

func (sc *StructCodec) EncodeValue(r EncodeContext, vw ValueWriter, i interface{}) error

EncodeValue handles encoding generic struct types.

type StructTagParser

type StructTagParser interface {
	ParseStructTags(reflect.StructField) (StructTags, error)
}

StructTagParser returns the struct tags for a given struct field.

type StructTagParserFunc

type StructTagParserFunc func(reflect.StructField) (StructTags, error)

StructTagParserFunc is an adapter that allows a generic function to be used as a StructTagParser.

var DefaultStructTagParser StructTagParserFunc = func(sf reflect.StructField) (StructTags, error) {
	key := strings.ToLower(sf.Name)
	tag, ok := sf.Tag.Lookup("bson")
	if !ok && !strings.Contains(string(sf.Tag), ":") && len(sf.Tag) > 0 {
		tag = string(sf.Tag)
	}
	var st StructTags
	if tag == "-" {
		st.Skip = true
		return st, nil
	}

	for idx, str := range strings.Split(tag, ",") {
		if idx == 0 && str != "" {
			key = str
		}
		switch str {
		case "omitempty":
			st.OmitEmpty = true
		case "minsize":
			st.MinSize = true
		case "truncate":
			st.Truncate = true
		case "inline":
			st.Inline = true
		}
	}

	st.Name = key

	return st, nil
}

DefaultStructTagParser is the StructTagParser used by the StructCodec by default. It will handle the bson struct tag. See the documentation for StructTags to see what each of the returned fields means.

If there is no name in the struct tag fields, the struct field name is lowercased. The tag formats accepted are:

"[<key>][,<flag1>[,<flag2>]]"

`(...) bson:"[<key>][,<flag1>[,<flag2>]]" (...)`

An example:

type T struct {
    A bool
    B int    "myb"
    C string "myc,omitempty"
    D string `bson:",omitempty" json:"jsonkey"`
    E int64  ",minsize"
    F int64  "myf,omitempty,minsize"
}

A struct tag either consisting entirely of '-' or with a bson key with a value consisting entirely of '-' will return a StructTags with Skip true and the remaining fields will be their default values.

func (StructTagParserFunc) ParseStructTags

func (stpf StructTagParserFunc) ParseStructTags(sf reflect.StructField) (StructTags, error)

ParseStructTags implements the StructTagParser interface.

type StructTags

type StructTags struct {
	Name      string
	OmitEmpty bool
	MinSize   bool
	Truncate  bool
	Inline    bool
	Skip      bool
}

StructTags represents the struct tag fields that the StructCodec uses during the encoding and decoding process.

In the case of a struct, the lowercased field name is used as the key for each exported field but this behavior may be changed using a struct tag. The tag may also contain flags to adjust the marshalling behavior for the field.

The properties are defined below:

OmitEmpty  Only include the field if it's not set to the zero value for the type or to
           empty slices or maps.

MinSize    Marshal an integer of a type larger than 32 bits value as an int32, if that's
           feasible while preserving the numeric value.

Truncate   When unmarshaling a BSON double, it is permitted to lose precision to fit within
           a float32.

Inline     Inline the field, which must be a struct or a map, causing all of its fields
           or keys to be processed as if they were part of the outer struct. For maps,
           keys must not conflict with the bson keys of other struct fields.

Skip       This struct field should be skipped. This is usually denoted by parsing a "-"
           for the name.

TODO(skriptble): Add tags for undefined as nil and for null as nil.

type TransitionError

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

TransitionError is an error returned when an invalid progressing a ValueReader or ValueWriter state machine occurs.

func (TransitionError) Error

func (te TransitionError) Error() string

type Unmarshaler

type Unmarshaler interface {
	UnmarshalBSON([]byte) error
}

Unmarshaler is an interface implemented by types that can unmarshal a BSON document representation of themselves. The BSON bytes can be assumed to be valid. UnmarshalBSON must copy the BSON bytes if it wishes to retain the data after returning.

type ValueCodec

type ValueCodec interface {
	ValueEncoder
	ValueDecoder
}

ValueCodec is the interface that groups the methods to encode and decode values.

type ValueDecoder

type ValueDecoder interface {
	DecodeValue(DecodeContext, ValueReader, interface{}) error
}

ValueDecoder is the interface implemented by types that can handle the decoding of a value. Implementations must handle pointers to values, including pointers to pointer values. The implementation may create a new value and assign it to the pointer if necessary.

type ValueDecoderError

type ValueDecoderError struct {
	Name     string
	Types    []interface{}
	Received interface{}
}

ValueDecoderError is an error returned from a ValueDecoder when the provided value can't be decoded by the ValueDecoder.

func (ValueDecoderError) Error

func (vde ValueDecoderError) Error() string

type ValueDecoderFunc

type ValueDecoderFunc func(DecodeContext, ValueReader, interface{}) error

ValueDecoderFunc is an adapter function that allows a function with the correct signature to be used as a ValueDecoder.

func (ValueDecoderFunc) DecodeValue

func (fn ValueDecoderFunc) DecodeValue(dc DecodeContext, vr ValueReader, val interface{}) error

DecodeValue implements the ValueDecoder interface.

type ValueEncoder

type ValueEncoder interface {
	EncodeValue(EncodeContext, ValueWriter, interface{}) error
}

ValueEncoder is the interface implemented by types that can handle the encoding of a value. Implementations must handle both values and pointers to values.

type ValueEncoderError

type ValueEncoderError struct {
	Name     string
	Types    []interface{}
	Received interface{}
}

ValueEncoderError is an error returned from a ValueEncoder when the provided value can't be encoded by the ValueEncoder.

func (ValueEncoderError) Error

func (vee ValueEncoderError) Error() string

type ValueEncoderFunc

type ValueEncoderFunc func(EncodeContext, ValueWriter, interface{}) error

ValueEncoderFunc is an adapter function that allows a function with the correct signature to be used as a ValueEncoder.

func (ValueEncoderFunc) EncodeValue

func (fn ValueEncoderFunc) EncodeValue(ec EncodeContext, vw ValueWriter, val interface{}) error

EncodeValue implements the ValueEncoder interface.

type ValueMarshaler

type ValueMarshaler interface {
	MarshalBSONValue() (bson.Type, []byte, error)
}

ValueMarshaler is an interface implemented by types that can marshal themselves into a BSON value as bytes. The type must be the valid type for the bytes returned. The bytes and byte type together must be valid if the error is nil.

type ValueReader

type ValueReader interface {
	Type() bson.Type
	Skip() error

	ReadArray() (ArrayReader, error)
	ReadBinary() (b []byte, btype byte, err error)
	ReadBoolean() (bool, error)
	ReadDocument() (DocumentReader, error)
	ReadCodeWithScope() (code string, dr DocumentReader, err error)
	ReadDBPointer() (ns string, oid objectid.ObjectID, err error)
	ReadDateTime() (int64, error)
	ReadDecimal128() (decimal.Decimal128, error)
	ReadDouble() (float64, error)
	ReadInt32() (int32, error)
	ReadInt64() (int64, error)
	ReadJavascript() (code string, err error)
	ReadMaxKey() error
	ReadMinKey() error
	ReadNull() error
	ReadObjectID() (objectid.ObjectID, error)
	ReadRegex() (pattern, options string, err error)
	ReadString() (string, error)
	ReadSymbol() (symbol string, err error)
	ReadTimestamp() (t, i uint32, err error)
	ReadUndefined() error
}

ValueReader is a generic interface used to read values from BSON. This type is implemented by several types with different underlying representations of BSON, such as a bson.Document, raw BSON bytes, or extended JSON.

func NewDocumentValueReader

func NewDocumentValueReader(d *bson.Document) (ValueReader, error)

NewDocumentValueReader creates a new ValueReader from the given *Document.

func NewExtJSONValueReader added in v0.0.16

func NewExtJSONValueReader(r io.Reader, canonical bool) ValueReader

NewExtJSONValueReader creates a new ValueReader from a given io.Reader It will interpret the JSON of r as canonical or relaxed according to the given canonical flag

func NewValueReader

func NewValueReader(b []byte) ValueReader

NewValueReader returns a ValueReader using b for the underlying BSON representation.

type ValueUnmarshaler

type ValueUnmarshaler interface {
	UnmarshalBSONValue(bson.Type, []byte) error
}

ValueUnmarshaler is an interface implemented by types that can unmarshal a BSON value representaiton of themselves. The BSON bytes and type can be assumed to be valid. UnmarshalBSONValue must copy the BSON value bytes if it wishes to retain the data after returning.

type ValueWriter

type ValueWriter interface {
	WriteArray() (ArrayWriter, error)
	WriteBinary(b []byte) error
	WriteBinaryWithSubtype(b []byte, btype byte) error
	WriteBoolean(bool) error
	WriteCodeWithScope(code string) (DocumentWriter, error)
	WriteDBPointer(ns string, oid objectid.ObjectID) error
	WriteDateTime(dt int64) error
	WriteDecimal128(decimal.Decimal128) error
	WriteDouble(float64) error
	WriteInt32(int32) error
	WriteInt64(int64) error
	WriteJavascript(code string) error
	WriteMaxKey() error
	WriteMinKey() error
	WriteNull() error
	WriteObjectID(objectid.ObjectID) error
	WriteRegex(pattern, options string) error
	WriteString(string) error
	WriteDocument() (DocumentWriter, error)
	WriteSymbol(symbol string) error
	WriteTimestamp(t, i uint32) error
	WriteUndefined() error
}

ValueWriter is the interface used to write BSON values. Implementations of this interface handle creating BSON or BSON adjacent representations of the values.

func NewBSONValueWriter

func NewBSONValueWriter(w io.Writer) (ValueWriter, error)

NewBSONValueWriter creates a ValueWriter that writes BSON to w.

This ValueWriter will only write entire documents to the io.Writer and it will buffer the document as it is built.

func NewDocumentValueWriter

func NewDocumentValueWriter(d *bson.Document) (ValueWriter, error)

NewDocumentValueWriter creates a ValueWriter that adds elements to d.

func NewExtJSONValueWriter

func NewExtJSONValueWriter(w io.Writer, canonical, escapeHTML bool) (ValueWriter, error)

NewExtJSONValueWriter creates a ValueWriter that writes Extended JSON to w.

Jump to

Keyboard shortcuts

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