diff --git a/bson/bsoncodec.go b/bson/bsoncodec.go index b7aaadf2c2..e3ae365fd4 100644 --- a/bson/bsoncodec.go +++ b/bson/bsoncodec.go @@ -77,12 +77,10 @@ func (vde ValueDecoderError) Error() string { type EncodeContext struct { *Registry - // MinSize causes the Encoder to marshal Go integer values (int, int8, int16, int32, int64, + // 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 + minSize bool errorOnInlineDuplicates bool stringifyMapKeysWithFmt bool @@ -93,85 +91,16 @@ type EncodeContext struct { useJSONStructTags bool } -// 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) ErrorOnInlineDuplicates() { - ec.errorOnInlineDuplicates = true -} - -// 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) StringifyMapKeysWithFmt() { - ec.stringifyMapKeysWithFmt = true -} - -// 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) NilMapAsEmpty() { - ec.nilMapAsEmpty = true -} - -// 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) NilSliceAsEmpty() { - ec.nilSliceAsEmpty = true -} - -// 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) NilByteSliceAsEmpty() { - ec.nilByteSliceAsEmpty = true -} - -// 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) OmitZeroStruct() { - ec.omitZeroStruct = true -} - -// 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. -func (ec *EncodeContext) UseJSONStructTags() { - ec.useJSONStructTags = true -} - // 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 + // 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 + ancestor reflect.Type // defaultDocumentType specifies the Go type to decode top-level and nested BSON documents into. In particular, the // usage for this field is restricted to data typed as "interface{}" or "map[string]interface{}". If DocumentType is @@ -179,6 +108,12 @@ type DecodeContext struct { // error. DocumentType overrides the Ancestor field. defaultDocumentType reflect.Type + // 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. + truncate bool + binaryAsSlice bool decodeObjectIDAsHex bool useJSONStructTags bool @@ -187,85 +122,13 @@ type DecodeContext struct { zeroStructs bool } -// 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 Binary. -// -// Deprecated: Use [go.mongodb.org/mongo-driver/bson.Decoder.BinaryAsSlice] instead. -func (dc *DecodeContext) BinaryAsSlice() { - dc.binaryAsSlice = true -} - -// DecodeObjectIDAsHex causes the Decoder to unmarshal BSON ObjectID as a hexadecimal string. -// -// Deprecated: Use [go.mongodb.org/mongo-driver/bson.Decoder.DecodeObjectIDAsHex] instead. -func (dc *DecodeContext) DecodeObjectIDAsHex() { - dc.decodeObjectIDAsHex = true -} - -// 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) UseJSONStructTags() { - dc.useJSONStructTags = true -} - -// 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) UseLocalTimeZone() { - dc.useLocalTimeZone = true -} - -// 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) ZeroMaps() { - dc.zeroMaps = true -} - -// 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. -func (dc *DecodeContext) ZeroStructs() { - dc.zeroStructs = true -} - -// DefaultDocumentM causes the Decoder to always unmarshal documents into the 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) DefaultDocumentM() { - dc.defaultDocumentType = reflect.TypeOf(M{}) -} - -// DefaultDocumentD causes the Decoder to always unmarshal documents into the 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) DefaultDocumentD() { - dc.defaultDocumentType = reflect.TypeOf(D{}) -} - -// ValueCodec is an interface for encoding and decoding a reflect.Value. -// values. -// -// Deprecated: Use [ValueEncoder] and [ValueDecoder] instead. -type ValueCodec interface { - ValueEncoder - ValueDecoder -} - -// ValueEncoder is the interface implemented by types that can encode a provided Go type to BSON. +// valueEncoder is the interface implemented by types that can encode a provided Go type to BSON. // The value to encode is provided as a reflect.Value and a bson.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. -type ValueEncoder interface { +type valueEncoder interface { EncodeValue(EncodeContext, ValueWriter, reflect.Value) error } @@ -278,12 +141,12 @@ func (fn ValueEncoderFunc) EncodeValue(ec EncodeContext, vw ValueWriter, val ref return fn(ec, vw, val) } -// ValueDecoder is the interface implemented by types that can decode BSON to a provided Go type. +// valueDecoder is the interface implemented by types that can decode BSON to a provided Go type. // 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. -type ValueDecoder interface { +type valueDecoder interface { DecodeValue(DecodeContext, ValueReader, reflect.Value) error } @@ -314,17 +177,17 @@ type decodeAdapter struct { typeDecoderFunc } -var _ ValueDecoder = decodeAdapter{} +var _ valueDecoder = decodeAdapter{} var _ typeDecoder = decodeAdapter{} // decodeTypeOrValue calls decoder.decodeType is decoder is a typeDecoder. Otherwise, it allocates a new element of type // t and calls decoder.DecodeValue on it. -func decodeTypeOrValue(decoder ValueDecoder, dc DecodeContext, vr ValueReader, t reflect.Type) (reflect.Value, error) { +func decodeTypeOrValue(decoder valueDecoder, dc DecodeContext, vr ValueReader, t reflect.Type) (reflect.Value, error) { td, _ := decoder.(typeDecoder) return decodeTypeOrValueWithInfo(decoder, td, dc, vr, t, true) } -func decodeTypeOrValueWithInfo(vd ValueDecoder, td typeDecoder, dc DecodeContext, vr ValueReader, t reflect.Type, convert bool) (reflect.Value, error) { +func decodeTypeOrValueWithInfo(vd valueDecoder, td typeDecoder, dc DecodeContext, vr ValueReader, t reflect.Type, convert bool) (reflect.Value, error) { if td != nil { val, err := td.decodeType(dc, vr, t) if err == nil && convert && val.Type() != t { diff --git a/bson/bsoncodec_test.go b/bson/bsoncodec_test.go index d1dc21a953..797bc9b383 100644 --- a/bson/bsoncodec_test.go +++ b/bson/bsoncodec_test.go @@ -7,40 +7,10 @@ package bson import ( - "fmt" "reflect" "testing" ) -func ExampleValueEncoder() { - var _ ValueEncoderFunc = func(ec EncodeContext, vw 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()) - } -} - -func ExampleValueDecoder() { - var _ ValueDecoderFunc = func(dc DecodeContext, vr 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() != TypeString { - 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 - } -} - type llCodec struct { t *testing.T decodeval interface{} diff --git a/bson/codec_cache.go b/bson/codec_cache.go index b4042822e6..f28ec30911 100644 --- a/bson/codec_cache.go +++ b/bson/codec_cache.go @@ -29,20 +29,20 @@ type typeEncoderCache struct { cache sync.Map // map[reflect.Type]ValueEncoder } -func (c *typeEncoderCache) Store(rt reflect.Type, enc ValueEncoder) { +func (c *typeEncoderCache) Store(rt reflect.Type, enc valueEncoder) { c.cache.Store(rt, enc) } -func (c *typeEncoderCache) Load(rt reflect.Type) (ValueEncoder, bool) { +func (c *typeEncoderCache) Load(rt reflect.Type) (valueEncoder, bool) { if v, _ := c.cache.Load(rt); v != nil { - return v.(ValueEncoder), true + return v.(valueEncoder), true } return nil, false } -func (c *typeEncoderCache) LoadOrStore(rt reflect.Type, enc ValueEncoder) ValueEncoder { +func (c *typeEncoderCache) LoadOrStore(rt reflect.Type, enc valueEncoder) valueEncoder { if v, loaded := c.cache.LoadOrStore(rt, enc); loaded { - enc = v.(ValueEncoder) + enc = v.(valueEncoder) } return enc } @@ -62,20 +62,20 @@ type typeDecoderCache struct { cache sync.Map // map[reflect.Type]ValueDecoder } -func (c *typeDecoderCache) Store(rt reflect.Type, dec ValueDecoder) { +func (c *typeDecoderCache) Store(rt reflect.Type, dec valueDecoder) { c.cache.Store(rt, dec) } -func (c *typeDecoderCache) Load(rt reflect.Type) (ValueDecoder, bool) { +func (c *typeDecoderCache) Load(rt reflect.Type) (valueDecoder, bool) { if v, _ := c.cache.Load(rt); v != nil { - return v.(ValueDecoder), true + return v.(valueDecoder), true } return nil, false } -func (c *typeDecoderCache) LoadOrStore(rt reflect.Type, dec ValueDecoder) ValueDecoder { +func (c *typeDecoderCache) LoadOrStore(rt reflect.Type, dec valueDecoder) valueDecoder { if v, loaded := c.cache.LoadOrStore(rt, dec); loaded { - dec = v.(ValueDecoder) + dec = v.(valueDecoder) } return dec } @@ -96,20 +96,20 @@ func (c *typeDecoderCache) Clone() *typeDecoderCache { // is always the same (since different concrete types may implement the // ValueEncoder interface). type kindEncoderCacheEntry struct { - enc ValueEncoder + enc valueEncoder } type kindEncoderCache struct { entries [reflect.UnsafePointer + 1]atomic.Value // *kindEncoderCacheEntry } -func (c *kindEncoderCache) Store(rt reflect.Kind, enc ValueEncoder) { +func (c *kindEncoderCache) Store(rt reflect.Kind, enc valueEncoder) { if enc != nil && rt < reflect.Kind(len(c.entries)) { c.entries[rt].Store(&kindEncoderCacheEntry{enc: enc}) } } -func (c *kindEncoderCache) Load(rt reflect.Kind) (ValueEncoder, bool) { +func (c *kindEncoderCache) Load(rt reflect.Kind) (valueEncoder, bool) { if rt < reflect.Kind(len(c.entries)) { if ent, ok := c.entries[rt].Load().(*kindEncoderCacheEntry); ok { return ent.enc, ent.enc != nil @@ -133,20 +133,20 @@ func (c *kindEncoderCache) Clone() *kindEncoderCache { // is always the same (since different concrete types may implement the // ValueDecoder interface). type kindDecoderCacheEntry struct { - dec ValueDecoder + dec valueDecoder } type kindDecoderCache struct { entries [reflect.UnsafePointer + 1]atomic.Value // *kindDecoderCacheEntry } -func (c *kindDecoderCache) Store(rt reflect.Kind, dec ValueDecoder) { +func (c *kindDecoderCache) Store(rt reflect.Kind, dec valueDecoder) { if rt < reflect.Kind(len(c.entries)) { c.entries[rt].Store(&kindDecoderCacheEntry{dec: dec}) } } -func (c *kindDecoderCache) Load(rt reflect.Kind) (ValueDecoder, bool) { +func (c *kindDecoderCache) Load(rt reflect.Kind) (valueDecoder, bool) { if rt < reflect.Kind(len(c.entries)) { if ent, ok := c.entries[rt].Load().(*kindDecoderCacheEntry); ok { return ent.dec, ent.dec != nil diff --git a/bson/codec_cache_test.go b/bson/codec_cache_test.go index d48e05f5a3..3937db68ac 100644 --- a/bson/codec_cache_test.go +++ b/bson/codec_cache_test.go @@ -134,7 +134,7 @@ func TestKindCacheClone(t *testing.T) { func TestKindCacheEncoderNilEncoder(t *testing.T) { t.Run("Encoder", func(t *testing.T) { c := new(kindEncoderCache) - c.Store(reflect.Invalid, ValueEncoder(nil)) + c.Store(reflect.Invalid, valueEncoder(nil)) v, ok := c.Load(reflect.Invalid) if v != nil || ok { t.Errorf("Load of nil ValueEncoder should return: nil, false; got: %v, %t", v, ok) @@ -142,7 +142,7 @@ func TestKindCacheEncoderNilEncoder(t *testing.T) { }) t.Run("Decoder", func(t *testing.T) { c := new(kindDecoderCache) - c.Store(reflect.Invalid, ValueDecoder(nil)) + c.Store(reflect.Invalid, valueDecoder(nil)) v, ok := c.Load(reflect.Invalid) if v != nil || ok { t.Errorf("Load of nil ValueDecoder should return: nil, false; got: %v, %t", v, ok) diff --git a/bson/cond_addr_codec.go b/bson/cond_addr_codec.go index 26eed212f1..a64a9c434f 100644 --- a/bson/cond_addr_codec.go +++ b/bson/cond_addr_codec.go @@ -12,11 +12,11 @@ import ( // condAddrEncoder is the encoder used when a pointer to the encoding value has an encoder. type condAddrEncoder struct { - canAddrEnc ValueEncoder - elseEnc ValueEncoder + canAddrEnc valueEncoder + elseEnc valueEncoder } -var _ ValueEncoder = (*condAddrEncoder)(nil) +var _ valueEncoder = (*condAddrEncoder)(nil) // EncodeValue is the ValueEncoderFunc for a value that may be addressable. func (cae *condAddrEncoder) EncodeValue(ec EncodeContext, vw ValueWriter, val reflect.Value) error { @@ -31,14 +31,14 @@ func (cae *condAddrEncoder) EncodeValue(ec EncodeContext, vw ValueWriter, val re // condAddrDecoder is the decoder used when a pointer to the value has a decoder. type condAddrDecoder struct { - canAddrDec ValueDecoder - elseDec ValueDecoder + canAddrDec valueDecoder + elseDec valueDecoder } -var _ ValueDecoder = (*condAddrDecoder)(nil) +var _ valueDecoder = (*condAddrDecoder)(nil) // newCondAddrDecoder returns an CondAddrDecoder. -func newCondAddrDecoder(canAddrDec, elseDec ValueDecoder) *condAddrDecoder { +func newCondAddrDecoder(canAddrDec, elseDec valueDecoder) *condAddrDecoder { decoder := condAddrDecoder{canAddrDec: canAddrDec, elseDec: elseDec} return &decoder } diff --git a/bson/decoder.go b/bson/decoder.go index 898dbc87af..cc53f422f9 100644 --- a/bson/decoder.go +++ b/bson/decoder.go @@ -30,18 +30,6 @@ var decPool = sync.Pool{ type Decoder struct { dc DecodeContext vr ValueReader - - // We persist defaultDocumentM and defaultDocumentD on the Decoder to prevent overwriting from - // (*Decoder).SetContext. - defaultDocumentM bool - defaultDocumentD bool - - binaryAsSlice bool - decodeObjectIDAsHex bool - useJSONStructTags bool - useLocalTimeZone bool - zeroMaps bool - zeroStructs bool } // NewDecoder returns a new decoder that uses the DefaultRegistry to read from vr. @@ -85,31 +73,6 @@ func (d *Decoder) Decode(val interface{}) error { return err } - if d.defaultDocumentM { - d.dc.DefaultDocumentM() - } - if d.defaultDocumentD { - d.dc.DefaultDocumentD() - } - if d.binaryAsSlice { - d.dc.BinaryAsSlice() - } - if d.decodeObjectIDAsHex { - d.dc.DecodeObjectIDAsHex() - } - if d.useJSONStructTags { - d.dc.UseJSONStructTags() - } - if d.useLocalTimeZone { - d.dc.UseLocalTimeZone() - } - if d.zeroMaps { - d.dc.ZeroMaps() - } - if d.zeroStructs { - d.dc.ZeroStructs() - } - return decoder.DecodeValue(d.dc, d.vr, rval) } @@ -127,53 +90,53 @@ func (d *Decoder) SetRegistry(r *Registry) { // 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{}". func (d *Decoder) DefaultDocumentM() { - d.defaultDocumentM = true + d.dc.defaultDocumentType = reflect.TypeOf(M{}) } // 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{}". func (d *Decoder) DefaultDocumentD() { - d.defaultDocumentD = true + d.dc.defaultDocumentType = reflect.TypeOf(D{}) } // AllowTruncatingDoubles causes the Decoder to truncate the fractional part of BSON "double" values // when attempting to unmarshal them into a Go integer (int, int8, int16, int32, or int64) struct // field. The truncation logic does not apply to BSON "decimal128" values. func (d *Decoder) AllowTruncatingDoubles() { - d.dc.Truncate = true + d.dc.truncate = true } // 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. func (d *Decoder) BinaryAsSlice() { - d.binaryAsSlice = true + d.dc.binaryAsSlice = true } // DecodeObjectIDAsHex causes the Decoder to unmarshal BSON ObjectID as a hexadecimal string. func (d *Decoder) DecodeObjectIDAsHex() { - d.decodeObjectIDAsHex = true + d.dc.decodeObjectIDAsHex = true } // UseJSONStructTags causes the Decoder to fall back to using the "json" struct tag if a "bson" // struct tag is not specified. func (d *Decoder) UseJSONStructTags() { - d.useJSONStructTags = true + d.dc.useJSONStructTags = true } // UseLocalTimeZone causes the Decoder to unmarshal time.Time values in the local timezone instead // of the UTC timezone. func (d *Decoder) UseLocalTimeZone() { - d.useLocalTimeZone = true + d.dc.useLocalTimeZone = true } // 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. func (d *Decoder) ZeroMaps() { - d.zeroMaps = true + d.dc.zeroMaps = true } // 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. func (d *Decoder) ZeroStructs() { - d.zeroStructs = true + d.dc.zeroStructs = true } diff --git a/bson/default_value_decoders.go b/bson/default_value_decoders.go index 5ad52e19e2..cfa09bdd10 100644 --- a/bson/default_value_decoders.go +++ b/bson/default_value_decoders.go @@ -118,7 +118,7 @@ func dDecodeValue(dc DecodeContext, vr ValueReader, val reflect.Value) error { switch vrType := vr.Type(); vrType { case Type(0), TypeEmbeddedDocument: - dc.Ancestor = tD + dc.ancestor = tD case TypeNull: val.Set(reflect.Zero(val.Type())) return vr.ReadNull() @@ -248,7 +248,7 @@ func intDecodeType(dc DecodeContext, vr ValueReader, t reflect.Type) (reflect.Va if err != nil { return emptyValue, err } - if !dc.Truncate && math.Floor(f64) != f64 { + if !dc.truncate && math.Floor(f64) != f64 { return emptyValue, errCannotTruncate } if f64 > float64(math.MaxInt64) { @@ -373,7 +373,7 @@ func floatDecodeType(dc DecodeContext, vr ValueReader, t reflect.Type) (reflect. switch t.Kind() { case reflect.Float32: - if !dc.Truncate && float64(float32(f)) != f { + if !dc.truncate && float64(float32(f)) != f { return emptyValue, errCannotTruncate } diff --git a/bson/default_value_decoders_test.go b/bson/default_value_decoders_test.go index 258ef9e758..fc86145bfd 100644 --- a/bson/default_value_decoders_test.go +++ b/bson/default_value_decoders_test.go @@ -65,7 +65,7 @@ func TestDefaultValueDecoders(t *testing.T) { testCases := []struct { name string - vd ValueDecoder + vd valueDecoder subtests []subtest }{ { @@ -191,7 +191,7 @@ func TestDefaultValueDecoders(t *testing.T) { nil, }, { - "ReadDouble (truncate)", int64(3), &DecodeContext{Truncate: true}, + "ReadDouble (truncate)", int64(3), &DecodeContext{truncate: true}, &valueReaderWriter{BSONType: TypeDouble, Return: float64(3.14)}, readDouble, nil, }, @@ -423,7 +423,7 @@ func TestDefaultValueDecoders(t *testing.T) { nil, }, { - "ReadDouble (truncate)", uint64(3), &DecodeContext{Truncate: true}, + "ReadDouble (truncate)", uint64(3), &DecodeContext{truncate: true}, &valueReaderWriter{BSONType: TypeDouble, Return: float64(3.14)}, readDouble, nil, }, @@ -674,7 +674,7 @@ func TestDefaultValueDecoders(t *testing.T) { nil, }, { - "float32/fast path (truncate)", float32(3.14), &DecodeContext{Truncate: true}, + "float32/fast path (truncate)", float32(3.14), &DecodeContext{truncate: true}, &valueReaderWriter{BSONType: TypeDouble, Return: float64(3.14)}, readDouble, nil, }, @@ -712,7 +712,7 @@ func TestDefaultValueDecoders(t *testing.T) { nil, }, { - "float32/reflection path (truncate)", myfloat32(3.14), &DecodeContext{Truncate: true}, + "float32/reflection path (truncate)", myfloat32(3.14), &DecodeContext{truncate: true}, &valueReaderWriter{BSONType: TypeDouble, Return: float64(3.14)}, readDouble, nil, }, @@ -3566,7 +3566,7 @@ func TestDefaultValueDecoders(t *testing.T) { val interface{} vr ValueReader registry *Registry // buildDefaultRegistry will be used if this is nil - decoder ValueDecoder + decoder valueDecoder err error }{ { diff --git a/bson/default_value_encoders.go b/bson/default_value_encoders.go index 6b2ff14f61..31c4bf314c 100644 --- a/bson/default_value_encoders.go +++ b/bson/default_value_encoders.go @@ -124,7 +124,7 @@ func intEncodeValue(ec EncodeContext, vw ValueWriter, val reflect.Value) error { return vw.WriteInt64(i64) case reflect.Int64: i64 := val.Int() - if ec.MinSize && fitsIn32Bits(i64) { + if ec.minSize && fitsIn32Bits(i64) { return vw.WriteInt32(int32(i64)) } return vw.WriteInt64(i64) @@ -263,7 +263,7 @@ func arrayEncodeValue(ec EncodeContext, vw ValueWriter, val reflect.Value) error return aw.WriteArrayEnd() } -func lookupElementEncoder(ec EncodeContext, origEncoder ValueEncoder, currVal reflect.Value) (ValueEncoder, reflect.Value, error) { +func lookupElementEncoder(ec EncodeContext, origEncoder valueEncoder, currVal reflect.Value) (valueEncoder, reflect.Value, error) { if origEncoder != nil || (currVal.Kind() != reflect.Interface) { return origEncoder, currVal, nil } diff --git a/bson/default_value_encoders_test.go b/bson/default_value_encoders_test.go index 797a77322a..9c869448f9 100644 --- a/bson/default_value_encoders_test.go +++ b/bson/default_value_encoders_test.go @@ -74,7 +74,7 @@ func TestDefaultValueEncoders(t *testing.T) { testCases := []struct { name string - ve ValueEncoder + ve valueEncoder subtests []subtest }{ { @@ -113,9 +113,9 @@ func TestDefaultValueEncoders(t *testing.T) { {"int16/fast path", int16(32767), nil, nil, writeInt32, nil}, {"int32/fast path", int32(2147483647), nil, nil, writeInt32, nil}, {"int64/fast path", int64(1234567890987), nil, nil, writeInt64, nil}, - {"int64/fast path - minsize", int64(math.MaxInt32), &EncodeContext{MinSize: true}, nil, writeInt32, nil}, - {"int64/fast path - minsize too large", int64(math.MaxInt32 + 1), &EncodeContext{MinSize: true}, nil, writeInt64, nil}, - {"int64/fast path - minsize too small", int64(math.MinInt32 - 1), &EncodeContext{MinSize: true}, nil, writeInt64, nil}, + {"int64/fast path - minsize", int64(math.MaxInt32), &EncodeContext{minSize: true}, nil, writeInt32, nil}, + {"int64/fast path - minsize too large", int64(math.MaxInt32 + 1), &EncodeContext{minSize: true}, nil, writeInt64, nil}, + {"int64/fast path - minsize too small", int64(math.MinInt32 - 1), &EncodeContext{minSize: true}, nil, writeInt64, nil}, {"int/fast path - positive int32", int(math.MaxInt32 - 1), nil, nil, writeInt32, nil}, {"int/fast path - negative int32", int(math.MinInt32 + 1), nil, nil, writeInt32, nil}, {"int/fast path - MaxInt32", int(math.MaxInt32), nil, nil, writeInt32, nil}, @@ -124,9 +124,9 @@ func TestDefaultValueEncoders(t *testing.T) { {"int16/reflection path", myint16(32767), nil, nil, writeInt32, nil}, {"int32/reflection path", myint32(2147483647), nil, nil, writeInt32, nil}, {"int64/reflection path", myint64(1234567890987), nil, nil, writeInt64, nil}, - {"int64/reflection path - minsize", myint64(math.MaxInt32), &EncodeContext{MinSize: true}, nil, writeInt32, nil}, - {"int64/reflection path - minsize too large", myint64(math.MaxInt32 + 1), &EncodeContext{MinSize: true}, nil, writeInt64, nil}, - {"int64/reflection path - minsize too small", myint64(math.MinInt32 - 1), &EncodeContext{MinSize: true}, nil, writeInt64, nil}, + {"int64/reflection path - minsize", myint64(math.MaxInt32), &EncodeContext{minSize: true}, nil, writeInt32, nil}, + {"int64/reflection path - minsize too large", myint64(math.MaxInt32 + 1), &EncodeContext{minSize: true}, nil, writeInt64, nil}, + {"int64/reflection path - minsize too small", myint64(math.MinInt32 - 1), &EncodeContext{minSize: true}, nil, writeInt64, nil}, {"int/reflection path - positive int32", myint(math.MaxInt32 - 1), nil, nil, writeInt32, nil}, {"int/reflection path - negative int32", myint(math.MinInt32 + 1), nil, nil, writeInt32, nil}, {"int/reflection path - MaxInt32", myint(math.MaxInt32), nil, nil, writeInt32, nil}, @@ -154,23 +154,23 @@ func TestDefaultValueEncoders(t *testing.T) { {"uint32/fast path", uint32(2147483647), nil, nil, writeInt64, nil}, {"uint64/fast path", uint64(1234567890987), nil, nil, writeInt64, nil}, {"uint/fast path", uint(1234567), nil, nil, writeInt64, nil}, - {"uint32/fast path - minsize", uint32(2147483647), &EncodeContext{MinSize: true}, nil, writeInt32, nil}, - {"uint64/fast path - minsize", uint64(2147483647), &EncodeContext{MinSize: true}, nil, writeInt32, nil}, - {"uint/fast path - minsize", uint(2147483647), &EncodeContext{MinSize: true}, nil, writeInt32, nil}, - {"uint32/fast path - minsize too large", uint32(2147483648), &EncodeContext{MinSize: true}, nil, writeInt64, nil}, - {"uint64/fast path - minsize too large", uint64(2147483648), &EncodeContext{MinSize: true}, nil, writeInt64, nil}, - {"uint/fast path - minsize too large", uint(2147483648), &EncodeContext{MinSize: true}, nil, writeInt64, nil}, + {"uint32/fast path - minsize", uint32(2147483647), &EncodeContext{minSize: true}, nil, writeInt32, nil}, + {"uint64/fast path - minsize", uint64(2147483647), &EncodeContext{minSize: true}, nil, writeInt32, nil}, + {"uint/fast path - minsize", uint(2147483647), &EncodeContext{minSize: true}, nil, writeInt32, nil}, + {"uint32/fast path - minsize too large", uint32(2147483648), &EncodeContext{minSize: true}, nil, writeInt64, nil}, + {"uint64/fast path - minsize too large", uint64(2147483648), &EncodeContext{minSize: true}, nil, writeInt64, nil}, + {"uint/fast path - minsize too large", uint(2147483648), &EncodeContext{minSize: true}, nil, writeInt64, nil}, {"uint64/fast path - overflow", uint64(1 << 63), nil, nil, nothing, fmt.Errorf("%d overflows int64", uint64(1<<63))}, {"uint8/reflection path", myuint8(127), nil, nil, writeInt32, nil}, {"uint16/reflection path", myuint16(32767), nil, nil, writeInt32, nil}, {"uint32/reflection path", myuint32(2147483647), nil, nil, writeInt64, nil}, {"uint64/reflection path", myuint64(1234567890987), nil, nil, writeInt64, nil}, - {"uint32/reflection path - minsize", myuint32(2147483647), &EncodeContext{MinSize: true}, nil, writeInt32, nil}, - {"uint64/reflection path - minsize", myuint64(2147483647), &EncodeContext{MinSize: true}, nil, writeInt32, nil}, - {"uint/reflection path - minsize", myuint(2147483647), &EncodeContext{MinSize: true}, nil, writeInt32, nil}, - {"uint32/reflection path - minsize too large", myuint(1 << 31), &EncodeContext{MinSize: true}, nil, writeInt64, nil}, - {"uint64/reflection path - minsize too large", myuint64(1 << 31), &EncodeContext{MinSize: true}, nil, writeInt64, nil}, - {"uint/reflection path - minsize too large", myuint(2147483648), &EncodeContext{MinSize: true}, nil, writeInt64, nil}, + {"uint32/reflection path - minsize", myuint32(2147483647), &EncodeContext{minSize: true}, nil, writeInt32, nil}, + {"uint64/reflection path - minsize", myuint64(2147483647), &EncodeContext{minSize: true}, nil, writeInt32, nil}, + {"uint/reflection path - minsize", myuint(2147483647), &EncodeContext{minSize: true}, nil, writeInt32, nil}, + {"uint32/reflection path - minsize too large", myuint(1 << 31), &EncodeContext{minSize: true}, nil, writeInt64, nil}, + {"uint64/reflection path - minsize too large", myuint64(1 << 31), &EncodeContext{minSize: true}, nil, writeInt64, nil}, + {"uint/reflection path - minsize too large", myuint(2147483648), &EncodeContext{minSize: true}, nil, writeInt64, nil}, {"uint64/reflection path - overflow", myuint64(1 << 63), nil, nil, nothing, fmt.Errorf("%d overflows int64", uint64(1<<63))}, }, }, diff --git a/bson/empty_interface_codec.go b/bson/empty_interface_codec.go index da9efdded3..fce09c0ac1 100644 --- a/bson/empty_interface_codec.go +++ b/bson/empty_interface_codec.go @@ -47,11 +47,11 @@ func (eic emptyInterfaceCodec) getEmptyInterfaceDecodeType(dc DecodeContext, val // that type. return dc.defaultDocumentType, nil } - if dc.Ancestor != nil { + if dc.ancestor != nil { // Using ancestor information rather than looking up the type map entry forces consistent decoding. // If we're decoding into a bson.D, subdocuments should also be decoded as bson.D, even if a type map entry // has been registered. - return dc.Ancestor, nil + return dc.ancestor, nil } } diff --git a/bson/encoder.go b/bson/encoder.go index fb865cd285..1b348f9488 100644 --- a/bson/encoder.go +++ b/bson/encoder.go @@ -25,15 +25,6 @@ var encPool = sync.Pool{ type Encoder struct { ec EncodeContext vw ValueWriter - - errorOnInlineDuplicates bool - intMinSize bool - stringifyMapKeysWithFmt bool - nilMapAsEmpty bool - nilSliceAsEmpty bool - nilByteSliceAsEmpty bool - omitZeroStruct bool - useJSONStructTags bool } // NewEncoder returns a new encoder that uses the DefaultRegistry to write to vw. @@ -62,33 +53,6 @@ func (e *Encoder) Encode(val interface{}) error { return err } - // Copy the configurations applied to the Encoder over to the EncodeContext, which actually - // communicates those configurations to the default ValueEncoders. - if e.errorOnInlineDuplicates { - e.ec.ErrorOnInlineDuplicates() - } - if e.intMinSize { - e.ec.MinSize = true - } - if e.stringifyMapKeysWithFmt { - e.ec.StringifyMapKeysWithFmt() - } - if e.nilMapAsEmpty { - e.ec.NilMapAsEmpty() - } - if e.nilSliceAsEmpty { - e.ec.NilSliceAsEmpty() - } - if e.nilByteSliceAsEmpty { - e.ec.NilByteSliceAsEmpty() - } - if e.omitZeroStruct { - e.ec.OmitZeroStruct() - } - if e.useJSONStructTags { - e.ec.UseJSONStructTags() - } - return encoder.EncodeValue(e.ec, e.vw, reflect.ValueOf(val)) } @@ -106,38 +70,38 @@ func (e *Encoder) SetRegistry(r *Registry) { // 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. func (e *Encoder) ErrorOnInlineDuplicates() { - e.errorOnInlineDuplicates = true + e.ec.errorOnInlineDuplicates = true } // IntMinSize 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. func (e *Encoder) IntMinSize() { - e.intMinSize = true + e.ec.minSize = true } // StringifyMapKeysWithFmt causes the Encoder to convert Go map keys to BSON document field name // strings using fmt.Sprint instead of the default string conversion logic. func (e *Encoder) StringifyMapKeysWithFmt() { - e.stringifyMapKeysWithFmt = true + e.ec.stringifyMapKeysWithFmt = true } // NilMapAsEmpty causes the Encoder to marshal nil Go maps as empty BSON documents instead of BSON // null. func (e *Encoder) NilMapAsEmpty() { - e.nilMapAsEmpty = true + e.ec.nilMapAsEmpty = true } // NilSliceAsEmpty causes the Encoder to marshal nil Go slices as empty BSON arrays instead of BSON // null. func (e *Encoder) NilSliceAsEmpty() { - e.nilSliceAsEmpty = true + e.ec.nilSliceAsEmpty = true } // NilByteSliceAsEmpty causes the Encoder to marshal nil Go byte slices as empty BSON binary values // instead of BSON null. func (e *Encoder) NilByteSliceAsEmpty() { - e.nilByteSliceAsEmpty = true + e.ec.nilByteSliceAsEmpty = true } // TODO(GODRIVER-2820): Update the description to remove the note about only examining exported @@ -149,11 +113,11 @@ func (e *Encoder) NilByteSliceAsEmpty() { // 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. func (e *Encoder) OmitZeroStruct() { - e.omitZeroStruct = true + e.ec.omitZeroStruct = true } // UseJSONStructTags causes the Encoder to fall back to using the "json" struct tag if a "bson" // struct tag is not specified. func (e *Encoder) UseJSONStructTags() { - e.useJSONStructTags = true + e.ec.useJSONStructTags = true } diff --git a/bson/map_codec.go b/bson/map_codec.go index e11ffaf726..4da32ccae4 100644 --- a/bson/map_codec.go +++ b/bson/map_codec.go @@ -159,7 +159,7 @@ func (mc *mapCodec) DecodeValue(dc DecodeContext, vr ValueReader, val reflect.Va eTypeDecoder, _ := decoder.(typeDecoder) if eType == tEmpty { - dc.Ancestor = val.Type() + dc.ancestor = val.Type() } keyType := val.Type().Key() diff --git a/bson/pointer_codec.go b/bson/pointer_codec.go index 4ed1de3013..0fd7fdb81c 100644 --- a/bson/pointer_codec.go +++ b/bson/pointer_codec.go @@ -10,8 +10,8 @@ import ( "reflect" ) -var _ ValueEncoder = &pointerCodec{} -var _ ValueDecoder = &pointerCodec{} +var _ valueEncoder = &pointerCodec{} +var _ valueDecoder = &pointerCodec{} // pointerCodec is the Codec used for pointers. type pointerCodec struct { diff --git a/bson/primitive_codecs_test.go b/bson/primitive_codecs_test.go index 6b0c4c1e05..a2e1e61e89 100644 --- a/bson/primitive_codecs_test.go +++ b/bson/primitive_codecs_test.go @@ -45,7 +45,7 @@ func TestPrimitiveValueEncoders(t *testing.T) { testCases := []struct { name string - ve ValueEncoder + ve valueEncoder subtests []subtest }{ { @@ -491,7 +491,7 @@ func TestPrimitiveValueDecoders(t *testing.T) { testCases := []struct { name string - vd ValueDecoder + vd valueDecoder subtests []subtest }{ { diff --git a/bson/registry.go b/bson/registry.go index 71d65259d6..9c680e4e66 100644 --- a/bson/registry.go +++ b/bson/registry.go @@ -125,7 +125,7 @@ func NewRegistry() *Registry { // interface. To get the latter behavior, call RegisterHookEncoder instead. // // RegisterTypeEncoder should not be called concurrently with any other Registry method. -func (r *Registry) RegisterTypeEncoder(valueType reflect.Type, enc ValueEncoder) { +func (r *Registry) RegisterTypeEncoder(valueType reflect.Type, enc valueEncoder) { r.typeEncoders.Store(valueType, enc) } @@ -139,7 +139,7 @@ func (r *Registry) RegisterTypeEncoder(valueType reflect.Type, enc ValueEncoder) // 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) RegisterTypeDecoder(valueType reflect.Type, dec ValueDecoder) { +func (r *Registry) RegisterTypeDecoder(valueType reflect.Type, dec valueDecoder) { r.typeDecoders.Store(valueType, dec) } @@ -155,7 +155,7 @@ func (r *Registry) RegisterTypeDecoder(valueType reflect.Type, dec ValueDecoder) // reg.RegisterKindEncoder(reflect.Int32, myEncoder) // // RegisterKindEncoder should not be called concurrently with any other Registry method. -func (r *Registry) RegisterKindEncoder(kind reflect.Kind, enc ValueEncoder) { +func (r *Registry) RegisterKindEncoder(kind reflect.Kind, enc valueEncoder) { r.kindEncoders.Store(kind, enc) } @@ -171,7 +171,7 @@ func (r *Registry) RegisterKindEncoder(kind reflect.Kind, enc ValueEncoder) { // reg.RegisterKindDecoder(reflect.Int32, myDecoder) // // RegisterKindDecoder should not be called concurrently with any other Registry method. -func (r *Registry) RegisterKindDecoder(kind reflect.Kind, dec ValueDecoder) { +func (r *Registry) RegisterKindDecoder(kind reflect.Kind, dec valueDecoder) { r.kindDecoders.Store(kind, dec) } @@ -181,7 +181,7 @@ func (r *Registry) RegisterKindDecoder(kind reflect.Kind, dec ValueDecoder) { // (i.e. iface.Kind() != reflect.Interface), this method will panic. // // RegisterInterfaceEncoder should not be called concurrently with any other Registry method. -func (r *Registry) RegisterInterfaceEncoder(iface reflect.Type, enc ValueEncoder) { +func (r *Registry) RegisterInterfaceEncoder(iface reflect.Type, enc valueEncoder) { if iface.Kind() != reflect.Interface { panicStr := fmt.Errorf("RegisterInterfaceEncoder expects a type with kind reflect.Interface, "+ "got type %s with kind %s", iface, iface.Kind()) @@ -204,7 +204,7 @@ func (r *Registry) RegisterInterfaceEncoder(iface reflect.Type, enc ValueEncoder // this method will panic. // // RegisterInterfaceDecoder should not be called concurrently with any other Registry method. -func (r *Registry) RegisterInterfaceDecoder(iface reflect.Type, dec ValueDecoder) { +func (r *Registry) RegisterInterfaceDecoder(iface reflect.Type, dec valueDecoder) { if iface.Kind() != reflect.Interface { panicStr := fmt.Errorf("RegisterInterfaceDecoder expects a type with kind reflect.Interface, "+ "got type %s with kind %s", iface, iface.Kind()) @@ -251,7 +251,7 @@ func (r *Registry) RegisterTypeMapEntry(bt Type, rt reflect.Type) { // // 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) LookupEncoder(valueType reflect.Type) (ValueEncoder, error) { +func (r *Registry) LookupEncoder(valueType reflect.Type) (valueEncoder, error) { if valueType == nil { return nil, ErrNoEncoder{Type: valueType} } @@ -274,15 +274,15 @@ func (r *Registry) LookupEncoder(valueType reflect.Type) (ValueEncoder, error) { return nil, ErrNoEncoder{Type: valueType} } -func (r *Registry) storeTypeEncoder(rt reflect.Type, enc ValueEncoder) ValueEncoder { +func (r *Registry) storeTypeEncoder(rt reflect.Type, enc valueEncoder) valueEncoder { return r.typeEncoders.LoadOrStore(rt, enc) } -func (r *Registry) lookupTypeEncoder(rt reflect.Type) (ValueEncoder, bool) { +func (r *Registry) lookupTypeEncoder(rt reflect.Type) (valueEncoder, bool) { return r.typeEncoders.Load(rt) } -func (r *Registry) lookupInterfaceEncoder(valueType reflect.Type, allowAddr bool) (ValueEncoder, bool) { +func (r *Registry) lookupInterfaceEncoder(valueType reflect.Type, allowAddr bool) (valueEncoder, bool) { if valueType == nil { return nil, false } @@ -320,7 +320,7 @@ func (r *Registry) lookupInterfaceEncoder(valueType reflect.Type, allowAddr bool // // 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) LookupDecoder(valueType reflect.Type) (ValueDecoder, error) { +func (r *Registry) LookupDecoder(valueType reflect.Type) (valueDecoder, error) { if valueType == nil { return nil, ErrNilType } @@ -343,15 +343,15 @@ func (r *Registry) LookupDecoder(valueType reflect.Type) (ValueDecoder, error) { return nil, ErrNoDecoder{Type: valueType} } -func (r *Registry) lookupTypeDecoder(valueType reflect.Type) (ValueDecoder, bool) { +func (r *Registry) lookupTypeDecoder(valueType reflect.Type) (valueDecoder, bool) { return r.typeDecoders.Load(valueType) } -func (r *Registry) storeTypeDecoder(typ reflect.Type, dec ValueDecoder) ValueDecoder { +func (r *Registry) storeTypeDecoder(typ reflect.Type, dec valueDecoder) valueDecoder { return r.typeDecoders.LoadOrStore(typ, dec) } -func (r *Registry) lookupInterfaceDecoder(valueType reflect.Type, allowAddr bool) (ValueDecoder, bool) { +func (r *Registry) lookupInterfaceDecoder(valueType reflect.Type, allowAddr bool) (valueDecoder, bool) { for _, idec := range r.interfaceDecoders { if valueType.Implements(idec.i) { return idec.vd, true @@ -383,10 +383,10 @@ func (r *Registry) LookupTypeMapEntry(bt Type) (reflect.Type, error) { type interfaceValueEncoder struct { i reflect.Type - ve ValueEncoder + ve valueEncoder } type interfaceValueDecoder struct { i reflect.Type - vd ValueDecoder + vd valueDecoder } diff --git a/bson/registry_test.go b/bson/registry_test.go index b897f04db6..2a5150ad24 100644 --- a/bson/registry_test.go +++ b/bson/registry_test.go @@ -62,7 +62,7 @@ func TestRegistryBuilder(t *testing.T) { reg.RegisterTypeEncoder(reflect.TypeOf(ft4), fc4) want := []struct { t reflect.Type - c ValueEncoder + c valueEncoder }{ {reflect.TypeOf(ft1), fc3}, {reflect.TypeOf(ft2), fc2}, @@ -90,7 +90,7 @@ func TestRegistryBuilder(t *testing.T) { reg.RegisterKindEncoder(k4, fc4) want := []struct { k reflect.Kind - c ValueEncoder + c valueEncoder }{ {k1, fc3}, {k2, fc2}, @@ -173,8 +173,8 @@ func TestRegistryBuilder(t *testing.T) { }) t.Run("Lookup", func(t *testing.T) { type Codec interface { - ValueEncoder - ValueDecoder + valueEncoder + valueDecoder } var ( @@ -472,7 +472,7 @@ func TestRegistry(t *testing.T) { want := []struct { t reflect.Type - c ValueEncoder + c valueEncoder }{ {reflect.TypeOf(ft1), fc3}, {reflect.TypeOf(ft2), fc2}, @@ -502,7 +502,7 @@ func TestRegistry(t *testing.T) { want := []struct { k reflect.Kind - c ValueEncoder + c valueEncoder }{ {k1, fc3}, {k2, fc2}, @@ -588,8 +588,8 @@ func TestRegistry(t *testing.T) { t.Parallel() type Codec interface { - ValueEncoder - ValueDecoder + valueEncoder + valueDecoder } var ( @@ -887,7 +887,7 @@ func TestRegistry(t *testing.T) { } // get is only for testing as it does return if the value was found -func (c *kindEncoderCache) get(rt reflect.Kind) ValueEncoder { +func (c *kindEncoderCache) get(rt reflect.Kind) valueEncoder { e, _ := c.Load(rt) return e } diff --git a/bson/slice_codec.go b/bson/slice_codec.go index 6d26f6283c..b25efc6bff 100644 --- a/bson/slice_codec.go +++ b/bson/slice_codec.go @@ -152,7 +152,7 @@ func (sc *sliceCodec) DecodeValue(dc DecodeContext, vr ValueReader, val reflect. var elemsFunc func(DecodeContext, ValueReader, reflect.Value) ([]reflect.Value, error) switch val.Type().Elem() { case tE: - dc.Ancestor = val.Type() + dc.ancestor = val.Type() elemsFunc = decodeD default: elemsFunc = decodeDefault diff --git a/bson/struct_codec.go b/bson/struct_codec.go index 0c3eac5c73..c8f783a00f 100644 --- a/bson/struct_codec.go +++ b/bson/struct_codec.go @@ -73,8 +73,8 @@ type structCodec struct { overwriteDuplicatedInlinedFields bool } -var _ ValueEncoder = &structCodec{} -var _ ValueDecoder = &structCodec{} +var _ valueEncoder = &structCodec{} +var _ valueDecoder = &structCodec{} // newStructCodec returns a StructCodec that uses p for struct tag parsing. func newStructCodec(p StructTagParser) *structCodec { @@ -158,7 +158,7 @@ func (sc *structCodec) EncodeValue(ec EncodeContext, vw ValueWriter, val reflect ectx := EncodeContext{ Registry: ec.Registry, - MinSize: desc.minSize || ec.MinSize, + minSize: desc.minSize || ec.minSize, errorOnInlineDuplicates: ec.errorOnInlineDuplicates, stringifyMapKeysWithFmt: ec.stringifyMapKeysWithFmt, nilMapAsEmpty: ec.nilMapAsEmpty, @@ -239,7 +239,7 @@ func (sc *structCodec) DecodeValue(dc DecodeContext, vr ValueReader, val reflect val.Set(deepZero(val.Type())) } - var decoder ValueDecoder + var decoder valueDecoder var inlineMap reflect.Value if sd.inlineMap >= 0 { inlineMap = val.Field(sd.inlineMap) @@ -287,7 +287,7 @@ func (sc *structCodec) DecodeValue(dc DecodeContext, vr ValueReader, val reflect } elem := reflect.New(inlineMap.Type().Elem()).Elem() - dc.Ancestor = inlineMap.Type() + dc.ancestor = inlineMap.Type() err = decoder.DecodeValue(dc, vr, elem) if err != nil { return err @@ -317,7 +317,7 @@ func (sc *structCodec) DecodeValue(dc DecodeContext, vr ValueReader, val reflect dctx := DecodeContext{ Registry: dc.Registry, - Truncate: fd.truncate || dc.Truncate, + truncate: fd.truncate || dc.truncate, defaultDocumentType: dc.defaultDocumentType, binaryAsSlice: dc.binaryAsSlice, useJSONStructTags: dc.useJSONStructTags, @@ -385,8 +385,8 @@ type fieldDescription struct { minSize bool truncate bool inline []int - encoder ValueEncoder - decoder ValueDecoder + encoder valueEncoder + decoder valueDecoder } type byIndex []fieldDescription diff --git a/bson/truncation_test.go b/bson/truncation_test.go index 865917cfe4..a9aeea278b 100644 --- a/bson/truncation_test.go +++ b/bson/truncation_test.go @@ -41,7 +41,7 @@ func TestTruncation(t *testing.T) { var output outputArgs dc := DecodeContext{ Registry: DefaultRegistry, - Truncate: true, + truncate: true, } err = UnmarshalWithContext(dc, buf.Bytes(), &output) @@ -67,7 +67,7 @@ func TestTruncation(t *testing.T) { var output outputArgs dc := DecodeContext{ Registry: DefaultRegistry, - Truncate: false, + truncate: false, } // case throws an error when truncation is disabled diff --git a/bson/uint_codec.go b/bson/uint_codec.go index b8f97ae5ab..27a297d043 100644 --- a/bson/uint_codec.go +++ b/bson/uint_codec.go @@ -32,7 +32,7 @@ func (uic *uintCodec) EncodeValue(ec EncodeContext, vw ValueWriter, val reflect. u64 := val.Uint() // If ec.MinSize or if encodeToMinSize is true for a non-uint64 value we should write val as an int32 - useMinSize := ec.MinSize || (uic.encodeToMinSize && val.Kind() != reflect.Uint64) + useMinSize := ec.minSize || (uic.encodeToMinSize && val.Kind() != reflect.Uint64) if u64 <= math.MaxInt32 && useMinSize { return vw.WriteInt32(int32(u64)) @@ -70,7 +70,7 @@ func (uic *uintCodec) decodeType(dc DecodeContext, vr ValueReader, t reflect.Typ if err != nil { return emptyValue, err } - if !dc.Truncate && math.Floor(f64) != f64 { + if !dc.truncate && math.Floor(f64) != f64 { return emptyValue, errCannotTruncate } if f64 > float64(math.MaxInt64) {