summaryrefslogtreecommitdiff
path: root/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/map_codec.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/map_codec.go')
-rw-r--r--vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/map_codec.go60
1 files changed, 47 insertions, 13 deletions
diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/map_codec.go b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/map_codec.go
index e1fbef9c6..d7e00ffa8 100644
--- a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/map_codec.go
+++ b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/map_codec.go
@@ -8,6 +8,7 @@ package bsoncodec
import (
"encoding"
+ "errors"
"fmt"
"reflect"
"strconv"
@@ -20,14 +21,44 @@ import (
var defaultMapCodec = NewMapCodec()
// 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 bool
- EncodeNilAsEmpty bool
+ // 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
}
-var _ ValueCodec = &MapCodec{}
-
// 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 {
@@ -45,6 +76,9 @@ type KeyUnmarshaler interface {
}
// NewMapCodec returns a MapCodec with options opts.
+//
+// Deprecated: NewMapCodec will not be available in Go Driver 2.0. See
+// [MapCodec] for more details.
func NewMapCodec(opts ...*bsonoptions.MapCodecOptions) *MapCodec {
mapOpt := bsonoptions.MergeMapCodecOptions(opts...)
@@ -67,7 +101,7 @@ func (mc *MapCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val ref
return ValueEncoderError{Name: "MapEncodeValue", Kinds: []reflect.Kind{reflect.Map}, Received: val}
}
- if val.IsNil() && !mc.EncodeNilAsEmpty {
+ if val.IsNil() && !mc.EncodeNilAsEmpty && !ec.nilMapAsEmpty {
// If we have a nil map but we can't WriteNull, that means we're probably trying to encode
// to a TopLevel document. We can't currently tell if this is what actually happened, but if
// there's a deeper underlying problem, the error will also be returned from WriteDocument,
@@ -100,7 +134,7 @@ func (mc *MapCodec) mapEncodeValue(ec EncodeContext, dw bsonrw.DocumentWriter, v
keys := val.MapKeys()
for _, key := range keys {
- keyStr, err := mc.encodeKey(key)
+ keyStr, err := mc.encodeKey(key, ec.stringifyMapKeysWithFmt)
if err != nil {
return err
}
@@ -110,7 +144,7 @@ func (mc *MapCodec) mapEncodeValue(ec EncodeContext, dw bsonrw.DocumentWriter, v
}
currEncoder, currVal, lookupErr := defaultValueEncoders.lookupElementEncoder(ec, encoder, val.MapIndex(key))
- if lookupErr != nil && lookupErr != errInvalidValue {
+ if lookupErr != nil && !errors.Is(lookupErr, errInvalidValue) {
return lookupErr
}
@@ -119,7 +153,7 @@ func (mc *MapCodec) mapEncodeValue(ec EncodeContext, dw bsonrw.DocumentWriter, v
return err
}
- if lookupErr == errInvalidValue {
+ if errors.Is(lookupErr, errInvalidValue) {
err = vw.WriteNull()
if err != nil {
return err
@@ -163,7 +197,7 @@ func (mc *MapCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val ref
val.Set(reflect.MakeMap(val.Type()))
}
- if val.Len() > 0 && mc.DecodeZerosMap {
+ if val.Len() > 0 && (mc.DecodeZerosMap || dc.zeroMaps) {
clearMap(val)
}
@@ -182,7 +216,7 @@ func (mc *MapCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val ref
for {
key, vr, err := dr.ReadElement()
- if err == bsonrw.ErrEOD {
+ if errors.Is(err, bsonrw.ErrEOD) {
break
}
if err != nil {
@@ -211,8 +245,8 @@ func clearMap(m reflect.Value) {
}
}
-func (mc *MapCodec) encodeKey(val reflect.Value) (string, error) {
- if mc.EncodeKeysWithStringer {
+func (mc *MapCodec) encodeKey(val reflect.Value, encodeKeysWithStringer bool) (string, error) {
+ if mc.EncodeKeysWithStringer || encodeKeysWithStringer {
return fmt.Sprint(val), nil
}
@@ -295,7 +329,7 @@ func (mc *MapCodec) decodeKey(key string, keyType reflect.Type) (reflect.Value,
if mc.EncodeKeysWithStringer {
parsed, err := strconv.ParseFloat(key, 64)
if err != nil {
- return keyVal, fmt.Errorf("Map key is defined to be a decimal type (%v) but got error %v", keyType.Kind(), err)
+ return keyVal, fmt.Errorf("Map key is defined to be a decimal type (%v) but got error %w", keyType.Kind(), err)
}
keyVal = reflect.ValueOf(parsed)
break