ErrNilType is returned when nil is passed to either LookupEncoder or LookupDecoder.
Deprecated: ErrNilType will not be supported in Go Driver 2.0.
var ErrNilType = errors.New("cannot perform a decoder lookup on <nil>")
ErrNotInterface is returned when the provided type is not an interface.
Deprecated: ErrNotInterface will not be supported in Go Driver 2.0.
var ErrNotInterface = errors.New("The provided type is not an interface")
ErrNotPointer is returned when a non-pointer type is provided to LookupDecoder.
Deprecated: ErrNotPointer will not be supported in Go Driver 2.0.
var ErrNotPointer = errors.New("non-pointer provided to LookupDecoder")
ArrayCodec is the Codec used for bsoncore.Array values.
Deprecated: ArrayCodec will not be directly accessible in Go Driver 2.0.
type ArrayCodec struct{}
func NewArrayCodec() *ArrayCodec
NewArrayCodec returns an ArrayCodec.
Deprecated: NewArrayCodec will not be available in Go Driver 2.0. See ArrayCodec for more details.
func (ac *ArrayCodec) DecodeValue(_ DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DecodeValue is the ValueDecoder for bsoncore.Array values.
func (ac *ArrayCodec) EncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
EncodeValue is the ValueEncoder for bsoncore.Array values.
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.
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 }
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 (bsc *ByteSliceCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DecodeValue is the ValueDecoder for []byte.
func (bsc *ByteSliceCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
EncodeValue is the ValueEncoder for []byte.
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 CodecZeroer interface { IsTypeZero(interface{}) bool }
DecodeContext is the contextual information required for a Codec to decode a value.
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 }
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 (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 (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 (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 (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 (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 (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.
DecodeError represents an error that occurs when unmarshalling BSON bytes into a native Go type.
type DecodeError struct {
// contains filtered or unexported fields
}
func (de *DecodeError) Error() string
Error implements the error interface.
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 (de *DecodeError) Unwrap() error
Unwrap returns the underlying error
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.
type DefaultValueDecoders struct{}
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 (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 (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 (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 (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(_ 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 (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 (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 (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 (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 (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 (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 (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 (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 (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 (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 (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 (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 (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 (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 (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 (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 (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 (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 (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 (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 (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 (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 (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 (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 (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 (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.
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.
type DefaultValueEncoders struct{}
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(_ 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 (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 (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 (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(_ 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(_ 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(_ 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 (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 (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 (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 (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 (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(_ 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 (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 (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(_ 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(_ 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(_ 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 (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 (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(_ 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 (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 (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 (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(_ 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 (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(_ 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 (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 (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(_ 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 (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.
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.
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 }
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 (eic EmptyInterfaceCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DecodeValue is the ValueDecoderFunc for interface{}.
func (eic EmptyInterfaceCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
EncodeValue is the ValueEncoderFunc for interface{}.
EncodeContext is the contextual information required for a Codec to encode a value.
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 }
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 (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 (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 (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 (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 (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 (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.
ErrNoDecoder is returned when there wasn't a decoder available for a type.
Deprecated: ErrNoDecoder will not be supported in Go Driver 2.0.
type ErrNoDecoder struct { Type reflect.Type }
func (end ErrNoDecoder) Error() string
ErrNoEncoder is returned when there wasn't an encoder available for a type.
Deprecated: ErrNoEncoder will not be supported in Go Driver 2.0.
type ErrNoEncoder struct { Type reflect.Type }
func (ene ErrNoEncoder) Error() string
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.
type ErrNoTypeMapEntry struct { Type bsontype.Type }
func (entme ErrNoTypeMapEntry) Error() string
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 KeyMarshaler interface { MarshalKey() (key string, err error) }
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 KeyUnmarshaler interface { UnmarshalKey(key string) error }
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.
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 }
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 (mc *MapCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DecodeValue is the ValueDecoder for map[string/decimal]* types.
func (mc *MapCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
EncodeValue is the ValueEncoder for map[*]* types.
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 Marshaler interface { MarshalBSON() ([]byte, error) }
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)
type PointerCodec struct {
// contains filtered or unexported fields
}
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 (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 (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.
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 Proxy interface { ProxyBSON() (interface{}, error) }
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.
type Registry struct {
// contains filtered or unexported fields
}
▹ Example (CustomDecoder)
▹ Example (CustomEncoder)
func NewRegistry() *Registry
NewRegistry creates a new empty Registry.
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 (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 (r *Registry) LookupTypeMapEntry(bt bsontype.Type) (reflect.Type, error)
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 (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 (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 (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
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
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 (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 (r *Registry) RegisterTypeMapEntry(bt bsontype.Type, rt reflect.Type)
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{}))
A RegistryBuilder is used to build a Registry. This type is not goroutine safe.
Deprecated: Use Registry instead.
type RegistryBuilder struct {
// contains filtered or unexported fields
}
func NewRegistryBuilder() *RegistryBuilder
NewRegistryBuilder creates a new empty RegistryBuilder.
Deprecated: Use NewRegistry instead.
func (rb *RegistryBuilder) Build() *Registry
Build creates a Registry from the current state of this RegistryBuilder.
Deprecated: Use NewRegistry instead.
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 (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 (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 (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 (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 (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 (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 (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 (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 (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.
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.
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 }
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 (sc *SliceCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DecodeValue is the ValueDecoder for slice types.
func (sc SliceCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
EncodeValue is the ValueEncoder for slice types.
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)
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 }
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 (sc *StringCodec) DecodeValue(dctx DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DecodeValue is the ValueDecoder for string types.
func (sc *StringCodec) EncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
EncodeValue is the ValueEncoder for string types.
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.
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 }
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 (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 (sc *StructCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
EncodeValue handles encoding generic struct types.
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 StructTagParser interface { ParseStructTags(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.
type StructTagParserFunc func(reflect.StructField) (StructTags, error)
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 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) }
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.
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) }
func (stpf StructTagParserFunc) ParseStructTags(sf reflect.StructField) (StructTags, error)
ParseStructTags implements the StructTagParser interface.
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 StructTags struct { Name string OmitEmpty bool MinSize bool Truncate bool Inline bool Skip 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.
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 }
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 (tc *TimeCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DecodeValue is the ValueDecoderFunc for time.Time.
func (tc *TimeCodec) EncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
EncodeValue is the ValueEncoderFunc for time.TIme.
TransitionError is an error returned when an invalid progressing a ValueReader or ValueWriter state machine occurs.
type TransitionError struct {
// contains filtered or unexported fields
}
func (te TransitionError) Error() string
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.
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 }
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 (uic *UIntCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DecodeValue is the ValueDecoder for uint types.
func (uic *UIntCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
EncodeValue is the ValueEncoder for uint types.
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 Unmarshaler interface { UnmarshalBSON([]byte) error }
ValueCodec is an interface for encoding and decoding a reflect.Value. values.
Deprecated: Use ValueEncoder and ValueDecoder instead.
type ValueCodec interface { ValueEncoder ValueDecoder }
ValueDecoder is the interface implemented by types that can handle the decoding of a value.
type ValueDecoder interface { DecodeValue(DecodeContext, bsonrw.ValueReader, reflect.Value) error }
▹ Example
ValueDecoderError is an error returned from a ValueDecoder when the provided value can't be decoded by the ValueDecoder.
type ValueDecoderError struct { Name string Types []reflect.Type Kinds []reflect.Kind Received reflect.Value }
func (vde ValueDecoderError) Error() string
ValueDecoderFunc is an adapter function that allows a function with the correct signature to be used as a ValueDecoder.
type ValueDecoderFunc func(DecodeContext, bsonrw.ValueReader, reflect.Value) error
func (fn ValueDecoderFunc) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error
DecodeValue implements the ValueDecoder interface.
ValueEncoder is the interface implemented by types that can handle the encoding of a value.
type ValueEncoder interface { EncodeValue(EncodeContext, bsonrw.ValueWriter, reflect.Value) error }
▹ Example
ValueEncoderError is an error returned from a ValueEncoder when the provided value can't be encoded by the ValueEncoder.
type ValueEncoderError struct { Name string Types []reflect.Type Kinds []reflect.Kind Received reflect.Value }
func (vee ValueEncoderError) Error() string
ValueEncoderFunc is an adapter function that allows a function with the correct signature to be used as a ValueEncoder.
type ValueEncoderFunc func(EncodeContext, bsonrw.ValueWriter, reflect.Value) error
func (fn ValueEncoderFunc) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error
EncodeValue implements the ValueEncoder interface.
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 ValueMarshaler interface { MarshalBSONValue() (bsontype.Type, []byte, error) }
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.
type ValueUnmarshaler interface { UnmarshalBSONValue(bsontype.Type, []byte) error }
Zeroer allows custom struct types to implement a report of zero state. All struct types that don't implement Zeroer or where IsZero returns false are considered to be not zero.
type Zeroer interface { IsZero() bool }