bsoncodec

package
v0.0.17 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

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

This section is empty.

Types

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 DecodeContext

type DecodeContext struct {
	*Registry
	Truncate bool
}

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

type DefaultValueDecoders

type DefaultValueDecoders struct{}

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

func (DefaultValueDecoders) BooleanDecodeValue

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

BooleanDecodeValue is the ValueDecoderFunc for bool types.

func (DefaultValueDecoders) ByteSliceDecodeValue

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

ByteSliceDecodeValue is the ValueDecoderFunc for []byte.

func (DefaultValueDecoders) Decimal128DecodeValue

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

Decimal128DecodeValue is the ValueDecoderFunc for decimal.Decimal128.

func (DefaultValueDecoders) EmptyInterfaceDecodeValue

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

EmptyInterfaceDecodeValue is the ValueDecoderFunc for interface{}.

func (DefaultValueDecoders) FloatDecodeValue

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

FloatDecodeValue is the ValueDecoderFunc for float types.

func (DefaultValueDecoders) IntDecodeValue

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

IntDecodeValue is the ValueDecoderFunc for bool types.

func (DefaultValueDecoders) JSONNumberDecodeValue

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

JSONNumberDecodeValue is the ValueDecoderFunc for json.Number.

func (DefaultValueDecoders) MapDecodeValue

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

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

func (DefaultValueDecoders) ObjectIDDecodeValue

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

ObjectIDDecodeValue is the ValueDecoderFunc for objectid.ObjectID.

func (DefaultValueDecoders) RegisterDefaultDecoders added in v0.0.17

func (dvd DefaultValueDecoders) RegisterDefaultDecoders(rb *RegistryBuilder)

RegisterDefaultDecoders will register the decoder methods attached to DefaultValueDecoders with the provided RegistryBuilder.

There is no support for decoding map[string]interface{} becuase there is no decoder for interface{}, so users must either register this decoder themselves or use the EmptyInterfaceDecoder avaialble in the bson package.

func (DefaultValueDecoders) SliceDecodeValue

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

SliceDecodeValue is the ValueDecoderFunc for []* types.

func (DefaultValueDecoders) StringDecodeValue

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

StringDecodeValue is the ValueDecoderFunc for string types.

func (DefaultValueDecoders) TimeDecodeValue

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

TimeDecodeValue is the ValueDecoderFunc for time.Time.

func (DefaultValueDecoders) URLDecodeValue

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

URLDecodeValue is the ValueDecoderFunc for url.URL.

func (DefaultValueDecoders) UintDecodeValue

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

UintDecodeValue is the ValueDecoderFunc for uint types.

func (DefaultValueDecoders) ValueUnmarshalerDecodeValue

func (dvd DefaultValueDecoders) ValueUnmarshalerDecodeValue(dc DecodeContext, vr bsonrw.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) BooleanEncodeValue

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

BooleanEncodeValue is the ValueEncoderFunc for bool types.

func (DefaultValueEncoders) ByteSliceEncodeValue

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

ByteSliceEncodeValue is the ValueEncoderFunc for []byte.

func (DefaultValueEncoders) Decimal128EncodeValue

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

Decimal128EncodeValue is the ValueEncoderFunc for decimal.Decimal128.

func (DefaultValueEncoders) EmptyInterfaceEncodeValue

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

EmptyInterfaceEncodeValue is the ValueEncoderFunc for interface{}.

func (DefaultValueEncoders) FloatEncodeValue

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

FloatEncodeValue is the ValueEncoderFunc for float types.

func (DefaultValueEncoders) IntEncodeValue

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

IntEncodeValue is the ValueEncoderFunc for int types.

func (DefaultValueEncoders) JSONNumberEncodeValue

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

JSONNumberEncodeValue is the ValueEncoderFunc for json.Number.

func (DefaultValueEncoders) MapEncodeValue

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

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

func (DefaultValueEncoders) ObjectIDEncodeValue

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

ObjectIDEncodeValue is the ValueEncoderFunc for objectid.ObjectID.

func (DefaultValueEncoders) RegisterDefaultEncoders added in v0.0.17

func (dve DefaultValueEncoders) RegisterDefaultEncoders(rb *RegistryBuilder)

RegisterDefaultEncoders will register the encoder methods attached to DefaultValueEncoders with the provided RegistryBuilder.

func (DefaultValueEncoders) SliceEncodeValue

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

SliceEncodeValue is the ValueEncoderFunc for []* types.

func (DefaultValueEncoders) StringEncodeValue

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

StringEncodeValue is the ValueEncoderFunc for string types.

func (DefaultValueEncoders) TimeEncodeValue

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

TimeEncodeValue is the ValueEncoderFunc for time.TIme.

func (DefaultValueEncoders) URLEncodeValue

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

URLEncodeValue is the ValueEncoderFunc for url.URL.

func (DefaultValueEncoders) UintEncodeValue

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

UintEncodeValue is the ValueEncoderFunc for uint types.

func (DefaultValueEncoders) ValueMarshalerEncodeValue

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

ValueMarshalerEncodeValue is the ValueEncoderFunc for ValueMarshaler implementations.

type EncodeContext

type EncodeContext struct {
	*Registry
	MinSize bool
}

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

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 NewRegistryBuilder

func NewRegistryBuilder() *RegistryBuilder

NewRegistryBuilder creates a new empty 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 bsonrw.ValueReader, i interface{}) error

DecodeValue implements the Codec interface.

func (*StructCodec) EncodeValue

func (sc *StructCodec) EncodeValue(r EncodeContext, vw bsonrw.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, bsonrw.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, bsonrw.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 bsonrw.ValueReader, val interface{}) error

DecodeValue implements the ValueDecoder interface.

type ValueEncoder

type ValueEncoder interface {
	EncodeValue(EncodeContext, bsonrw.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, bsonrw.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 bsonrw.ValueWriter, val interface{}) error

EncodeValue implements the ValueEncoder interface.

type ValueMarshaler

type ValueMarshaler interface {
	MarshalBSONValue() (bsontype.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 ValueUnmarshaler

type ValueUnmarshaler interface {
	UnmarshalBSONValue(bsontype.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 Zeroer added in v0.0.17

type Zeroer interface {
	IsZero() bool
}

Zeroer allows custom struct types to implement a report of zero state. All struct types that don't implement Zeroer or where IsZero returns false are considered to be not zero.

Jump to

Keyboard shortcuts

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