Documentation ¶
Overview ¶
Package bsoncodec provides a system for encoding values to BSON representations and decoding values from BSON representations. This package considers both binary BSON and ExtendedJSON as BSON representations. The types in this package enable a flexible system for handling this encoding and decoding.
The codec system is composed of two parts:
1) ValueEncoders and ValueDecoders that handle encoding and decoding Go values to and from BSON representations.
2) A Registry that holds these ValueEncoders and ValueDecoders and provides methods for retrieving them.
ValueEncoders and ValueDecoders ¶
The ValueEncoder interface is implemented by types that can encode a provided Go type to BSON. The value to encode is provided as a reflect.Value and a bsonrw.ValueWriter is used within the EncodeValue method to actually create the BSON representation. For convenience, ValueEncoderFunc is provided to allow use of a function with the correct signature as a ValueEncoder. An EncodeContext instance is provided to allow implementations to lookup further ValueEncoders and to provide configuration information.
The ValueDecoder interface is the inverse of the ValueEncoder. Implementations should ensure that the value they receive is settable. Similar to ValueEncoderFunc, ValueDecoderFunc is provided to allow the use of a function with the correct signature as a ValueDecoder. A DecodeContext instance is provided and serves similar functionality to the EncodeContext.
Registry ¶
A Registry is a store for ValueEncoders, ValueDecoders, and a type map. See the Registry type documentation for examples of registering various custom encoders and decoders. A Registry can have three main types of codecs:
1. Type encoders/decoders - These can be registered using the RegisterTypeEncoder and RegisterTypeDecoder methods. The registered codec will be invoked when encoding/decoding a value whose type matches the registered type exactly. If the registered type is an interface, the codec will be invoked when encoding or decoding values whose type is the interface, but not for values with concrete types that implement the interface.
2. Hook encoders/decoders - These can be registered using the RegisterHookEncoder and RegisterHookDecoder methods. These methods only accept interface types and the registered codecs will be invoked when encoding or decoding values whose types implement the interface. An example of a hook defined by the driver is bson.Marshaler. The driver will call the MarshalBSON method for any value whose type implements bson.Marshaler, regardless of the value's concrete type.
3. Type map entries - This can be used to associate a BSON type with a Go type. These type associations are used when decoding into a bson.D/bson.M or a struct field of type interface{}. For example, by default, BSON int32 and int64 values decode as Go int32 and int64 instances, respectively, when decoding into a bson.D. The following code would change the behavior so these values decode as Go int instances instead:
intType := reflect.TypeOf(int(0)) registry.RegisterTypeMapEntry(bsontype.Int32, intType).RegisterTypeMapEntry(bsontype.Int64, intType)
4. Kind encoder/decoders - These can be registered using the RegisterDefaultEncoder and RegisterDefaultDecoder methods. The registered codec will be invoked when encoding or decoding values whose reflect.Kind matches the registered reflect.Kind as long as the value's type doesn't match a registered type or hook encoder/decoder first. These methods should be used to change the behavior for all values for a specific kind.
Registry Lookup Procedure ¶
When looking up an encoder in a Registry, the precedence rules are as follows:
1. A type encoder registered for the exact type of the value.
2. A hook encoder registered for an interface that is implemented by the value or by a pointer to the value. If the value matches multiple hooks (e.g. the type implements bsoncodec.Marshaler and bsoncodec.ValueMarshaler), the first one registered will be selected. Note that registries constructed using bson.NewRegistry have driver-defined hooks registered for the bsoncodec.Marshaler, bsoncodec.ValueMarshaler, and bsoncodec.Proxy interfaces, so those will take precedence over any new hooks.
3. A kind encoder registered for the value's kind.
If all of these lookups fail to find an encoder, an error of type ErrNoEncoder is returned. The same precedence rules apply for decoders, with the exception that an error of type ErrNoDecoder will be returned if no decoder is found.
DefaultValueEncoders and DefaultValueDecoders ¶
The DefaultValueEncoders and DefaultValueDecoders types provide a full set of ValueEncoders and ValueDecoders for handling a wide range of Go types, including all of the types within the primitive package. To make registering these codecs easier, a helper method on each type is provided. For the DefaultValueEncoders type the method is called RegisterDefaultEncoders and for the DefaultValueDecoders type the method is called RegisterDefaultDecoders, this method also handles registering type map entries for each BSON type.
Index ¶
- Variables
- type ArrayCodec
- type ByteSliceCodec
- type CodecZeroerdeprecated
- type DecodeContext
- type DecodeError
- type DefaultValueDecodersdeprecated
- func (dvd DefaultValueDecoders) ArrayDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) BinaryDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) BooleanDecodeValue(dctx DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) ByteSliceDecodeValue(_ DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) CodeWithScopeDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (DefaultValueDecoders) CoreDocumentDecodeValue(_ DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) DBPointerDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) DDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) DateTimeDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) Decimal128DecodeValue(dctx DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) EmptyInterfaceDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) FloatDecodeValue(ec DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) IntDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) JSONNumberDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) JavaScriptDecodeValue(dctx DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) MapDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) MaxKeyDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) MinKeyDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) NullDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) ObjectIDDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) RegexDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) RegisterDefaultDecoders(rb *RegistryBuilder)deprecated
- func (dvd DefaultValueDecoders) SliceDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) StringDecodeValue(_ DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) SymbolDecodeValue(dctx DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) TimeDecodeValue(_ DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) TimestampDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) URLDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) UintDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) UndefinedDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) UnmarshalerDecodeValue(_ DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- func (dvd DefaultValueDecoders) ValueUnmarshalerDecodeValue(_ DecodeContext, vr bsonrw.ValueReader, val reflect.Value) errordeprecated
- type DefaultValueEncodersdeprecated
- func (dve DefaultValueEncoders) ArrayEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (DefaultValueEncoders) BinaryEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (dve DefaultValueEncoders) BooleanEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (dve DefaultValueEncoders) ByteSliceEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (dve DefaultValueEncoders) CodeWithScopeEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (DefaultValueEncoders) CoreDocumentEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (DefaultValueEncoders) DBPointerEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (DefaultValueEncoders) DateTimeEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (dve DefaultValueEncoders) Decimal128EncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (dve DefaultValueEncoders) EmptyInterfaceEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (dve DefaultValueEncoders) FloatEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (dve DefaultValueEncoders) IntEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (dve DefaultValueEncoders) JSONNumberEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (DefaultValueEncoders) JavaScriptEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (dve DefaultValueEncoders) MapEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (dve DefaultValueEncoders) MarshalerEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (DefaultValueEncoders) MaxKeyEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (DefaultValueEncoders) MinKeyEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (DefaultValueEncoders) NullEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (dve DefaultValueEncoders) ObjectIDEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (dve DefaultValueEncoders) ProxyEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (DefaultValueEncoders) RegexEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (dve DefaultValueEncoders) RegisterDefaultEncoders(rb *RegistryBuilder)deprecated
- func (dve DefaultValueEncoders) SliceEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (dve DefaultValueEncoders) StringEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (DefaultValueEncoders) SymbolEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (dve DefaultValueEncoders) TimeEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (DefaultValueEncoders) TimestampEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (dve DefaultValueEncoders) URLEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (dve DefaultValueEncoders) UintEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (DefaultValueEncoders) UndefinedEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- func (dve DefaultValueEncoders) ValueMarshalerEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) errordeprecated
- type EmptyInterfaceCodec
- type EncodeContext
- type ErrNoDecoder
- type ErrNoEncoder
- type ErrNoTypeMapEntry
- type KeyMarshaler
- type KeyUnmarshaler
- type MapCodec
- type Marshalerdeprecated
- type PointerCodec
- type Proxy
- type Registry
- func (r *Registry) LookupDecoder(valueType reflect.Type) (ValueDecoder, error)
- func (r *Registry) LookupEncoder(valueType reflect.Type) (ValueEncoder, error)
- func (r *Registry) LookupTypeMapEntry(bt bsontype.Type) (reflect.Type, error)
- func (r *Registry) RegisterInterfaceDecoder(iface reflect.Type, dec ValueDecoder)
- func (r *Registry) RegisterInterfaceEncoder(iface reflect.Type, enc ValueEncoder)
- func (r *Registry) RegisterKindDecoder(kind reflect.Kind, dec ValueDecoder)
- func (r *Registry) RegisterKindEncoder(kind reflect.Kind, enc ValueEncoder)
- func (r *Registry) RegisterTypeDecoder(valueType reflect.Type, dec ValueDecoder)
- func (r *Registry) RegisterTypeEncoder(valueType reflect.Type, enc ValueEncoder)
- func (r *Registry) RegisterTypeMapEntry(bt bsontype.Type, rt reflect.Type)
- type RegistryBuilderdeprecated
- func (rb *RegistryBuilder) Build() *Registrydeprecated
- func (rb *RegistryBuilder) RegisterCodec(t reflect.Type, codec ValueCodec) *RegistryBuilderdeprecated
- func (rb *RegistryBuilder) RegisterDecoder(t reflect.Type, dec ValueDecoder) *RegistryBuilderdeprecated
- func (rb *RegistryBuilder) RegisterDefaultDecoder(kind reflect.Kind, dec ValueDecoder) *RegistryBuilderdeprecated
- func (rb *RegistryBuilder) RegisterDefaultEncoder(kind reflect.Kind, enc ValueEncoder) *RegistryBuilderdeprecated
- func (rb *RegistryBuilder) RegisterEncoder(t reflect.Type, enc ValueEncoder) *RegistryBuilderdeprecated
- func (rb *RegistryBuilder) RegisterHookDecoder(t reflect.Type, dec ValueDecoder) *RegistryBuilderdeprecated
- func (rb *RegistryBuilder) RegisterHookEncoder(t reflect.Type, enc ValueEncoder) *RegistryBuilderdeprecated
- func (rb *RegistryBuilder) RegisterTypeDecoder(t reflect.Type, dec ValueDecoder) *RegistryBuilderdeprecated
- func (rb *RegistryBuilder) RegisterTypeEncoder(t reflect.Type, enc ValueEncoder) *RegistryBuilderdeprecated
- func (rb *RegistryBuilder) RegisterTypeMapEntry(bt bsontype.Type, rt reflect.Type) *RegistryBuilderdeprecated
- type SliceCodec
- type StringCodec
- type StructCodec
- type StructTagParser
- type StructTagParserFunc
- type StructTags
- type TimeCodec
- type TransitionError
- type UIntCodec
- type Unmarshalerdeprecated
- type ValueCodecdeprecated
- type ValueDecoder
- type ValueDecoderError
- type ValueDecoderFunc
- type ValueEncoder
- type ValueEncoderError
- type ValueEncoderFunc
- type ValueMarshalerdeprecated
- type ValueUnmarshalerdeprecated
- type Zeroer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNilType = errors.New("cannot perform a decoder lookup on <nil>")
ErrNilType is returned when nil is passed to either LookupEncoder or LookupDecoder.
var ErrNotInterface = errors.New("The provided type is not an interface")
ErrNotInterface is returned when the provided type is not an interface.
var ErrNotPointer = errors.New("non-pointer provided to LookupDecoder")
ErrNotPointer is returned when a non-pointer type is provided to LookupDecoder.
Functions ¶
This section is empty.
Types ¶
type ArrayCodec ¶
type ArrayCodec struct{}
ArrayCodec is the Codec used for bsoncore.Array values.
func (*ArrayCodec) DecodeValue ¶
func (ac *ArrayCodec) DecodeValue(_ DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DecodeValue is the ValueDecoder for bsoncore.Array values.
func (*ArrayCodec) EncodeValue ¶
func (ac *ArrayCodec) EncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
EncodeValue is the ValueEncoder for bsoncore.Array values.
type ByteSliceCodec ¶
type ByteSliceCodec struct {
EncodeNilAsEmpty bool
}
ByteSliceCodec is the Codec used for []byte values.
func NewByteSliceCodec ¶
func NewByteSliceCodec(opts ...*bsonoptions.ByteSliceCodecOptions) *ByteSliceCodec
NewByteSliceCodec returns a StringCodec with options opts.
func (*ByteSliceCodec) DecodeValue ¶
func (bsc *ByteSliceCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DecodeValue is the ValueDecoder for []byte.
func (*ByteSliceCodec) EncodeValue ¶
func (bsc *ByteSliceCodec) EncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
EncodeValue is the ValueEncoder for []byte.
type CodecZeroer
deprecated
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.
Deprecated: Defining custom rules for the zero/empty value will not be supported in Go Driver 2.0. Users who want to omit empty complex values should use a pointer field and set the value to nil instead.
type DecodeContext ¶
type DecodeContext struct { *Registry Truncate bool // Ancestor is the type of a containing document. This is mainly used to determine what type // should be used when decoding an embedded document into an empty interface. For example, if // Ancestor is a bson.M, BSON embedded document values being decoded into an empty interface // will be decoded into a bson.M. // // Deprecated: Use DefaultDocumentM or DefaultDocumentD instead. Ancestor reflect.Type // contains filtered or unexported fields }
DecodeContext is the contextual information required for a Codec to decode a value.
func (*DecodeContext) DefaultDocumentD ¶
func (dc *DecodeContext) DefaultDocumentD()
DefaultDocumentD will decode empty documents using the primitive.D type. This behavior is restricted to data typed as "interface{}" or "map[string]interface{}".
func (*DecodeContext) DefaultDocumentM ¶
func (dc *DecodeContext) DefaultDocumentM()
DefaultDocumentM will decode empty documents using the primitive.M type. This behavior is restricted to data typed as "interface{}" or "map[string]interface{}".
type DecodeError ¶
type DecodeError struct {
// contains filtered or unexported fields
}
DecodeError represents an error that occurs when unmarshalling BSON bytes into a native Go type.
func (*DecodeError) Error ¶
func (de *DecodeError) Error() string
Error implements the error interface.
func (*DecodeError) Keys ¶
func (de *DecodeError) Keys() []string
Keys returns the BSON key path that caused an error as a slice of strings. The keys in the slice are in top-down order. For example, if the document being unmarshalled was {a: {b: {c: 1}}} and the value for c was supposed to be a string, the keys slice will be ["a", "b", "c"].
func (*DecodeError) Unwrap ¶
func (de *DecodeError) Unwrap() error
Unwrap returns the underlying error
type DefaultValueDecoders
deprecated
type DefaultValueDecoders struct{}
DefaultValueDecoders is a namespace type for the default ValueDecoders used when creating a registry.
Deprecated: Use bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) ArrayDecodeValue
deprecated
func (dvd DefaultValueDecoders) ArrayDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
ArrayDecodeValue is the ValueDecoderFunc for array types.
Deprecated: Use bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) BinaryDecodeValue
deprecated
func (dvd DefaultValueDecoders) BinaryDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
BinaryDecodeValue is the ValueDecoderFunc for Binary.
Deprecated: Use bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) BooleanDecodeValue
deprecated
func (dvd DefaultValueDecoders) BooleanDecodeValue(dctx DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
BooleanDecodeValue is the ValueDecoderFunc for bool types.
Deprecated: Use bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) ByteSliceDecodeValue
deprecated
func (dvd DefaultValueDecoders) ByteSliceDecodeValue(_ DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
ByteSliceDecodeValue is the ValueDecoderFunc for []byte.
Deprecated: ByteSliceDecodeValue is not registered by default. Use ByteSliceCodec.DecodeValue instead.
func (DefaultValueDecoders) CodeWithScopeDecodeValue
deprecated
func (dvd DefaultValueDecoders) CodeWithScopeDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
CodeWithScopeDecodeValue is the ValueDecoderFunc for CodeWithScope.
Deprecated: Use bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) CoreDocumentDecodeValue
deprecated
func (DefaultValueDecoders) CoreDocumentDecodeValue(_ DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
CoreDocumentDecodeValue is the ValueDecoderFunc for bsoncore.Document.
Deprecated: Use bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) DBPointerDecodeValue
deprecated
func (dvd DefaultValueDecoders) DBPointerDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DBPointerDecodeValue is the ValueDecoderFunc for DBPointer.
Deprecated: Use bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) DDecodeValue
deprecated
func (dvd DefaultValueDecoders) DDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DDecodeValue is the ValueDecoderFunc for primitive.D instances.
Deprecated: Use bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) DateTimeDecodeValue
deprecated
func (dvd DefaultValueDecoders) DateTimeDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DateTimeDecodeValue is the ValueDecoderFunc for DateTime.
Deprecated: Use bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) Decimal128DecodeValue
deprecated
func (dvd DefaultValueDecoders) Decimal128DecodeValue(dctx DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
Decimal128DecodeValue is the ValueDecoderFunc for primitive.Decimal128.
Deprecated: Use bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) EmptyInterfaceDecodeValue
deprecated
func (dvd DefaultValueDecoders) EmptyInterfaceDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
EmptyInterfaceDecodeValue is the ValueDecoderFunc for interface{}.
Deprecated: EmptyInterfaceDecodeValue is not registered by default. Use EmptyInterfaceCodec.DecodeValue instead.
func (DefaultValueDecoders) FloatDecodeValue
deprecated
func (dvd DefaultValueDecoders) FloatDecodeValue(ec DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
FloatDecodeValue is the ValueDecoderFunc for float types.
Deprecated: Use bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) IntDecodeValue
deprecated
func (dvd DefaultValueDecoders) IntDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
IntDecodeValue is the ValueDecoderFunc for int types.
Deprecated: Use bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) JSONNumberDecodeValue
deprecated
func (dvd DefaultValueDecoders) JSONNumberDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
JSONNumberDecodeValue is the ValueDecoderFunc for json.Number.
Deprecated: Use bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) JavaScriptDecodeValue
deprecated
func (dvd DefaultValueDecoders) JavaScriptDecodeValue(dctx DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
JavaScriptDecodeValue is the ValueDecoderFunc for the primitive.JavaScript type.
Deprecated: Use bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) MapDecodeValue
deprecated
func (dvd DefaultValueDecoders) MapDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
MapDecodeValue is the ValueDecoderFunc for map[string]* types.
Deprecated: MapDecodeValue is not registered by default. Use MapCodec.DecodeValue instead.
func (DefaultValueDecoders) MaxKeyDecodeValue
deprecated
func (dvd DefaultValueDecoders) MaxKeyDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
MaxKeyDecodeValue is the ValueDecoderFunc for MaxKey.
Deprecated: Use bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) MinKeyDecodeValue
deprecated
func (dvd DefaultValueDecoders) MinKeyDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
MinKeyDecodeValue is the ValueDecoderFunc for MinKey.
Deprecated: Use bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) NullDecodeValue
deprecated
func (dvd DefaultValueDecoders) NullDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
NullDecodeValue is the ValueDecoderFunc for Null.
Deprecated: Use bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) ObjectIDDecodeValue
deprecated
func (dvd DefaultValueDecoders) ObjectIDDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
ObjectIDDecodeValue is the ValueDecoderFunc for primitive.ObjectID.
Deprecated: Use bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) RegexDecodeValue
deprecated
func (dvd DefaultValueDecoders) RegexDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
RegexDecodeValue is the ValueDecoderFunc for Regex.
Deprecated: Use bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) RegisterDefaultDecoders
deprecated
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{} because there is no decoder for interface{}, so users must either register this decoder themselves or use the EmptyInterfaceDecoder available in the bson package.
Deprecated: Use bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) SliceDecodeValue
deprecated
func (dvd DefaultValueDecoders) SliceDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
SliceDecodeValue is the ValueDecoderFunc for slice types.
Deprecated: SliceDecodeValue is not registered by default. Use SliceCodec.DecodeValue instead.
func (DefaultValueDecoders) StringDecodeValue
deprecated
func (dvd DefaultValueDecoders) StringDecodeValue(_ DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
StringDecodeValue is the ValueDecoderFunc for string types.
Deprecated: StringDecodeValue is not registered by default. Use StringCodec.DecodeValue instead.
func (DefaultValueDecoders) SymbolDecodeValue
deprecated
func (dvd DefaultValueDecoders) SymbolDecodeValue(dctx DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
SymbolDecodeValue is the ValueDecoderFunc for the primitive.Symbol type.
Deprecated: Use bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) TimeDecodeValue
deprecated
func (dvd DefaultValueDecoders) TimeDecodeValue(_ DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
TimeDecodeValue is the ValueDecoderFunc for time.Time.
Deprecated: TimeDecodeValue is not registered by default. Use TimeCodec.DecodeValue instead.
func (DefaultValueDecoders) TimestampDecodeValue
deprecated
func (dvd DefaultValueDecoders) TimestampDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
TimestampDecodeValue is the ValueDecoderFunc for Timestamp.
Deprecated: Use bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) URLDecodeValue
deprecated
func (dvd DefaultValueDecoders) URLDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
URLDecodeValue is the ValueDecoderFunc for url.URL.
Deprecated: Use bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) UintDecodeValue
deprecated
func (dvd DefaultValueDecoders) UintDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
UintDecodeValue is the ValueDecoderFunc for uint types.
Deprecated: UintDecodeValue is not registered by default. Use UintCodec.DecodeValue instead.
func (DefaultValueDecoders) UndefinedDecodeValue
deprecated
func (dvd DefaultValueDecoders) UndefinedDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
UndefinedDecodeValue is the ValueDecoderFunc for Undefined.
Deprecated: Use bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) UnmarshalerDecodeValue
deprecated
func (dvd DefaultValueDecoders) UnmarshalerDecodeValue(_ DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
UnmarshalerDecodeValue is the ValueDecoderFunc for Unmarshaler implementations.
Deprecated: Use bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) ValueUnmarshalerDecodeValue
deprecated
func (dvd DefaultValueDecoders) ValueUnmarshalerDecodeValue(_ DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
ValueUnmarshalerDecodeValue is the ValueDecoderFunc for ValueUnmarshaler implementations.
Deprecated: Use bson.NewRegistry to get a registry with all default value decoders registered.
type DefaultValueEncoders
deprecated
type DefaultValueEncoders struct{}
DefaultValueEncoders is a namespace type for the default ValueEncoders used when creating a registry.
Deprecated: Use bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) ArrayEncodeValue
deprecated
func (dve DefaultValueEncoders) ArrayEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
ArrayEncodeValue is the ValueEncoderFunc for array types.
Deprecated: Use bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) BinaryEncodeValue
deprecated
func (DefaultValueEncoders) BinaryEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
BinaryEncodeValue is the ValueEncoderFunc for Binary.
Deprecated: Use bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) BooleanEncodeValue
deprecated
func (dve DefaultValueEncoders) BooleanEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
BooleanEncodeValue is the ValueEncoderFunc for bool types.
Deprecated: Use bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) ByteSliceEncodeValue
deprecated
func (dve DefaultValueEncoders) ByteSliceEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
ByteSliceEncodeValue is the ValueEncoderFunc for []byte.
Deprecated: ByteSliceEncodeValue is not registered by default. Use ByteSliceCodec.EncodeValue instead.
func (DefaultValueEncoders) CodeWithScopeEncodeValue
deprecated
func (dve DefaultValueEncoders) CodeWithScopeEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
CodeWithScopeEncodeValue is the ValueEncoderFunc for CodeWithScope.
Deprecated: Use bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) CoreDocumentEncodeValue
deprecated
func (DefaultValueEncoders) CoreDocumentEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
CoreDocumentEncodeValue is the ValueEncoderFunc for bsoncore.Document.
Deprecated: Use bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) DBPointerEncodeValue
deprecated
func (DefaultValueEncoders) DBPointerEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
DBPointerEncodeValue is the ValueEncoderFunc for DBPointer.
Deprecated: Use bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) DateTimeEncodeValue
deprecated
func (DefaultValueEncoders) DateTimeEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
DateTimeEncodeValue is the ValueEncoderFunc for DateTime.
Deprecated: Use bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) Decimal128EncodeValue
deprecated
func (dve DefaultValueEncoders) Decimal128EncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
Decimal128EncodeValue is the ValueEncoderFunc for primitive.Decimal128.
Deprecated: Use bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) EmptyInterfaceEncodeValue
deprecated
func (dve DefaultValueEncoders) EmptyInterfaceEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
EmptyInterfaceEncodeValue is the ValueEncoderFunc for interface{}.
Deprecated: EmptyInterfaceEncodeValue is not registered by default. Use EmptyInterfaceCodec.EncodeValue instead.
func (DefaultValueEncoders) FloatEncodeValue
deprecated
func (dve DefaultValueEncoders) FloatEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
FloatEncodeValue is the ValueEncoderFunc for float types.
Deprecated: Use bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) IntEncodeValue
deprecated
func (dve DefaultValueEncoders) IntEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
IntEncodeValue is the ValueEncoderFunc for int types.
Deprecated: Use bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) JSONNumberEncodeValue
deprecated
func (dve DefaultValueEncoders) JSONNumberEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
JSONNumberEncodeValue is the ValueEncoderFunc for json.Number.
Deprecated: Use bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) JavaScriptEncodeValue
deprecated
func (DefaultValueEncoders) JavaScriptEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
JavaScriptEncodeValue is the ValueEncoderFunc for the primitive.JavaScript type.
Deprecated: Use bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) MapEncodeValue
deprecated
func (dve DefaultValueEncoders) MapEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
MapEncodeValue is the ValueEncoderFunc for map[string]* types.
Deprecated: MapEncodeValue is not registered by default. Use MapCodec.EncodeValue instead.
func (DefaultValueEncoders) MarshalerEncodeValue
deprecated
func (dve DefaultValueEncoders) MarshalerEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
MarshalerEncodeValue is the ValueEncoderFunc for Marshaler implementations.
Deprecated: Use bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) MaxKeyEncodeValue
deprecated
func (DefaultValueEncoders) MaxKeyEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
MaxKeyEncodeValue is the ValueEncoderFunc for MaxKey.
Deprecated: Use bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) MinKeyEncodeValue
deprecated
func (DefaultValueEncoders) MinKeyEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
MinKeyEncodeValue is the ValueEncoderFunc for MinKey.
Deprecated: Use bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) NullEncodeValue
deprecated
func (DefaultValueEncoders) NullEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
NullEncodeValue is the ValueEncoderFunc for Null.
Deprecated: Use bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) ObjectIDEncodeValue
deprecated
func (dve DefaultValueEncoders) ObjectIDEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
ObjectIDEncodeValue is the ValueEncoderFunc for primitive.ObjectID.
Deprecated: Use bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) ProxyEncodeValue
deprecated
func (dve DefaultValueEncoders) ProxyEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
ProxyEncodeValue is the ValueEncoderFunc for Proxy implementations.
Deprecated: Use bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) RegexEncodeValue
deprecated
func (DefaultValueEncoders) RegexEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
RegexEncodeValue is the ValueEncoderFunc for Regex.
Deprecated: Use bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) RegisterDefaultEncoders
deprecated
func (dve DefaultValueEncoders) RegisterDefaultEncoders(rb *RegistryBuilder)
RegisterDefaultEncoders will register the encoder methods attached to DefaultValueEncoders with the provided RegistryBuilder.
Deprecated: Use bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) SliceEncodeValue
deprecated
func (dve DefaultValueEncoders) SliceEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
SliceEncodeValue is the ValueEncoderFunc for slice types.
Deprecated: SliceEncodeValue is not registered by default. Use SliceCodec.EncodeValue instead.
func (DefaultValueEncoders) StringEncodeValue
deprecated
func (dve DefaultValueEncoders) StringEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
StringEncodeValue is the ValueEncoderFunc for string types.
Deprecated: StringEncodeValue is not registered by default. Use StringCodec.EncodeValue instead.
func (DefaultValueEncoders) SymbolEncodeValue
deprecated
func (DefaultValueEncoders) SymbolEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
SymbolEncodeValue is the ValueEncoderFunc for the primitive.Symbol type.
Deprecated: Use bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) TimeEncodeValue
deprecated
func (dve DefaultValueEncoders) TimeEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
TimeEncodeValue is the ValueEncoderFunc for time.TIme.
Deprecated: TimeEncodeValue is not registered by default. Use TimeCodec.EncodeValue instead.
func (DefaultValueEncoders) TimestampEncodeValue
deprecated
func (DefaultValueEncoders) TimestampEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
TimestampEncodeValue is the ValueEncoderFunc for Timestamp.
Deprecated: Use bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) URLEncodeValue
deprecated
func (dve DefaultValueEncoders) URLEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
URLEncodeValue is the ValueEncoderFunc for url.URL.
Deprecated: Use bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) UintEncodeValue
deprecated
func (dve DefaultValueEncoders) UintEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
UintEncodeValue is the ValueEncoderFunc for uint types.
Deprecated: UintEncodeValue is not registered by default. Use UintCodec.EncodeValue instead.
func (DefaultValueEncoders) UndefinedEncodeValue
deprecated
func (DefaultValueEncoders) UndefinedEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
UndefinedEncodeValue is the ValueEncoderFunc for Undefined.
Deprecated: Use bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) ValueMarshalerEncodeValue
deprecated
func (dve DefaultValueEncoders) ValueMarshalerEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
ValueMarshalerEncodeValue is the ValueEncoderFunc for ValueMarshaler implementations.
Deprecated: Use bson.NewRegistry to get a registry with all default value encoders registered.
type EmptyInterfaceCodec ¶
type EmptyInterfaceCodec struct {
DecodeBinaryAsSlice bool
}
EmptyInterfaceCodec is the Codec used for interface{} values.
func NewEmptyInterfaceCodec ¶
func NewEmptyInterfaceCodec(opts ...*bsonoptions.EmptyInterfaceCodecOptions) *EmptyInterfaceCodec
NewEmptyInterfaceCodec returns a EmptyInterfaceCodec with options opts.
func (EmptyInterfaceCodec) DecodeValue ¶
func (eic EmptyInterfaceCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DecodeValue is the ValueDecoderFunc for interface{}.
func (EmptyInterfaceCodec) EncodeValue ¶
func (eic EmptyInterfaceCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
EncodeValue is the ValueEncoderFunc for interface{}.
type EncodeContext ¶
EncodeContext is the contextual information required for a Codec to encode a value.
type ErrNoDecoder ¶
ErrNoDecoder is returned when there wasn't a decoder available for a type.
func (ErrNoDecoder) Error ¶
func (end ErrNoDecoder) Error() string
type ErrNoEncoder ¶
ErrNoEncoder is returned when there wasn't an encoder available for a type.
func (ErrNoEncoder) Error ¶
func (ene ErrNoEncoder) Error() string
type ErrNoTypeMapEntry ¶
ErrNoTypeMapEntry is returned when there wasn't a type available for the provided BSON type.
func (ErrNoTypeMapEntry) Error ¶
func (entme ErrNoTypeMapEntry) Error() string
type KeyMarshaler ¶
KeyMarshaler is the interface implemented by an object that can marshal itself into a string key. This applies to types used as map keys and is similar to encoding.TextMarshaler.
type KeyUnmarshaler ¶
KeyUnmarshaler is the interface implemented by an object that can unmarshal a string representation of itself. This applies to types used as map keys and is similar to encoding.TextUnmarshaler.
UnmarshalKey must be able to decode the form generated by MarshalKey. UnmarshalKey must copy the text if it wishes to retain the text after returning.
type MapCodec ¶
MapCodec is the Codec used for map values.
func NewMapCodec ¶
func NewMapCodec(opts ...*bsonoptions.MapCodecOptions) *MapCodec
NewMapCodec returns a MapCodec with options opts.
func (*MapCodec) DecodeValue ¶
func (mc *MapCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DecodeValue is the ValueDecoder for map[string/decimal]* types.
func (*MapCodec) EncodeValue ¶
func (mc *MapCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
EncodeValue is the ValueEncoder for map[*]* types.
type Marshaler
deprecated
type PointerCodec ¶
type PointerCodec struct {
// contains filtered or unexported fields
}
PointerCodec is the Codec used for pointers.
func NewPointerCodec ¶
func NewPointerCodec() *PointerCodec
NewPointerCodec returns a PointerCodec that has been initialized.
func (*PointerCodec) DecodeValue ¶
func (pc *PointerCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DecodeValue handles decoding a pointer by looking up a decoder for the type it points to and using that to decode. If the BSON value is Null, this method will set the pointer to nil.
func (*PointerCodec) EncodeValue ¶
func (pc *PointerCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
EncodeValue handles encoding a pointer by either encoding it to BSON Null if the pointer is nil or looking up an encoder for the type of value the pointer points to.
type Proxy ¶
type Proxy interface {
ProxyBSON() (interface{}, error)
}
Proxy is an interface implemented by types that cannot themselves be directly encoded. Types that implement this interface with have ProxyBSON called during the encoding process and that value will be encoded in place for the implementer.
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.
Example (CustomDecoder) ¶
package main import ( "fmt" "reflect" "github.com/BlackMocca/mongo-go-driver/bson" "github.com/BlackMocca/mongo-go-driver/bson/bsoncodec" "github.com/BlackMocca/mongo-go-driver/bson/bsonrw" "github.com/BlackMocca/mongo-go-driver/bson/bsontype" ) func main() { // Create a custom decoder for a boolean type that can be stored in the // database as a BSON boolean, int32, or int64. For our custom decoder, BSON // int32 or int64 values are considered "true" if they are non-zero. type lenientBool bool lenientBoolType := reflect.TypeOf(lenientBool(true)) lenientBoolDecoder := func( dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value, ) error { // All decoder implementations should check that val is valid, settable, // and is of the correct kind before proceeding. if !val.IsValid() || !val.CanSet() || val.Type() != lenientBoolType { return bsoncodec.ValueDecoderError{ Name: "lenientBoolDecoder", Types: []reflect.Type{lenientBoolType}, Received: val, } } var result bool switch vr.Type() { case bsontype.Boolean: b, err := vr.ReadBoolean() if err != nil { return err } result = b case bsontype.Int32: i32, err := vr.ReadInt32() if err != nil { return err } result = i32 != 0 case bsontype.Int64: i64, err := vr.ReadInt64() if err != nil { return err } result = i64 != 0 default: return fmt.Errorf( "received invalid BSON type to decode into lenientBool: %s", vr.Type()) } val.SetBool(result) return nil } reg := bson.NewRegistry() reg.RegisterTypeDecoder( lenientBoolType, bsoncodec.ValueDecoderFunc(lenientBoolDecoder)) // Marshal a BSON document with a single field "isOK" that is a non-zero // integer value. b, err := bson.Marshal(bson.M{"isOK": 1}) if err != nil { panic(err) } // Now try to decode the BSON document to a struct with a field "IsOK" that // is type lenientBool. Expect that the non-zero integer value is decoded // as boolean true. type MyDocument struct { IsOK lenientBool `bson:"isOK"` } var doc MyDocument err = bson.UnmarshalWithRegistry(reg, b, &doc) if err != nil { panic(err) } fmt.Printf("%+v\n", doc) }
Output: {IsOK:true}
Example (CustomEncoder) ¶
package main import ( "fmt" "math" "reflect" "github.com/BlackMocca/mongo-go-driver/bson" "github.com/BlackMocca/mongo-go-driver/bson/bsoncodec" "github.com/BlackMocca/mongo-go-driver/bson/bsonrw" ) func main() { // Create a custom encoder for an integer type that multiplies the input // value by -1 when encoding. type negatedInt int negatedIntType := reflect.TypeOf(negatedInt(0)) negatedIntEncoder := func( ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value, ) error { // All encoder implementations should check that val is valid and is of // the correct type before proceeding. if !val.IsValid() || val.Type() != negatedIntType { return bsoncodec.ValueEncoderError{ Name: "negatedIntEncoder", Types: []reflect.Type{negatedIntType}, Received: val, } } // Negate val and encode as a BSON int32 if it can fit in 32 bits and a // BSON int64 otherwise. negatedVal := val.Int() * -1 if math.MinInt32 <= negatedVal && negatedVal <= math.MaxInt32 { return vw.WriteInt32(int32(negatedVal)) } return vw.WriteInt64(negatedVal) } reg := bson.NewRegistry() reg.RegisterTypeEncoder( negatedIntType, bsoncodec.ValueEncoderFunc(negatedIntEncoder)) // Define a document that includes both int and negatedInt fields with the // same value. type myDocument struct { Int int NegatedInt negatedInt } doc := myDocument{ Int: 1, NegatedInt: 1, } // Marshal the document as BSON. Expect that the int field is encoded to the // same value and that the negatedInt field is encoded as the negated value. b, err := bson.MarshalWithRegistry(reg, doc) if err != nil { panic(err) } fmt.Println(bson.Raw(b).String()) }
Output: {"int": {"$numberInt":"1"},"negatedint": {"$numberInt":"-1"}}
func (*Registry) LookupDecoder ¶
func (r *Registry) LookupDecoder(valueType reflect.Type) (ValueDecoder, error)
LookupDecoder returns the first matching decoder in the Registry. It uses the following lookup order:
1. A decoder registered for the exact type. If the given type is an interface, a decoder registered using RegisterTypeDecoder for that interface will be selected.
2. A decoder registered using RegisterInterfaceDecoder for an interface implemented by the type or by a pointer to the type.
3. A decoder registered using RegisterKindDecoder for the kind of value.
If no decoder is found, an error of type ErrNoDecoder is returned. LookupDecoder is safe for concurrent use by multiple goroutines after all codecs and decoders are registered.
func (*Registry) LookupEncoder ¶
func (r *Registry) LookupEncoder(valueType reflect.Type) (ValueEncoder, error)
LookupEncoder returns the first matching encoder in the Registry. It uses the following lookup order:
1. An encoder registered for the exact type. If the given type is an interface, an encoder registered using RegisterTypeEncoder for that interface will be selected.
2. An encoder registered using RegisterInterfaceEncoder for an interface implemented by the type or by a pointer to the type.
3. An encoder registered using RegisterKindEncoder for the kind of value.
If no encoder is found, an error of type ErrNoEncoder is returned. LookupEncoder is safe for concurrent use by multiple goroutines after all codecs and encoders are registered.
func (*Registry) LookupTypeMapEntry ¶
LookupTypeMapEntry inspects the registry's type map for a Go type for the corresponding BSON type. If no type is found, ErrNoTypeMapEntry is returned.
LookupTypeMapEntry should not be called concurrently with any other Registry method.
func (*Registry) RegisterInterfaceDecoder ¶
func (r *Registry) RegisterInterfaceDecoder(iface reflect.Type, dec ValueDecoder)
RegisterInterfaceDecoder registers an decoder for the provided interface type iface. This decoder will be called when unmarshaling into a type if the type implements iface or a pointer to the type implements iface. If the provided type is not an interface (i.e. iface.Kind() != reflect.Interface), this method will panic.
RegisterInterfaceDecoder should not be called concurrently with any other Registry method.
func (*Registry) RegisterInterfaceEncoder ¶
func (r *Registry) RegisterInterfaceEncoder(iface reflect.Type, enc ValueEncoder)
RegisterInterfaceEncoder registers an encoder for the provided interface type iface. This encoder will be called when marshaling a type if the type implements iface or a pointer to the type implements iface. If the provided type is not an interface (i.e. iface.Kind() != reflect.Interface), this method will panic.
RegisterInterfaceEncoder should not be called concurrently with any other Registry method.
func (*Registry) RegisterKindDecoder ¶
func (r *Registry) RegisterKindDecoder(kind reflect.Kind, dec ValueDecoder)
RegisterKindDecoder registers the provided ValueDecoder for the provided kind.
Use RegisterKindDecoder to register a decoder for any type with the same underlying kind. For example, consider the type MyInt defined as
type MyInt int32
To define an decoder for MyInt and int32, use RegisterKindDecoder like
reg.RegisterKindDecoder(reflect.Int32, myDecoder)
RegisterKindDecoder should not be called concurrently with any other Registry method.
Example ¶
package main import ( "fmt" "reflect" "github.com/BlackMocca/mongo-go-driver/bson" "github.com/BlackMocca/mongo-go-driver/bson/bsoncodec" "github.com/BlackMocca/mongo-go-driver/bson/bsonrw" "github.com/BlackMocca/mongo-go-driver/bson/bsontype" ) func main() { // Create a custom decoder that can decode any integer value, including // integer values encoded as floating point numbers, to any Go type // with underlying type int64. To do that, we register the decoder as a // "kind" decoder for kind reflect.Int64. That way, we can even decode to // user-defined types with underlying type int64. flexibleInt64KindDecoder := func( dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value, ) error { // All decoder implementations should check that val is valid, settable, // and is of the correct kind before proceeding. if !val.IsValid() || !val.CanSet() || val.Kind() != reflect.Int64 { return bsoncodec.ValueDecoderError{ Name: "flexibleInt64KindDecoder", Kinds: []reflect.Kind{reflect.Int64}, Received: val, } } var result int64 switch vr.Type() { case bsontype.Int64: i64, err := vr.ReadInt64() if err != nil { return err } result = i64 case bsontype.Int32: i32, err := vr.ReadInt32() if err != nil { return err } result = int64(i32) case bsontype.Double: d, err := vr.ReadDouble() if err != nil { return err } i64 := int64(d) // Make sure the double field is an integer value. if d != float64(i64) { return fmt.Errorf("double %f is not an integer value", d) } result = i64 default: return fmt.Errorf( "received invalid BSON type to decode into int64: %s", vr.Type()) } val.SetInt(result) return nil } reg := bson.NewRegistry() reg.RegisterKindDecoder( reflect.Int64, bsoncodec.ValueDecoderFunc(flexibleInt64KindDecoder)) // Marshal a BSON document with fields that are mixed numeric types but all // hold integer values (i.e. values with no fractional part). b, err := bson.Marshal(bson.M{"myInt": float64(8), "int64": int32(9)}) if err != nil { panic(err) } // Now try to decode the BSON document to a struct with fields // that is type int32. Expect that the float value is successfully decoded. type myInt int64 type myDocument struct { MyInt myInt Int64 int64 } var doc myDocument err = bson.UnmarshalWithRegistry(reg, b, &doc) if err != nil { panic(err) } fmt.Printf("%+v\n", doc) }
Output: {MyInt:8 Int64:9}
func (*Registry) RegisterKindEncoder ¶
func (r *Registry) RegisterKindEncoder(kind reflect.Kind, enc ValueEncoder)
RegisterKindEncoder registers the provided ValueEncoder for the provided kind.
Use RegisterKindEncoder to register an encoder for any type with the same underlying kind. For example, consider the type MyInt defined as
type MyInt int32
To define an encoder for MyInt and int32, use RegisterKindEncoder like
reg.RegisterKindEncoder(reflect.Int32, myEncoder)
RegisterKindEncoder should not be called concurrently with any other Registry method.
Example ¶
package main import ( "fmt" "reflect" "github.com/BlackMocca/mongo-go-driver/bson" "github.com/BlackMocca/mongo-go-driver/bson/bsoncodec" "github.com/BlackMocca/mongo-go-driver/bson/bsonrw" ) func main() { // Create a custom encoder that writes any Go type that has underlying type // int32 as an a BSON int64. To do that, we register the encoder as a "kind" // encoder for kind reflect.Int32. That way, even user-defined types with // underlying type int32 will be encoded as a BSON int64. int32To64Encoder := func( ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value, ) error { // All encoder implementations should check that val is valid and is of // the correct type or kind before proceeding. if !val.IsValid() || val.Kind() != reflect.Int32 { return bsoncodec.ValueEncoderError{ Name: "int32To64Encoder", Kinds: []reflect.Kind{reflect.Int32}, Received: val, } } return vw.WriteInt64(val.Int()) } // Create a default registry and register our int32-to-int64 encoder for // kind reflect.Int32. reg := bson.NewRegistry() reg.RegisterKindEncoder( reflect.Int32, bsoncodec.ValueEncoderFunc(int32To64Encoder)) // Define a document that includes an int32, an int64, and a user-defined // type "myInt" that has underlying type int32. type myInt int32 type myDocument struct { MyInt myInt Int32 int32 Int64 int64 } doc := myDocument{ Int32: 1, Int64: 1, MyInt: 1, } // Marshal the document as BSON. Expect that all fields are encoded as BSON // int64 (represented as "$numberLong" when encoded as Extended JSON). b, err := bson.MarshalWithRegistry(reg, doc) if err != nil { panic(err) } fmt.Println(bson.Raw(b).String()) }
Output: {"myint": {"$numberLong":"1"},"int32": {"$numberLong":"1"},"int64": {"$numberLong":"1"}}
func (*Registry) RegisterTypeDecoder ¶
func (r *Registry) RegisterTypeDecoder(valueType reflect.Type, dec ValueDecoder)
RegisterTypeDecoder registers the provided ValueDecoder for the provided type.
The type will be used as provided, so a decoder can be registered for a type and a different decoder can be registered for a pointer to that type.
If the given type is an interface, the decoder will be called when unmarshaling into a type that is that interface. It will not be called when unmarshaling into a non-interface type that implements the interface. To get the latter behavior, call RegisterHookDecoder instead.
RegisterTypeDecoder should not be called concurrently with any other Registry method.
func (*Registry) RegisterTypeEncoder ¶
func (r *Registry) RegisterTypeEncoder(valueType reflect.Type, enc ValueEncoder)
RegisterTypeEncoder registers the provided ValueEncoder for the provided type.
The type will be used as provided, so an encoder can be registered for a type and a different encoder can be registered for a pointer to that type.
If the given type is an interface, the encoder will be called when marshaling a type that is that interface. It will not be called when marshaling a non-interface type that implements the interface. To get the latter behavior, call RegisterHookEncoder instead.
RegisterTypeEncoder should not be called concurrently with any other Registry method.
func (*Registry) RegisterTypeMapEntry ¶
RegisterTypeMapEntry will register the provided type to the BSON type. The primary usage for this mapping is decoding situations where an empty interface is used and a default type needs to be created and decoded into.
By default, BSON documents will decode into interface{} values as bson.D. To change the default type for BSON documents, a type map entry for bsontype.EmbeddedDocument should be registered. For example, to force BSON documents to decode to bson.Raw, use the following code:
reg.RegisterTypeMapEntry(bsontype.EmbeddedDocument, reflect.TypeOf(bson.Raw{}))
type RegistryBuilder
deprecated
type RegistryBuilder struct {
// contains filtered or unexported fields
}
A RegistryBuilder is used to build a Registry. This type is not goroutine safe.
Deprecated: Use Registry instead.
func NewRegistryBuilder
deprecated
func NewRegistryBuilder() *RegistryBuilder
NewRegistryBuilder creates a new empty RegistryBuilder.
Deprecated: Use NewRegistry instead.
func (*RegistryBuilder) Build
deprecated
func (rb *RegistryBuilder) Build() *Registry
Build creates a Registry from the current state of this RegistryBuilder.
Deprecated: Use NewRegistry instead.
func (*RegistryBuilder) RegisterCodec
deprecated
func (rb *RegistryBuilder) RegisterCodec(t reflect.Type, codec ValueCodec) *RegistryBuilder
RegisterCodec will register the provided ValueCodec for the provided type.
Deprecated: Use Registry.RegisterTypeEncoder and Registry.RegisterTypeDecoder instead.
func (*RegistryBuilder) RegisterDecoder
deprecated
func (rb *RegistryBuilder) RegisterDecoder(t reflect.Type, dec ValueDecoder) *RegistryBuilder
RegisterDecoder registers the provided type and decoder pair.
Deprecated: Use Registry.RegisterTypeDecoder or Registry.RegisterInterfaceDecoder instead.
func (*RegistryBuilder) RegisterDefaultDecoder
deprecated
func (rb *RegistryBuilder) RegisterDefaultDecoder(kind reflect.Kind, dec ValueDecoder) *RegistryBuilder
RegisterDefaultDecoder will register the provided ValueDecoder to the provided kind.
Deprecated: Use Registry.RegisterKindDecoder instead.
func (*RegistryBuilder) RegisterDefaultEncoder
deprecated
func (rb *RegistryBuilder) RegisterDefaultEncoder(kind reflect.Kind, enc ValueEncoder) *RegistryBuilder
RegisterDefaultEncoder will register the provided ValueEncoder to the provided kind.
Deprecated: Use Registry.RegisterKindEncoder instead.
func (*RegistryBuilder) RegisterEncoder
deprecated
func (rb *RegistryBuilder) RegisterEncoder(t reflect.Type, enc ValueEncoder) *RegistryBuilder
RegisterEncoder registers the provided type and encoder pair.
Deprecated: Use Registry.RegisterTypeEncoder or Registry.RegisterInterfaceEncoder instead.
func (*RegistryBuilder) RegisterHookDecoder
deprecated
func (rb *RegistryBuilder) RegisterHookDecoder(t reflect.Type, dec ValueDecoder) *RegistryBuilder
RegisterHookDecoder will register an decoder for the provided interface type t. This decoder will be called when unmarshaling into a type if the type implements t or a pointer to the type implements t. If the provided type is not an interface (i.e. t.Kind() != reflect.Interface), this method will panic.
Deprecated: Use Registry.RegisterInterfaceDecoder instead.
func (*RegistryBuilder) RegisterHookEncoder
deprecated
func (rb *RegistryBuilder) RegisterHookEncoder(t reflect.Type, enc ValueEncoder) *RegistryBuilder
RegisterHookEncoder will register an encoder for the provided interface type t. This encoder will be called when marshaling a type if the type implements t or a pointer to the type implements t. If the provided type is not an interface (i.e. t.Kind() != reflect.Interface), this method will panic.
Deprecated: Use Registry.RegisterInterfaceEncoder instead.
func (*RegistryBuilder) RegisterTypeDecoder
deprecated
func (rb *RegistryBuilder) RegisterTypeDecoder(t reflect.Type, dec ValueDecoder) *RegistryBuilder
RegisterTypeDecoder will register the provided ValueDecoder for the provided type.
The type will be used directly, so a decoder can be registered for a type and a different decoder can be registered for a pointer to that type.
If the given type is an interface, the decoder will be called when unmarshaling into a type that is that interface. It will not be called when unmarshaling into a non-interface type that implements the interface.
Deprecated: Use Registry.RegisterTypeDecoder instead.
func (*RegistryBuilder) RegisterTypeEncoder
deprecated
func (rb *RegistryBuilder) RegisterTypeEncoder(t reflect.Type, enc ValueEncoder) *RegistryBuilder
RegisterTypeEncoder will register the provided ValueEncoder for the provided type.
The type will be used directly, so an encoder can be registered for a type and a different encoder can be registered for a pointer to that type.
If the given type is an interface, the encoder will be called when marshaling a type that is that interface. It will not be called when marshaling a non-interface type that implements the interface.
Deprecated: Use Registry.RegisterTypeEncoder instead.
func (*RegistryBuilder) RegisterTypeMapEntry
deprecated
func (rb *RegistryBuilder) RegisterTypeMapEntry(bt bsontype.Type, rt reflect.Type) *RegistryBuilder
RegisterTypeMapEntry will register the provided type to the BSON type. The primary usage for this mapping is decoding situations where an empty interface is used and a default type needs to be created and decoded into.
By default, BSON documents will decode into interface{} values as bson.D. To change the default type for BSON documents, a type map entry for bsontype.EmbeddedDocument should be registered. For example, to force BSON documents to decode to bson.Raw, use the following code:
rb.RegisterTypeMapEntry(bsontype.EmbeddedDocument, reflect.TypeOf(bson.Raw{}))
Deprecated: Use Registry.RegisterTypeMapEntry instead.
type SliceCodec ¶
type SliceCodec struct {
EncodeNilAsEmpty bool
}
SliceCodec is the Codec used for slice values.
func NewSliceCodec ¶
func NewSliceCodec(opts ...*bsonoptions.SliceCodecOptions) *SliceCodec
NewSliceCodec returns a MapCodec with options opts.
func (*SliceCodec) DecodeValue ¶
func (sc *SliceCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DecodeValue is the ValueDecoder for slice types.
func (SliceCodec) EncodeValue ¶
func (sc SliceCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
EncodeValue is the ValueEncoder for slice types.
type StringCodec ¶
type StringCodec struct {
DecodeObjectIDAsHex bool
}
StringCodec is the Codec used for string values.
func NewStringCodec ¶
func NewStringCodec(opts ...*bsonoptions.StringCodecOptions) *StringCodec
NewStringCodec returns a StringCodec with options opts.
func (*StringCodec) DecodeValue ¶
func (sc *StringCodec) DecodeValue(dctx DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DecodeValue is the ValueDecoder for string types.
func (*StringCodec) EncodeValue ¶
func (sc *StringCodec) EncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
EncodeValue is the ValueEncoder for string types.
type StructCodec ¶
type StructCodec struct { DecodeZeroStruct bool DecodeDeepZeroInline bool EncodeOmitDefaultStruct bool AllowUnexportedFields bool OverwriteDuplicatedInlinedFields bool // contains filtered or unexported fields }
StructCodec is the Codec used for struct values.
func NewStructCodec ¶
func NewStructCodec(p StructTagParser, opts ...*bsonoptions.StructCodecOptions) (*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, val reflect.Value) error
DecodeValue implements the Codec interface. By default, map types in val will not be cleared. If a map has existing key/value pairs, it will be extended with the new ones from vr. For slices, the decoder will set the length of the slice to zero and append all elements. The underlying array will not be cleared.
func (*StructCodec) EncodeValue ¶
func (sc *StructCodec) EncodeValue(r EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) 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) } return parseTags(key, tag) }
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.
var JSONFallbackStructTagParser StructTagParserFunc = func(sf reflect.StructField) (StructTags, error) { key := strings.ToLower(sf.Name) tag, ok := sf.Tag.Lookup("bson") if !ok { tag, ok = sf.Tag.Lookup("json") } if !ok && !strings.Contains(string(sf.Tag), ":") && len(sf.Tag) > 0 { tag = string(sf.Tag) } return parseTags(key, tag) }
JSONFallbackStructTagParser has the same behavior as DefaultStructTagParser but will also fallback to parsing the json tag instead on a field where the bson tag isn't available.
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.
type TimeCodec ¶
type TimeCodec struct {
UseLocalTimeZone bool
}
TimeCodec is the Codec used for time.Time values.
func NewTimeCodec ¶
func NewTimeCodec(opts ...*bsonoptions.TimeCodecOptions) *TimeCodec
NewTimeCodec returns a TimeCodec with options opts.
func (*TimeCodec) DecodeValue ¶
func (tc *TimeCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DecodeValue is the ValueDecoderFunc for time.Time.
func (*TimeCodec) EncodeValue ¶
func (tc *TimeCodec) EncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
EncodeValue is the ValueEncoderFunc for time.TIme.
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 UIntCodec ¶
type UIntCodec struct {
EncodeToMinSize bool
}
UIntCodec is the Codec used for uint values.
func NewUIntCodec ¶
func NewUIntCodec(opts ...*bsonoptions.UIntCodecOptions) *UIntCodec
NewUIntCodec returns a UIntCodec with options opts.
func (*UIntCodec) DecodeValue ¶
func (uic *UIntCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DecodeValue is the ValueDecoder for uint types.
func (*UIntCodec) EncodeValue ¶
func (uic *UIntCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
EncodeValue is the ValueEncoder for uint types.
type Unmarshaler
deprecated
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.
Deprecated: Use bson.Unmarshaler instead.
type ValueCodec
deprecated
type ValueCodec interface { ValueEncoder ValueDecoder }
ValueCodec is an interface for encoding and decoding a reflect.Value. values.
Deprecated: Use ValueEncoder and ValueDecoder instead.
type ValueDecoder ¶
type ValueDecoder interface {
DecodeValue(DecodeContext, bsonrw.ValueReader, reflect.Value) error
}
ValueDecoder is the interface implemented by types that can handle the decoding of a value.
Example ¶
var _ ValueDecoderFunc = func(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { if !val.CanSet() || val.Kind() != reflect.String { return ValueDecoderError{Name: "StringDecodeValue", Kinds: []reflect.Kind{reflect.String}, Received: val} } if vr.Type() != bsontype.String { return fmt.Errorf("cannot decode %v into a string type", vr.Type()) } str, err := vr.ReadString() if err != nil { return err } val.SetString(str) return nil }
Output:
type ValueDecoderError ¶
type ValueDecoderError struct { Name string Types []reflect.Type Kinds []reflect.Kind Received reflect.Value }
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, reflect.Value) 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 reflect.Value) error
DecodeValue implements the ValueDecoder interface.
type ValueEncoder ¶
type ValueEncoder interface {
EncodeValue(EncodeContext, bsonrw.ValueWriter, reflect.Value) error
}
ValueEncoder is the interface implemented by types that can handle the encoding of a value.
Example ¶
var _ ValueEncoderFunc = func(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error { if val.Kind() != reflect.String { return ValueEncoderError{Name: "StringEncodeValue", Kinds: []reflect.Kind{reflect.String}, Received: val} } return vw.WriteString(val.String()) }
Output:
type ValueEncoderError ¶
type ValueEncoderError struct { Name string Types []reflect.Type Kinds []reflect.Kind Received reflect.Value }
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, reflect.Value) 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 reflect.Value) error
EncodeValue implements the ValueEncoder interface.
type ValueMarshaler
deprecated
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.
Deprecated: Use bson.ValueMarshaler instead.
type ValueUnmarshaler
deprecated
ValueUnmarshaler is an interface implemented by types that can unmarshal a BSON value representation 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.
Deprecated: Use bson.ValueUnmarshaler instead.
Source Files ¶
- array_codec.go
- bsoncodec.go
- byte_slice_codec.go
- cond_addr_codec.go
- default_value_decoders.go
- default_value_encoders.go
- doc.go
- empty_interface_codec.go
- map_codec.go
- mode.go
- pointer_codec.go
- proxy.go
- registry.go
- slice_codec.go
- string_codec.go
- struct_codec.go
- struct_tag_parser.go
- time_codec.go
- types.go
- uint_codec.go