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 ArrayCodecdeprecated
- func NewArrayCodec() *ArrayCodecdeprecated
- type ByteSliceCodecdeprecated
- type CodecZeroerdeprecated
- type DecodeContext
- func (dc *DecodeContext) BinaryAsSlice()deprecated
- func (dc *DecodeContext) DefaultDocumentD()deprecated
- func (dc *DecodeContext) DefaultDocumentM()deprecated
- func (dc *DecodeContext) UseJSONStructTags()deprecated
- func (dc *DecodeContext) UseLocalTimeZone()deprecated
- func (dc *DecodeContext) ZeroMaps()deprecated
- func (dc *DecodeContext) ZeroStructs()deprecated
- 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 EmptyInterfaceCodecdeprecated
- type EncodeContext
- func (ec *EncodeContext) ErrorOnInlineDuplicates()deprecated
- func (ec *EncodeContext) NilByteSliceAsEmpty()deprecated
- func (ec *EncodeContext) NilMapAsEmpty()deprecated
- func (ec *EncodeContext) NilSliceAsEmpty()deprecated
- func (ec *EncodeContext) OmitZeroStruct()deprecated
- func (ec *EncodeContext) StringifyMapKeysWithFmt()deprecated
- func (ec *EncodeContext) UseJSONStructTags()deprecated
- type ErrNoDecoderdeprecated
- type ErrNoEncoderdeprecated
- type ErrNoTypeMapEntrydeprecated
- type KeyMarshaler
- type KeyUnmarshaler
- type MapCodecdeprecated
- type Marshalerdeprecated
- type PointerCodecdeprecated
- func NewPointerCodec() *PointerCodecdeprecated
- 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 SliceCodecdeprecated
- type StringCodecdeprecated
- type StructCodecdeprecated
- type StructTagParserdeprecated
- type StructTagParserFuncdeprecated
- type StructTagsdeprecated
- type TimeCodecdeprecated
- type TransitionError
- type UIntCodecdeprecated
- 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.
Deprecated: ErrNilType will not be supported in Go Driver 2.0.
var ErrNotInterface = errors.New("The provided type is not an interface")
ErrNotInterface is returned when the provided type is not an interface.
Deprecated: ErrNotInterface will not be supported in Go Driver 2.0.
var ErrNotPointer = errors.New("non-pointer provided to LookupDecoder")
ErrNotPointer is returned when a non-pointer type is provided to LookupDecoder.
Deprecated: ErrNotPointer will not be supported in Go Driver 2.0.
Functions ¶
This section is empty.
Types ¶
type ArrayCodec
deprecated
added in
v1.5.0
type ArrayCodec struct{}
ArrayCodec is the Codec used for bsoncore.Array values.
Deprecated: ArrayCodec will not be directly accessible in Go Driver 2.0.
func NewArrayCodec
deprecated
added in
v1.5.0
func NewArrayCodec() *ArrayCodec
NewArrayCodec returns an ArrayCodec.
Deprecated: NewArrayCodec will not be available in Go Driver 2.0. See ArrayCodec for more details.
func (*ArrayCodec) DecodeValue ¶ added in v1.5.0
func (ac *ArrayCodec) DecodeValue(_ DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DecodeValue is the ValueDecoder for bsoncore.Array values.
func (*ArrayCodec) EncodeValue ¶ added in v1.5.0
func (ac *ArrayCodec) EncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
EncodeValue is the ValueEncoder for bsoncore.Array values.
type ByteSliceCodec
deprecated
added in
v1.3.0
type ByteSliceCodec struct { // EncodeNilAsEmpty causes EncodeValue to marshal nil Go byte slices as empty BSON binary values // instead of BSON null. // // Deprecated: Use bson.Encoder.NilByteSliceAsEmpty or options.BSONOptions.NilByteSliceAsEmpty // instead. EncodeNilAsEmpty bool }
ByteSliceCodec is the Codec used for []byte values.
Deprecated: ByteSliceCodec will not be directly configurable in Go Driver 2.0. To configure the byte slice encode and decode behavior, use the configuration methods on a go.mongodb.org/mongo-driver/bson.Encoder or go.mongodb.org/mongo-driver/bson.Decoder. To configure the byte slice encode and decode behavior for a mongo.Client, use go.mongodb.org/mongo-driver/mongo/options.ClientOptions.SetBSONOptions.
For example, to configure a mongo.Client to encode nil byte slices as empty BSON binary values, use:
opt := options.Client().SetBSONOptions(&options.BSONOptions{ NilByteSliceAsEmpty: true, })
See the deprecation notice for each field in ByteSliceCodec for the corresponding settings.
func NewByteSliceCodec
deprecated
added in
v1.3.0
func NewByteSliceCodec(opts ...*bsonoptions.ByteSliceCodecOptions) *ByteSliceCodec
NewByteSliceCodec returns a ByteSliceCodec with options opts.
Deprecated: NewByteSliceCodec will not be available in Go Driver 2.0. See ByteSliceCodec for more details.
func (*ByteSliceCodec) DecodeValue ¶ added in v1.3.0
func (bsc *ByteSliceCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DecodeValue is the ValueDecoder for []byte.
func (*ByteSliceCodec) EncodeValue ¶ added in v1.3.0
func (bsc *ByteSliceCodec) EncodeValue(ec 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, if true, instructs decoders to to truncate the fractional part of BSON "double" // values when attempting to unmarshal them into a Go integer (int, int8, int16, int32, int64, // uint, uint8, uint16, uint32, or uint64) struct field. The truncation logic does not apply to // BSON "decimal128" values. // // Deprecated: Use bson.Decoder.AllowTruncatingDoubles instead. 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 bson.Decoder.DefaultDocumentM or bson.Decoder.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) BinaryAsSlice
deprecated
added in
v1.12.0
func (dc *DecodeContext) BinaryAsSlice()
BinaryAsSlice causes the Decoder to unmarshal BSON binary field values that are the "Generic" or "Old" BSON binary subtype as a Go byte slice instead of a primitive.Binary.
Deprecated: Use go.mongodb.org/mongo-driver/bson.Decoder.BinaryAsSlice instead.
func (*DecodeContext) DefaultDocumentD
deprecated
added in
v1.10.0
func (dc *DecodeContext) DefaultDocumentD()
DefaultDocumentD causes the Decoder to always unmarshal documents into the primitive.D type. This behavior is restricted to data typed as "interface{}" or "map[string]interface{}".
Deprecated: Use go.mongodb.org/mongo-driver/bson.Decoder.DefaultDocumentD instead.
func (*DecodeContext) DefaultDocumentM
deprecated
added in
v1.10.0
func (dc *DecodeContext) DefaultDocumentM()
DefaultDocumentM causes the Decoder to always unmarshal documents into the primitive.M type. This behavior is restricted to data typed as "interface{}" or "map[string]interface{}".
Deprecated: Use go.mongodb.org/mongo-driver/bson.Decoder.DefaultDocumentM instead.
func (*DecodeContext) UseJSONStructTags
deprecated
added in
v1.12.0
func (dc *DecodeContext) UseJSONStructTags()
UseJSONStructTags causes the Decoder to fall back to using the "json" struct tag if a "bson" struct tag is not specified.
Deprecated: Use go.mongodb.org/mongo-driver/bson.Decoder.UseJSONStructTags instead.
func (*DecodeContext) UseLocalTimeZone
deprecated
added in
v1.12.0
func (dc *DecodeContext) UseLocalTimeZone()
UseLocalTimeZone causes the Decoder to unmarshal time.Time values in the local timezone instead of the UTC timezone.
Deprecated: Use go.mongodb.org/mongo-driver/bson.Decoder.UseLocalTimeZone instead.
func (*DecodeContext) ZeroMaps
deprecated
added in
v1.12.0
func (dc *DecodeContext) ZeroMaps()
ZeroMaps causes the Decoder to delete any existing values from Go maps in the destination value passed to Decode before unmarshaling BSON documents into them.
Deprecated: Use go.mongodb.org/mongo-driver/bson.Decoder.ZeroMaps instead.
func (*DecodeContext) ZeroStructs
deprecated
added in
v1.12.0
func (dc *DecodeContext) ZeroStructs()
ZeroStructs causes the Decoder to delete any existing values from Go structs in the destination value passed to Decode before unmarshaling BSON documents into them.
Deprecated: Use go.mongodb.org/mongo-driver/bson.Decoder.ZeroStructs instead.
type DecodeError ¶ added in v1.4.0
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 ¶ added in v1.4.0
func (de *DecodeError) Error() string
Error implements the error interface.
func (*DecodeError) Keys ¶ added in v1.4.0
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 ¶ added in v1.4.0
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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) CoreDocumentDecodeValue
deprecated
added in
v0.1.0
func (DefaultValueDecoders) CoreDocumentDecodeValue(_ DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
CoreDocumentDecodeValue is the ValueDecoderFunc for bsoncore.Document.
Deprecated: Use go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) DDecodeValue
deprecated
added in
v1.5.0
func (dvd DefaultValueDecoders) DDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DDecodeValue is the ValueDecoderFunc for primitive.D instances.
Deprecated: Use go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) JavaScriptDecodeValue
deprecated
added in
v0.1.0
func (dvd DefaultValueDecoders) JavaScriptDecodeValue(dctx DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
JavaScriptDecodeValue is the ValueDecoderFunc for the primitive.JavaScript type.
Deprecated: Use go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) RegisterDefaultDecoders
deprecated
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{} 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 go.mongodb.org/mongo-driver/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
added in
v0.1.0
func (dvd DefaultValueDecoders) SymbolDecodeValue(dctx DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
SymbolDecodeValue is the ValueDecoderFunc for the primitive.Symbol type.
Deprecated: Use go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/bson.NewRegistry to get a registry with all default value decoders registered.
func (DefaultValueDecoders) UnmarshalerDecodeValue
deprecated
added in
v0.1.0
func (dvd DefaultValueDecoders) UnmarshalerDecodeValue(_ DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
UnmarshalerDecodeValue is the ValueDecoderFunc for Unmarshaler implementations.
Deprecated: Use go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) CoreDocumentEncodeValue
deprecated
added in
v0.1.0
func (DefaultValueEncoders) CoreDocumentEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
CoreDocumentEncodeValue is the ValueEncoderFunc for bsoncore.Document.
Deprecated: Use go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) JavaScriptEncodeValue
deprecated
added in
v0.1.0
func (DefaultValueEncoders) JavaScriptEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
JavaScriptEncodeValue is the ValueEncoderFunc for the primitive.JavaScript type.
Deprecated: Use go.mongodb.org/mongo-driver/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
added in
v0.1.0
func (dve DefaultValueEncoders) MarshalerEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
MarshalerEncodeValue is the ValueEncoderFunc for Marshaler implementations.
Deprecated: Use go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) ProxyEncodeValue
deprecated
added in
v0.0.18
func (dve DefaultValueEncoders) ProxyEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
ProxyEncodeValue is the ValueEncoderFunc for Proxy implementations.
Deprecated: Use go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/bson.NewRegistry to get a registry with all default value encoders registered.
func (DefaultValueEncoders) RegisterDefaultEncoders
deprecated
added in
v0.0.17
func (dve DefaultValueEncoders) RegisterDefaultEncoders(rb *RegistryBuilder)
RegisterDefaultEncoders will register the encoder methods attached to DefaultValueEncoders with the provided RegistryBuilder.
Deprecated: Use go.mongodb.org/mongo-driver/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
added in
v0.1.0
func (DefaultValueEncoders) SymbolEncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
SymbolEncodeValue is the ValueEncoderFunc for the primitive.Symbol type.
Deprecated: Use go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/bson.NewRegistry to get a registry with all default value encoders registered.
type EmptyInterfaceCodec
deprecated
added in
v1.3.0
type EmptyInterfaceCodec struct { // DecodeBinaryAsSlice causes DecodeValue to unmarshal BSON binary field values that are the // "Generic" or "Old" BSON binary subtype as a Go byte slice instead of a primitive.Binary. // // Deprecated: Use bson.Decoder.BinaryAsSlice or options.BSONOptions.BinaryAsSlice instead. DecodeBinaryAsSlice bool }
EmptyInterfaceCodec is the Codec used for interface{} values.
Deprecated: EmptyInterfaceCodec will not be directly configurable in Go Driver 2.0. To configure the empty interface encode and decode behavior, use the configuration methods on a go.mongodb.org/mongo-driver/bson.Encoder or go.mongodb.org/mongo-driver/bson.Decoder. To configure the empty interface encode and decode behavior for a mongo.Client, use go.mongodb.org/mongo-driver/mongo/options.ClientOptions.SetBSONOptions.
For example, to configure a mongo.Client to unmarshal BSON binary field values as a Go byte slice, use:
opt := options.Client().SetBSONOptions(&options.BSONOptions{ BinaryAsSlice: true, })
See the deprecation notice for each field in EmptyInterfaceCodec for the corresponding settings.
func NewEmptyInterfaceCodec
deprecated
added in
v1.3.0
func NewEmptyInterfaceCodec(opts ...*bsonoptions.EmptyInterfaceCodecOptions) *EmptyInterfaceCodec
NewEmptyInterfaceCodec returns a EmptyInterfaceCodec with options opts.
Deprecated: NewEmptyInterfaceCodec will not be available in Go Driver 2.0. See EmptyInterfaceCodec for more details.
func (EmptyInterfaceCodec) DecodeValue ¶ added in v1.3.0
func (eic EmptyInterfaceCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DecodeValue is the ValueDecoderFunc for interface{}.
func (EmptyInterfaceCodec) EncodeValue ¶ added in v1.3.0
func (eic EmptyInterfaceCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
EncodeValue is the ValueEncoderFunc for interface{}.
type EncodeContext ¶
type EncodeContext struct { *Registry // MinSize causes the Encoder to marshal Go integer values (int, int8, int16, int32, int64, // uint, uint8, uint16, uint32, or uint64) as the minimum BSON int size (either 32 or 64 bits) // that can represent the integer value. // // Deprecated: Use bson.Encoder.IntMinSize instead. MinSize bool // contains filtered or unexported fields }
EncodeContext is the contextual information required for a Codec to encode a value.
func (*EncodeContext) ErrorOnInlineDuplicates
deprecated
added in
v1.12.0
func (ec *EncodeContext) ErrorOnInlineDuplicates()
ErrorOnInlineDuplicates causes the Encoder to return an error if there is a duplicate field in the marshaled BSON when the "inline" struct tag option is set.
Deprecated: Use go.mongodb.org/mongo-driver/bson.Encoder.ErrorOnInlineDuplicates instead.
func (*EncodeContext) NilByteSliceAsEmpty
deprecated
added in
v1.12.0
func (ec *EncodeContext) NilByteSliceAsEmpty()
NilByteSliceAsEmpty causes the Encoder to marshal nil Go byte slices as empty BSON binary values instead of BSON null.
Deprecated: Use go.mongodb.org/mongo-driver/bson.Encoder.NilByteSliceAsEmpty instead.
func (*EncodeContext) NilMapAsEmpty
deprecated
added in
v1.12.0
func (ec *EncodeContext) NilMapAsEmpty()
NilMapAsEmpty causes the Encoder to marshal nil Go maps as empty BSON documents instead of BSON null.
Deprecated: Use go.mongodb.org/mongo-driver/bson.Encoder.NilMapAsEmpty instead.
func (*EncodeContext) NilSliceAsEmpty
deprecated
added in
v1.12.0
func (ec *EncodeContext) NilSliceAsEmpty()
NilSliceAsEmpty causes the Encoder to marshal nil Go slices as empty BSON arrays instead of BSON null.
Deprecated: Use go.mongodb.org/mongo-driver/bson.Encoder.NilSliceAsEmpty instead.
func (*EncodeContext) OmitZeroStruct
deprecated
added in
v1.12.0
func (ec *EncodeContext) OmitZeroStruct()
OmitZeroStruct causes the Encoder to consider the zero value for a struct (e.g. MyStruct{}) as empty and omit it from the marshaled BSON when the "omitempty" struct tag option is set.
Note that the Encoder only examines exported struct fields when determining if a struct is the zero value. It considers pointers to a zero struct value (e.g. &MyStruct{}) not empty.
Deprecated: Use go.mongodb.org/mongo-driver/bson.Encoder.OmitZeroStruct instead.
func (*EncodeContext) StringifyMapKeysWithFmt
deprecated
added in
v1.12.0
func (ec *EncodeContext) StringifyMapKeysWithFmt()
StringifyMapKeysWithFmt causes the Encoder to convert Go map keys to BSON document field name strings using fmt.Sprintf() instead of the default string conversion logic.
Deprecated: Use go.mongodb.org/mongo-driver/bson.Encoder.StringifyMapKeysWithFmt instead.
func (*EncodeContext) UseJSONStructTags
deprecated
added in
v1.12.0
func (ec *EncodeContext) UseJSONStructTags()
UseJSONStructTags causes the Encoder to fall back to using the "json" struct tag if a "bson" struct tag is not specified.
Deprecated: Use go.mongodb.org/mongo-driver/bson.Encoder.UseJSONStructTags instead.
type ErrNoDecoder
deprecated
type ErrNoEncoder
deprecated
type ErrNoTypeMapEntry
deprecated
added in
v0.1.0
ErrNoTypeMapEntry is returned when there wasn't a type available for the provided BSON type.
Deprecated: ErrNoTypeMapEntry will not be supported in Go Driver 2.0.
func (ErrNoTypeMapEntry) Error ¶ added in v0.1.0
func (entme ErrNoTypeMapEntry) Error() string
type KeyMarshaler ¶ added in v1.4.0
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 ¶ added in v1.4.0
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
deprecated
added in
v1.2.0
type MapCodec struct { // DecodeZerosMap causes DecodeValue to delete any existing values from Go maps in the destination // value passed to Decode before unmarshaling BSON documents into them. // // Deprecated: Use bson.Decoder.ZeroMaps or options.BSONOptions.ZeroMaps instead. DecodeZerosMap bool // EncodeNilAsEmpty causes EncodeValue to marshal nil Go maps as empty BSON documents instead of // BSON null. // // Deprecated: Use bson.Encoder.NilMapAsEmpty or options.BSONOptions.NilMapAsEmpty instead. EncodeNilAsEmpty bool // EncodeKeysWithStringer causes the Encoder to convert Go map keys to BSON document field name // strings using fmt.Sprintf() instead of the default string conversion logic. // // Deprecated: Use bson.Encoder.StringifyMapKeysWithFmt or // options.BSONOptions.StringifyMapKeysWithFmt instead. EncodeKeysWithStringer bool }
MapCodec is the Codec used for map values.
Deprecated: MapCodec will not be directly configurable in Go Driver 2.0. To configure the map encode and decode behavior, use the configuration methods on a go.mongodb.org/mongo-driver/bson.Encoder or go.mongodb.org/mongo-driver/bson.Decoder. To configure the map encode and decode behavior for a mongo.Client, use go.mongodb.org/mongo-driver/mongo/options.ClientOptions.SetBSONOptions.
For example, to configure a mongo.Client to marshal nil Go maps as empty BSON documents, use:
opt := options.Client().SetBSONOptions(&options.BSONOptions{ NilMapAsEmpty: true, })
See the deprecation notice for each field in MapCodec for the corresponding settings.
func NewMapCodec
deprecated
added in
v1.2.0
func NewMapCodec(opts ...*bsonoptions.MapCodecOptions) *MapCodec
NewMapCodec returns a MapCodec with options opts.
Deprecated: NewMapCodec will not be available in Go Driver 2.0. See MapCodec for more details.
func (*MapCodec) DecodeValue ¶ added in v1.2.0
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 ¶ added in v1.2.0
func (mc *MapCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
EncodeValue is the ValueEncoder for map[*]* types.
type Marshaler
deprecated
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.
Deprecated: Use go.mongodb.org/mongo-driver/bson.Marshaler instead.
type PointerCodec
deprecated
added in
v0.1.0
type PointerCodec struct {
// contains filtered or unexported fields
}
PointerCodec is the Codec used for pointers.
Deprecated: PointerCodec will not be directly accessible in Go Driver 2.0. To override the default pointer encode and decode behavior, create a new registry with go.mongodb.org/mongo-driver/bson.NewRegistry and register a new encoder and decoder for pointers.
For example,
reg := bson.NewRegistry() reg.RegisterKindEncoder(reflect.Ptr, myPointerEncoder) reg.RegisterKindDecoder(reflect.Ptr, myPointerDecoder)
func NewPointerCodec
deprecated
added in
v0.1.0
func NewPointerCodec() *PointerCodec
NewPointerCodec returns a PointerCodec that has been initialized.
Deprecated: NewPointerCodec will not be available in Go Driver 2.0. See PointerCodec for more details.
func (*PointerCodec) DecodeValue ¶ added in v0.1.0
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 ¶ added in v0.1.0
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 ¶ added in v0.0.18
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" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/bsoncodec" "go.mongodb.org/mongo-driver/bson/bsonrw" "go.mongodb.org/mongo-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" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/bsoncodec" "go.mongodb.org/mongo-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 NewRegistry ¶ added in v1.12.0
func NewRegistry() *Registry
NewRegistry creates a new empty Registry.
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 ¶ added in v0.1.0
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 ¶ added in v1.12.0
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 ¶ added in v1.12.0
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 ¶ added in v1.12.0
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" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/bsoncodec" "go.mongodb.org/mongo-driver/bson/bsonrw" "go.mongodb.org/mongo-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 ¶ added in v1.12.0
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" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/bsoncodec" "go.mongodb.org/mongo-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 ¶ added in v1.12.0
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 ¶ added in v1.12.0
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 ¶ added in v1.12.0
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
added in
v1.3.0
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
added in
v1.3.0
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
added in
v1.3.0
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
added in
v1.3.0
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
added in
v0.1.0
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
deprecated
added in
v1.3.0
type SliceCodec struct { // EncodeNilAsEmpty causes EncodeValue to marshal nil Go slices as empty BSON arrays instead of // BSON null. // // Deprecated: Use bson.Encoder.NilSliceAsEmpty instead. EncodeNilAsEmpty bool }
SliceCodec is the Codec used for slice values.
Deprecated: SliceCodec will not be directly configurable in Go Driver 2.0. To configure the slice encode and decode behavior, use the configuration methods on a go.mongodb.org/mongo-driver/bson.Encoder or go.mongodb.org/mongo-driver/bson.Decoder. To configure the slice encode and decode behavior for a mongo.Client, use go.mongodb.org/mongo-driver/mongo/options.ClientOptions.SetBSONOptions.
For example, to configure a mongo.Client to marshal nil Go slices as empty BSON arrays, use:
opt := options.Client().SetBSONOptions(&options.BSONOptions{ NilSliceAsEmpty: true, })
See the deprecation notice for each field in SliceCodec for the corresponding settings.
func NewSliceCodec
deprecated
added in
v1.3.0
func NewSliceCodec(opts ...*bsonoptions.SliceCodecOptions) *SliceCodec
NewSliceCodec returns a MapCodec with options opts.
Deprecated: NewSliceCodec will not be available in Go Driver 2.0. See SliceCodec for more details.
func (*SliceCodec) DecodeValue ¶ added in v1.3.0
func (sc *SliceCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DecodeValue is the ValueDecoder for slice types.
func (SliceCodec) EncodeValue ¶ added in v1.3.0
func (sc SliceCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
EncodeValue is the ValueEncoder for slice types.
type StringCodec
deprecated
added in
v1.2.0
type StringCodec struct { // DecodeObjectIDAsHex specifies if object IDs should be decoded as their hex representation. // If false, a string made from the raw object ID bytes will be used. Defaults to true. // // Deprecated: Decoding object IDs as raw bytes will not be supported in Go Driver 2.0. DecodeObjectIDAsHex bool }
StringCodec is the Codec used for string values.
Deprecated: StringCodec will not be directly accessible in Go Driver 2.0. To override the default string encode and decode behavior, create a new registry with go.mongodb.org/mongo-driver/bson.NewRegistry and register a new encoder and decoder for strings.
For example,
reg := bson.NewRegistry() reg.RegisterKindEncoder(reflect.String, myStringEncoder) reg.RegisterKindDecoder(reflect.String, myStringDecoder)
func NewStringCodec
deprecated
added in
v1.2.0
func NewStringCodec(opts ...*bsonoptions.StringCodecOptions) *StringCodec
NewStringCodec returns a StringCodec with options opts.
Deprecated: NewStringCodec will not be available in Go Driver 2.0. See StringCodec for more details.
func (*StringCodec) DecodeValue ¶ added in v1.2.0
func (sc *StringCodec) DecodeValue(dctx DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DecodeValue is the ValueDecoder for string types.
func (*StringCodec) EncodeValue ¶ added in v1.2.0
func (sc *StringCodec) EncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
EncodeValue is the ValueEncoder for string types.
type StructCodec
deprecated
type StructCodec struct { // DecodeZeroStruct causes DecodeValue to delete any existing values from Go structs in the // destination value passed to Decode before unmarshaling BSON documents into them. // // Deprecated: Use bson.Decoder.ZeroStructs or options.BSONOptions.ZeroStructs instead. DecodeZeroStruct bool // DecodeDeepZeroInline causes DecodeValue to delete any existing values from Go structs in the // destination value passed to Decode before unmarshaling BSON documents into them. // // Deprecated: DecodeDeepZeroInline will not be supported in Go Driver 2.0. DecodeDeepZeroInline bool // EncodeOmitDefaultStruct causes the Encoder to consider the zero value for a struct (e.g. // MyStruct{}) as empty and omit it from the marshaled BSON when the "omitempty" struct tag // option is set. // // Deprecated: Use bson.Encoder.OmitZeroStruct or options.BSONOptions.OmitZeroStruct instead. EncodeOmitDefaultStruct bool // AllowUnexportedFields allows encoding and decoding values from un-exported struct fields. // // Deprecated: AllowUnexportedFields does not work on recent versions of Go and will not be // supported in Go Driver 2.0. AllowUnexportedFields bool // OverwriteDuplicatedInlinedFields, if false, causes EncodeValue to return an error if there is // a duplicate field in the marshaled BSON when the "inline" struct tag option is set. The // default value is true. // // Deprecated: Use bson.Encoder.ErrorOnInlineDuplicates or // options.BSONOptions.ErrorOnInlineDuplicates instead. OverwriteDuplicatedInlinedFields bool // contains filtered or unexported fields }
StructCodec is the Codec used for struct values.
Deprecated: StructCodec will not be directly configurable in Go Driver 2.0. To configure the struct encode and decode behavior, use the configuration methods on a go.mongodb.org/mongo-driver/bson.Encoder or go.mongodb.org/mongo-driver/bson.Decoder. To configure the struct encode and decode behavior for a mongo.Client, use go.mongodb.org/mongo-driver/mongo/options.ClientOptions.SetBSONOptions.
For example, to configure a mongo.Client to omit zero-value structs when using the "omitempty" struct tag, use:
opt := options.Client().SetBSONOptions(&options.BSONOptions{ OmitZeroStruct: true, })
See the deprecation notice for each field in StructCodec for the corresponding settings.
func NewStructCodec
deprecated
func NewStructCodec(p StructTagParser, opts ...*bsonoptions.StructCodecOptions) (*StructCodec, error)
NewStructCodec returns a StructCodec that uses p for struct tag parsing.
Deprecated: NewStructCodec will not be available in Go Driver 2.0. See StructCodec for more details.
func (*StructCodec) DecodeValue ¶
func (sc *StructCodec) DecodeValue(dc 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(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
EncodeValue handles encoding generic struct types.
type StructTagParser
deprecated
type StructTagParser interface {
ParseStructTags(reflect.StructField) (StructTags, error)
}
StructTagParser returns the struct tags for a given struct field.
Deprecated: Defining custom BSON struct tag parsers will not be supported in Go Driver 2.0.
type StructTagParserFunc
deprecated
type StructTagParserFunc func(reflect.StructField) (StructTags, error)
StructTagParserFunc is an adapter that allows a generic function to be used as a StructTagParser.
Deprecated: Defining custom BSON struct tag parsers will not be supported in Go Driver 2.0.
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.
Deprecated: DefaultStructTagParser will be removed in Go Driver 2.0.
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.
Deprecated: Use go.mongodb.org/mongo-driver/bson.Encoder.UseJSONStructTags and go.mongodb.org/mongo-driver/bson.Decoder.UseJSONStructTags instead.
func (StructTagParserFunc) ParseStructTags ¶
func (stpf StructTagParserFunc) ParseStructTags(sf reflect.StructField) (StructTags, error)
ParseStructTags implements the StructTagParser interface.
type StructTags
deprecated
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.
Deprecated: Defining custom BSON struct tag parsers will not be supported in Go Driver 2.0.
type TimeCodec
deprecated
added in
v1.2.0
type TimeCodec struct { // UseLocalTimeZone specifies if we should decode into the local time zone. Defaults to false. // // Deprecated: Use bson.Decoder.UseLocalTimeZone or options.BSONOptions.UseLocalTimeZone // instead. UseLocalTimeZone bool }
TimeCodec is the Codec used for time.Time values.
Deprecated: TimeCodec will not be directly configurable in Go Driver 2.0. To configure the time.Time encode and decode behavior, use the configuration methods on a go.mongodb.org/mongo-driver/bson.Encoder or go.mongodb.org/mongo-driver/bson.Decoder. To configure the time.Time encode and decode behavior for a mongo.Client, use go.mongodb.org/mongo-driver/mongo/options.ClientOptions.SetBSONOptions.
For example, to configure a mongo.Client to ..., use:
opt := options.Client().SetBSONOptions(&options.BSONOptions{ UseLocalTimeZone: true, })
See the deprecation notice for each field in TimeCodec for the corresponding settings.
func NewTimeCodec
deprecated
added in
v1.2.0
func NewTimeCodec(opts ...*bsonoptions.TimeCodecOptions) *TimeCodec
NewTimeCodec returns a TimeCodec with options opts.
Deprecated: NewTimeCodec will not be available in Go Driver 2.0. See TimeCodec for more details.
func (*TimeCodec) DecodeValue ¶ added in v1.2.0
func (tc *TimeCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DecodeValue is the ValueDecoderFunc for time.Time.
func (*TimeCodec) EncodeValue ¶ added in v1.2.0
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
deprecated
added in
v1.3.0
type UIntCodec struct { // EncodeToMinSize causes EncodeValue to marshal Go uint values (excluding uint64) as the // minimum BSON int size (either 32-bit or 64-bit) that can represent the integer value. // // Deprecated: Use bson.Encoder.IntMinSize or options.BSONOptions.IntMinSize instead. EncodeToMinSize bool }
UIntCodec is the Codec used for uint values.
Deprecated: UIntCodec will not be directly configurable in Go Driver 2.0. To configure the uint encode and decode behavior, use the configuration methods on a go.mongodb.org/mongo-driver/bson.Encoder or go.mongodb.org/mongo-driver/bson.Decoder. To configure the uint encode and decode behavior for a mongo.Client, use go.mongodb.org/mongo-driver/mongo/options.ClientOptions.SetBSONOptions.
For example, to configure a mongo.Client to marshal Go uint values as the minimum BSON int size that can represent the value, use:
opt := options.Client().SetBSONOptions(&options.BSONOptions{ IntMinSize: true, })
See the deprecation notice for each field in UIntCodec for the corresponding settings.
func NewUIntCodec
deprecated
added in
v1.3.0
func NewUIntCodec(opts ...*bsonoptions.UIntCodecOptions) *UIntCodec
NewUIntCodec returns a UIntCodec with options opts.
Deprecated: NewUIntCodec will not be available in Go Driver 2.0. See UIntCodec for more details.
func (*UIntCodec) DecodeValue ¶ added in v1.3.0
func (uic *UIntCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DecodeValue is the ValueDecoder for uint types.
func (*UIntCodec) EncodeValue ¶ added in v1.3.0
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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/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 go.mongodb.org/mongo-driver/bson.ValueUnmarshaler instead.
Source Files ¶
- array_codec.go
- bsoncodec.go
- byte_slice_codec.go
- codec_cache.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