diff options
Diffstat (limited to 'vendor/go.mongodb.org/mongo-driver')
40 files changed, 2842 insertions, 563 deletions
diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bson.go b/vendor/go.mongodb.org/mongo-driver/bson/bson.go index 37bf9811f3..ae1a87faa7 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/bson.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/bson.go @@ -22,39 +22,29 @@ type Zeroer interface { IsZero() bool } -// D represents a BSON Document. This type can be used to represent BSON in a concise and readable -// manner. It should generally be used when serializing to BSON. For deserializing, the Raw or -// Document types should be used. +// D is an ordered representation of a BSON document. This type should be used when the order of the elements matters, +// such as MongoDB command documents. If the order of the elements does not matter, an M should be used instead. // // Example usage: // // bson.D{{"foo", "bar"}, {"hello", "world"}, {"pi", 3.14159}} -// -// This type should be used in situations where order matters, such as MongoDB commands. If the -// order is not important, a map is more comfortable and concise. type D = primitive.D // E represents a BSON element for a D. It is usually used inside a D. type E = primitive.E -// M is an unordered, concise representation of a BSON Document. It should generally be used to -// serialize BSON when the order of the elements of a BSON document do not matter. If the element -// order matters, use a D instead. +// M is an unordered representation of a BSON document. This type should be used when the order of the elements does not +// matter. This type is handled as a regular map[string]interface{} when encoding and decoding. Elements will be +// serialized in an undefined, random order. If the order of the elements matters, a D should be used instead. // // Example usage: // // bson.M{"foo": "bar", "hello": "world", "pi": 3.14159} -// -// This type is handled in the encoders as a regular map[string]interface{}. The elements will be -// serialized in an undefined, random order, and the order will be different each time. type M = primitive.M -// An A represents a BSON array. This type can be used to represent a BSON array in a concise and -// readable manner. It should generally be used when serializing to BSON. For deserializing, the -// RawArray or Array types should be used. +// An A is an ordered representation of a BSON array. // // Example usage: // // bson.A{"bar", "world", 3.14159, bson.D{{"qux", 12345}}} -// type A = primitive.A diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bson_1_8.go b/vendor/go.mongodb.org/mongo-driver/bson/bson_1_8.go index caf5f50a71..bbe7792848 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/bson_1_8.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/bson_1_8.go @@ -21,16 +21,12 @@ type Zeroer interface { IsZero() bool } -// D represents a BSON Document. This type can be used to represent BSON in a concise and readable -// manner. It should generally be used when serializing to BSON. For deserializing, the Raw or -// Document types should be used. +// D is an ordered representation of a BSON document. This type should be used when the order of the elements matters, +// such as MongoDB command documents. If the order of the elements does not matter, an M should be used instead. // // Example usage: // -// primitive.D{{"foo", "bar"}, {"hello", "world"}, {"pi", 3.14159}} -// -// This type should be used in situations where order matters, such as MongoDB commands. If the -// order is not important, a map is more comfortable and concise. +// bson.D{{"foo", "bar"}, {"hello", "world"}, {"pi", 3.14159}} type D []E // Map creates a map from the elements of the D. @@ -48,26 +44,20 @@ type E struct { Value interface{} } -// M is an unordered, concise representation of a BSON Document. It should generally be used to -// serialize BSON when the order of the elements of a BSON document do not matter. If the element -// order matters, use a D instead. +// M is an unordered representation of a BSON document. This type should be used when the order of the elements does not +// matter. This type is handled as a regular map[string]interface{} when encoding and decoding. Elements will be +// serialized in an undefined, random order. If the order of the elements matters, a D should be used instead. // // Example usage: // -// primitive.M{"foo": "bar", "hello": "world", "pi": 3.14159} -// -// This type is handled in the encoders as a regular map[string]interface{}. The elements will be -// serialized in an undefined, random order, and the order will be different each time. +// bson.M{"foo": "bar", "hello": "world", "pi": 3.14159} type M map[string]interface{} -// An A represents a BSON array. This type can be used to represent a BSON array in a concise and -// readable manner. It should generally be used when serializing to BSON. For deserializing, the -// RawArray or Array types should be used. +// An A is an ordered representation of a BSON array. // // Example usage: // -// primitive.A{"bar", "world", 3.14159, primitive.D{{"qux", 12345}}} -// +// bson.A{"bar", "world", 3.14159, bson.D{{"qux", 12345}}} type A []interface{} func formatDouble(f float64) string { diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/byte_slice_codec.go b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/byte_slice_codec.go new file mode 100644 index 0000000000..9eed911ac7 --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/byte_slice_codec.go @@ -0,0 +1,87 @@ +// Copyright (C) MongoDB, Inc. 2017-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package bsoncodec + +import ( + "fmt" + "reflect" + + "go.mongodb.org/mongo-driver/bson/bsonoptions" + "go.mongodb.org/mongo-driver/bson/bsonrw" + "go.mongodb.org/mongo-driver/bson/bsontype" +) + +var defaultByteSliceCodec = NewByteSliceCodec() + +// ByteSliceCodec is the Codec used for []byte values. +type ByteSliceCodec struct { + EncodeNilAsEmpty bool +} + +var _ ValueCodec = &ByteSliceCodec{} + +// NewByteSliceCodec returns a StringCodec with options opts. +func NewByteSliceCodec(opts ...*bsonoptions.ByteSliceCodecOptions) *ByteSliceCodec { + byteSliceOpt := bsonoptions.MergeByteSliceCodecOptions(opts...) + codec := ByteSliceCodec{} + if byteSliceOpt.EncodeNilAsEmpty != nil { + codec.EncodeNilAsEmpty = *byteSliceOpt.EncodeNilAsEmpty + } + return &codec +} + +// EncodeValue is the ValueEncoder for []byte. +func (bsc *ByteSliceCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error { + if !val.IsValid() || val.Type() != tByteSlice { + return ValueEncoderError{Name: "ByteSliceEncodeValue", Types: []reflect.Type{tByteSlice}, Received: val} + } + if val.IsNil() && !bsc.EncodeNilAsEmpty { + return vw.WriteNull() + } + return vw.WriteBinary(val.Interface().([]byte)) +} + +// DecodeValue is the ValueDecoder for []byte. +func (bsc *ByteSliceCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { + if !val.CanSet() || val.Type() != tByteSlice { + return ValueDecoderError{Name: "ByteSliceDecodeValue", Types: []reflect.Type{tByteSlice}, Received: val} + } + + var data []byte + var err error + switch vrType := vr.Type(); vrType { + case bsontype.String: + str, err := vr.ReadString() + if err != nil { + return err + } + data = []byte(str) + case bsontype.Symbol: + sym, err := vr.ReadSymbol() + if err != nil { + return err + } + data = []byte(sym) + case bsontype.Binary: + var subtype byte + data, subtype, err = vr.ReadBinary() + if err != nil { + return err + } + if subtype != bsontype.BinaryGeneric && subtype != bsontype.BinaryBinaryOld { + return fmt.Errorf("ByteSliceDecodeValue can only be used to decode subtype 0x00 or 0x02 for %s, got %v", bsontype.Binary, subtype) + } + case bsontype.Null: + val.Set(reflect.Zero(val.Type())) + return vr.ReadNull() + default: + return fmt.Errorf("cannot decode %v into a []byte", vrType) + } + + val.Set(reflect.ValueOf(data)) + return nil +} diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/cond_addr_codec.go b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/cond_addr_codec.go new file mode 100644 index 0000000000..cb8180f25c --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/cond_addr_codec.go @@ -0,0 +1,63 @@ +// Copyright (C) MongoDB, Inc. 2017-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package bsoncodec + +import ( + "reflect" + + "go.mongodb.org/mongo-driver/bson/bsonrw" +) + +// condAddrEncoder is the encoder used when a pointer to the encoding value has an encoder. +type condAddrEncoder struct { + canAddrEnc ValueEncoder + elseEnc ValueEncoder +} + +var _ ValueEncoder = (*condAddrEncoder)(nil) + +// newCondAddrEncoder returns an condAddrEncoder. +func newCondAddrEncoder(canAddrEnc, elseEnc ValueEncoder) *condAddrEncoder { + encoder := condAddrEncoder{canAddrEnc: canAddrEnc, elseEnc: elseEnc} + return &encoder +} + +// EncodeValue is the ValueEncoderFunc for a value that may be addressable. +func (cae *condAddrEncoder) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error { + if val.CanAddr() { + return cae.canAddrEnc.EncodeValue(ec, vw, val) + } + if cae.elseEnc != nil { + return cae.elseEnc.EncodeValue(ec, vw, val) + } + return ErrNoEncoder{Type: val.Type()} +} + +// condAddrDecoder is the decoder used when a pointer to the value has a decoder. +type condAddrDecoder struct { + canAddrDec ValueDecoder + elseDec ValueDecoder +} + +var _ ValueDecoder = (*condAddrDecoder)(nil) + +// newCondAddrDecoder returns an CondAddrDecoder. +func newCondAddrDecoder(canAddrDec, elseDec ValueDecoder) *condAddrDecoder { + decoder := condAddrDecoder{canAddrDec: canAddrDec, elseDec: elseDec} + return &decoder +} + +// DecodeValue is the ValueDecoderFunc for a value that may be addressable. +func (cad *condAddrDecoder) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { + if val.CanAddr() { + return cad.canAddrDec.DecodeValue(dc, vr, val) + } + if cad.elseDec != nil { + return cad.elseDec.DecodeValue(dc, vr, val) + } + return ErrNoDecoder{Type: val.Type()} +} diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/default_value_decoders.go b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/default_value_decoders.go index 65cd1c0a55..52d2365a34 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/default_value_decoders.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/default_value_decoders.go @@ -40,46 +40,44 @@ func (dvd DefaultValueDecoders) RegisterDefaultDecoders(rb *RegistryBuilder) { } rb. - RegisterDecoder(tBinary, ValueDecoderFunc(dvd.BinaryDecodeValue)). - RegisterDecoder(tUndefined, ValueDecoderFunc(dvd.UndefinedDecodeValue)). - RegisterDecoder(tDateTime, ValueDecoderFunc(dvd.DateTimeDecodeValue)). - RegisterDecoder(tNull, ValueDecoderFunc(dvd.NullDecodeValue)). - RegisterDecoder(tRegex, ValueDecoderFunc(dvd.RegexDecodeValue)). - RegisterDecoder(tDBPointer, ValueDecoderFunc(dvd.DBPointerDecodeValue)). - RegisterDecoder(tTimestamp, ValueDecoderFunc(dvd.TimestampDecodeValue)). - RegisterDecoder(tMinKey, ValueDecoderFunc(dvd.MinKeyDecodeValue)). - RegisterDecoder(tMaxKey, ValueDecoderFunc(dvd.MaxKeyDecodeValue)). - RegisterDecoder(tJavaScript, ValueDecoderFunc(dvd.JavaScriptDecodeValue)). - RegisterDecoder(tSymbol, ValueDecoderFunc(dvd.SymbolDecodeValue)). - RegisterDecoder(tByteSlice, ValueDecoderFunc(dvd.ByteSliceDecodeValue)). - RegisterDecoder(tTime, ValueDecoderFunc(dvd.TimeDecodeValue)). - RegisterDecoder(tEmpty, ValueDecoderFunc(dvd.EmptyInterfaceDecodeValue)). - RegisterDecoder(tOID, ValueDecoderFunc(dvd.ObjectIDDecodeValue)). - RegisterDecoder(tDecimal, ValueDecoderFunc(dvd.Decimal128DecodeValue)). - RegisterDecoder(tJSONNumber, ValueDecoderFunc(dvd.JSONNumberDecodeValue)). - RegisterDecoder(tURL, ValueDecoderFunc(dvd.URLDecodeValue)). - RegisterDecoder(tValueUnmarshaler, ValueDecoderFunc(dvd.ValueUnmarshalerDecodeValue)). - RegisterDecoder(tUnmarshaler, ValueDecoderFunc(dvd.UnmarshalerDecodeValue)). - RegisterDecoder(tCoreDocument, ValueDecoderFunc(dvd.CoreDocumentDecodeValue)). - RegisterDecoder(tCodeWithScope, ValueDecoderFunc(dvd.CodeWithScopeDecodeValue)). + RegisterTypeDecoder(tBinary, ValueDecoderFunc(dvd.BinaryDecodeValue)). + RegisterTypeDecoder(tUndefined, ValueDecoderFunc(dvd.UndefinedDecodeValue)). + RegisterTypeDecoder(tDateTime, ValueDecoderFunc(dvd.DateTimeDecodeValue)). + RegisterTypeDecoder(tNull, ValueDecoderFunc(dvd.NullDecodeValue)). + RegisterTypeDecoder(tRegex, ValueDecoderFunc(dvd.RegexDecodeValue)). + RegisterTypeDecoder(tDBPointer, ValueDecoderFunc(dvd.DBPointerDecodeValue)). + RegisterTypeDecoder(tTimestamp, ValueDecoderFunc(dvd.TimestampDecodeValue)). + RegisterTypeDecoder(tMinKey, ValueDecoderFunc(dvd.MinKeyDecodeValue)). + RegisterTypeDecoder(tMaxKey, ValueDecoderFunc(dvd.MaxKeyDecodeValue)). + RegisterTypeDecoder(tJavaScript, ValueDecoderFunc(dvd.JavaScriptDecodeValue)). + RegisterTypeDecoder(tSymbol, ValueDecoderFunc(dvd.SymbolDecodeValue)). + RegisterTypeDecoder(tByteSlice, defaultByteSliceCodec). + RegisterTypeDecoder(tTime, defaultTimeCodec). + RegisterTypeDecoder(tEmpty, defaultEmptyInterfaceCodec). + RegisterTypeDecoder(tOID, ValueDecoderFunc(dvd.ObjectIDDecodeValue)). + RegisterTypeDecoder(tDecimal, ValueDecoderFunc(dvd.Decimal128DecodeValue)). + RegisterTypeDecoder(tJSONNumber, ValueDecoderFunc(dvd.JSONNumberDecodeValue)). + RegisterTypeDecoder(tURL, ValueDecoderFunc(dvd.URLDecodeValue)). + RegisterTypeDecoder(tCoreDocument, ValueDecoderFunc(dvd.CoreDocumentDecodeValue)). + RegisterTypeDecoder(tCodeWithScope, ValueDecoderFunc(dvd.CodeWithScopeDecodeValue)). RegisterDefaultDecoder(reflect.Bool, ValueDecoderFunc(dvd.BooleanDecodeValue)). RegisterDefaultDecoder(reflect.Int, ValueDecoderFunc(dvd.IntDecodeValue)). RegisterDefaultDecoder(reflect.Int8, ValueDecoderFunc(dvd.IntDecodeValue)). RegisterDefaultDecoder(reflect.Int16, ValueDecoderFunc(dvd.IntDecodeValue)). RegisterDefaultDecoder(reflect.Int32, ValueDecoderFunc(dvd.IntDecodeValue)). RegisterDefaultDecoder(reflect.Int64, ValueDecoderFunc(dvd.IntDecodeValue)). - RegisterDefaultDecoder(reflect.Uint, ValueDecoderFunc(dvd.UintDecodeValue)). - RegisterDefaultDecoder(reflect.Uint8, ValueDecoderFunc(dvd.UintDecodeValue)). - RegisterDefaultDecoder(reflect.Uint16, ValueDecoderFunc(dvd.UintDecodeValue)). - RegisterDefaultDecoder(reflect.Uint32, ValueDecoderFunc(dvd.UintDecodeValue)). - RegisterDefaultDecoder(reflect.Uint64, ValueDecoderFunc(dvd.UintDecodeValue)). + RegisterDefaultDecoder(reflect.Uint, defaultUIntCodec). + RegisterDefaultDecoder(reflect.Uint8, defaultUIntCodec). + RegisterDefaultDecoder(reflect.Uint16, defaultUIntCodec). + RegisterDefaultDecoder(reflect.Uint32, defaultUIntCodec). + RegisterDefaultDecoder(reflect.Uint64, defaultUIntCodec). RegisterDefaultDecoder(reflect.Float32, ValueDecoderFunc(dvd.FloatDecodeValue)). RegisterDefaultDecoder(reflect.Float64, ValueDecoderFunc(dvd.FloatDecodeValue)). RegisterDefaultDecoder(reflect.Array, ValueDecoderFunc(dvd.ArrayDecodeValue)). - RegisterDefaultDecoder(reflect.Map, ValueDecoderFunc(dvd.MapDecodeValue)). - RegisterDefaultDecoder(reflect.Slice, ValueDecoderFunc(dvd.SliceDecodeValue)). - RegisterDefaultDecoder(reflect.String, ValueDecoderFunc(dvd.StringDecodeValue)). - RegisterDefaultDecoder(reflect.Struct, &StructCodec{cache: make(map[reflect.Type]*structDescription), parser: DefaultStructTagParser}). + RegisterDefaultDecoder(reflect.Map, defaultMapCodec). + RegisterDefaultDecoder(reflect.Slice, defaultSliceCodec). + RegisterDefaultDecoder(reflect.String, defaultStringCodec). + RegisterDefaultDecoder(reflect.Struct, defaultStructCodec). RegisterDefaultDecoder(reflect.Ptr, NewPointerCodec()). RegisterTypeMapEntry(bsontype.Double, tFloat64). RegisterTypeMapEntry(bsontype.String, tString). @@ -100,28 +98,68 @@ func (dvd DefaultValueDecoders) RegisterDefaultDecoders(rb *RegistryBuilder) { RegisterTypeMapEntry(bsontype.Decimal128, tDecimal). RegisterTypeMapEntry(bsontype.MinKey, tMinKey). RegisterTypeMapEntry(bsontype.MaxKey, tMaxKey). - RegisterTypeMapEntry(bsontype.Type(0), tD) + RegisterTypeMapEntry(bsontype.Type(0), tD). + RegisterTypeMapEntry(bsontype.EmbeddedDocument, tD). + RegisterHookDecoder(tValueUnmarshaler, ValueDecoderFunc(dvd.ValueUnmarshalerDecodeValue)). + RegisterHookDecoder(tUnmarshaler, ValueDecoderFunc(dvd.UnmarshalerDecodeValue)) } // BooleanDecodeValue is the ValueDecoderFunc for bool types. func (dvd DefaultValueDecoders) BooleanDecodeValue(dctx DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { - if vr.Type() != bsontype.Boolean { - return fmt.Errorf("cannot decode %v into a boolean", vr.Type()) - } if !val.IsValid() || !val.CanSet() || val.Kind() != reflect.Bool { return ValueDecoderError{Name: "BooleanDecodeValue", Kinds: []reflect.Kind{reflect.Bool}, Received: val} } - b, err := vr.ReadBoolean() + var b bool + var err error + switch vrType := vr.Type(); vrType { + case bsontype.Int32: + i32, err := vr.ReadInt32() + if err != nil { + return err + } + b = (i32 != 0) + case bsontype.Int64: + i64, err := vr.ReadInt64() + if err != nil { + return err + } + b = (i64 != 0) + case bsontype.Double: + f64, err := vr.ReadDouble() + if err != nil { + return err + } + b = (f64 != 0) + case bsontype.Boolean: + b, err = vr.ReadBoolean() + if err != nil { + return err + } + case bsontype.Null: + if err = vr.ReadNull(); err != nil { + return err + } + default: + return fmt.Errorf("cannot decode %v into a boolean", vrType) + } val.SetBool(b) - return err + return nil } -// IntDecodeValue is the ValueDecoderFunc for bool types. +// IntDecodeValue is the ValueDecoderFunc for int types. func (dvd DefaultValueDecoders) IntDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { + if !val.CanSet() { + return ValueDecoderError{ + Name: "IntDecodeValue", + Kinds: []reflect.Kind{reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int}, + Received: val, + } + } + var i64 int64 var err error - switch vr.Type() { + switch vrType := vr.Type(); vrType { case bsontype.Int32: i32, err := vr.ReadInt32() if err != nil { @@ -145,16 +183,20 @@ func (dvd DefaultValueDecoders) IntDecodeValue(dc DecodeContext, vr bsonrw.Value return fmt.Errorf("%g overflows int64", f64) } i64 = int64(f64) - default: - return fmt.Errorf("cannot decode %v into an integer type", vr.Type()) - } - - if !val.CanSet() { - return ValueDecoderError{ - Name: "IntDecodeValue", - Kinds: []reflect.Kind{reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int}, - Received: val, + case bsontype.Boolean: + b, err := vr.ReadBoolean() + if err != nil { + return err + } + if b { + i64 = 1 + } + case bsontype.Null: + if err = vr.ReadNull(); err != nil { + return err } + default: + return fmt.Errorf("cannot decode %v into an integer type", vrType) } switch val.Kind() { @@ -188,6 +230,8 @@ func (dvd DefaultValueDecoders) IntDecodeValue(dc DecodeContext, vr bsonrw.Value } // UintDecodeValue is the ValueDecoderFunc for uint types. +// This method is deprecated and does not have any stability guarantees. It may be removed in the +// future. Use UIntCodec.DecodeValue instead. func (dvd DefaultValueDecoders) UintDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { var i64 int64 var err error @@ -215,6 +259,14 @@ func (dvd DefaultValueDecoders) UintDecodeValue(dc DecodeContext, vr bsonrw.Valu return fmt.Errorf("%g overflows int64", f64) } i64 = int64(f64) + case bsontype.Boolean: + b, err := vr.ReadBoolean() + if err != nil { + return err + } + if b { + i64 = 1 + } default: return fmt.Errorf("cannot decode %v into an integer type", vr.Type()) } @@ -262,9 +314,17 @@ func (dvd DefaultValueDecoders) UintDecodeValue(dc DecodeContext, vr bsonrw.Valu // FloatDecodeValue is the ValueDecoderFunc for float types. func (dvd DefaultValueDecoders) FloatDecodeValue(ec DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { + if !val.CanSet() { + return ValueDecoderError{ + Name: "FloatDecodeValue", + Kinds: []reflect.Kind{reflect.Float32, reflect.Float64}, + Received: val, + } + } + var f float64 var err error - switch vr.Type() { + switch vrType := vr.Type(); vrType { case bsontype.Int32: i32, err := vr.ReadInt32() if err != nil { @@ -282,12 +342,20 @@ func (dvd DefaultValueDecoders) FloatDecodeValue(ec DecodeContext, vr bsonrw.Val if err != nil { return err } + case bsontype.Boolean: + b, err := vr.ReadBoolean() + if err != nil { + return err + } + if b { + f = 1 + } + case bsontype.Null: + if err = vr.ReadNull(); err != nil { + return err + } default: - return fmt.Errorf("cannot decode %v into a float32 or float64 type", vr.Type()) - } - - if !val.CanSet() { - return ValueDecoderError{Name: "FloatDecodeValue", Kinds: []reflect.Kind{reflect.Float32, reflect.Float64}, Received: val} + return fmt.Errorf("cannot decode %v into a float32 or float64 type", vrType) } switch val.Kind() { @@ -305,6 +373,8 @@ func (dvd DefaultValueDecoders) FloatDecodeValue(ec DecodeContext, vr bsonrw.Val } // StringDecodeValue is the ValueDecoderFunc for string types. +// This method is deprecated and does not have any stability guarantees. It may be removed in the +// future. Use StringCodec.DecodeValue instead. func (dvd DefaultValueDecoders) StringDecodeValue(dctx DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { var str string var err error @@ -329,18 +399,23 @@ func (dvd DefaultValueDecoders) StringDecodeValue(dctx DecodeContext, vr bsonrw. // JavaScriptDecodeValue is the ValueDecoderFunc for the primitive.JavaScript type. func (DefaultValueDecoders) JavaScriptDecodeValue(dctx DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { if !val.CanSet() || val.Type() != tJavaScript { - return ValueDecoderError{Name: "BinaryDecodeValue", Types: []reflect.Type{tJavaScript}, Received: val} + return ValueDecoderError{Name: "JavaScriptDecodeValue", Types: []reflect.Type{tJavaScript}, Received: val} } - if vr.Type() != bsontype.JavaScript { - return fmt.Errorf("cannot decode %v into a primitive.JavaScript", vr.Type()) + var js string + var err error + switch vrType := vr.Type(); vrType { + case bsontype.JavaScript: + js, err = vr.ReadJavascript() + case bsontype.Null: + err = vr.ReadNull() + default: + return fmt.Errorf("cannot decode %v into a primitive.JavaScript", vrType) } - js, err := vr.ReadJavascript() if err != nil { return err } - val.SetString(js) return nil } @@ -348,16 +423,37 @@ func (DefaultValueDecoders) JavaScriptDecodeValue(dctx DecodeContext, vr bsonrw. // SymbolDecodeValue is the ValueDecoderFunc for the primitive.Symbol type. func (DefaultValueDecoders) SymbolDecodeValue(dctx DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { if !val.CanSet() || val.Type() != tSymbol { - return ValueDecoderError{Name: "BinaryDecodeValue", Types: []reflect.Type{tSymbol}, Received: val} - } - - if vr.Type() != bsontype.Symbol { - return fmt.Errorf("cannot decode %v into a primitive.Symbol", vr.Type()) + return ValueDecoderError{Name: "SymbolDecodeValue", Types: []reflect.Type{tSymbol}, Received: val} } - symbol, err := vr.ReadSymbol() - if err != nil { - return err + var symbol string + var err error + switch vrType := vr.Type(); vrType { + case bsontype.String: + symbol, err = vr.ReadString() + if err != nil { + return err + } + case bsontype.Symbol: + symbol, err = vr.ReadSymbol() + if err != nil { + return err + } + case bsontype.Binary: + data, subtype, err := vr.ReadBinary() + if err != nil { + return err + } + if subtype != bsontype.BinaryGeneric && subtype != bsontype.BinaryBinaryOld { + return fmt.Errorf("SymbolDecodeValue can only be used to decode subtype 0x00 or 0x02 for %s, got %v", bsontype.Binary, subtype) + } + symbol = string(data) + case bsontype.Null: + if err = vr.ReadNull(); err != nil { + return err + } + default: + return fmt.Errorf("cannot decode %v into a primitive.Symbol", vrType) } val.SetString(symbol) @@ -370,15 +466,21 @@ func (DefaultValueDecoders) BinaryDecodeValue(dc DecodeContext, vr bsonrw.ValueR return ValueDecoderError{Name: "BinaryDecodeValue", Types: []reflect.Type{tBinary}, Received: val} } - if vr.Type() != bsontype.Binary { - return fmt.Errorf("cannot decode %v into a Binary", vr.Type()) + var data []byte + var subtype byte + var err error + switch vrType := vr.Type(); vrType { + case bsontype.Binary: + data, subtype, err = vr.ReadBinary() + case bsontype.Null: + err = vr.ReadNull() + default: + return fmt.Errorf("cannot decode %v into a Binary", vrType) } - data, subtype, err := vr.ReadBinary() if err != nil { return err } - val.Set(reflect.ValueOf(primitive.Binary{Subtype: subtype, Data: data})) return nil } @@ -389,12 +491,21 @@ func (DefaultValueDecoders) UndefinedDecodeValue(dc DecodeContext, vr bsonrw.Val return ValueDecoderError{Name: "UndefinedDecodeValue", Types: []reflect.Type{tUndefined}, Received: val} } - if vr.Type() != bsontype.Undefined { + var err error + switch vrType := vr.Type(); vrType { + case bsontype.Undefined: + err = vr.ReadUndefined() + case bsontype.Null: + err = vr.ReadNull() + default: return fmt.Errorf("cannot decode %v into an Undefined", vr.Type()) } + if err != nil { + return err + } val.Set(reflect.ValueOf(primitive.Undefined{})) - return vr.ReadUndefined() + return nil } // ObjectIDDecodeValue is the ValueDecoderFunc for primitive.ObjectID. @@ -403,12 +514,34 @@ func (dvd DefaultValueDecoders) ObjectIDDecodeValue(dc DecodeContext, vr bsonrw. return ValueDecoderError{Name: "ObjectIDDecodeValue", Types: []reflect.Type{tOID}, Received: val} } - if vr.Type() != bsontype.ObjectID { - return fmt.Errorf("cannot decode %v into an ObjectID", vr.Type()) + var oid primitive.ObjectID + var err error + switch vrType := vr.Type(); vrType { + case bsontype.ObjectID: + oid, err = vr.ReadObjectID() + if err != nil { + return err + } + case bsontype.String: + str, err := vr.ReadString() + if err != nil { + return err + } + if len(str) != 12 { + return fmt.Errorf("an ObjectID string must be exactly 12 bytes long (got %v)", len(str)) + } + byteArr := []byte(str) + copy(oid[:], byteArr) + case bsontype.Null: + if err = vr.ReadNull(); err != nil { + return err + } + default: + return fmt.Errorf("cannot decode %v into an ObjectID", vrType) } - oid, err := vr.ReadObjectID() + val.Set(reflect.ValueOf(oid)) - return err + return nil } // DateTimeDecodeValue is the ValueDecoderFunc for DateTime. @@ -417,15 +550,20 @@ func (DefaultValueDecoders) DateTimeDecodeValue(dc DecodeContext, vr bsonrw.Valu return ValueDecoderError{Name: "DateTimeDecodeValue", Types: []reflect.Type{tDateTime}, Received: val} } - if vr.Type() != bsontype.DateTime { - return fmt.Errorf("cannot decode %v into a DateTime", vr.Type()) + var dt int64 + var err error + switch vrType := vr.Type(); vrType { + case bsontype.DateTime: + dt, err = vr.ReadDateTime() + case bsontype.Null: + err = vr.ReadNull() + default: + return fmt.Errorf("cannot decode %v into a DateTime", vrType) } - dt, err := vr.ReadDateTime() if err != nil { return err } - val.Set(reflect.ValueOf(primitive.DateTime(dt))) return nil } @@ -436,8 +574,8 @@ func (DefaultValueDecoders) NullDecodeValue(dc DecodeContext, vr bsonrw.ValueRea return ValueDecoderError{Name: "NullDecodeValue", Types: []reflect.Type{tNull}, Received: val} } - if vr.Type() != bsontype.Null { - return fmt.Errorf("cannot decode %v into a Null", vr.Type()) + if vrType := vr.Type(); vrType != bsontype.Null { + return fmt.Errorf("cannot decode %v into a Null", vrType) } val.Set(reflect.ValueOf(primitive.Null{})) @@ -450,15 +588,20 @@ func (DefaultValueDecoders) RegexDecodeValue(dc DecodeContext, vr bsonrw.ValueRe return ValueDecoderError{Name: "RegexDecodeValue", Types: []reflect.Type{tRegex}, Received: val} } - if vr.Type() != bsontype.Regex { - return fmt.Errorf("cannot decode %v into a Regex", vr.Type()) + var pattern, options string + var err error + switch vrType := vr.Type(); vrType { + case bsontype.Regex: + pattern, options, err = vr.ReadRegex() + case bsontype.Null: + err = vr.ReadNull() + default: + return fmt.Errorf("cannot decode %v into a Regex", vrType) } - pattern, options, err := vr.ReadRegex() if err != nil { return err } - val.Set(reflect.ValueOf(primitive.Regex{Pattern: pattern, Options: options})) return nil } @@ -469,15 +612,21 @@ func (DefaultValueDecoders) DBPointerDecodeValue(dc DecodeContext, vr bsonrw.Val return ValueDecoderError{Name: "DBPointerDecodeValue", Types: []reflect.Type{tDBPointer}, Received: val} } - if vr.Type() != bsontype.DBPointer { - return fmt.Errorf("cannot decode %v into a DBPointer", vr.Type()) + var ns string + var pointer primitive.ObjectID + var err error + switch vrType := vr.Type(); vrType { + case bsontype.DBPointer: + ns, pointer, err = vr.ReadDBPointer() + case bsontype.Null: + err = vr.ReadNull() + default: + return fmt.Errorf("cannot decode %v into a DBPointer", vrType) } - ns, pointer, err := vr.ReadDBPointer() if err != nil { return err } - val.Set(reflect.ValueOf(primitive.DBPointer{DB: ns, Pointer: pointer})) return nil } @@ -488,15 +637,20 @@ func (DefaultValueDecoders) TimestampDecodeValue(dc DecodeContext, vr bsonrw.Val return ValueDecoderError{Name: "TimestampDecodeValue", Types: []reflect.Type{tTimestamp}, Received: val} } - if vr.Type() != bsontype.Timestamp { - return fmt.Errorf("cannot decode %v into a Timestamp", vr.Type()) + var t, incr uint32 + var err error + switch vrType := vr.Type(); vrType { + case bsontype.Timestamp: + t, incr, err = vr.ReadTimestamp() + case bsontype.Null: + err = vr.ReadNull() + default: + return fmt.Errorf("cannot decode %v into a Timestamp", vrType) } - t, incr, err := vr.ReadTimestamp() if err != nil { return err } - val.Set(reflect.ValueOf(primitive.Timestamp{T: t, I: incr})) return nil } @@ -507,12 +661,21 @@ func (DefaultValueDecoders) MinKeyDecodeValue(dc DecodeContext, vr bsonrw.ValueR return ValueDecoderError{Name: "MinKeyDecodeValue", Types: []reflect.Type{tMinKey}, Received: val} } - if vr.Type() != bsontype.MinKey { + var err error + switch vrType := vr.Type(); vrType { + case bsontype.MinKey: + err = vr.ReadMinKey() + case bsontype.Null: + err = vr.ReadNull() + default: return fmt.Errorf("cannot decode %v into a MinKey", vr.Type()) } + if err != nil { + return err + } val.Set(reflect.ValueOf(primitive.MinKey{})) - return vr.ReadMinKey() + return nil } // MaxKeyDecodeValue is the ValueDecoderFunc for MaxKey. @@ -521,24 +684,43 @@ func (DefaultValueDecoders) MaxKeyDecodeValue(dc DecodeContext, vr bsonrw.ValueR return ValueDecoderError{Name: "MaxKeyDecodeValue", Types: []reflect.Type{tMaxKey}, Received: val} } - if vr.Type() != bsontype.MaxKey { + var err error + switch vrType := vr.Type(); vrType { + case bsontype.MaxKey: + err = vr.ReadMaxKey() + case bsontype.Null: + err = vr.ReadNull() + default: return fmt.Errorf("cannot decode %v into a MaxKey", vr.Type()) } + if err != nil { + return err + } val.Set(reflect.ValueOf(primitive.MaxKey{})) - return vr.ReadMaxKey() + return nil } // Decimal128DecodeValue is the ValueDecoderFunc for primitive.Decimal128. func (dvd DefaultValueDecoders) Decimal128DecodeValue(dctx DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { - if vr.Type() != bsontype.Decimal128 { + if !val.CanSet() || val.Type() != tDecimal { + return ValueDecoderError{Name: "Decimal128DecodeValue", Types: []reflect.Type{tDecimal}, Received: val} + } + + var d128 primitive.Decimal128 + var err error + switch vrType := vr.Type(); vrType { + case bsontype.Decimal128: + d128, err = vr.ReadDecimal128() + case bsontype.Null: + err = vr.ReadNull() + default: return fmt.Errorf("cannot decode %v into a primitive.Decimal128", vr.Type()) } - if !val.CanSet() || val.Type() != tDecimal { - return ValueDecoderError{Name: "Decimal128DecodeValue", Types: []reflect.Type{tDecimal}, Received: val} + if err != nil { + return err } - d128, err := vr.ReadDecimal128() val.Set(reflect.ValueOf(d128)) return err } @@ -549,13 +731,13 @@ func (dvd DefaultValueDecoders) JSONNumberDecodeValue(dc DecodeContext, vr bsonr return ValueDecoderError{Name: "JSONNumberDecodeValue", Types: []reflect.Type{tJSONNumber}, Received: val} } - switch vr.Type() { + switch vrType := vr.Type(); vrType { case bsontype.Double: f64, err := vr.ReadDouble() if err != nil { return err } - val.Set(reflect.ValueOf(json.Number(strconv.FormatFloat(f64, 'g', -1, 64)))) + val.Set(reflect.ValueOf(json.Number(strconv.FormatFloat(f64, 'f', -1, 64)))) case bsontype.Int32: i32, err := vr.ReadInt32() if err != nil { @@ -568,8 +750,13 @@ func (dvd DefaultValueDecoders) JSONNumberDecodeValue(dc DecodeContext, vr bsonr return err } val.Set(reflect.ValueOf(json.Number(strconv.FormatInt(i64, 10)))) + case bsontype.Null: + if err := vr.ReadNull(); err != nil { + return err + } + val.SetString("") default: - return fmt.Errorf("cannot decode %v into a json.Number", vr.Type()) + return fmt.Errorf("cannot decode %v into a json.Number", vrType) } return nil @@ -577,29 +764,37 @@ func (dvd DefaultValueDecoders) JSONNumberDecodeValue(dc DecodeContext, vr bsonr // URLDecodeValue is the ValueDecoderFunc for url.URL. func (dvd DefaultValueDecoders) URLDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { - if vr.Type() != bsontype.String { - return fmt.Errorf("cannot decode %v into a *url.URL", vr.Type()) - } - - str, err := vr.ReadString() - if err != nil { - return err - } - - u, err := url.Parse(str) - if err != nil { - return err - } - if !val.CanSet() || val.Type() != tURL { return ValueDecoderError{Name: "URLDecodeValue", Types: []reflect.Type{tURL}, Received: val} } - val.Set(reflect.ValueOf(u).Elem()) - return nil + switch vrType := vr.Type(); vrType { + case bsontype.String: + str, err := vr.ReadString() + if err != nil { + return err + } + + parsedURL, err := url.Parse(str) + if err != nil { + return err + } + val.Set(reflect.ValueOf(parsedURL).Elem()) + return nil + case bsontype.Null: + if err := vr.ReadNull(); err != nil { + return err + } + val.Set(reflect.ValueOf(url.URL{})) + return nil + default: + return fmt.Errorf("cannot decode %v into a *url.URL", vrType) + } } // TimeDecodeValue is the ValueDecoderFunc for time.Time. +// This method is deprecated and does not have any stability guarantees. It may be removed in the +// future. Use Time.DecodeValue instead. func (dvd DefaultValueDecoders) TimeDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { if vr.Type() != bsontype.DateTime { return fmt.Errorf("cannot decode %v into a time.Time", vr.Type()) @@ -619,6 +814,8 @@ func (dvd DefaultValueDecoders) TimeDecodeValue(dc DecodeContext, vr bsonrw.Valu } // ByteSliceDecodeValue is the ValueDecoderFunc for []byte. +// This method is deprecated and does not have any stability guarantees. It may be removed in the +// future. Use ByteSliceCodec.DecodeValue instead. func (dvd DefaultValueDecoders) ByteSliceDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { if vr.Type() != bsontype.Binary && vr.Type() != bsontype.Null { return fmt.Errorf("cannot decode %v into a []byte", vr.Type()) @@ -646,6 +843,8 @@ func (dvd DefaultValueDecoders) ByteSliceDecodeValue(dc DecodeContext, vr bsonrw } // MapDecodeValue is the ValueDecoderFunc for map[string]* types. +// This method is deprecated and does not have any stability guarantees. It may be removed in the +// future. Use Map.DecodeValue instead. func (dvd DefaultValueDecoders) MapDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { if !val.CanSet() || val.Kind() != reflect.Map || val.Type().Key().Kind() != reflect.String { return ValueDecoderError{Name: "MapDecodeValue", Kinds: []reflect.Kind{reflect.Map}, Received: val} @@ -707,14 +906,37 @@ func (dvd DefaultValueDecoders) ArrayDecodeValue(dc DecodeContext, vr bsonrw.Val return ValueDecoderError{Name: "ArrayDecodeValue", Kinds: []reflect.Kind{reflect.Array}, Received: val} } - switch vr.Type() { + switch vrType := vr.Type(); vrType { case bsontype.Array: case bsontype.Type(0), bsontype.EmbeddedDocument: if val.Type().Elem() != tE { return fmt.Errorf("cannot decode document into %s", val.Type()) } + case bsontype.Binary: + if val.Type().Elem() != tByte { + return fmt.Errorf("ArrayDecodeValue can only be used to decode binary into a byte array, got %v", vrType) + } + data, subtype, err := vr.ReadBinary() + if err != nil { + return err + } + if subtype != bsontype.BinaryGeneric && subtype != bsontype.BinaryBinaryOld { + return fmt.Errorf("ArrayDecodeValue can only be used to decode subtype 0x00 or 0x02 for %s, got %v", bsontype.Binary, subtype) + } + + if len(data) > val.Len() { + return fmt.Errorf("more elements returned in array than can fit inside %s", val.Type()) + } + + for idx, elem := range data { + val.Index(idx).Set(reflect.ValueOf(elem)) + } + return nil + case bsontype.Null: + val.Set(reflect.Zero(val.Type())) + return vr.ReadNull() default: - return fmt.Errorf("cannot decode %v into an array", vr.Type()) + return fmt.Errorf("cannot decode %v into an array", vrType) } var elemsFunc func(DecodeContext, bsonrw.ValueReader, reflect.Value) ([]reflect.Value, error) @@ -731,7 +953,7 @@ func (dvd DefaultValueDecoders) ArrayDecodeValue(dc DecodeContext, vr bsonrw.Val } if len(elems) > val.Len() { - return fmt.Errorf("more elements returned in array than can fit inside %s", val.Type()) + return fmt.Errorf("more elements returned in array than can fit inside %s, got %v elements", val.Type(), len(elems)) } for idx, elem := range elems { @@ -742,6 +964,8 @@ func (dvd DefaultValueDecoders) ArrayDecodeValue(dc DecodeContext, vr bsonrw.Val } // SliceDecodeValue is the ValueDecoderFunc for slice types. +// This method is deprecated and does not have any stability guarantees. It may be removed in the +// future. Use SliceCodec.DecodeValue instead. func (dvd DefaultValueDecoders) SliceDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { if !val.CanSet() || val.Kind() != reflect.Slice { return ValueDecoderError{Name: "SliceDecodeValue", Kinds: []reflect.Kind{reflect.Slice}, Received: val} @@ -851,6 +1075,8 @@ func (dvd DefaultValueDecoders) UnmarshalerDecodeValue(dc DecodeContext, vr bson } // EmptyInterfaceDecodeValue is the ValueDecoderFunc for interface{}. +// This method is deprecated and does not have any stability guarantees. It may be removed in the +// future. Use EmptyInterfaceCodec.DecodeValue instead. func (dvd DefaultValueDecoders) EmptyInterfaceDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { if !val.CanSet() || val.Type() != tEmpty { return ValueDecoderError{Name: "EmptyInterfaceDecodeValue", Types: []reflect.Type{tEmpty}, Received: val} @@ -947,27 +1173,36 @@ func (dvd DefaultValueDecoders) CodeWithScopeDecodeValue(dc DecodeContext, vr bs return ValueDecoderError{Name: "CodeWithScopeDecodeValue", Types: []reflect.Type{tCodeWithScope}, Received: val} } - if vr.Type() != bsontype.CodeWithScope { - return fmt.Errorf("cannot decode %v into a primitive.CodeWithScope", vr.Type()) - } + switch vrType := vr.Type(); vrType { + case bsontype.CodeWithScope: + code, dr, err := vr.ReadCodeWithScope() + if err != nil { + return err + } - code, dr, err := vr.ReadCodeWithScope() - if err != nil { - return err - } + scope := reflect.New(tD).Elem() + elems, err := dvd.decodeElemsFromDocumentReader(dc, dr) + if err != nil { + return err + } - scope := reflect.New(tD).Elem() + scope.Set(reflect.MakeSlice(tD, 0, len(elems))) + scope.Set(reflect.Append(scope, elems...)) - elems, err := dvd.decodeElemsFromDocumentReader(dc, dr) - if err != nil { - return err + val.Set(reflect.ValueOf(primitive.CodeWithScope{ + Code: primitive.JavaScript(code), + Scope: scope.Interface().(primitive.D), + })) + return nil + case bsontype.Null: + if err := vr.ReadNull(); err != nil { + return err + } + val.Set(reflect.ValueOf(primitive.CodeWithScope{})) + return nil + default: + return fmt.Errorf("cannot decode %v into a primitive.CodeWithScope", vrType) } - - scope.Set(reflect.MakeSlice(tD, 0, len(elems))) - scope.Set(reflect.Append(scope, elems...)) - - val.Set(reflect.ValueOf(primitive.CodeWithScope{Code: primitive.JavaScript(code), Scope: scope.Interface().(primitive.D)})) - return nil } func (dvd DefaultValueDecoders) decodeD(dc DecodeContext, vr bsonrw.ValueReader, _ reflect.Value) ([]reflect.Value, error) { diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/default_value_encoders.go b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/default_value_encoders.go index 39ebfc7ecd..08078b304d 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/default_value_encoders.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/default_value_encoders.go @@ -26,6 +26,8 @@ var defaultValueEncoders DefaultValueEncoders var bvwPool = bsonrw.NewBSONValueWriterPool() +var errInvalidValue = errors.New("cannot encode invalid element") + var sliceWriterPool = sync.Pool{ New: func() interface{} { sw := make(bsonrw.SliceWriter, 0, 0) @@ -65,48 +67,48 @@ func (dve DefaultValueEncoders) RegisterDefaultEncoders(rb *RegistryBuilder) { panic(errors.New("argument to RegisterDefaultEncoders must not be nil")) } rb. - RegisterEncoder(tByteSlice, ValueEncoderFunc(dve.ByteSliceEncodeValue)). - RegisterEncoder(tTime, ValueEncoderFunc(dve.TimeEncodeValue)). - RegisterEncoder(tEmpty, ValueEncoderFunc(dve.EmptyInterfaceEncodeValue)). - RegisterEncoder(tOID, ValueEncoderFunc(dve.ObjectIDEncodeValue)). - RegisterEncoder(tDecimal, ValueEncoderFunc(dve.Decimal128EncodeValue)). - RegisterEncoder(tJSONNumber, ValueEncoderFunc(dve.JSONNumberEncodeValue)). - RegisterEncoder(tURL, ValueEncoderFunc(dve.URLEncodeValue)). - RegisterEncoder(tValueMarshaler, ValueEncoderFunc(dve.ValueMarshalerEncodeValue)). - RegisterEncoder(tMarshaler, ValueEncoderFunc(dve.MarshalerEncodeValue)). - RegisterEncoder(tProxy, ValueEncoderFunc(dve.ProxyEncodeValue)). - RegisterEncoder(tJavaScript, ValueEncoderFunc(dve.JavaScriptEncodeValue)). - RegisterEncoder(tSymbol, ValueEncoderFunc(dve.SymbolEncodeValue)). - RegisterEncoder(tBinary, ValueEncoderFunc(dve.BinaryEncodeValue)). - RegisterEncoder(tUndefined, ValueEncoderFunc(dve.UndefinedEncodeValue)). - RegisterEncoder(tDateTime, ValueEncoderFunc(dve.DateTimeEncodeValue)). - RegisterEncoder(tNull, ValueEncoderFunc(dve.NullEncodeValue)). - RegisterEncoder(tRegex, ValueEncoderFunc(dve.RegexEncodeValue)). - RegisterEncoder(tDBPointer, ValueEncoderFunc(dve.DBPointerEncodeValue)). - RegisterEncoder(tTimestamp, ValueEncoderFunc(dve.TimestampEncodeValue)). - RegisterEncoder(tMinKey, ValueEncoderFunc(dve.MinKeyEncodeValue)). - RegisterEncoder(tMaxKey, ValueEncoderFunc(dve.MaxKeyEncodeValue)). - RegisterEncoder(tCoreDocument, ValueEncoderFunc(dve.CoreDocumentEncodeValue)). - RegisterEncoder(tCodeWithScope, ValueEncoderFunc(dve.CodeWithScopeEncodeValue)). + RegisterTypeEncoder(tByteSlice, defaultByteSliceCodec). + RegisterTypeEncoder(tTime, defaultTimeCodec). + RegisterTypeEncoder(tEmpty, defaultEmptyInterfaceCodec). + RegisterTypeEncoder(tOID, ValueEncoderFunc(dve.ObjectIDEncodeValue)). + RegisterTypeEncoder(tDecimal, ValueEncoderFunc(dve.Decimal128EncodeValue)). + RegisterTypeEncoder(tJSONNumber, ValueEncoderFunc(dve.JSONNumberEncodeValue)). + RegisterTypeEncoder(tURL, ValueEncoderFunc(dve.URLEncodeValue)). + RegisterTypeEncoder(tJavaScript, ValueEncoderFunc(dve.JavaScriptEncodeValue)). + RegisterTypeEncoder(tSymbol, ValueEncoderFunc(dve.SymbolEncodeValue)). + RegisterTypeEncoder(tBinary, ValueEncoderFunc(dve.BinaryEncodeValue)). + RegisterTypeEncoder(tUndefined, ValueEncoderFunc(dve.UndefinedEncodeValue)). + RegisterTypeEncoder(tDateTime, ValueEncoderFunc(dve.DateTimeEncodeValue)). + RegisterTypeEncoder(tNull, ValueEncoderFunc(dve.NullEncodeValue)). + RegisterTypeEncoder(tRegex, ValueEncoderFunc(dve.RegexEncodeValue)). + RegisterTypeEncoder(tDBPointer, ValueEncoderFunc(dve.DBPointerEncodeValue)). + RegisterTypeEncoder(tTimestamp, ValueEncoderFunc(dve.TimestampEncodeValue)). + RegisterTypeEncoder(tMinKey, ValueEncoderFunc(dve.MinKeyEncodeValue)). + RegisterTypeEncoder(tMaxKey, ValueEncoderFunc(dve.MaxKeyEncodeValue)). + RegisterTypeEncoder(tCoreDocument, ValueEncoderFunc(dve.CoreDocumentEncodeValue)). + RegisterTypeEncoder(tCodeWithScope, ValueEncoderFunc(dve.CodeWithScopeEncodeValue)). RegisterDefaultEncoder(reflect.Bool, ValueEncoderFunc(dve.BooleanEncodeValue)). RegisterDefaultEncoder(reflect.Int, ValueEncoderFunc(dve.IntEncodeValue)). RegisterDefaultEncoder(reflect.Int8, ValueEncoderFunc(dve.IntEncodeValue)). RegisterDefaultEncoder(reflect.Int16, ValueEncoderFunc(dve.IntEncodeValue)). RegisterDefaultEncoder(reflect.Int32, ValueEncoderFunc(dve.IntEncodeValue)). RegisterDefaultEncoder(reflect.Int64, ValueEncoderFunc(dve.IntEncodeValue)). - RegisterDefaultEncoder(reflect.Uint, ValueEncoderFunc(dve.UintEncodeValue)). - RegisterDefaultEncoder(reflect.Uint8, ValueEncoderFunc(dve.UintEncodeValue)). - RegisterDefaultEncoder(reflect.Uint16, ValueEncoderFunc(dve.UintEncodeValue)). - RegisterDefaultEncoder(reflect.Uint32, ValueEncoderFunc(dve.UintEncodeValue)). - RegisterDefaultEncoder(reflect.Uint64, ValueEncoderFunc(dve.UintEncodeValue)). + RegisterDefaultEncoder(reflect.Uint, defaultUIntCodec). + RegisterDefaultEncoder(reflect.Uint8, defaultUIntCodec). + RegisterDefaultEncoder(reflect.Uint16, defaultUIntCodec). + RegisterDefaultEncoder(reflect.Uint32, defaultUIntCodec). + RegisterDefaultEncoder(reflect.Uint64, defaultUIntCodec). RegisterDefaultEncoder(reflect.Float32, ValueEncoderFunc(dve.FloatEncodeValue)). RegisterDefaultEncoder(reflect.Float64, ValueEncoderFunc(dve.FloatEncodeValue)). RegisterDefaultEncoder(reflect.Array, ValueEncoderFunc(dve.ArrayEncodeValue)). - RegisterDefaultEncoder(reflect.Map, ValueEncoderFunc(dve.MapEncodeValue)). - RegisterDefaultEncoder(reflect.Slice, ValueEncoderFunc(dve.SliceEncodeValue)). - RegisterDefaultEncoder(reflect.String, ValueEncoderFunc(dve.StringEncodeValue)). - RegisterDefaultEncoder(reflect.Struct, &StructCodec{cache: make(map[reflect.Type]*structDescription), parser: DefaultStructTagParser}). - RegisterDefaultEncoder(reflect.Ptr, NewPointerCodec()) + RegisterDefaultEncoder(reflect.Map, defaultMapCodec). + RegisterDefaultEncoder(reflect.Slice, defaultSliceCodec). + RegisterDefaultEncoder(reflect.String, defaultStringCodec). + RegisterDefaultEncoder(reflect.Struct, defaultStructCodec). + RegisterDefaultEncoder(reflect.Ptr, NewPointerCodec()). + RegisterHookEncoder(tValueMarshaler, ValueEncoderFunc(dve.ValueMarshalerEncodeValue)). + RegisterHookEncoder(tMarshaler, ValueEncoderFunc(dve.MarshalerEncodeValue)). + RegisterHookEncoder(tProxy, ValueEncoderFunc(dve.ProxyEncodeValue)) } // BooleanEncodeValue is the ValueEncoderFunc for bool types. @@ -148,6 +150,8 @@ func (dve DefaultValueEncoders) IntEncodeValue(ec EncodeContext, vw bsonrw.Value } // UintEncodeValue is the ValueEncoderFunc for uint types. +// This method is deprecated and does not have any stability guarantees. It may be removed in the +// future. Use UIntCodec.EncodeValue instead. func (dve DefaultValueEncoders) UintEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error { switch val.Kind() { case reflect.Uint8, reflect.Uint16: @@ -181,6 +185,8 @@ func (dve DefaultValueEncoders) FloatEncodeValue(ec EncodeContext, vw bsonrw.Val } // StringEncodeValue is the ValueEncoderFunc for string types. +// This method is deprecated and does not have any stability guarantees. It may be removed in the +// future. Use StringCodec.EncodeValue instead. func (dve DefaultValueEncoders) StringEncodeValue(ectx EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error { if val.Kind() != reflect.String { return ValueEncoderError{ @@ -239,6 +245,8 @@ func (dve DefaultValueEncoders) URLEncodeValue(ec EncodeContext, vw bsonrw.Value } // TimeEncodeValue is the ValueEncoderFunc for time.TIme. +// This method is deprecated and does not have any stability guarantees. It may be removed in the +// future. Use TimeCodec.EncodeValue instead. func (dve DefaultValueEncoders) TimeEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error { if !val.IsValid() || val.Type() != tTime { return ValueEncoderError{Name: "TimeEncodeValue", Types: []reflect.Type{tTime}, Received: val} @@ -248,6 +256,8 @@ func (dve DefaultValueEncoders) TimeEncodeValue(ec EncodeContext, vw bsonrw.Valu } // ByteSliceEncodeValue is the ValueEncoderFunc for []byte. +// This method is deprecated and does not have any stability guarantees. It may be removed in the +// future. Use ByteSliceCodec.EncodeValue instead. func (dve DefaultValueEncoders) ByteSliceEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error { if !val.IsValid() || val.Type() != tByteSlice { return ValueEncoderError{Name: "ByteSliceEncodeValue", Types: []reflect.Type{tByteSlice}, Received: val} @@ -259,6 +269,8 @@ func (dve DefaultValueEncoders) ByteSliceEncodeValue(ec EncodeContext, vw bsonrw } // MapEncodeValue is the ValueEncoderFunc for map[string]* types. +// This method is deprecated and does not have any stability guarantees. It may be removed in the +// future. Use MapCodec.EncodeValue instead. func (dve DefaultValueEncoders) MapEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error { if !val.IsValid() || val.Kind() != reflect.Map || val.Type().Key().Kind() != reflect.String { return ValueEncoderError{Name: "MapEncodeValue", Kinds: []reflect.Kind{reflect.Map}, Received: val} @@ -289,8 +301,9 @@ func (dve DefaultValueEncoders) MapEncodeValue(ec EncodeContext, vw bsonrw.Value // struct codec. func (dve DefaultValueEncoders) mapEncodeValue(ec EncodeContext, dw bsonrw.DocumentWriter, val reflect.Value, collisionFn func(string) bool) error { - encoder, err := ec.LookupEncoder(val.Type().Elem()) - if err != nil { + elemType := val.Type().Elem() + encoder, err := ec.LookupEncoder(elemType) + if err != nil && elemType.Kind() != reflect.Interface { return err } @@ -299,19 +312,33 @@ func (dve DefaultValueEncoders) mapEncodeValue(ec EncodeContext, dw bsonrw.Docum if collisionFn != nil && collisionFn(key.String()) { return fmt.Errorf("Key %s of inlined map conflicts with a struct field name", key) } + + currEncoder, currVal, lookupErr := dve.lookupElementEncoder(ec, encoder, val.MapIndex(key)) + if lookupErr != nil && lookupErr != errInvalidValue { + return lookupErr + } + vw, err := dw.WriteDocumentElement(key.String()) if err != nil { return err } - if enc, ok := encoder.(ValueEncoder); ok { - err = enc.EncodeValue(ec, vw, val.MapIndex(key)) + if lookupErr == errInvalidValue { + err = vw.WriteNull() + if err != nil { + return err + } + continue + } + + if enc, ok := currEncoder.(ValueEncoder); ok { + err = enc.EncodeValue(ec, vw, currVal) if err != nil { return err } continue } - err = encoder.EncodeValue(ec, vw, val.MapIndex(key)) + err = encoder.EncodeValue(ec, vw, currVal) if err != nil { return err } @@ -344,23 +371,46 @@ func (dve DefaultValueEncoders) ArrayEncodeValue(ec EncodeContext, vw bsonrw.Val return dw.WriteDocumentEnd() } + // If we have a []byte we want to treat it as a binary instead of as an array. + if val.Type().Elem() == tByte { + var byteSlice []byte + for idx := 0; idx < val.Len(); idx++ { + byteSlice = append(byteSlice, val.Index(idx).Interface().(byte)) + } + return vw.WriteBinary(byteSlice) + } + aw, err := vw.WriteArray() if err != nil { return err } - encoder, err := ec.LookupEncoder(val.Type().Elem()) - if err != nil { + elemType := val.Type().Elem() + encoder, err := ec.LookupEncoder(elemType) + if err != nil && elemType.Kind() != reflect.Interface { return err } for idx := 0; idx < val.Len(); idx++ { + currEncoder, currVal, lookupErr := dve.lookupElementEncoder(ec, encoder, val.Index(idx)) + if lookupErr != nil && lookupErr != errInvalidValue { + return lookupErr + } + vw, err := aw.WriteArrayElement() if err != nil { return err } - err = encoder.EncodeValue(ec, vw, val.Index(idx)) + if lookupErr == errInvalidValue { + err = vw.WriteNull() + if err != nil { + return err + } + continue + } + + err = currEncoder.EncodeValue(ec, vw, currVal) if err != nil { return err } @@ -369,6 +419,8 @@ func (dve DefaultValueEncoders) ArrayEncodeValue(ec EncodeContext, vw bsonrw.Val } // SliceEncodeValue is the ValueEncoderFunc for slice types. +// This method is deprecated and does not have any stability guarantees. It may be removed in the +// future. Use SliceCodec.EncodeValue instead. func (dve DefaultValueEncoders) SliceEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error { if !val.IsValid() || val.Kind() != reflect.Slice { return ValueEncoderError{Name: "SliceEncodeValue", Kinds: []reflect.Kind{reflect.Slice}, Received: val} @@ -402,18 +454,32 @@ func (dve DefaultValueEncoders) SliceEncodeValue(ec EncodeContext, vw bsonrw.Val return err } - encoder, err := ec.LookupEncoder(val.Type().Elem()) - if err != nil { + elemType := val.Type().Elem() + encoder, err := ec.LookupEncoder(elemType) + if err != nil && elemType.Kind() != reflect.Interface { return err } for idx := 0; idx < val.Len(); idx++ { + currEncoder, currVal, lookupErr := dve.lookupElementEncoder(ec, encoder, val.Index(idx)) + if lookupErr != nil && lookupErr != errInvalidValue { + return lookupErr + } + vw, err := aw.WriteArrayElement() if err != nil { return err } - err = encoder.EncodeValue(ec, vw, val.Index(idx)) + if lookupErr == errInvalidValue { + err = vw.WriteNull() + if err != nil { + return err + } + continue + } + + err = currEncoder.EncodeValue(ec, vw, currVal) if err != nil { return err } @@ -421,7 +487,22 @@ func (dve DefaultValueEncoders) SliceEncodeValue(ec EncodeContext, vw bsonrw.Val return aw.WriteArrayEnd() } +func (dve DefaultValueEncoders) lookupElementEncoder(ec EncodeContext, origEncoder ValueEncoder, currVal reflect.Value) (ValueEncoder, reflect.Value, error) { + if origEncoder != nil || (currVal.Kind() != reflect.Interface) { + return origEncoder, currVal, nil + } + currVal = currVal.Elem() + if !currVal.IsValid() { + return nil, currVal, errInvalidValue + } + currEncoder, err := ec.LookupEncoder(currVal.Type()) + + return currEncoder, currVal, err +} + // EmptyInterfaceEncodeValue is the ValueEncoderFunc for interface{}. +// This method is deprecated and does not have any stability guarantees. It may be removed in the +// future. Use EmptyInterfaceCodec.EncodeValue instead. func (dve DefaultValueEncoders) EmptyInterfaceEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error { if !val.IsValid() || val.Type() != tEmpty { return ValueEncoderError{Name: "EmptyInterfaceEncodeValue", Types: []reflect.Type{tEmpty}, Received: val} @@ -440,7 +521,18 @@ func (dve DefaultValueEncoders) EmptyInterfaceEncodeValue(ec EncodeContext, vw b // ValueMarshalerEncodeValue is the ValueEncoderFunc for ValueMarshaler implementations. func (dve DefaultValueEncoders) ValueMarshalerEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error { - if !val.IsValid() || !val.Type().Implements(tValueMarshaler) { + // Either val or a pointer to val must implement ValueMarshaler + switch { + case !val.IsValid(): + return ValueEncoderError{Name: "ValueMarshalerEncodeValue", Types: []reflect.Type{tValueMarshaler}, Received: val} + case val.Type().Implements(tValueMarshaler): + // If ValueMarshaler is implemented on a concrete type, make sure that val isn't a nil pointer + if isImplementationNil(val, tValueMarshaler) { + return vw.WriteNull() + } + case reflect.PtrTo(val.Type()).Implements(tValueMarshaler) && val.CanAddr(): + val = val.Addr() + default: return ValueEncoderError{Name: "ValueMarshalerEncodeValue", Types: []reflect.Type{tValueMarshaler}, Received: val} } @@ -455,7 +547,18 @@ func (dve DefaultValueEncoders) ValueMarshalerEncodeValue(ec EncodeContext, vw b // MarshalerEncodeValue is the ValueEncoderFunc for Marshaler implementations. func (dve DefaultValueEncoders) MarshalerEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error { - if !val.IsValid() || !val.Type().Implements(tMarshaler) { + // Either val or a pointer to val must implement Marshaler + switch { + case !val.IsValid(): + return ValueEncoderError{Name: "MarshalerEncodeValue", Types: []reflect.Type{tMarshaler}, Received: val} + case val.Type().Implements(tMarshaler): + // If Marshaler is implemented on a concrete type, make sure that val isn't a nil pointer + if isImplementationNil(val, tMarshaler) { + return vw.WriteNull() + } + case reflect.PtrTo(val.Type()).Implements(tMarshaler) && val.CanAddr(): + val = val.Addr() + default: return ValueEncoderError{Name: "MarshalerEncodeValue", Types: []reflect.Type{tMarshaler}, Received: val} } @@ -470,7 +573,18 @@ func (dve DefaultValueEncoders) MarshalerEncodeValue(ec EncodeContext, vw bsonrw // ProxyEncodeValue is the ValueEncoderFunc for Proxy implementations. func (dve DefaultValueEncoders) ProxyEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error { - if !val.IsValid() || !val.Type().Implements(tProxy) { + // Either val or a pointer to val must implement Proxy + switch { + case !val.IsValid(): + return ValueEncoderError{Name: "ProxyEncodeValue", Types: []reflect.Type{tProxy}, Received: val} + case val.Type().Implements(tProxy): + // If Proxy is implemented on a concrete type, make sure that val isn't a nil pointer + if isImplementationNil(val, tProxy) { + return vw.WriteNull() + } + case reflect.PtrTo(val.Type()).Implements(tProxy) && val.CanAddr(): + val = val.Addr() + default: return ValueEncoderError{Name: "ProxyEncodeValue", Types: []reflect.Type{tProxy}, Received: val} } @@ -646,3 +760,12 @@ func (dve DefaultValueEncoders) CodeWithScopeEncodeValue(ec EncodeContext, vw bs } return dw.WriteDocumentEnd() } + +// isImplementationNil returns if val is a nil pointer and inter is implemented on a concrete type +func isImplementationNil(val reflect.Value, inter reflect.Type) bool { + vt := val.Type() + for vt.Kind() == reflect.Ptr { + vt = vt.Elem() + } + return vt.Implements(inter) && val.Kind() == reflect.Ptr && val.IsNil() +} diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/doc.go b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/doc.go index 978511cbfa..c1e20f9489 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/doc.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/doc.go @@ -27,28 +27,51 @@ // // Registry and RegistryBuilder // -// A Registry is an immutable store for ValueEncoders, ValueDecoders, and a type map. For looking up -// ValueEncoders and Decoders the Registry first attempts to find a ValueEncoder or ValueDecoder for -// the type provided; if one cannot be found it then checks to see if a registered ValueEncoder or -// ValueDecoder exists for an interface the type implements. Finally, the reflect.Kind of the type -// is used to lookup a default ValueEncoder or ValueDecoder for that kind. If no ValueEncoder or -// ValueDecoder can be found, an error is returned. -// -// The Registry also holds a type map. This allows users to retrieve the Go type that should be used -// when decoding a BSON value into an empty interface. This is primarily only used for the empty -// interface ValueDecoder. -// -// A RegistryBuilder is used to construct a Registry. The Register methods are used to associate -// either a reflect.Type or a reflect.Kind with a ValueEncoder or ValueDecoder. A RegistryBuilder -// returned from NewRegistryBuilder contains no registered ValueEncoders nor ValueDecoders and -// contains an empty type map. -// -// The RegisterTypeMapEntry method handles associating a BSON type with a Go type. For example, if -// you want to decode BSON int64 and int32 values into Go int instances, you would do the following: -// -// var regbuilder *RegistryBuilder = ... intType := reflect.TypeOf(int(0)) -// regbuilder.RegisterTypeMapEntry(bsontype.Int64, intType).RegisterTypeMapEntry(bsontype.Int32, -// intType) +// A Registry is an immutable store for ValueEncoders, ValueDecoders, and a type map. See the Registry type +// documentation for examples of registering various custom encoders and decoders. A Registry can be constructed using a +// RegistryBuilder, which handles three main types of codecs: +// +// 1. Type encoders/decoders - These can be registered using the RegisterTypeEncoder and RegisterTypeDecoder methods. +// The registered codec will be invoked when encoding/decoding a value whose type matches the registered type exactly. +// If the registered type is an interface, the codec will be invoked when encoding or decoding values whose type is the +// interface, but not for values with concrete types that implement the interface. +// +// 2. Hook encoders/decoders - These can be registered using the RegisterHookEncoder and RegisterHookDecoder methods. +// These methods only accept interface types and the registered codecs will be invoked when encoding or decoding values +// whose types implement the interface. An example of a hook defined by the driver is bson.Marshaler. The driver will +// call the MarshalBSON method for any value whose type implements bson.Marshaler, regardless of the value's concrete +// type. +// +// 3. Type map entries - This can be used to associate a BSON type with a Go type. These type associations are used when +// decoding into a bson.D/bson.M or a struct field of type interface{}. For example, by default, BSON int32 and int64 +// values decode as Go int32 and int64 instances, respectively, when decoding into a bson.D. The following code would +// change the behavior so these values decode as Go int instances instead: +// +// intType := reflect.TypeOf(int(0)) +// registryBuilder.RegisterTypeMapEntry(bsontype.Int32, intType).RegisterTypeMapEntry(bsontype.Int64, intType) +// +// 4. Kind encoder/decoders - These can be registered using the RegisterDefaultEncoder and RegisterDefaultDecoder +// methods. The registered codec will be invoked when encoding or decoding values whose reflect.Kind matches the +// registered reflect.Kind as long as the value's type doesn't match a registered type or hook encoder/decoder first. +// These methods should be used to change the behavior for all values for a specific kind. +// +// Registry Lookup Procedure +// +// When looking up an encoder in a Registry, the precedence rules are as follows: +// +// 1. A type encoder registered for the exact type of the value. +// +// 2. A hook encoder registered for an interface that is implemented by the value or by a pointer to the value. If the +// value matches multiple hooks (e.g. the type implements bsoncodec.Marshaler and bsoncodec.ValueMarshaler), the first +// one registered will be selected. Note that registries constructed using bson.NewRegistryBuilder have driver-defined +// hooks registered for the bsoncodec.Marshaler, bsoncodec.ValueMarshaler, and bsoncodec.Proxy interfaces, so those +// will take precedence over any new hooks. +// +// 3. A kind encoder registered for the value's kind. +// +// If all of these lookups fail to find an encoder, an error of type ErrNoEncoder is returned. The same precedence +// rules apply for decoders, with the exception that an error of type ErrNoDecoder will be returned if no decoder is +// found. // // DefaultValueEncoders and DefaultValueDecoders // diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/empty_interface_codec.go b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/empty_interface_codec.go new file mode 100644 index 0000000000..c215ec3849 --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/empty_interface_codec.go @@ -0,0 +1,125 @@ +// Copyright (C) MongoDB, Inc. 2017-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package bsoncodec + +import ( + "reflect" + + "go.mongodb.org/mongo-driver/bson/bsonoptions" + "go.mongodb.org/mongo-driver/bson/bsonrw" + "go.mongodb.org/mongo-driver/bson/bsontype" + "go.mongodb.org/mongo-driver/bson/primitive" +) + +var defaultEmptyInterfaceCodec = NewEmptyInterfaceCodec() + +// EmptyInterfaceCodec is the Codec used for interface{} values. +type EmptyInterfaceCodec struct { + DecodeBinaryAsSlice bool +} + +var _ ValueCodec = &EmptyInterfaceCodec{} + +// NewEmptyInterfaceCodec returns a EmptyInterfaceCodec with options opts. +func NewEmptyInterfaceCodec(opts ...*bsonoptions.EmptyInterfaceCodecOptions) *EmptyInterfaceCodec { + interfaceOpt := bsonoptions.MergeEmptyInterfaceCodecOptions(opts...) + + codec := EmptyInterfaceCodec{} + if interfaceOpt.DecodeBinaryAsSlice != nil { + codec.DecodeBinaryAsSlice = *interfaceOpt.DecodeBinaryAsSlice + } + return &codec +} + +// EncodeValue is the ValueEncoderFunc for interface{}. +func (eic EmptyInterfaceCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error { + if !val.IsValid() || val.Type() != tEmpty { + return ValueEncoderError{Name: "EmptyInterfaceEncodeValue", Types: []reflect.Type{tEmpty}, Received: val} + } + + if val.IsNil() { + return vw.WriteNull() + } + encoder, err := ec.LookupEncoder(val.Elem().Type()) + if err != nil { + return err + } + + return encoder.EncodeValue(ec, vw, val.Elem()) +} + +func (eic EmptyInterfaceCodec) getEmptyInterfaceDecodeType(dc DecodeContext, valueType bsontype.Type) (reflect.Type, error) { + isDocument := valueType == bsontype.Type(0) || valueType == bsontype.EmbeddedDocument + if isDocument && 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 + } + + rtype, err := dc.LookupTypeMapEntry(valueType) + if err == nil { + return rtype, nil + } + + if isDocument { + // For documents, fallback to looking up a type map entry for bsontype.Type(0) or bsontype.EmbeddedDocument, + // depending on the original valueType. + var lookupType bsontype.Type + switch valueType { + case bsontype.Type(0): + lookupType = bsontype.EmbeddedDocument + case bsontype.EmbeddedDocument: + lookupType = bsontype.Type(0) + } + + rtype, err = dc.LookupTypeMapEntry(lookupType) + if err == nil { + return rtype, nil + } + } + + return nil, err +} + +// DecodeValue is the ValueDecoderFunc for interface{}. +func (eic EmptyInterfaceCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { + if !val.CanSet() || val.Type() != tEmpty { + return ValueDecoderError{Name: "EmptyInterfaceDecodeValue", Types: []reflect.Type{tEmpty}, Received: val} + } + + rtype, err := eic.getEmptyInterfaceDecodeType(dc, vr.Type()) + if err != nil { + switch vr.Type() { + case bsontype.Null: + val.Set(reflect.Zero(val.Type())) + return vr.ReadNull() + default: + return err + } + } + + decoder, err := dc.LookupDecoder(rtype) + if err != nil { + return err + } + + elem := reflect.New(rtype).Elem() + err = decoder.DecodeValue(dc, vr, elem) + if err != nil { + return err + } + if eic.DecodeBinaryAsSlice && rtype == tBinary { + binElem := elem.Interface().(primitive.Binary) + if binElem.Subtype == bsontype.BinaryGeneric || binElem.Subtype == bsontype.BinaryBinaryOld { + elem = reflect.ValueOf(binElem.Data) + } + } + + val.Set(elem) + return nil +} 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 new file mode 100644 index 0000000000..85ae9c6a19 --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/map_codec.go @@ -0,0 +1,206 @@ +// Copyright (C) MongoDB, Inc. 2017-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package bsoncodec + +import ( + "fmt" + "reflect" + "strconv" + + "go.mongodb.org/mongo-driver/bson/bsonoptions" + "go.mongodb.org/mongo-driver/bson/bsonrw" + "go.mongodb.org/mongo-driver/bson/bsontype" +) + +var defaultMapCodec = NewMapCodec() + +// MapCodec is the Codec used for map values. +type MapCodec struct { + DecodeZerosMap bool + EncodeNilAsEmpty bool +} + +var _ ValueCodec = &MapCodec{} + +// NewMapCodec returns a MapCodec with options opts. +func NewMapCodec(opts ...*bsonoptions.MapCodecOptions) *MapCodec { + mapOpt := bsonoptions.MergeMapCodecOptions(opts...) + + codec := MapCodec{} + if mapOpt.DecodeZerosMap != nil { + codec.DecodeZerosMap = *mapOpt.DecodeZerosMap + } + if mapOpt.EncodeNilAsEmpty != nil { + codec.EncodeNilAsEmpty = *mapOpt.EncodeNilAsEmpty + } + return &codec +} + +// EncodeValue is the ValueEncoder for map[*]* types. +func (mc *MapCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error { + if !val.IsValid() || val.Kind() != reflect.Map { + return ValueEncoderError{Name: "MapEncodeValue", Kinds: []reflect.Kind{reflect.Map}, Received: val} + } + + if val.IsNil() && !mc.EncodeNilAsEmpty { + // 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, + // so just continue. The operations on a map reflection value are valid, so we can call + // MapKeys within mapEncodeValue without a problem. + err := vw.WriteNull() + if err == nil { + return nil + } + } + + dw, err := vw.WriteDocument() + if err != nil { + return err + } + + return mc.mapEncodeValue(ec, dw, val, nil) +} + +// mapEncodeValue handles encoding of the values of a map. The collisionFn returns +// true if the provided key exists, this is mainly used for inline maps in the +// struct codec. +func (mc *MapCodec) mapEncodeValue(ec EncodeContext, dw bsonrw.DocumentWriter, val reflect.Value, collisionFn func(string) bool) error { + + elemType := val.Type().Elem() + encoder, err := ec.LookupEncoder(elemType) + if err != nil && elemType.Kind() != reflect.Interface { + return err + } + + keys := val.MapKeys() + for _, key := range keys { + keyStr := fmt.Sprint(key) + if collisionFn != nil && collisionFn(keyStr) { + return fmt.Errorf("Key %s of inlined map conflicts with a struct field name", key) + } + + currEncoder, currVal, lookupErr := defaultValueEncoders.lookupElementEncoder(ec, encoder, val.MapIndex(key)) + if lookupErr != nil && lookupErr != errInvalidValue { + return lookupErr + } + + vw, err := dw.WriteDocumentElement(keyStr) + if err != nil { + return err + } + + if lookupErr == errInvalidValue { + err = vw.WriteNull() + if err != nil { + return err + } + continue + } + + if enc, ok := currEncoder.(ValueEncoder); ok { + err = enc.EncodeValue(ec, vw, currVal) + if err != nil { + return err + } + continue + } + err = encoder.EncodeValue(ec, vw, currVal) + if err != nil { + return err + } + } + + return dw.WriteDocumentEnd() +} + +// DecodeValue is the ValueDecoder for map[string/decimal]* types. +func (mc *MapCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { + if val.Kind() != reflect.Map || (!val.CanSet() && val.IsNil()) { + return ValueDecoderError{Name: "MapDecodeValue", Kinds: []reflect.Kind{reflect.Map}, Received: val} + } + + switch vrType := vr.Type(); vrType { + case bsontype.Type(0), bsontype.EmbeddedDocument: + case bsontype.Null: + val.Set(reflect.Zero(val.Type())) + return vr.ReadNull() + default: + return fmt.Errorf("cannot decode %v into a %s", vrType, val.Type()) + } + + dr, err := vr.ReadDocument() + if err != nil { + return err + } + + if val.IsNil() { + val.Set(reflect.MakeMap(val.Type())) + } + + if val.Len() > 0 && mc.DecodeZerosMap { + clearMap(val) + } + + eType := val.Type().Elem() + decoder, err := dc.LookupDecoder(eType) + if err != nil { + return err + } + + if eType == tEmpty { + dc.Ancestor = val.Type() + } + + keyType := val.Type().Key() + keyKind := keyType.Kind() + + for { + key, vr, err := dr.ReadElement() + if err == bsonrw.ErrEOD { + break + } + if err != nil { + return err + } + + k := reflect.ValueOf(key) + if keyType != tString { + switch keyKind { + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, + reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, + reflect.Float32, reflect.Float64: + parsed, err := strconv.ParseFloat(k.String(), 64) + if err != nil { + return fmt.Errorf("Map key is defined to be a decimal type (%v) but got error %v", keyKind, err) + } + k = reflect.ValueOf(parsed) + case reflect.String: // if keyType wraps string + default: + return fmt.Errorf("BSON map must have string or decimal keys. Got:%v", val.Type()) + } + + k = k.Convert(keyType) + } + + elem := reflect.New(eType).Elem() + err = decoder.DecodeValue(dc, vr, elem) + if err != nil { + return err + } + + val.SetMapIndex(k, elem) + } + return nil +} + +func clearMap(m reflect.Value) { + var none reflect.Value + for _, k := range m.MapKeys() { + m.SetMapIndex(k, none) + } +} diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/registry.go b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/registry.go index 42e362b6ad..02b63bbe46 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/registry.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/registry.go @@ -8,6 +8,7 @@ package bsoncodec import ( "errors" + "fmt" "reflect" "sync" @@ -115,15 +116,79 @@ func buildDefaultRegistry() *Registry { // RegisterCodec will register the provided ValueCodec for the provided type. func (rb *RegistryBuilder) RegisterCodec(t reflect.Type, codec ValueCodec) *RegistryBuilder { - rb.RegisterEncoder(t, codec) - rb.RegisterDecoder(t, codec) + rb.RegisterTypeEncoder(t, codec) + rb.RegisterTypeDecoder(t, codec) return rb } -// RegisterEncoder will register the provided ValueEncoder to the provided type. +// RegisterTypeEncoder will register the provided ValueEncoder for the provided type. // -// The type registered 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. +// 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 marshalling a type that is that interface. It +// will not be called when marshalling a non-interface type that implements the interface. +func (rb *RegistryBuilder) RegisterTypeEncoder(t reflect.Type, enc ValueEncoder) *RegistryBuilder { + rb.typeEncoders[t] = enc + return rb +} + +// RegisterHookEncoder will register an encoder for the provided interface type t. This encoder will be called when +// marshalling 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. +func (rb *RegistryBuilder) RegisterHookEncoder(t reflect.Type, enc ValueEncoder) *RegistryBuilder { + if t.Kind() != reflect.Interface { + panicStr := fmt.Sprintf("RegisterHookEncoder expects a type with kind reflect.Interface, "+ + "got type %s with kind %s", t, t.Kind()) + panic(panicStr) + } + + for idx, encoder := range rb.interfaceEncoders { + if encoder.i == t { + rb.interfaceEncoders[idx].ve = enc + return rb + } + } + + rb.interfaceEncoders = append(rb.interfaceEncoders, interfaceValueEncoder{i: t, ve: enc}) + return rb +} + +// 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 unmarshalling into a type that is that interface. +// It will not be called when unmarshalling into a non-interface type that implements the interface. +func (rb *RegistryBuilder) RegisterTypeDecoder(t reflect.Type, dec ValueDecoder) *RegistryBuilder { + rb.typeDecoders[t] = dec + return rb +} + +// RegisterHookDecoder will register an decoder for the provided interface type t. This decoder will be called when +// unmarshalling 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. +func (rb *RegistryBuilder) RegisterHookDecoder(t reflect.Type, dec ValueDecoder) *RegistryBuilder { + if t.Kind() != reflect.Interface { + panicStr := fmt.Sprintf("RegisterHookDecoder expects a type with kind reflect.Interface, "+ + "got type %s with kind %s", t, t.Kind()) + panic(panicStr) + } + + for idx, decoder := range rb.interfaceDecoders { + if decoder.i == t { + rb.interfaceDecoders[idx].vd = dec + return rb + } + } + + rb.interfaceDecoders = append(rb.interfaceDecoders, interfaceValueDecoder{i: t, vd: dec}) + return rb +} + +// RegisterEncoder has been deprecated and will be removed in a future major version release. Use RegisterTypeEncoder +// or RegisterHookEncoder instead. func (rb *RegistryBuilder) RegisterEncoder(t reflect.Type, enc ValueEncoder) *RegistryBuilder { if t == tEmpty { rb.typeEncoders[t] = enc @@ -145,10 +210,8 @@ func (rb *RegistryBuilder) RegisterEncoder(t reflect.Type, enc ValueEncoder) *Re return rb } -// RegisterDecoder will register the provided ValueDecoder to the provided type. -// -// The type registered 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. +// RegisterDecoder has been deprecated and will be removed in a future major version release. Use RegisterTypeDecoder +// or RegisterHookDecoder instead. func (rb *RegistryBuilder) RegisterDecoder(t reflect.Type, dec ValueDecoder) *RegistryBuilder { if t == nil { rb.typeDecoders[nil] = dec @@ -192,11 +255,10 @@ func (rb *RegistryBuilder) RegisterDefaultDecoder(kind reflect.Kind, dec ValueDe // mapping is decoding situations where an empty interface is used and a default type needs to be // created and decoded into. // -// NOTE: It is unlikely that registering a type for BSON Embedded Document is actually desired. By -// registering a type map entry for BSON Embedded Document the type registered will be used in any -// case where a BSON Embedded Document will be decoded into an empty interface. For example, if you -// register primitive.M, the EmptyInterface decoder will always use primitive.M, even if an ancestor -// was a primitive.D. +// 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{})) func (rb *RegistryBuilder) RegisterTypeMapEntry(bt bsontype.Type, rt reflect.Type) *RegistryBuilder { rb.typeMap[bt] = rt return rb @@ -240,11 +302,17 @@ func (rb *RegistryBuilder) Build() *Registry { return registry } -// LookupEncoder will inspect the registry for an encoder that satisfies the -// type provided. An encoder registered for a specific type will take -// precedence over an encoder registered for an interface the type satisfies, -// which takes precedence over an encoder for the reflect.Kind of the value. If -// no encoder can be found, an error is returned. +// LookupEncoder inspects the registry for an encoder for the given type. The lookup precendence works as follows: +// +// 1. An encoder registered for the exact type. If the given type represents an interface, an encoder registered using +// RegisterTypeEncoder for the interface will be selected. +// +// 2. An encoder registered using RegisterHookEncoder for an interface implemented by the type or by a pointer to the +// type. +// +// 3. An encoder registered for the reflect.Kind of the value. +// +// If no encoder is found, an error of type ErrNoEncoder is returned. func (r *Registry) LookupEncoder(t reflect.Type) (ValueEncoder, error) { encodererr := ErrNoEncoder{Type: t} r.mu.RLock() @@ -257,7 +325,7 @@ func (r *Registry) LookupEncoder(t reflect.Type) (ValueEncoder, error) { return enc, nil } - enc, found = r.lookupInterfaceEncoder(t) + enc, found = r.lookupInterfaceEncoder(t, true) if found { r.mu.Lock() r.typeEncoders[t] = enc @@ -291,25 +359,38 @@ func (r *Registry) lookupTypeEncoder(t reflect.Type) (ValueEncoder, bool) { return enc, found } -func (r *Registry) lookupInterfaceEncoder(t reflect.Type) (ValueEncoder, bool) { +func (r *Registry) lookupInterfaceEncoder(t reflect.Type, allowAddr bool) (ValueEncoder, bool) { if t == nil { return nil, false } for _, ienc := range r.interfaceEncoders { - if !t.Implements(ienc.i) { - continue + if t.Implements(ienc.i) { + return ienc.ve, true + } + if allowAddr && t.Kind() != reflect.Ptr && reflect.PtrTo(t).Implements(ienc.i) { + // if *t implements an interface, this will catch if t implements an interface further ahead + // in interfaceEncoders + defaultEnc, found := r.lookupInterfaceEncoder(t, false) + if !found { + defaultEnc, _ = r.kindEncoders[t.Kind()] + } + return newCondAddrEncoder(ienc.ve, defaultEnc), true } - - return ienc.ve, true } return nil, false } -// LookupDecoder will inspect the registry for a decoder that satisfies the -// type provided. A decoder registered for a specific type will take -// precedence over a decoder registered for an interface the type satisfies, -// which takes precedence over a decoder for the reflect.Kind of the value. If -// no decoder can be found, an error is returned. +// LookupDecoder inspects the registry for an decoder for the given type. The lookup precendence works as follows: +// +// 1. A decoder registered for the exact type. If the given type represents an interface, a decoder registered using +// RegisterTypeDecoder for the interface will be selected. +// +// 2. A decoder registered using RegisterHookDecoder for an interface implemented by the type or by a pointer to the +// type. +// +// 3. A decoder registered for the reflect.Kind of the value. +// +// If no decoder is found, an error of type ErrNoDecoder is returned. func (r *Registry) LookupDecoder(t reflect.Type) (ValueDecoder, error) { if t == nil { return nil, ErrNilType @@ -325,7 +406,7 @@ func (r *Registry) LookupDecoder(t reflect.Type) (ValueDecoder, error) { return dec, nil } - dec, found = r.lookupInterfaceDecoder(t) + dec, found = r.lookupInterfaceDecoder(t, true) if found { r.mu.Lock() r.typeDecoders[t] = dec @@ -352,13 +433,20 @@ func (r *Registry) lookupTypeDecoder(t reflect.Type) (ValueDecoder, bool) { return dec, found } -func (r *Registry) lookupInterfaceDecoder(t reflect.Type) (ValueDecoder, bool) { +func (r *Registry) lookupInterfaceDecoder(t reflect.Type, allowAddr bool) (ValueDecoder, bool) { for _, idec := range r.interfaceDecoders { - if !t.Implements(idec.i) && !reflect.PtrTo(t).Implements(idec.i) { - continue + if t.Implements(idec.i) { + return idec.vd, true + } + if allowAddr && t.Kind() != reflect.Ptr && reflect.PtrTo(t).Implements(idec.i) { + // if *t implements an interface, this will catch if t implements an interface further ahead + // in interfaceDecoders + defaultDec, found := r.lookupInterfaceDecoder(t, false) + if !found { + defaultDec, _ = r.kindDecoders[t.Kind()] + } + return newCondAddrDecoder(idec.vd, defaultDec), true } - - return idec.vd, true } return nil, false } diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/slice_codec.go b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/slice_codec.go new file mode 100644 index 0000000000..f0282eb23b --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/slice_codec.go @@ -0,0 +1,196 @@ +// Copyright (C) MongoDB, Inc. 2017-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package bsoncodec + +import ( + "fmt" + "reflect" + + "go.mongodb.org/mongo-driver/bson/bsonoptions" + "go.mongodb.org/mongo-driver/bson/bsonrw" + "go.mongodb.org/mongo-driver/bson/bsontype" + "go.mongodb.org/mongo-driver/bson/primitive" +) + +var defaultSliceCodec = NewSliceCodec() + +// SliceCodec is the Codec used for slice values. +type SliceCodec struct { + EncodeNilAsEmpty bool +} + +var _ ValueCodec = &MapCodec{} + +// NewSliceCodec returns a MapCodec with options opts. +func NewSliceCodec(opts ...*bsonoptions.SliceCodecOptions) *SliceCodec { + sliceOpt := bsonoptions.MergeSliceCodecOptions(opts...) + + codec := SliceCodec{} + if sliceOpt.EncodeNilAsEmpty != nil { + codec.EncodeNilAsEmpty = *sliceOpt.EncodeNilAsEmpty + } + return &codec +} + +// EncodeValue is the ValueEncoder for slice types. +func (sc SliceCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error { + if !val.IsValid() || val.Kind() != reflect.Slice { + return ValueEncoderError{Name: "SliceEncodeValue", Kinds: []reflect.Kind{reflect.Slice}, Received: val} + } + + if val.IsNil() && !sc.EncodeNilAsEmpty { + return vw.WriteNull() + } + + // If we have a []byte we want to treat it as a binary instead of as an array. + if val.Type().Elem() == tByte { + var byteSlice []byte + for idx := 0; idx < val.Len(); idx++ { + byteSlice = append(byteSlice, val.Index(idx).Interface().(byte)) + } + return vw.WriteBinary(byteSlice) + } + + // If we have a []primitive.E we want to treat it as a document instead of as an array. + if val.Type().ConvertibleTo(tD) { + d := val.Convert(tD).Interface().(primitive.D) + + dw, err := vw.WriteDocument() + if err != nil { + return err + } + + for _, e := range d { + err = encodeElement(ec, dw, e) + if err != nil { + return err + } + } + + return dw.WriteDocumentEnd() + } + + aw, err := vw.WriteArray() + if err != nil { + return err + } + + elemType := val.Type().Elem() + encoder, err := ec.LookupEncoder(elemType) + if err != nil && elemType.Kind() != reflect.Interface { + return err + } + + for idx := 0; idx < val.Len(); idx++ { + currEncoder, currVal, lookupErr := defaultValueEncoders.lookupElementEncoder(ec, encoder, val.Index(idx)) + if lookupErr != nil && lookupErr != errInvalidValue { + return lookupErr + } + + vw, err := aw.WriteArrayElement() + if err != nil { + return err + } + + if lookupErr == errInvalidValue { + err = vw.WriteNull() + if err != nil { + return err + } + continue + } + + err = currEncoder.EncodeValue(ec, vw, currVal) + if err != nil { + return err + } + } + return aw.WriteArrayEnd() +} + +// DecodeValue is the ValueDecoder for slice types. +func (sc *SliceCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { + if !val.CanSet() || val.Kind() != reflect.Slice { + return ValueDecoderError{Name: "SliceDecodeValue", Kinds: []reflect.Kind{reflect.Slice}, Received: val} + } + + switch vrType := vr.Type(); vrType { + case bsontype.Array: + case bsontype.Null: + val.Set(reflect.Zero(val.Type())) + return vr.ReadNull() + case bsontype.Type(0), bsontype.EmbeddedDocument: + if val.Type().Elem() != tE { + return fmt.Errorf("cannot decode document into %s", val.Type()) + } + case bsontype.Binary: + if val.Type().Elem() != tByte { + return fmt.Errorf("SliceDecodeValue can only decode a binary into a byte array, got %v", vrType) + } + data, subtype, err := vr.ReadBinary() + if err != nil { + return err + } + if subtype != bsontype.BinaryGeneric && subtype != bsontype.BinaryBinaryOld { + return fmt.Errorf("SliceDecodeValue can only be used to decode subtype 0x00 or 0x02 for %s, got %v", bsontype.Binary, subtype) + } + + if val.IsNil() { + val.Set(reflect.MakeSlice(val.Type(), 0, len(data))) + } + + val.SetLen(0) + for _, elem := range data { + val.Set(reflect.Append(val, reflect.ValueOf(elem))) + } + return nil + case bsontype.String: + if val.Type().Elem() != tByte { + return fmt.Errorf("SliceDecodeValue can only decode a string into a byte array, got %v", vrType) + } + str, err := vr.ReadString() + if err != nil { + return err + } + byteStr := []byte(str) + + if val.IsNil() { + val.Set(reflect.MakeSlice(val.Type(), 0, len(byteStr))) + } + + val.SetLen(0) + for _, elem := range byteStr { + val.Set(reflect.Append(val, reflect.ValueOf(elem))) + } + return nil + default: + return fmt.Errorf("cannot decode %v into a slice", vrType) + } + + var elemsFunc func(DecodeContext, bsonrw.ValueReader, reflect.Value) ([]reflect.Value, error) + switch val.Type().Elem() { + case tE: + dc.Ancestor = val.Type() + elemsFunc = defaultValueDecoders.decodeD + default: + elemsFunc = defaultValueDecoders.decodeDefault + } + + elems, err := elemsFunc(dc, vr, val) + if err != nil { + return err + } + + if val.IsNil() { + val.Set(reflect.MakeSlice(val.Type(), 0, len(elems))) + } + + val.SetLen(0) + val.Set(reflect.Append(val, elems...)) + + return nil +} diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/string_codec.go b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/string_codec.go new file mode 100644 index 0000000000..c672cf5a68 --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/string_codec.go @@ -0,0 +1,94 @@ +// Copyright (C) MongoDB, Inc. 2017-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package bsoncodec + +import ( + "fmt" + "reflect" + + "go.mongodb.org/mongo-driver/bson/bsonoptions" + "go.mongodb.org/mongo-driver/bson/bsonrw" + "go.mongodb.org/mongo-driver/bson/bsontype" +) + +var defaultStringCodec = NewStringCodec() + +// StringCodec is the Codec used for struct values. +type StringCodec struct { + DecodeObjectIDAsHex bool +} + +var _ ValueCodec = &StringCodec{} + +// NewStringCodec returns a StringCodec with options opts. +func NewStringCodec(opts ...*bsonoptions.StringCodecOptions) *StringCodec { + stringOpt := bsonoptions.MergeStringCodecOptions(opts...) + return &StringCodec{*stringOpt.DecodeObjectIDAsHex} +} + +// EncodeValue is the ValueEncoder for string types. +func (sc *StringCodec) EncodeValue(ectx EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error { + if val.Kind() != reflect.String { + return ValueEncoderError{ + Name: "StringEncodeValue", + Kinds: []reflect.Kind{reflect.String}, + Received: val, + } + } + + return vw.WriteString(val.String()) +} + +// DecodeValue is the ValueDecoder for string types. +func (sc *StringCodec) DecodeValue(dctx DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { + if !val.CanSet() || val.Kind() != reflect.String { + return ValueDecoderError{Name: "StringDecodeValue", Kinds: []reflect.Kind{reflect.String}, Received: val} + } + var str string + var err error + switch vr.Type() { + case bsontype.String: + str, err = vr.ReadString() + if err != nil { + return err + } + case bsontype.ObjectID: + oid, err := vr.ReadObjectID() + if err != nil { + return err + } + if sc.DecodeObjectIDAsHex { + str = oid.Hex() + } else { + byteArray := [12]byte(oid) + str = string(byteArray[:]) + } + case bsontype.Symbol: + str, err = vr.ReadSymbol() + if err != nil { + return err + } + case bsontype.Binary: + data, subtype, err := vr.ReadBinary() + if err != nil { + return err + } + if subtype != bsontype.BinaryGeneric && subtype != bsontype.BinaryBinaryOld { + return fmt.Errorf("SliceDecodeValue can only be used to decode subtype 0x00 or 0x02 for %s, got %v", bsontype.Binary, subtype) + } + str = string(data) + case bsontype.Null: + if err = vr.ReadNull(); err != nil { + return err + } + default: + return fmt.Errorf("cannot decode %v into a string type", vr.Type()) + } + + val.SetString(str) + return nil +} diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/struct_codec.go b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/struct_codec.go index 56eeff94b1..777cdfb695 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/struct_codec.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/struct_codec.go @@ -12,7 +12,9 @@ import ( "reflect" "strings" "sync" + "time" + "go.mongodb.org/mongo-driver/bson/bsonoptions" "go.mongodb.org/mongo-driver/bson/bsonrw" "go.mongodb.org/mongo-driver/bson/bsontype" ) @@ -31,24 +33,45 @@ type Zeroer interface { // StructCodec is the Codec used for struct values. type StructCodec struct { - cache map[reflect.Type]*structDescription - l sync.RWMutex - parser StructTagParser + cache map[reflect.Type]*structDescription + l sync.RWMutex + parser StructTagParser + DecodeZeroStruct bool + DecodeDeepZeroInline bool + EncodeOmitDefaultStruct bool + AllowUnexportedFields bool } var _ ValueEncoder = &StructCodec{} var _ ValueDecoder = &StructCodec{} // NewStructCodec returns a StructCodec that uses p for struct tag parsing. -func NewStructCodec(p StructTagParser) (*StructCodec, error) { +func NewStructCodec(p StructTagParser, opts ...*bsonoptions.StructCodecOptions) (*StructCodec, error) { if p == nil { return nil, errors.New("a StructTagParser must be provided to NewStructCodec") } - return &StructCodec{ + structOpt := bsonoptions.MergeStructCodecOptions(opts...) + + codec := &StructCodec{ cache: make(map[reflect.Type]*structDescription), parser: p, - }, nil + } + + if structOpt.DecodeZeroStruct != nil { + codec.DecodeZeroStruct = *structOpt.DecodeZeroStruct + } + if structOpt.DecodeDeepZeroInline != nil { + codec.DecodeDeepZeroInline = *structOpt.DecodeDeepZeroInline + } + if structOpt.EncodeOmitDefaultStruct != nil { + codec.EncodeOmitDefaultStruct = *structOpt.EncodeOmitDefaultStruct + } + if structOpt.AllowUnexportedFields != nil { + codec.AllowUnexportedFields = *structOpt.AllowUnexportedFields + } + + return codec, nil } // EncodeValue handles encoding generic struct types. @@ -71,7 +94,31 @@ func (sc *StructCodec) EncodeValue(r EncodeContext, vw bsonrw.ValueWriter, val r if desc.inline == nil { rv = val.Field(desc.idx) } else { - rv = val.FieldByIndex(desc.inline) + rv, err = fieldByIndexErr(val, desc.inline) + if err != nil { + continue + } + } + + desc.encoder, rv, err = defaultValueEncoders.lookupElementEncoder(r, desc.encoder, rv) + + if err != nil && err != errInvalidValue { + return err + } + + if err == errInvalidValue { + if desc.omitEmpty { + continue + } + vw2, err := dw.WriteDocumentElement(desc.name) + if err != nil { + return err + } + err = vw2.WriteNull() + if err != nil { + return err + } + continue } if desc.encoder == nil { @@ -80,12 +127,17 @@ func (sc *StructCodec) EncodeValue(r EncodeContext, vw bsonrw.ValueWriter, val r encoder := desc.encoder - iszero := sc.isZero - if iz, ok := encoder.(CodecZeroer); ok { - iszero = iz.IsTypeZero + var isZero bool + rvInterface := rv.Interface() + if cz, ok := encoder.(CodecZeroer); ok { + isZero = cz.IsTypeZero(rvInterface) + } else if rv.Kind() == reflect.Interface { + // sc.isZero will not treat an interface rv as an interface, so we need to check for the zero interface separately. + isZero = rv.IsNil() + } else { + isZero = sc.isZero(rvInterface) } - - if desc.omitEmpty && iszero(rv.Interface()) { + if desc.omitEmpty && isZero { continue } @@ -108,7 +160,7 @@ func (sc *StructCodec) EncodeValue(r EncodeContext, vw bsonrw.ValueWriter, val r return exists } - return defaultValueEncoders.mapEncodeValue(r, dw, rv, collisionFn) + return defaultMapCodec.mapEncodeValue(r, dw, rv, collisionFn) } return dw.WriteDocumentEnd() @@ -122,10 +174,17 @@ func (sc *StructCodec) DecodeValue(r DecodeContext, vr bsonrw.ValueReader, val r return ValueDecoderError{Name: "StructCodec.DecodeValue", Kinds: []reflect.Kind{reflect.Struct}, Received: val} } - switch vr.Type() { + switch vrType := vr.Type(); vrType { case bsontype.Type(0), bsontype.EmbeddedDocument: + case bsontype.Null: + if err := vr.ReadNull(); err != nil { + return err + } + + val.Set(reflect.Zero(val.Type())) + return nil default: - return fmt.Errorf("cannot decode %v into a %s", vr.Type(), val.Type()) + return fmt.Errorf("cannot decode %v into a %s", vrType, val.Type()) } sd, err := sc.describeStruct(r.Registry, val.Type()) @@ -133,13 +192,17 @@ func (sc *StructCodec) DecodeValue(r DecodeContext, vr bsonrw.ValueReader, val r return err } + if sc.DecodeZeroStruct { + val.Set(reflect.Zero(val.Type())) + } + if sc.DecodeDeepZeroInline && sd.inline { + val.Set(deepZero(val.Type())) + } + var decoder ValueDecoder var inlineMap reflect.Value if sd.inlineMap >= 0 { inlineMap = val.Field(sd.inlineMap) - if inlineMap.IsNil() { - inlineMap.Set(reflect.MakeMap(inlineMap.Type())) - } decoder, err = r.LookupDecoder(inlineMap.Type().Elem()) if err != nil { return err @@ -179,7 +242,12 @@ func (sc *StructCodec) DecodeValue(r DecodeContext, vr bsonrw.ValueReader, val r continue } + if inlineMap.IsNil() { + inlineMap.Set(reflect.MakeMap(inlineMap.Type())) + } + elem := reflect.New(inlineMap.Type().Elem()).Elem() + r.Ancestor = inlineMap.Type() err = decoder.DecodeValue(r, vr, elem) if err != nil { return err @@ -192,7 +260,10 @@ func (sc *StructCodec) DecodeValue(r DecodeContext, vr bsonrw.ValueReader, val r if fd.inline == nil { field = val.Field(fd.idx) } else { - field = val.FieldByIndex(fd.inline) + field, err = getInlineField(val, fd.inline) + if err != nil { + return err + } } if !field.CanSet() { // Being settable is a super set of being addressable. @@ -249,6 +320,23 @@ func (sc *StructCodec) isZero(i interface{}) bool { return v.Float() == 0 case reflect.Interface, reflect.Ptr: return v.IsNil() + case reflect.Struct: + if sc.EncodeOmitDefaultStruct { + vt := v.Type() + if vt == tTime { + return v.Interface().(time.Time).IsZero() + } + for i := 0; i < v.NumField(); i++ { + if vt.Field(i).PkgPath != "" && !vt.Field(i).Anonymous { + continue // Private field + } + fld := v.Field(i) + if !sc.isZero(fld.Interface()) { + return false + } + } + return true + } } return false @@ -258,6 +346,7 @@ type structDescription struct { fm map[string]fieldDescription fl []fieldDescription inlineMap int + inline bool } type fieldDescription struct { @@ -290,16 +379,17 @@ func (sc *StructCodec) describeStruct(r *Registry, t reflect.Type) (*structDescr for i := 0; i < numFields; i++ { sf := t.Field(i) - if sf.PkgPath != "" { - // unexported, ignore + if sf.PkgPath != "" && (!sc.AllowUnexportedFields || !sf.Anonymous) { + // field is private or unexported fields aren't allowed, ignore continue } - encoder, err := r.LookupEncoder(sf.Type) + sfType := sf.Type + encoder, err := r.LookupEncoder(sfType) if err != nil { encoder = nil } - decoder, err := r.LookupDecoder(sf.Type) + decoder, err := r.LookupDecoder(sfType) if err != nil { decoder = nil } @@ -319,17 +409,24 @@ func (sc *StructCodec) describeStruct(r *Registry, t reflect.Type) (*structDescr description.truncate = stags.Truncate if stags.Inline { - switch sf.Type.Kind() { + sd.inline = true + switch sfType.Kind() { case reflect.Map: if sd.inlineMap >= 0 { return nil, errors.New("(struct " + t.String() + ") multiple inline maps") } - if sf.Type.Key() != tString { + if sfType.Key() != tString { return nil, errors.New("(struct " + t.String() + ") inline map must have a string keys") } sd.inlineMap = description.idx + case reflect.Ptr: + sfType = sfType.Elem() + if sfType.Kind() != reflect.Struct { + return nil, fmt.Errorf("(struct %s) inline fields must be a struct, a struct pointer, or a map", t.String()) + } + fallthrough case reflect.Struct: - inlinesf, err := sc.describeStruct(r, sf.Type) + inlinesf, err := sc.describeStruct(r, sfType) if err != nil { return nil, err } @@ -346,7 +443,7 @@ func (sc *StructCodec) describeStruct(r *Registry, t reflect.Type) (*structDescr sd.fl = append(sd.fl, fd) } default: - return nil, fmt.Errorf("(struct %s) inline fields must be either a struct or a map", t.String()) + return nil, fmt.Errorf("(struct %s) inline fields must be a struct, a struct pointer, or a map", t.String()) } continue } @@ -365,3 +462,75 @@ func (sc *StructCodec) describeStruct(r *Registry, t reflect.Type) (*structDescr return sd, nil } + +func fieldByIndexErr(v reflect.Value, index []int) (result reflect.Value, err error) { + defer func() { + if recovered := recover(); recovered != nil { + switch r := recovered.(type) { + case string: + err = fmt.Errorf("%s", r) + case error: + err = r + } + } + }() + + result = v.FieldByIndex(index) + return +} + +func getInlineField(val reflect.Value, index []int) (reflect.Value, error) { + field, err := fieldByIndexErr(val, index) + if err == nil { + return field, nil + } + + // if parent of this element doesn't exist, fix its parent + inlineParent := index[:len(index)-1] + var fParent reflect.Value + if fParent, err = fieldByIndexErr(val, inlineParent); err != nil { + fParent, err = getInlineField(val, inlineParent) + if err != nil { + return fParent, err + } + } + fParent.Set(reflect.New(fParent.Type().Elem())) + + return fieldByIndexErr(val, index) +} + +// DeepZero returns recursive zero object +func deepZero(st reflect.Type) (result reflect.Value) { + result = reflect.Indirect(reflect.New(st)) + + if result.Kind() == reflect.Struct { + for i := 0; i < result.NumField(); i++ { + if f := result.Field(i); f.Kind() == reflect.Ptr { + if f.CanInterface() { + if ft := reflect.TypeOf(f.Interface()); ft.Elem().Kind() == reflect.Struct { + result.Field(i).Set(recursivePointerTo(deepZero(ft.Elem()))) + } + } + } + } + } + + return +} + +// recursivePointerTo calls reflect.New(v.Type) but recursively for its fields inside +func recursivePointerTo(v reflect.Value) reflect.Value { + v = reflect.Indirect(v) + result := reflect.New(v.Type()) + if v.Kind() == reflect.Struct { + for i := 0; i < v.NumField(); i++ { + if f := v.Field(i); f.Kind() == reflect.Ptr { + if f.Elem().Kind() == reflect.Struct { + result.Elem().Field(i).Set(recursivePointerTo(f)) + } + } + } + } + + return result +} diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/time_codec.go b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/time_codec.go new file mode 100644 index 0000000000..6f1b724d19 --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/time_codec.go @@ -0,0 +1,101 @@ +// Copyright (C) MongoDB, Inc. 2017-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package bsoncodec + +import ( + "fmt" + "reflect" + "time" + + "go.mongodb.org/mongo-driver/bson/bsonoptions" + "go.mongodb.org/mongo-driver/bson/bsonrw" + "go.mongodb.org/mongo-driver/bson/bsontype" +) + +const ( + timeFormatString = "2006-01-02T15:04:05.999Z07:00" +) + +var defaultTimeCodec = NewTimeCodec() + +// TimeCodec is the Codec used for time.Time values. +type TimeCodec struct { + UseLocalTimeZone bool +} + +var _ ValueCodec = &TimeCodec{} + +// NewTimeCodec returns a TimeCodec with options opts. +func NewTimeCodec(opts ...*bsonoptions.TimeCodecOptions) *TimeCodec { + timeOpt := bsonoptions.MergeTimeCodecOptions(opts...) + + codec := TimeCodec{} + if timeOpt.UseLocalTimeZone != nil { + codec.UseLocalTimeZone = *timeOpt.UseLocalTimeZone + } + return &codec +} + +// DecodeValue is the ValueDecoderFunc for time.Time. +func (tc *TimeCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { + if !val.CanSet() || val.Type() != tTime { + return ValueDecoderError{Name: "TimeDecodeValue", Types: []reflect.Type{tTime}, Received: val} + } + + var timeVal time.Time + switch vrType := vr.Type(); vrType { + case bsontype.DateTime: + dt, err := vr.ReadDateTime() + if err != nil { + return err + } + timeVal = time.Unix(dt/1000, dt%1000*1000000) + case bsontype.String: + // assume strings are in the isoTimeFormat + timeStr, err := vr.ReadString() + if err != nil { + return err + } + timeVal, err = time.Parse(timeFormatString, timeStr) + if err != nil { + return err + } + case bsontype.Int64: + i64, err := vr.ReadInt64() + if err != nil { + return err + } + timeVal = time.Unix(i64/1000, i64%1000*1000000) + case bsontype.Timestamp: + t, _, err := vr.ReadTimestamp() + if err != nil { + return err + } + timeVal = time.Unix(int64(t), 0) + case bsontype.Null: + if err := vr.ReadNull(); err != nil { + return err + } + default: + return fmt.Errorf("cannot decode %v into a time.Time", vrType) + } + + if !tc.UseLocalTimeZone { + timeVal = timeVal.UTC() + } + val.Set(reflect.ValueOf(timeVal)) + return nil +} + +// EncodeValue is the ValueEncoderFunc for time.TIme. +func (tc *TimeCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error { + if !val.IsValid() || val.Type() != tTime { + return ValueEncoderError{Name: "TimeEncodeValue", Types: []reflect.Type{tTime}, Received: val} + } + tt := val.Interface().(time.Time) + return vw.WriteDateTime(tt.Unix()*1000 + int64(tt.Nanosecond()/1e6)) +} diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/types.go b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/types.go index 77264876aa..bbb6bb9cea 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/types.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/types.go @@ -74,6 +74,7 @@ var tDecimal = reflect.TypeOf(primitive.Decimal128{}) var tMinKey = reflect.TypeOf(primitive.MinKey{}) var tMaxKey = reflect.TypeOf(primitive.MaxKey{}) var tD = reflect.TypeOf(primitive.D{}) +var tM = reflect.TypeOf(primitive.M{}) var tA = reflect.TypeOf(primitive.A{}) var tE = reflect.TypeOf(primitive.E{}) diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/uint_codec.go b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/uint_codec.go new file mode 100644 index 0000000000..e0df058374 --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsoncodec/uint_codec.go @@ -0,0 +1,150 @@ +// Copyright (C) MongoDB, Inc. 2017-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package bsoncodec + +import ( + "errors" + "fmt" + "math" + "reflect" + + "go.mongodb.org/mongo-driver/bson/bsonoptions" + "go.mongodb.org/mongo-driver/bson/bsonrw" + "go.mongodb.org/mongo-driver/bson/bsontype" +) + +var defaultUIntCodec = NewUIntCodec() + +// UIntCodec is the Codec used for uint values. +type UIntCodec struct { + EncodeToMinSize bool +} + +var _ ValueCodec = &UIntCodec{} + +// NewUIntCodec returns a UIntCodec with options opts. +func NewUIntCodec(opts ...*bsonoptions.UIntCodecOptions) *UIntCodec { + uintOpt := bsonoptions.MergeUIntCodecOptions(opts...) + + codec := UIntCodec{} + if uintOpt.EncodeToMinSize != nil { + codec.EncodeToMinSize = *uintOpt.EncodeToMinSize + } + return &codec +} + +// EncodeValue is the ValueEncoder for uint types. +func (uic *UIntCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error { + switch val.Kind() { + case reflect.Uint8, reflect.Uint16: + return vw.WriteInt32(int32(val.Uint())) + case reflect.Uint, reflect.Uint32, reflect.Uint64: + 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) + + if u64 <= math.MaxInt32 && useMinSize { + return vw.WriteInt32(int32(u64)) + } + if u64 > math.MaxInt64 { + return fmt.Errorf("%d overflows int64", u64) + } + return vw.WriteInt64(int64(u64)) + } + + return ValueEncoderError{ + Name: "UintEncodeValue", + Kinds: []reflect.Kind{reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint}, + Received: val, + } +} + +// DecodeValue is the ValueDecoder for uint types. +func (uic *UIntCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { + if !val.CanSet() { + return ValueDecoderError{ + Name: "UintDecodeValue", + Kinds: []reflect.Kind{reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint}, + Received: val, + } + } + + var i64 int64 + var err error + switch vrType := vr.Type(); vrType { + case bsontype.Int32: + i32, err := vr.ReadInt32() + if err != nil { + return err + } + i64 = int64(i32) + case bsontype.Int64: + i64, err = vr.ReadInt64() + if err != nil { + return err + } + case bsontype.Double: + f64, err := vr.ReadDouble() + if err != nil { + return err + } + if !dc.Truncate && math.Floor(f64) != f64 { + return errors.New("UintDecodeValue can only truncate float64 to an integer type when truncation is enabled") + } + if f64 > float64(math.MaxInt64) { + return fmt.Errorf("%g overflows int64", f64) + } + i64 = int64(f64) + case bsontype.Boolean: + b, err := vr.ReadBoolean() + if err != nil { + return err + } + if b { + i64 = 1 + } + case bsontype.Null: + if err = vr.ReadNull(); err != nil { + return err + } + default: + return fmt.Errorf("cannot decode %v into an integer type", vrType) + } + + switch val.Kind() { + case reflect.Uint8: + if i64 < 0 || i64 > math.MaxUint8 { + return fmt.Errorf("%d overflows uint8", i64) + } + case reflect.Uint16: + if i64 < 0 || i64 > math.MaxUint16 { + return fmt.Errorf("%d overflows uint16", i64) + } + case reflect.Uint32: + if i64 < 0 || i64 > math.MaxUint32 { + return fmt.Errorf("%d overflows uint32", i64) + } + case reflect.Uint64: + if i64 < 0 { + return fmt.Errorf("%d overflows uint64", i64) + } + case reflect.Uint: + if i64 < 0 || int64(uint(i64)) != i64 { // Can we fit this inside of an uint + return fmt.Errorf("%d overflows uint", i64) + } + default: + return ValueDecoderError{ + Name: "UintDecodeValue", + Kinds: []reflect.Kind{reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint}, + Received: val, + } + } + + val.SetUint(uint64(i64)) + return nil +} diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsonoptions/byte_slice_codec_options.go b/vendor/go.mongodb.org/mongo-driver/bson/bsonoptions/byte_slice_codec_options.go new file mode 100644 index 0000000000..b1256a4dca --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsonoptions/byte_slice_codec_options.go @@ -0,0 +1,38 @@ +// Copyright (C) MongoDB, Inc. 2017-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package bsonoptions + +// ByteSliceCodecOptions represents all possible options for byte slice encoding and decoding. +type ByteSliceCodecOptions struct { + EncodeNilAsEmpty *bool // Specifies if a nil byte slice should encode as an empty binary instead of null. Defaults to false. +} + +// ByteSliceCodec creates a new *ByteSliceCodecOptions +func ByteSliceCodec() *ByteSliceCodecOptions { + return &ByteSliceCodecOptions{} +} + +// SetEncodeNilAsEmpty specifies if a nil byte slice should encode as an empty binary instead of null. Defaults to false. +func (bs *ByteSliceCodecOptions) SetEncodeNilAsEmpty(b bool) *ByteSliceCodecOptions { + bs.EncodeNilAsEmpty = &b + return bs +} + +// MergeByteSliceCodecOptions combines the given *ByteSliceCodecOptions into a single *ByteSliceCodecOptions in a last one wins fashion. +func MergeByteSliceCodecOptions(opts ...*ByteSliceCodecOptions) *ByteSliceCodecOptions { + bs := ByteSliceCodec() + for _, opt := range opts { + if opt == nil { + continue + } + if opt.EncodeNilAsEmpty != nil { + bs.EncodeNilAsEmpty = opt.EncodeNilAsEmpty + } + } + + return bs +} diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsonoptions/empty_interface_codec_options.go b/vendor/go.mongodb.org/mongo-driver/bson/bsonoptions/empty_interface_codec_options.go new file mode 100644 index 0000000000..6caaa000e6 --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsonoptions/empty_interface_codec_options.go @@ -0,0 +1,38 @@ +// Copyright (C) MongoDB, Inc. 2017-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package bsonoptions + +// EmptyInterfaceCodecOptions represents all possible options for interface{} encoding and decoding. +type EmptyInterfaceCodecOptions struct { + DecodeBinaryAsSlice *bool // Specifies if Old and Generic type binarys should default to []slice instead of primitive.Binary. Defaults to false. +} + +// EmptyInterfaceCodec creates a new *EmptyInterfaceCodecOptions +func EmptyInterfaceCodec() *EmptyInterfaceCodecOptions { + return &EmptyInterfaceCodecOptions{} +} + +// SetDecodeBinaryAsSlice specifies if Old and Generic type binarys should default to []slice instead of primitive.Binary. Defaults to false. +func (e *EmptyInterfaceCodecOptions) SetDecodeBinaryAsSlice(b bool) *EmptyInterfaceCodecOptions { + e.DecodeBinaryAsSlice = &b + return e +} + +// MergeEmptyInterfaceCodecOptions combines the given *EmptyInterfaceCodecOptions into a single *EmptyInterfaceCodecOptions in a last one wins fashion. +func MergeEmptyInterfaceCodecOptions(opts ...*EmptyInterfaceCodecOptions) *EmptyInterfaceCodecOptions { + e := EmptyInterfaceCodec() + for _, opt := range opts { + if opt == nil { + continue + } + if opt.DecodeBinaryAsSlice != nil { + e.DecodeBinaryAsSlice = opt.DecodeBinaryAsSlice + } + } + + return e +} diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsonoptions/map_codec_options.go b/vendor/go.mongodb.org/mongo-driver/bson/bsonoptions/map_codec_options.go new file mode 100644 index 0000000000..1ac3e20088 --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsonoptions/map_codec_options.go @@ -0,0 +1,48 @@ +// Copyright (C) MongoDB, Inc. 2017-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package bsonoptions + +// MapCodecOptions represents all possible options for map encoding and decoding. +type MapCodecOptions struct { + DecodeZerosMap *bool // Specifies if the map should be zeroed before decoding into it. Defaults to false. + EncodeNilAsEmpty *bool // Specifies if a nil map should encode as an empty document instead of null. Defaults to false. +} + +// MapCodec creates a new *MapCodecOptions +func MapCodec() *MapCodecOptions { + return &MapCodecOptions{} +} + +// SetDecodeZerosMap specifies if the map should be zeroed before decoding into it. Defaults to false. +func (t *MapCodecOptions) SetDecodeZerosMap(b bool) *MapCodecOptions { + t.DecodeZerosMap = &b + return t +} + +// SetEncodeNilAsEmpty specifies if a nil map should encode as an empty document instead of null. Defaults to false. +func (t *MapCodecOptions) SetEncodeNilAsEmpty(b bool) *MapCodecOptions { + t.EncodeNilAsEmpty = &b + return t +} + +// MergeMapCodecOptions combines the given *MapCodecOptions into a single *MapCodecOptions in a last one wins fashion. +func MergeMapCodecOptions(opts ...*MapCodecOptions) *MapCodecOptions { + s := MapCodec() + for _, opt := range opts { + if opt == nil { + continue + } + if opt.DecodeZerosMap != nil { + s.DecodeZerosMap = opt.DecodeZerosMap + } + if opt.EncodeNilAsEmpty != nil { + s.EncodeNilAsEmpty = opt.EncodeNilAsEmpty + } + } + + return s +} diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsonoptions/slice_codec_options.go b/vendor/go.mongodb.org/mongo-driver/bson/bsonoptions/slice_codec_options.go new file mode 100644 index 0000000000..ef965e4b41 --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsonoptions/slice_codec_options.go @@ -0,0 +1,38 @@ +// Copyright (C) MongoDB, Inc. 2017-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package bsonoptions + +// SliceCodecOptions represents all possible options for slice encoding and decoding. +type SliceCodecOptions struct { + EncodeNilAsEmpty *bool // Specifies if a nil slice should encode as an empty array instead of null. Defaults to false. +} + +// SliceCodec creates a new *SliceCodecOptions +func SliceCodec() *SliceCodecOptions { + return &SliceCodecOptions{} +} + +// SetEncodeNilAsEmpty specifies if a nil slice should encode as an empty array instead of null. Defaults to false. +func (s *SliceCodecOptions) SetEncodeNilAsEmpty(b bool) *SliceCodecOptions { + s.EncodeNilAsEmpty = &b + return s +} + +// MergeSliceCodecOptions combines the given *SliceCodecOptions into a single *SliceCodecOptions in a last one wins fashion. +func MergeSliceCodecOptions(opts ...*SliceCodecOptions) *SliceCodecOptions { + s := SliceCodec() + for _, opt := range opts { + if opt == nil { + continue + } + if opt.EncodeNilAsEmpty != nil { + s.EncodeNilAsEmpty = opt.EncodeNilAsEmpty + } + } + + return s +} diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsonoptions/string_codec_options.go b/vendor/go.mongodb.org/mongo-driver/bson/bsonoptions/string_codec_options.go new file mode 100644 index 0000000000..65964f4207 --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsonoptions/string_codec_options.go @@ -0,0 +1,41 @@ +// Copyright (C) MongoDB, Inc. 2017-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package bsonoptions + +var defaultDecodeOIDAsHex = true + +// StringCodecOptions represents all possible options for string encoding and decoding. +type StringCodecOptions struct { + DecodeObjectIDAsHex *bool // Specifies if we should decode ObjectID as the hex value. Defaults to true. +} + +// StringCodec creates a new *StringCodecOptions +func StringCodec() *StringCodecOptions { + return &StringCodecOptions{} +} + +// SetDecodeObjectIDAsHex 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. +func (t *StringCodecOptions) SetDecodeObjectIDAsHex(b bool) *StringCodecOptions { + t.DecodeObjectIDAsHex = &b + return t +} + +// MergeStringCodecOptions combines the given *StringCodecOptions into a single *StringCodecOptions in a last one wins fashion. +func MergeStringCodecOptions(opts ...*StringCodecOptions) *StringCodecOptions { + s := &StringCodecOptions{&defaultDecodeOIDAsHex} + for _, opt := range opts { + if opt == nil { + continue + } + if opt.DecodeObjectIDAsHex != nil { + s.DecodeObjectIDAsHex = opt.DecodeObjectIDAsHex + } + } + + return s +} diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsonoptions/struct_codec_options.go b/vendor/go.mongodb.org/mongo-driver/bson/bsonoptions/struct_codec_options.go new file mode 100644 index 0000000000..ad32c7c382 --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsonoptions/struct_codec_options.go @@ -0,0 +1,70 @@ +// Copyright (C) MongoDB, Inc. 2017-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package bsonoptions + +// StructCodecOptions represents all possible options for struct encoding and decoding. +type StructCodecOptions struct { + DecodeZeroStruct *bool // Specifies if structs should be zeroed before decoding into them. Defaults to false. + DecodeDeepZeroInline *bool // Specifies if structs should be recursively zeroed when a inline value is decoded. Defaults to false. + EncodeOmitDefaultStruct *bool // Specifies if default structs should be considered empty by omitempty. Defaults to false. + AllowUnexportedFields *bool // Specifies if unexported fields should be marshaled/unmarshaled. Defaults to false. +} + +// StructCodec creates a new *StructCodecOptions +func StructCodec() *StructCodecOptions { + return &StructCodecOptions{} +} + +// SetDecodeZeroStruct specifies if structs should be zeroed before decoding into them. Defaults to false. +func (t *StructCodecOptions) SetDecodeZeroStruct(b bool) *StructCodecOptions { + t.DecodeZeroStruct = &b + return t +} + +// SetDecodeDeepZeroInline specifies if structs should be zeroed before decoding into them. Defaults to false. +func (t *StructCodecOptions) SetDecodeDeepZeroInline(b bool) *StructCodecOptions { + t.DecodeDeepZeroInline = &b + return t +} + +// SetEncodeOmitDefaultStruct specifies if default structs should be considered empty by omitempty. A default struct has all +// its values set to their default value. Defaults to false. +func (t *StructCodecOptions) SetEncodeOmitDefaultStruct(b bool) *StructCodecOptions { + t.EncodeOmitDefaultStruct = &b + return t +} + +// SetAllowUnexportedFields specifies if unexported fields should be marshaled/unmarshaled. Defaults to false. +func (t *StructCodecOptions) SetAllowUnexportedFields(b bool) *StructCodecOptions { + t.AllowUnexportedFields = &b + return t +} + +// MergeStructCodecOptions combines the given *StructCodecOptions into a single *StructCodecOptions in a last one wins fashion. +func MergeStructCodecOptions(opts ...*StructCodecOptions) *StructCodecOptions { + s := StructCodec() + for _, opt := range opts { + if opt == nil { + continue + } + + if opt.DecodeZeroStruct != nil { + s.DecodeZeroStruct = opt.DecodeZeroStruct + } + if opt.DecodeDeepZeroInline != nil { + s.DecodeDeepZeroInline = opt.DecodeDeepZeroInline + } + if opt.EncodeOmitDefaultStruct != nil { + s.EncodeOmitDefaultStruct = opt.EncodeOmitDefaultStruct + } + if opt.AllowUnexportedFields != nil { + s.AllowUnexportedFields = opt.AllowUnexportedFields + } + } + + return s +} diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsonoptions/time_codec_options.go b/vendor/go.mongodb.org/mongo-driver/bson/bsonoptions/time_codec_options.go new file mode 100644 index 0000000000..13496d1217 --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsonoptions/time_codec_options.go @@ -0,0 +1,38 @@ +// Copyright (C) MongoDB, Inc. 2017-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package bsonoptions + +// TimeCodecOptions represents all possible options for time.Time encoding and decoding. +type TimeCodecOptions struct { + UseLocalTimeZone *bool // Specifies if we should decode into the local time zone. Defaults to false. +} + +// TimeCodec creates a new *TimeCodecOptions +func TimeCodec() *TimeCodecOptions { + return &TimeCodecOptions{} +} + +// SetUseLocalTimeZone specifies if we should decode into the local time zone. Defaults to false. +func (t *TimeCodecOptions) SetUseLocalTimeZone(b bool) *TimeCodecOptions { + t.UseLocalTimeZone = &b + return t +} + +// MergeTimeCodecOptions combines the given *TimeCodecOptions into a single *TimeCodecOptions in a last one wins fashion. +func MergeTimeCodecOptions(opts ...*TimeCodecOptions) *TimeCodecOptions { + t := TimeCodec() + for _, opt := range opts { + if opt == nil { + continue + } + if opt.UseLocalTimeZone != nil { + t.UseLocalTimeZone = opt.UseLocalTimeZone + } + } + + return t +} diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsonoptions/uint_codec_options.go b/vendor/go.mongodb.org/mongo-driver/bson/bsonoptions/uint_codec_options.go new file mode 100644 index 0000000000..e08b7f192e --- /dev/null +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsonoptions/uint_codec_options.go @@ -0,0 +1,38 @@ +// Copyright (C) MongoDB, Inc. 2017-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package bsonoptions + +// UIntCodecOptions represents all possible options for uint encoding and decoding. +type UIntCodecOptions struct { + EncodeToMinSize *bool // Specifies if all uints except uint64 should be decoded to minimum size bsontype. Defaults to false. +} + +// UIntCodec creates a new *UIntCodecOptions +func UIntCodec() *UIntCodecOptions { + return &UIntCodecOptions{} +} + +// SetEncodeToMinSize specifies if all uints except uint64 should be decoded to minimum size bsontype. Defaults to false. +func (u *UIntCodecOptions) SetEncodeToMinSize(b bool) *UIntCodecOptions { + u.EncodeToMinSize = &b + return u +} + +// MergeUIntCodecOptions combines the given *UIntCodecOptions into a single *UIntCodecOptions in a last one wins fashion. +func MergeUIntCodecOptions(opts ...*UIntCodecOptions) *UIntCodecOptions { + u := UIntCodec() + for _, opt := range opts { + if opt == nil { + continue + } + if opt.EncodeToMinSize != nil { + u.EncodeToMinSize = opt.EncodeToMinSize + } + } + + return u +} diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/extjson_parser.go b/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/extjson_parser.go index 9e223edf9d..3ff17c197d 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/extjson_parser.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/extjson_parser.go @@ -86,6 +86,7 @@ func newExtJSONParser(r io.Reader, canonical bool) *extJSONParser { func (ejp *extJSONParser) peekType() (bsontype.Type, error) { var t bsontype.Type var err error + initialState := ejp.s ejp.advanceState() switch ejp.s { @@ -113,12 +114,15 @@ func (ejp *extJSONParser) peekType() (bsontype.Type, error) { case jpsInvalidState: err = ejp.err case jpsSawKey: + if initialState == jpsStartState { + return bsontype.EmbeddedDocument, nil + } t = wrapperKeyBSONType(ejp.k) - if t == bsontype.JavaScript { + switch t { + case bsontype.JavaScript: // just saw $code, need to check for $scope at same level - _, err := ejp.readValue(bsontype.JavaScript) - + _, err = ejp.readValue(bsontype.JavaScript) if err != nil { break } @@ -127,6 +131,7 @@ func (ejp *extJSONParser) peekType() (bsontype.Type, error) { case jpsSawEndObject: // type is TypeJavaScript case jpsSawComma: ejp.advanceState() + if ejp.s == jpsSawKey && ejp.k == "$scope" { t = bsontype.CodeWithScope } else { @@ -137,6 +142,8 @@ func (ejp *extJSONParser) peekType() (bsontype.Type, error) { default: err = ErrInvalidJSON } + case bsontype.CodeWithScope: + err = errors.New("invalid extended JSON: code with $scope must contain $code before $scope") } } } diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/extjson_wrappers.go b/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/extjson_wrappers.go index e6ca0e038a..7e9612c074 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/extjson_wrappers.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/extjson_wrappers.go @@ -180,7 +180,13 @@ func (ejv *extJSONValue) parseDBPointer() (ns string, oid primitive.ObjectID, er return ns, oid, nil } -const rfc3339Milli = "2006-01-02T15:04:05.999Z07:00" +const ( + rfc3339Milli = "2006-01-02T15:04:05.999Z07:00" +) + +var ( + timeFormats = []string{rfc3339Milli, "2006-01-02T15:04:05.999Z0700"} +) func (ejv *extJSONValue) parseDateTime() (int64, error) { switch ejv.t { @@ -198,7 +204,15 @@ func (ejv *extJSONValue) parseDateTime() (int64, error) { } func parseDatetimeString(data string) (int64, error) { - t, err := time.Parse(rfc3339Milli, data) + var t time.Time + var err error + // try acceptable time formats until one matches + for _, format := range timeFormats { + t, err = time.Parse(format, data) + if err == nil { + break + } + } if err != nil { return 0, fmt.Errorf("invalid $date value string: %s", data) } @@ -416,17 +430,20 @@ func (ejv *extJSONValue) parseTimestamp() (t, i uint32, err error) { switch val.t { case bsontype.Int32: - if val.v.(int32) < 0 { - return 0, fmt.Errorf("$timestamp %s number should be uint32: %s", key, string(val.v.(int32))) + value := val.v.(int32) + + if value < 0 { + return 0, fmt.Errorf("$timestamp %s number should be uint32: %d", key, value) } - return uint32(val.v.(int32)), nil + return uint32(value), nil case bsontype.Int64: - if val.v.(int64) < 0 || uint32(val.v.(int64)) > math.MaxUint32 { - return 0, fmt.Errorf("$timestamp %s number should be uint32: %s", key, string(val.v.(int32))) + value := val.v.(int64) + if value < 0 || value > int64(math.MaxUint32) { + return 0, fmt.Errorf("$timestamp %s number should be uint32: %d", key, value) } - return uint32(val.v.(int64)), nil + return uint32(value), nil default: return 0, fmt.Errorf("$timestamp %s value should be uint32, but instead is %s", key, val.t) } diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/extjson_writer.go b/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/extjson_writer.go index b34b937379..605e41a138 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/extjson_writer.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/extjson_writer.go @@ -536,7 +536,10 @@ func (ejvw *extJSONValueWriter) WriteUndefined() error { func (ejvw *extJSONValueWriter) WriteDocumentElement(key string) (ValueWriter, error) { switch ejvw.stack[ejvw.frame].mode { case mDocument, mTopLevel, mCodeWithScope: - ejvw.buf = append(ejvw.buf, []byte(fmt.Sprintf(`"%s":`, key))...) + var buf bytes.Buffer + writeStringWithEscapes(key, &buf, ejvw.escapeHTML) + + ejvw.buf = append(ejvw.buf, []byte(fmt.Sprintf(`%s:`, buf.String()))...) ejvw.push(mElement) default: return nil, ejvw.invalidTransitionErr(mElement, "WriteDocumentElement", []mode{mDocument, mTopLevel, mCodeWithScope}) diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/json_scanner.go b/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/json_scanner.go index 03aabf5527..212f348348 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/json_scanner.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/json_scanner.go @@ -183,7 +183,7 @@ func (js *jsonScanner) scanString() (*jsonToken, error) { case '\\': c, err = js.readNextByte() switch c { - case '"', '\\', '/', '\'': + case '"', '\\', '/': b.WriteByte(c) case 'b': b.WriteByte('\b') diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/value_reader.go b/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/value_reader.go index fad47ba7a2..55378093a2 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/value_reader.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/value_reader.go @@ -14,7 +14,6 @@ import ( "io" "math" "sync" - "unicode" "go.mongodb.org/mongo-driver/bson/bsontype" "go.mongodb.org/mongo-driver/bson/primitive" @@ -373,7 +372,8 @@ func (vr *valueReader) ReadBinary() (b []byte, btype byte, err error) { return nil, 0, err } - if btype == 0x02 { + // Check length in case it is an old binary without a length. + if btype == 0x02 && length > 4 { length, err = vr.readLength() if err != nil { return nil, 0, err @@ -449,6 +449,9 @@ func (vr *valueReader) ReadCodeWithScope() (code string, dr DocumentReader, err if err != nil { return "", nil, err } + if strLength <= 0 { + return "", nil, fmt.Errorf("invalid string length: %d", strLength) + } strBytes, err := vr.readBytes(strLength) if err != nil { return "", nil, err @@ -817,14 +820,6 @@ func (vr *valueReader) readString() (string, error) { start := vr.offset vr.offset += int64(length) - - if length == 2 { - asciiByte := vr.d[start] - if asciiByte > unicode.MaxASCII { - return "", fmt.Errorf("invalid ascii byte") - } - } - return string(vr.d[start : start+int64(length)-1]), nil } diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/value_writer.go b/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/value_writer.go index caa6fae3aa..3717198366 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/value_writer.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/value_writer.go @@ -56,6 +56,13 @@ func (bvwp *BSONValueWriterPool) Get(w io.Writer) ValueWriter { return vw } +// GetAtModeElement retrieves a ValueWriterFlusher from the pool and resets it to use w as the destination. +func (bvwp *BSONValueWriterPool) GetAtModeElement(w io.Writer) ValueWriterFlusher { + vw := bvwp.Get(w).(*valueWriter) + vw.push(mElement) + return vw +} + // Put inserts a ValueWriter into the pool. If the ValueWriter is not a BSON ValueWriter, nothing // happens and ok will be false. func (bvwp *BSONValueWriterPool) Put(vw ValueWriter) (ok bool) { @@ -512,17 +519,8 @@ func (vw *valueWriter) WriteDocumentEnd() error { } if vw.stack[vw.frame].mode == mTopLevel { - if vw.w != nil { - if sw, ok := vw.w.(*SliceWriter); ok { - *sw = vw.buf - } else { - _, err = vw.w.Write(vw.buf) - if err != nil { - return err - } - // reset buffer - vw.buf = vw.buf[:0] - } + if err = vw.Flush(); err != nil { + return err } } @@ -537,6 +535,23 @@ func (vw *valueWriter) WriteDocumentEnd() error { return nil } +func (vw *valueWriter) Flush() error { + if vw.w == nil { + return nil + } + + if sw, ok := vw.w.(*SliceWriter); ok { + *sw = vw.buf + return nil + } + if _, err := vw.w.Write(vw.buf); err != nil { + return err + } + // reset buffer + vw.buf = vw.buf[:0] + return nil +} + func (vw *valueWriter) WriteArrayElement() (ValueWriter, error) { if vw.stack[vw.frame].mode != mArray { return nil, vw.invalidTransitionError(mValue, "WriteArrayElement", []mode{mArray}) diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/writer.go b/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/writer.go index 128b79bdfa..7644df1290 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/writer.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsonrw/writer.go @@ -55,6 +55,12 @@ type ValueWriter interface { WriteUndefined() error } +// ValueWriterFlusher is a superset of ValueWriter that exposes functionality to flush to the underlying buffer. +type ValueWriterFlusher interface { + ValueWriter + Flush() error +} + // BytesWriter is the interface used to write BSON bytes to a ValueWriter. // This interface is meant to be a superset of ValueWriter, so that types that // implement ValueWriter may also implement this interface. diff --git a/vendor/go.mongodb.org/mongo-driver/bson/bsontype/bsontype.go b/vendor/go.mongodb.org/mongo-driver/bson/bsontype/bsontype.go index e76403a67f..63a59ca08f 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/bsontype/bsontype.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/bsontype/bsontype.go @@ -31,6 +31,14 @@ const ( Decimal128 Type = 0x13 MinKey Type = 0xFF MaxKey Type = 0x7F + + BinaryGeneric byte = 0x00 + BinaryFunction byte = 0x01 + BinaryBinaryOld byte = 0x02 + BinaryUUIDOld byte = 0x03 + BinaryUUID byte = 0x04 + BinaryMD5 byte = 0x05 + BinaryUserDefined byte = 0x80 ) // Type represents a BSON type. diff --git a/vendor/go.mongodb.org/mongo-driver/bson/decoder.go b/vendor/go.mongodb.org/mongo-driver/bson/decoder.go index 550541e941..7f6b7694f9 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/decoder.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/decoder.go @@ -78,13 +78,19 @@ func (d *Decoder) Decode(val interface{}) error { } rval := reflect.ValueOf(val) - if rval.Kind() != reflect.Ptr { - return fmt.Errorf("argument to Decode must be a pointer to a type, but got %v", rval) - } - if rval.IsNil() { - return ErrDecodeToNil + switch rval.Kind() { + case reflect.Ptr: + if rval.IsNil() { + return ErrDecodeToNil + } + rval = rval.Elem() + case reflect.Map: + if rval.IsNil() { + return ErrDecodeToNil + } + default: + return fmt.Errorf("argument to Decode must be a pointer or a map, but got %v", rval) } - rval = rval.Elem() decoder, err := d.dc.LookupDecoder(rval.Type()) if err != nil { return err diff --git a/vendor/go.mongodb.org/mongo-driver/bson/doc.go b/vendor/go.mongodb.org/mongo-driver/bson/doc.go index b3f6c52ffb..2943f14eca 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/doc.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/doc.go @@ -4,8 +4,12 @@ // not use this file except in compliance with the License. You may obtain // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 -// Package bson is a library for reading, writing, and manipulating BSON. The -// library has two families of types for representing BSON. +// Package bson is a library for reading, writing, and manipulating BSON. BSON is a binary serialization format used to +// store documents and make remote procedure calls in MongoDB. The BSON specification is located at https://bsonspec.org. +// The BSON library handles marshalling and unmarshalling of values through a configurable codec system. For a description +// of the codec system and examples of registering custom codecs, see the bsoncodec package. +// +// Raw BSON // // The Raw family of types is used to validate and retrieve elements from a slice of bytes. This // type is most useful when you want do lookups on BSON bytes without unmarshaling it into another @@ -19,24 +23,98 @@ // i32, ok := val.Int32OK() // // do something with i32... // -// The D family of types is used to build concise representations of BSON using native Go types. -// These types do not support automatic lookup. +// Native Go Types +// +// The D and M types defined in this package can be used to build representations of BSON using native Go types. D is a +// slice and M is a map. For more information about the use cases for these types, see the documentation on the type +// definitions. // // Example: // bson.D{{"foo", "bar"}, {"hello", "world"}, {"pi", 3.14159}} +// bson.M{"foo": "bar", "hello": "world", "pi": 3.14159} // +// When decoding BSON to a D or M, the following type mappings apply when unmarshalling: // -// Marshaling and Unmarshaling are handled with the Marshal and Unmarshal family of functions. If -// you need to write or read BSON from a non-slice source, an Encoder or Decoder can be used with a -// bsonrw.ValueWriter or bsonrw.ValueReader. +// 1. BSON int32 unmarshals to an int32. +// 2. BSON int64 unmarshals to an int64. +// 3. BSON double unmarshals to a float64. +// 4. BSON string unmarshals to a string. +// 5. BSON boolean unmarshals to a bool. +// 6. BSON embedded document unmarshals to the parent type (i.e. D for a D, M for an M). +// 7. BSON array unmarshals to a bson.A. +// 8. BSON ObjectId unmarshals to a primitive.ObjectID. +// 9. BSON datetime unmarshals to a primitive.Datetime. +// 10. BSON binary unmarshals to a primitive.Binary. +// 11. BSON regular expression unmarshals to a primitive.Regex. +// 12. BSON JavaScript unmarshals to a primitive.JavaScript. +// 13. BSON code with scope unmarshals to a primitive.CodeWithScope. +// 14. BSON timestamp unmarshals to an primitive.Timestamp. +// 15. BSON 128-bit decimal unmarshals to an primitive.Decimal128. +// 16. BSON min key unmarshals to an primitive.MinKey. +// 17. BSON max key unmarshals to an primitive.MaxKey. +// 18. BSON undefined unmarshals to a primitive.Undefined. +// 19. BSON null unmarshals to a primitive.Null. +// 20. BSON DBPointer unmarshals to a primitive.DBPointer. +// 21. BSON symbol unmarshals to a primitive.Symbol. // -// Example: -// b, err := bson.Marshal(bson.D{{"foo", "bar"}}) -// if err != nil { return err } -// var fooer struct { -// Foo string -// } -// err = bson.Unmarshal(b, &fooer) -// if err != nil { return err } -// // do something with fooer... +// The above mappings also apply when marshalling a D or M to BSON. Some other useful marshalling mappings are: +// +// 1. time.Time marshals to a BSON datetime. +// 2. int8, int16, and int32 marshal to a BSON int32. +// 3. int marshals to a BSON int32 if the value is between math.MinInt32 and math.MaxInt32, inclusive, and a BSON int64 +// otherwise. +// 4. int64 marshals to BSON int64. +// 5. uint8 and uint16 marshal to a BSON int32. +// 6. uint, uint32, and uint64 marshal to a BSON int32 if the value is between math.MinInt32 and math.MaxInt32, +// inclusive, and BSON int64 otherwise. +// 7. BSON null values will unmarshal into the zero value of a field (e.g. unmarshalling a BSON null value into a string +// will yield the empty string.). +// +// Structs +// +// Structs can be marshalled/unmarshalled to/from BSON. When transforming structs to/from BSON, the following rules +// apply: +// +// 1. Only exported fields in structs will be marshalled or unmarshalled. +// +// 2. When marshalling a struct, each field will be lowercased to generate the key for the corresponding BSON element. +// For example, a struct field named "Foo" will generate key "foo". This can be overriden via a struct tag (e.g. +// `bson:"fooField"` to generate key "fooField" instead). +// +// 3. An embedded struct field is marshalled as a subdocument. The key will be the lowercased name of the field's type. +// +// 4. A pointer field is marshalled as the underlying type if the pointer is non-nil. If the pointer is nil, it is +// marshalled as a BSON null value. +// +// 5. When unmarshalling, a field of type interface{} will follow the D/M type mappings listed above. BSON documents +// unmarshalled into an interface{} field will be unmarshalled as a D. +// +// The following struct tags can be used to configure behavior: +// +// 1. omitempty: If the omitempty struct tag is specified on a field, the field will not be marshalled if it is set to +// the zero value. By default, a struct field is only considered empty if the field's type implements the Zeroer +// interface and the IsZero method returns true. Struct fields of types that do not implement Zeroer are always +// marshalled as embedded documents. This tag should be used for all slice and map values. +// +// 2. minsize: If the minsize struct tag is specified on a field of type int64, uint, uint32, or uint64 and the value of +// the field can fit in a signed int32, the field will be serialized as a BSON int32 rather than a BSON int64. For other +// types, this tag is ignored. +// +// 3. truncate: If the truncate struct tag is specified on a field with a non-float numeric type, BSON doubles unmarshalled +// into that field will be trucated at the decimal point. For example, if 3.14 is unmarshalled into a field of type int, +// it will be unmarshalled as 3. If this tag is not specified, the decoder will throw an error if the value cannot be +// decoded without losing precision. For float64 or non-numeric types, this tag is ignored. +// +// 4. inline: If the inline struct tag is specified for a struct or map field, the field will be "flattened" when +// marshalling and "un-flattened" when unmarshalling. This means that all of the fields in that struct/map will be +// pulled up one level and will become top-level fields rather than being fields in a nested document. For example, if a +// map field named "Map" with value map[string]interface{}{"foo": "bar"} is inlined, the resulting document will be +// {"foo": "bar"} instead of {"map": {"foo": "bar"}}. There can only be one inlined map field in a struct. If there are +// duplicated fields in the resulting document when an inlined field is marshalled, an error will be returned. This tag +// can be used with fields that are pointers to structs. If an inlined pointer field is nil, it will not be marshalled. +// For fields that are not maps or structs, this tag is ignored. +// +// Marshalling and Unmarshalling +// +// Manually marshalling and unmarshalling can be done with the Marshal and Unmarshal family of functions. package bson diff --git a/vendor/go.mongodb.org/mongo-driver/bson/marshal.go b/vendor/go.mongodb.org/mongo-driver/bson/marshal.go index 9a4e28d4b9..381822af53 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/marshal.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/marshal.go @@ -32,7 +32,8 @@ type ValueMarshaler interface { MarshalBSONValue() (bsontype.Type, []byte, error) } -// Marshal returns the BSON encoding of val. +// Marshal returns the BSON encoding of val as a BSON document. If val is not a type that can be transformed into a +// document, MarshalValue should be used instead. // // Marshal will use the default registry created by NewRegistry to recursively // marshal val into a []byte. Marshal will inspect struct tags and alter the @@ -41,34 +42,37 @@ func Marshal(val interface{}) ([]byte, error) { return MarshalWithRegistry(DefaultRegistry, val) } -// MarshalAppend will append the BSON encoding of val to dst. If dst is not -// large enough to hold the BSON encoding of val, dst will be grown. +// MarshalAppend will encode val as a BSON document and append the bytes to dst. If dst is not large enough to hold the +// bytes, it will be grown. If val is not a type that can be transformed into a document, MarshalValueAppend should be +// used instead. func MarshalAppend(dst []byte, val interface{}) ([]byte, error) { return MarshalAppendWithRegistry(DefaultRegistry, dst, val) } -// MarshalWithRegistry returns the BSON encoding of val using Registry r. +// MarshalWithRegistry returns the BSON encoding of val as a BSON document. If val is not a type that can be transformed +// into a document, MarshalValueWithRegistry should be used instead. func MarshalWithRegistry(r *bsoncodec.Registry, val interface{}) ([]byte, error) { dst := make([]byte, 0, 256) // TODO: make the default cap a constant return MarshalAppendWithRegistry(r, dst, val) } -// MarshalWithContext returns the BSON encoding of val using EncodeContext ec. +// MarshalWithContext returns the BSON encoding of val as a BSON document using EncodeContext ec. If val is not a type +// that can be transformed into a document, MarshalValueWithContext should be used instead. func MarshalWithContext(ec bsoncodec.EncodeContext, val interface{}) ([]byte, error) { dst := make([]byte, 0, 256) // TODO: make the default cap a constant return MarshalAppendWithContext(ec, dst, val) } -// MarshalAppendWithRegistry will append the BSON encoding of val to dst using -// Registry r. If dst is not large enough to hold the BSON encoding of val, dst -// will be grown. +// MarshalAppendWithRegistry will encode val as a BSON document using Registry r and append the bytes to dst. If dst is +// not large enough to hold the bytes, it will be grown. If val is not a type that can be transformed into a document, +// MarshalValueAppendWithRegistry should be used instead. func MarshalAppendWithRegistry(r *bsoncodec.Registry, dst []byte, val interface{}) ([]byte, error) { return MarshalAppendWithContext(bsoncodec.EncodeContext{Registry: r}, dst, val) } -// MarshalAppendWithContext will append the BSON encoding of val to dst using -// EncodeContext ec. If dst is not large enough to hold the BSON encoding of val, dst -// will be grown. +// MarshalAppendWithContext will encode val as a BSON document using Registry r and EncodeContext ec and append the +// bytes to dst. If dst is not large enough to hold the bytes, it will be grown. If val is not a type that can be +// transformed into a document, MarshalValueAppendWithContext should be used instead. func MarshalAppendWithContext(ec bsoncodec.EncodeContext, dst []byte, val interface{}) ([]byte, error) { sw := new(bsonrw.SliceWriter) *sw = dst @@ -95,6 +99,69 @@ func MarshalAppendWithContext(ec bsoncodec.EncodeContext, dst []byte, val interf return *sw, nil } +// MarshalValue returns the BSON encoding of val. +// +// MarshalValue will use bson.DefaultRegistry to transform val into a BSON value. If val is a struct, this function will +// inspect struct tags and alter the marshalling process accordingly. +func MarshalValue(val interface{}) (bsontype.Type, []byte, error) { + return MarshalValueWithRegistry(DefaultRegistry, val) +} + +// MarshalValueAppend will append the BSON encoding of val to dst. If dst is not large enough to hold the BSON encoding +// of val, dst will be grown. +func MarshalValueAppend(dst []byte, val interface{}) (bsontype.Type, []byte, error) { + return MarshalValueAppendWithRegistry(DefaultRegistry, dst, val) +} + +// MarshalValueWithRegistry returns the BSON encoding of val using Registry r. +func MarshalValueWithRegistry(r *bsoncodec.Registry, val interface{}) (bsontype.Type, []byte, error) { + dst := make([]byte, 0, defaultDstCap) + return MarshalValueAppendWithRegistry(r, dst, val) +} + +// MarshalValueWithContext returns the BSON encoding of val using EncodeContext ec. +func MarshalValueWithContext(ec bsoncodec.EncodeContext, val interface{}) (bsontype.Type, []byte, error) { + dst := make([]byte, 0, defaultDstCap) + return MarshalValueAppendWithContext(ec, dst, val) +} + +// MarshalValueAppendWithRegistry will append the BSON encoding of val to dst using Registry r. If dst is not large +// enough to hold the BSON encoding of val, dst will be grown. +func MarshalValueAppendWithRegistry(r *bsoncodec.Registry, dst []byte, val interface{}) (bsontype.Type, []byte, error) { + return MarshalValueAppendWithContext(bsoncodec.EncodeContext{Registry: r}, dst, val) +} + +// MarshalValueAppendWithContext will append the BSON encoding of val to dst using EncodeContext ec. If dst is not large +// enough to hold the BSON encoding of val, dst will be grown. +func MarshalValueAppendWithContext(ec bsoncodec.EncodeContext, dst []byte, val interface{}) (bsontype.Type, []byte, error) { + // get a ValueWriter configured to write to dst + sw := new(bsonrw.SliceWriter) + *sw = dst + vwFlusher := bvwPool.GetAtModeElement(sw) + + // get an Encoder and encode the value + enc := encPool.Get().(*Encoder) + defer encPool.Put(enc) + if err := enc.Reset(vwFlusher); err != nil { + return 0, nil, err + } + if err := enc.SetContext(ec); err != nil { + return 0, nil, err + } + if err := enc.Encode(val); err != nil { + return 0, nil, err + } + + // flush the bytes written because we cannot guarantee that a full document has been written + // after the flush, *sw will be in the format + // [value type, 0 (null byte to indicate end of empty element name), value bytes..] + if err := vwFlusher.Flush(); err != nil { + return 0, nil, err + } + buffer := *sw + return bsontype.Type(buffer[0]), buffer[2:], nil +} + // MarshalExtJSON returns the extended JSON encoding of val. func MarshalExtJSON(val interface{}, canonical, escapeHTML bool) ([]byte, error) { return MarshalExtJSONWithRegistry(DefaultRegistry, val, canonical, escapeHTML) diff --git a/vendor/go.mongodb.org/mongo-driver/bson/primitive/decimal.go b/vendor/go.mongodb.org/mongo-driver/bson/primitive/decimal.go index d7fdb22805..bd0c5d161a 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/primitive/decimal.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/primitive/decimal.go @@ -10,11 +10,27 @@ package primitive import ( + "errors" "fmt" + "math/big" + "regexp" "strconv" "strings" ) +// These constants are the maximum and minimum values for the exponent field in a decimal128 value. +const ( + MaxDecimal128Exp = 6111 + MinDecimal128Exp = -6176 +) + +// These errors are returned when an invalid value is parsed as a big.Int. +var ( + ErrParseNaN = errors.New("cannot parse NaN as a *big.Int") + ErrParseInf = errors.New("cannot parse Infinity as a *big.Int") + ErrParseNegInf = errors.New("cannot parse -Infinity as a *big.Int") +) + // Decimal128 holds decimal128 BSON values. type Decimal128 struct { h, l uint64 @@ -25,7 +41,7 @@ func NewDecimal128(h, l uint64) Decimal128 { return Decimal128{h: h, l: l} } -// GetBytes returns the underlying bytes of the BSON decimal value as two uint16 values. The first +// GetBytes returns the underlying bytes of the BSON decimal value as two uint64 values. The first // contains the most first 8 bytes of the value and the second contains the latter. func (d Decimal128) GetBytes() (uint64, uint64) { return d.h, d.l @@ -33,52 +49,53 @@ func (d Decimal128) GetBytes() (uint64, uint64) { // String returns a string representation of the decimal value. func (d Decimal128) String() string { - var pos int // positive sign - var e int // exponent - var h, l uint64 // significand high/low + var posSign int // positive sign + var exp int // exponent + var high, low uint64 // significand high/low if d.h>>63&1 == 0 { - pos = 1 + posSign = 1 } switch d.h >> 58 & (1<<5 - 1) { case 0x1F: return "NaN" case 0x1E: - return "-Infinity"[pos:] + return "-Infinity"[posSign:] } - l = d.l + low = d.l if d.h>>61&3 == 3 { // Bits: 1*sign 2*ignored 14*exponent 111*significand. // Implicit 0b100 prefix in significand. - e = int(d.h>>47&(1<<14-1)) - 6176 - //h = 4<<47 | d.h&(1<<47-1) + exp = int(d.h >> 47 & (1<<14 - 1)) + //high = 4<<47 | d.h&(1<<47-1) // Spec says all of these values are out of range. - h, l = 0, 0 + high, low = 0, 0 } else { // Bits: 1*sign 14*exponent 113*significand - e = int(d.h>>49&(1<<14-1)) - 6176 - h = d.h & (1<<49 - 1) + exp = int(d.h >> 49 & (1<<14 - 1)) + high = d.h & (1<<49 - 1) } + exp += MinDecimal128Exp // Would be handled by the logic below, but that's trivial and common. - if h == 0 && l == 0 && e == 0 { - return "-0"[pos:] + if high == 0 && low == 0 && exp == 0 { + return "-0"[posSign:] } var repr [48]byte // Loop 5 times over 9 digits plus dot, negative sign, and leading zero. var last = len(repr) var i = len(repr) - var dot = len(repr) + e + var dot = len(repr) + exp var rem uint32 Loop: for d9 := 0; d9 < 5; d9++ { - h, l, rem = divmod(h, l, 1e9) + high, low, rem = divmod(high, low, 1e9) for d1 := 0; d1 < 9; d1++ { // Handle "-0.0", "0.00123400", "-1.00E-6", "1.050E+3", etc. - if i < len(repr) && (dot == i || l == 0 && h == 0 && rem > 0 && rem < 10 && (dot < i-6 || e > 0)) { - e += len(repr) - i + if i < len(repr) && (dot == i || low == 0 && high == 0 && rem > 0 && rem < 10 && (dot < i-6 || exp > 0)) { + exp += len(repr) - i i-- repr[i] = '.' last = i - 1 @@ -89,7 +106,7 @@ Loop: i-- repr[i] = c // Handle "0E+3", "1E+3", etc. - if l == 0 && h == 0 && rem == 0 && i == len(repr)-1 && (dot < i-5 || e > 0) { + if low == 0 && high == 0 && rem == 0 && i == len(repr)-1 && (dot < i-5 || exp > 0) { last = i break Loop } @@ -97,7 +114,7 @@ Loop: last = i } // Break early. Works without it, but why. - if dot > i && l == 0 && h == 0 && rem == 0 { + if dot > i && low == 0 && high == 0 && rem == 0 { break Loop } } @@ -105,13 +122,88 @@ Loop: repr[last-1] = '-' last-- - if e > 0 { - return string(repr[last+pos:]) + "E+" + strconv.Itoa(e) + if exp > 0 { + return string(repr[last+posSign:]) + "E+" + strconv.Itoa(exp) } - if e < 0 { - return string(repr[last+pos:]) + "E" + strconv.Itoa(e) + if exp < 0 { + return string(repr[last+posSign:]) + "E" + strconv.Itoa(exp) } - return string(repr[last+pos:]) + return string(repr[last+posSign:]) +} + +// BigInt returns significand as big.Int and exponent, bi * 10 ^ exp. +func (d Decimal128) BigInt() (bi *big.Int, exp int, err error) { + high, low := d.GetBytes() + var posSign bool // positive sign + + posSign = high>>63&1 == 0 + + switch high >> 58 & (1<<5 - 1) { + case 0x1F: + return nil, 0, ErrParseNaN + case 0x1E: + if posSign { + return nil, 0, ErrParseInf + } + return nil, 0, ErrParseNegInf + } + + if high>>61&3 == 3 { + // Bits: 1*sign 2*ignored 14*exponent 111*significand. + // Implicit 0b100 prefix in significand. + exp = int(high >> 47 & (1<<14 - 1)) + //high = 4<<47 | d.h&(1<<47-1) + // Spec says all of these values are out of range. + high, low = 0, 0 + } else { + // Bits: 1*sign 14*exponent 113*significand + exp = int(high >> 49 & (1<<14 - 1)) + high = high & (1<<49 - 1) + } + exp += MinDecimal128Exp + + // Would be handled by the logic below, but that's trivial and common. + if high == 0 && low == 0 && exp == 0 { + if posSign { + return new(big.Int), 0, nil + } + return new(big.Int), 0, nil + } + + bi = big.NewInt(0) + const host32bit = ^uint(0)>>32 == 0 + if host32bit { + bi.SetBits([]big.Word{big.Word(low), big.Word(low >> 32), big.Word(high), big.Word(high >> 32)}) + } else { + bi.SetBits([]big.Word{big.Word(low), big.Word(high)}) + } + + if !posSign { + return bi.Neg(bi), exp, nil + } + return +} + +// IsNaN returns whether d is NaN. +func (d Decimal128) IsNaN() bool { + return d.h>>58&(1<<5-1) == 0x1F +} + +// IsInf returns: +// +// +1 d == Infinity +// 0 other case +// -1 d == -Infinity +// +func (d Decimal128) IsInf() int { + if d.h>>58&(1<<5-1) != 0x1E { + return 0 + } + + if d.h>>63&1 == 0 { + return 1 + } + return -1 } func divmod(h, l uint64, div uint32) (qh, ql uint64, rem uint32) { @@ -139,19 +231,24 @@ func dErr(s string) (Decimal128, error) { return dNaN, fmt.Errorf("cannot parse %q as a decimal128", s) } -//ParseDecimal128 takes the given string and attempts to parse it into a valid +// match scientific notation number, example -10.15e-18 +var normalNumber = regexp.MustCompile(`^(?P<int>[-+]?\d*)?(?:\.(?P<dec>\d*))?(?:[Ee](?P<exp>[-+]?\d+))?$`) + +// ParseDecimal128 takes the given string and attempts to parse it into a valid // Decimal128 value. func ParseDecimal128(s string) (Decimal128, error) { - orig := s if s == "" { - return dErr(orig) - } - neg := s[0] == '-' - if neg || s[0] == '+' { - s = s[1:] + return dErr(s) } - if (len(s) == 3 || len(s) == 8) && (s[0] == 'N' || s[0] == 'n' || s[0] == 'I' || s[0] == 'i') { + matches := normalNumber.FindStringSubmatch(s) + if len(matches) == 0 { + orig := s + neg := s[0] == '-' + if neg || s[0] == '+' { + s = s[1:] + } + if s == "NaN" || s == "nan" || strings.EqualFold(s, "nan") { return dNaN, nil } @@ -164,144 +261,116 @@ func ParseDecimal128(s string) (Decimal128, error) { return dErr(orig) } - var h, l uint64 - var e int - - var add, ovr uint32 - var mul uint32 = 1 - var dot = -1 - var digits = 0 - var i = 0 - for i < len(s) { - c := s[i] - if mul == 1e9 { - h, l, ovr = muladd(h, l, mul, add) - mul, add = 1, 0 - if ovr > 0 || h&((1<<15-1)<<49) > 0 { - return dErr(orig) - } - } - if c >= '0' && c <= '9' { - i++ - if c > '0' || digits > 0 { - digits++ - } - if digits > 34 { - if c == '0' { - // Exact rounding. - e++ - continue - } - return dErr(orig) - } - mul *= 10 - add *= 10 - add += uint32(c - '0') - continue - } - if c == '.' { - i++ - if dot >= 0 || i == 1 && len(s) == 1 { - return dErr(orig) - } - if i == len(s) { - break - } - if s[i] < '0' || s[i] > '9' || e > 0 { - return dErr(orig) - } - dot = i - continue + intPart := matches[1] + decPart := matches[2] + expPart := matches[3] + + var err error + exp := 0 + if expPart != "" { + exp, err = strconv.Atoi(expPart) + if err != nil { + return dErr(s) } - break } - if i == 0 { - return dErr(orig) + if decPart != "" { + exp -= len(decPart) } - if mul > 1 { - h, l, ovr = muladd(h, l, mul, add) - if ovr > 0 || h&((1<<15-1)<<49) > 0 { - return dErr(orig) - } + + if len(strings.Trim(intPart+decPart, "-0")) > 35 { + return dErr(s) } - if dot >= 0 { - e += dot - i + + bi, ok := new(big.Int).SetString(intPart+decPart, 10) + if !ok { + return dErr(s) } - if i+1 < len(s) && (s[i] == 'E' || s[i] == 'e') { - i++ - eneg := s[i] == '-' - if eneg || s[i] == '+' { - i++ - if i == len(s) { - return dErr(orig) - } - } - n := 0 - for i < len(s) && n < 1e4 { - c := s[i] - i++ - if c < '0' || c > '9' { - return dErr(orig) - } - n *= 10 - n += int(c - '0') - } - if eneg { - n = -n + + d, ok := ParseDecimal128FromBigInt(bi, exp) + if !ok { + return dErr(s) + } + + if bi.Sign() == 0 && s[0] == '-' { + d.h |= 1 << 63 + } + + return d, nil +} + +var ( + ten = big.NewInt(10) + zero = new(big.Int) + + maxS, _ = new(big.Int).SetString("9999999999999999999999999999999999", 10) +) + +// ParseDecimal128FromBigInt attempts to parse the given significand and exponent into a valid Decimal128 value. +func ParseDecimal128FromBigInt(bi *big.Int, exp int) (Decimal128, bool) { + //copy + bi = new(big.Int).Set(bi) + + q := new(big.Int) + r := new(big.Int) + + for bigIntCmpAbs(bi, maxS) == 1 { + bi, _ = q.QuoRem(bi, ten, r) + if r.Cmp(zero) != 0 { + return Decimal128{}, false } - e += n - for e < -6176 { - // Subnormal. - var div uint32 = 1 - for div < 1e9 && e < -6176 { - div *= 10 - e++ - } - var rem uint32 - h, l, rem = divmod(h, l, div) - if rem > 0 { - return dErr(orig) - } + exp++ + if exp > MaxDecimal128Exp { + return Decimal128{}, false } - for e > 6111 { - // Clamped. - var mul uint32 = 1 - for mul < 1e9 && e > 6111 { - mul *= 10 - e-- - } - h, l, ovr = muladd(h, l, mul, 0) - if ovr > 0 || h&((1<<15-1)<<49) > 0 { - return dErr(orig) - } + } + + for exp < MinDecimal128Exp { + // Subnormal. + bi, _ = q.QuoRem(bi, ten, r) + if r.Cmp(zero) != 0 { + return Decimal128{}, false } - if e < -6176 || e > 6111 { - return dErr(orig) + exp++ + } + for exp > MaxDecimal128Exp { + // Clamped. + bi.Mul(bi, ten) + if bigIntCmpAbs(bi, maxS) == 1 { + return Decimal128{}, false } + exp-- } - if i < len(s) { - return dErr(orig) + b := bi.Bytes() + var h, l uint64 + for i := 0; i < len(b); i++ { + if i < len(b)-8 { + h = h<<8 | uint64(b[i]) + continue + } + l = l<<8 | uint64(b[i]) } - h |= uint64(e+6176) & uint64(1<<14-1) << 49 - if neg { + h |= uint64(exp-MinDecimal128Exp) & uint64(1<<14-1) << 49 + if bi.Sign() == -1 { h |= 1 << 63 } - return Decimal128{h, l}, nil -} -func muladd(h, l uint64, mul uint32, add uint32) (resh, resl uint64, overflow uint32) { - mul64 := uint64(mul) - a := mul64 * (l & (1<<32 - 1)) - b := a>>32 + mul64*(l>>32) - c := b>>32 + mul64*(h&(1<<32-1)) - d := c>>32 + mul64*(h>>32) + return Decimal128{h: h, l: l}, true +} - a = a&(1<<32-1) + uint64(add) - b = b&(1<<32-1) + a>>32 - c = c&(1<<32-1) + b>>32 - d = d&(1<<32-1) + c>>32 +// bigIntCmpAbs computes big.Int.Cmp(absoluteValue(x), absoluteValue(y)). +func bigIntCmpAbs(x, y *big.Int) int { + xAbs := bigIntAbsValue(x) + yAbs := bigIntAbsValue(y) + return xAbs.Cmp(yAbs) +} - return (d<<32 | c&(1<<32-1)), (b<<32 | a&(1<<32-1)), uint32(d >> 32) +// bigIntAbsValue returns a big.Int containing the absolute value of b. +// If b is already a non-negative number, it is returned without any changes or copies. +func bigIntAbsValue(b *big.Int) *big.Int { + if b.Sign() >= 0 { + return b // already positive + } + return new(big.Int).Abs(b) } diff --git a/vendor/go.mongodb.org/mongo-driver/bson/primitive/objectid.go b/vendor/go.mongodb.org/mongo-driver/bson/primitive/objectid.go index 0d00000040..41d1cf2886 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/primitive/objectid.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/primitive/objectid.go @@ -126,7 +126,7 @@ func (id *ObjectID) UnmarshalJSON(b []byte) error { } if len(str) != 24 { - return fmt.Errorf("cannot unmarshal into an ObjectID, the length must be 12 but it is %d", len(str)) + return fmt.Errorf("cannot unmarshal into an ObjectID, the length must be 24 but it is %d", len(str)) } _, err = hex.Decode(id[:], []byte(str)) diff --git a/vendor/go.mongodb.org/mongo-driver/bson/primitive/primitive.go b/vendor/go.mongodb.org/mongo-driver/bson/primitive/primitive.go index 20a597d0e8..f47f8df3c8 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/primitive/primitive.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/primitive/primitive.go @@ -21,7 +21,7 @@ type Binary struct { Data []byte } -// Equal compaes bp to bp2 and returns true is the are equal. +// Equal compares bp to bp2 and returns true is the are equal. func (bp Binary) Equal(bp2 Binary) bool { if bp.Subtype != bp2.Subtype { return false @@ -29,6 +29,11 @@ func (bp Binary) Equal(bp2 Binary) bool { return bytes.Equal(bp.Data, bp2.Data) } +// IsZero returns if bp is the empty Binary +func (bp Binary) IsZero() bool { + return bp.Subtype == 0 && len(bp.Data) == 0 +} + // Undefined represents the BSON undefined value type. type Undefined struct{} @@ -50,7 +55,7 @@ func NewDateTimeFromTime(t time.Time) DateTime { return DateTime(t.UnixNano() / 1000000) } -// Null repreesnts the BSON null value. +// Null represents the BSON null value. type Null struct{} // Regex represents a BSON regex value. @@ -63,9 +68,14 @@ func (rp Regex) String() string { return fmt.Sprintf(`{"pattern": "%s", "options": "%s"}`, rp.Pattern, rp.Options) } -// Equal compaes rp to rp2 and returns true is the are equal. +// Equal compares rp to rp2 and returns true is the are equal. func (rp Regex) Equal(rp2 Regex) bool { - return rp.Pattern == rp2.Pattern && rp.Options == rp.Options + return rp.Pattern == rp2.Pattern && rp.Options == rp2.Options +} + +// IsZero returns if rp is the empty Regex +func (rp Regex) IsZero() bool { + return rp.Pattern == "" && rp.Options == "" } // DBPointer represents a BSON dbpointer value. @@ -78,11 +88,16 @@ func (d DBPointer) String() string { return fmt.Sprintf(`{"db": "%s", "pointer": "%s"}`, d.DB, d.Pointer) } -// Equal compaes d to d2 and returns true is the are equal. +// Equal compares d to d2 and returns true is the are equal. func (d DBPointer) Equal(d2 DBPointer) bool { return d.DB == d2.DB && bytes.Equal(d.Pointer[:], d2.Pointer[:]) } +// IsZero returns if d is the empty DBPointer +func (d DBPointer) IsZero() bool { + return d.DB == "" && d.Pointer.IsZero() +} + // JavaScript represents a BSON JavaScript code value. type JavaScript string @@ -105,11 +120,16 @@ type Timestamp struct { I uint32 } -// Equal compaes tp to tp2 and returns true is the are equal. +// Equal compares tp to tp2 and returns true is the are equal. func (tp Timestamp) Equal(tp2 Timestamp) bool { return tp.T == tp2.T && tp.I == tp2.I } +// IsZero returns if tp is the zero Timestamp +func (tp Timestamp) IsZero() bool { + return tp.T == 0 && tp.I == 0 +} + // CompareTimestamp returns an integer comparing two Timestamps, where T is compared first, followed by I. // Returns 0 if tp = tp2, 1 if tp > tp2, -1 if tp < tp2. func CompareTimestamp(tp, tp2 Timestamp) int { @@ -136,16 +156,12 @@ type MinKey struct{} // MaxKey represents the BSON maxkey value. type MaxKey struct{} -// D represents a BSON Document. This type can be used to represent BSON in a concise and readable -// manner. It should generally be used when serializing to BSON. For deserializing, the Raw or -// Document types should be used. +// D is an ordered representation of a BSON document. This type should be used when the order of the elements matters, +// such as MongoDB command documents. If the order of the elements does not matter, an M should be used instead. // // Example usage: // -// primitive.D{{"foo", "bar"}, {"hello", "world"}, {"pi", 3.14159}} -// -// This type should be used in situations where order matters, such as MongoDB commands. If the -// order is not important, a map is more comfortable and concise. +// bson.D{{"foo", "bar"}, {"hello", "world"}, {"pi", 3.14159}} type D []E // Map creates a map from the elements of the D. @@ -163,24 +179,18 @@ type E struct { Value interface{} } -// M is an unordered, concise representation of a BSON Document. It should generally be used to -// serialize BSON when the order of the elements of a BSON document do not matter. If the element -// order matters, use a D instead. +// M is an unordered representation of a BSON document. This type should be used when the order of the elements does not +// matter. This type is handled as a regular map[string]interface{} when encoding and decoding. Elements will be +// serialized in an undefined, random order. If the order of the elements matters, a D should be used instead. // // Example usage: // -// primitive.M{"foo": "bar", "hello": "world", "pi": 3.14159} -// -// This type is handled in the encoders as a regular map[string]interface{}. The elements will be -// serialized in an undefined, random order, and the order will be different each time. +// bson.M{"foo": "bar", "hello": "world", "pi": 3.14159}. type M map[string]interface{} -// An A represents a BSON array. This type can be used to represent a BSON array in a concise and -// readable manner. It should generally be used when serializing to BSON. For deserializing, the -// RawArray or Array types should be used. +// An A is an ordered representation of a BSON array. // // Example usage: // -// primitive.A{"bar", "world", 3.14159, primitive.D{{"qux", 12345}}} -// +// bson.A{"bar", "world", 3.14159, bson.D{{"qux", 12345}}} type A []interface{} diff --git a/vendor/go.mongodb.org/mongo-driver/bson/primitive_codecs.go b/vendor/go.mongodb.org/mongo-driver/bson/primitive_codecs.go index 1dae2fc6f8..f397fa2d58 100644 --- a/vendor/go.mongodb.org/mongo-driver/bson/primitive_codecs.go +++ b/vendor/go.mongodb.org/mongo-driver/bson/primitive_codecs.go @@ -28,10 +28,10 @@ func (pc PrimitiveCodecs) RegisterPrimitiveCodecs(rb *bsoncodec.RegistryBuilder) } rb. - RegisterEncoder(tRawValue, bsoncodec.ValueEncoderFunc(pc.RawValueEncodeValue)). - RegisterEncoder(tRaw, bsoncodec.ValueEncoderFunc(pc.RawEncodeValue)). - RegisterDecoder(tRawValue, bsoncodec.ValueDecoderFunc(pc.RawValueDecodeValue)). - RegisterDecoder(tRaw, bsoncodec.ValueDecoderFunc(pc.RawDecodeValue)) + RegisterTypeEncoder(tRawValue, bsoncodec.ValueEncoderFunc(pc.RawValueEncodeValue)). + RegisterTypeEncoder(tRaw, bsoncodec.ValueEncoderFunc(pc.RawEncodeValue)). + RegisterTypeDecoder(tRawValue, bsoncodec.ValueDecoderFunc(pc.RawValueDecodeValue)). + RegisterTypeDecoder(tRaw, bsoncodec.ValueDecoderFunc(pc.RawDecodeValue)) } // RawValueEncodeValue is the ValueEncoderFunc for RawValue. diff --git a/vendor/go.mongodb.org/mongo-driver/x/bsonx/bsoncore/bsoncore.go b/vendor/go.mongodb.org/mongo-driver/x/bsonx/bsoncore/bsoncore.go index e61194ef27..2a20d2e075 100644 --- a/vendor/go.mongodb.org/mongo-driver/x/bsonx/bsoncore/bsoncore.go +++ b/vendor/go.mongodb.org/mongo-driver/x/bsonx/bsoncore/bsoncore.go @@ -123,6 +123,9 @@ func ReadElement(src []byte) (Element, []byte, bool) { if elemLength > len(src) { return nil, src, false } + if elemLength < 0 { + return nil, src, false + } return src[:elemLength], src[elemLength:], true } @@ -723,13 +726,18 @@ func appendi32(dst []byte, i32 int32) []byte { // ReadLength reads an int32 length from src and returns the length and the remaining bytes. If // there aren't enough bytes to read a valid length, src is returned unomdified and the returned // bool will be false. -func ReadLength(src []byte) (int32, []byte, bool) { return readi32(src) } +func ReadLength(src []byte) (int32, []byte, bool) { + ln, src, ok := readi32(src) + if ln < 0 { + return ln, src, false + } + return ln, src, ok +} func readi32(src []byte) (int32, []byte, bool) { if len(src) < 4 { return 0, src, false } - return (int32(src[0]) | int32(src[1])<<8 | int32(src[2])<<16 | int32(src[3])<<24), src[4:], true } |