Skip to content

Commit 0d4a3e7

Browse files
authored
Merge pull request #1 from mongodb/master
merge
2 parents a9c1a8e + bce51d3 commit 0d4a3e7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2861
-908
lines changed

benchmark/multi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func multiInsertCase(ctx context.Context, tm TimerManager, iters int, data strin
109109
return err
110110
}
111111

112-
_, err = db.RunCommand(ctx, bsonx.Doc{{"create", bsonx.String("corpus")}})
112+
err = db.RunCommand(ctx, bsonx.Doc{{"create", bsonx.String("corpus")}}).Err()
113113
if err != nil {
114114
return err
115115
}

benchmark/single.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func getClientDB(ctx context.Context) (*mongo.Database, error) {
2727
if err != nil {
2828
return nil, err
2929
}
30-
client, err := mongo.NewClientFromConnString(cs)
30+
client, err := mongo.NewClient(cs.String())
3131
if err != nil {
3232
return nil, err
3333
}
@@ -53,11 +53,13 @@ func SingleRunCommand(ctx context.Context, tm TimerManager, iters int) error {
5353

5454
tm.ResetTimer()
5555
for i := 0; i < iters; i++ {
56-
out, err := db.RunCommand(ctx, cmd)
56+
var doc bsonx.Doc
57+
err := db.RunCommand(ctx, cmd).Decode(&doc)
5758
if err != nil {
5859
return err
5960
}
6061
// read the document and then throw it away to prevent
62+
out, err := doc.MarshalBSON()
6163
if len(out) == 0 {
6264
return errors.New("output of ismaster is empty")
6365
}
@@ -135,7 +137,7 @@ func singleInsertCase(ctx context.Context, tm TimerManager, iters int, data stri
135137
return err
136138
}
137139

138-
_, err = db.RunCommand(ctx, bsonx.Doc{{"create", bsonx.String("corpus")}})
140+
err = db.RunCommand(ctx, bsonx.Doc{{"create", bsonx.String("corpus")}}).Err()
139141
if err != nil {
140142
return err
141143
}

bson/bsoncodec/default_value_decoders.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func (dvd DefaultValueDecoders) RegisterDefaultDecoders(rb *RegistryBuilder) {
4848
RegisterDecoder(reflect.PtrTo(tJSONNumber), ValueDecoderFunc(dvd.JSONNumberDecodeValue)).
4949
RegisterDecoder(reflect.PtrTo(tURL), ValueDecoderFunc(dvd.URLDecodeValue)).
5050
RegisterDecoder(tValueUnmarshaler, ValueDecoderFunc(dvd.ValueUnmarshalerDecodeValue)).
51+
RegisterDecoder(tUnmarshaler, ValueDecoderFunc(dvd.UnmarshalerDecodeValue)).
5152
RegisterDefaultDecoder(reflect.Bool, ValueDecoderFunc(dvd.BooleanDecodeValue)).
5253
RegisterDefaultDecoder(reflect.Int, ValueDecoderFunc(dvd.IntDecodeValue)).
5354
RegisterDefaultDecoder(reflect.Int8, ValueDecoderFunc(dvd.IntDecodeValue)).
@@ -735,6 +736,32 @@ func (dvd DefaultValueDecoders) ValueUnmarshalerDecodeValue(dc DecodeContext, vr
735736
return valueUnmarshaler.UnmarshalBSONValue(t, src)
736737
}
737738

739+
// UnmarshalerDecodeValue is the ValueDecoderFunc for Unmarshaler implementations.
740+
func (dvd DefaultValueDecoders) UnmarshalerDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, i interface{}) error {
741+
val := reflect.ValueOf(i)
742+
var unmarshaler Unmarshaler
743+
if val.Kind() == reflect.Ptr && val.IsNil() {
744+
return fmt.Errorf("UnmarshalerDecodeValue can only unmarshal into non-nil Unmarshaler values, got %T", i)
745+
}
746+
if val.Type().Implements(tUnmarshaler) {
747+
unmarshaler = val.Interface().(Unmarshaler)
748+
} else if val.Type().Kind() == reflect.Ptr && val.Elem().Type().Implements(tUnmarshaler) {
749+
if val.Elem().Kind() == reflect.Ptr && val.Elem().IsNil() {
750+
val.Elem().Set(reflect.New(val.Type().Elem().Elem()))
751+
}
752+
unmarshaler = val.Elem().Interface().(Unmarshaler)
753+
} else {
754+
return fmt.Errorf("UnmarshalerDecodeValue can only handle types or pointers to types that are a Unmarshaler, got %T", i)
755+
}
756+
757+
_, src, err := bsonrw.Copier{}.CopyValueToBytes(vr)
758+
if err != nil {
759+
return err
760+
}
761+
762+
return unmarshaler.UnmarshalBSON(src)
763+
}
764+
738765
// EmptyInterfaceDecodeValue is the ValueDecoderFunc for interface{}.
739766
func (dvd DefaultValueDecoders) EmptyInterfaceDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, i interface{}) error {
740767
target, ok := i.(*interface{})

bson/bsoncodec/default_value_decoders_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func TestDefaultValueDecoders(t *testing.T) {
5252
now := time.Now().Truncate(time.Millisecond)
5353
d128 := decimal.NewDecimal128(12345, 67890)
5454
var ptrPtrValueUnmarshaler **testValueUnmarshaler
55+
var ptrPtrUnmarshaler **testUnmarshaler
5556

5657
type subtest struct {
5758
name string
@@ -1099,6 +1100,36 @@ func TestDefaultValueDecoders(t *testing.T) {
10991100
},
11001101
},
11011102
},
1103+
{
1104+
"UnmarshalerDecodeValue",
1105+
ValueDecoderFunc(dvd.UnmarshalerDecodeValue),
1106+
[]subtest{
1107+
{
1108+
"wrong type",
1109+
wrong,
1110+
nil,
1111+
nil,
1112+
bsonrwtest.Nothing,
1113+
fmt.Errorf("UnmarshalerDecodeValue can only handle types or pointers to types that are a Unmarshaler, got %T", &wrong),
1114+
},
1115+
{
1116+
"Unmarshaler",
1117+
testUnmarshaler{Val: bsoncore.AppendDouble(nil, 3.14159)},
1118+
nil,
1119+
&bsonrwtest.ValueReaderWriter{BSONType: bsontype.Double, Return: float64(3.14159)},
1120+
bsonrwtest.ReadDouble,
1121+
nil,
1122+
},
1123+
{
1124+
"nil pointer to Unmarshaler",
1125+
ptrPtrUnmarshaler,
1126+
nil,
1127+
&bsonrwtest.ValueReaderWriter{BSONType: bsontype.Double, Return: float64(3.14159)},
1128+
bsonrwtest.Nothing,
1129+
fmt.Errorf("UnmarshalerDecodeValue can only unmarshal into non-nil Unmarshaler values, got %T", ptrPtrUnmarshaler),
1130+
},
1131+
},
1132+
},
11021133
}
11031134

11041135
for _, tc := range testCases {
@@ -1698,6 +1729,17 @@ func (tvu *testValueUnmarshaler) UnmarshalBSONValue(t bsontype.Type, val []byte)
16981729
tvu.t, tvu.val = t, val
16991730
return tvu.err
17001731
}
1732+
1733+
type testUnmarshaler struct {
1734+
Val []byte
1735+
Err error
1736+
}
1737+
1738+
func (tvu *testUnmarshaler) UnmarshalBSON(val []byte) error {
1739+
tvu.Val = val
1740+
return tvu.Err
1741+
}
1742+
17011743
func (tvu testValueUnmarshaler) Equal(tvu2 testValueUnmarshaler) bool {
17021744
return tvu.t == tvu2.t && bytes.Equal(tvu.val, tvu2.val)
17031745
}

bson/bsoncodec/default_value_encoders.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"time"
1717

1818
"github.com/mongodb/mongo-go-driver/bson/bsonrw"
19+
"github.com/mongodb/mongo-go-driver/bson/bsontype"
1920
"github.com/mongodb/mongo-go-driver/bson/decimal"
2021
"github.com/mongodb/mongo-go-driver/bson/objectid"
2122
)
@@ -41,6 +42,7 @@ func (dve DefaultValueEncoders) RegisterDefaultEncoders(rb *RegistryBuilder) {
4142
RegisterEncoder(tJSONNumber, ValueEncoderFunc(dve.JSONNumberEncodeValue)).
4243
RegisterEncoder(tURL, ValueEncoderFunc(dve.URLEncodeValue)).
4344
RegisterEncoder(tValueMarshaler, ValueEncoderFunc(dve.ValueMarshalerEncodeValue)).
45+
RegisterEncoder(tMarshaler, ValueEncoderFunc(dve.MarshalerEncodeValue)).
4446
RegisterEncoder(tProxy, ValueEncoderFunc(dve.ProxyEncodeValue)).
4547
RegisterDefaultEncoder(reflect.Bool, ValueEncoderFunc(dve.BooleanEncodeValue)).
4648
RegisterDefaultEncoder(reflect.Int, ValueEncoderFunc(dve.IntEncodeValue)).
@@ -442,6 +444,9 @@ func (dve DefaultValueEncoders) SliceEncodeValue(ec EncodeContext, vw bsonrw.Val
442444

443445
// EmptyInterfaceEncodeValue is the ValueEncoderFunc for interface{}.
444446
func (dve DefaultValueEncoders) EmptyInterfaceEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, i interface{}) error {
447+
if i == nil {
448+
return vw.WriteNull()
449+
}
445450
encoder, err := ec.LookupEncoder(reflect.TypeOf(i))
446451
if err != nil {
447452
return err
@@ -468,6 +473,24 @@ func (dve DefaultValueEncoders) ValueMarshalerEncodeValue(ec EncodeContext, vw b
468473
return bsonrw.Copier{}.CopyValueFromBytes(vw, t, val)
469474
}
470475

476+
// MarshalerEncodeValue is the ValueEncoderFunc for Marshaler implementations.
477+
func (dve DefaultValueEncoders) MarshalerEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, i interface{}) error {
478+
vm, ok := i.(Marshaler)
479+
if !ok {
480+
return ValueEncoderError{
481+
Name: "MarshalerEncodeValue",
482+
Types: []interface{}{(Marshaler)(nil)},
483+
Received: i,
484+
}
485+
}
486+
487+
val, err := vm.MarshalBSON()
488+
if err != nil {
489+
return err
490+
}
491+
return bsonrw.Copier{}.CopyValueFromBytes(vw, bsontype.EmbeddedDocument, val)
492+
}
493+
471494
// ProxyEncodeValue is the ValueEncoderFunc for Proxy implementations.
472495
func (dve DefaultValueEncoders) ProxyEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, i interface{}) error {
473496
proxy, ok := i.(Proxy)

bson/bsoncodec/default_value_encoders_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,13 @@ func TestDefaultValueEncoders(t *testing.T) {
430430
{"*[]byte/nil", pbytesliceNil, nil, nil, bsonrwtest.WriteNull, nil},
431431
},
432432
},
433+
{
434+
"EmptyInterfaceEncodeValue",
435+
ValueEncoderFunc(dve.EmptyInterfaceEncodeValue),
436+
[]subtest{
437+
{"interface/nil", nil, nil, nil, bsonrwtest.WriteNull, nil},
438+
},
439+
},
433440
{
434441
"ValueMarshalerEncodeValue",
435442
ValueEncoderFunc(dve.ValueMarshalerEncodeValue),
@@ -472,6 +479,40 @@ func TestDefaultValueEncoders(t *testing.T) {
472479
},
473480
},
474481
},
482+
{
483+
"MarshalerEncodeValue",
484+
ValueEncoderFunc(dve.MarshalerEncodeValue),
485+
[]subtest{
486+
{
487+
"wrong type",
488+
wrong,
489+
nil,
490+
nil,
491+
bsonrwtest.Nothing,
492+
ValueEncoderError{
493+
Name: "MarshalerEncodeValue",
494+
Types: []interface{}{(ValueMarshaler)(nil)},
495+
Received: wrong,
496+
},
497+
},
498+
{
499+
"MarshalBSON error",
500+
testMarshaler{err: errors.New("mbson error")},
501+
nil,
502+
nil,
503+
bsonrwtest.Nothing,
504+
errors.New("mbson error"),
505+
},
506+
{
507+
"success",
508+
testMarshaler{buf: bsoncore.BuildDocument(nil, bsoncore.AppendDoubleElement(nil, "pi", 3.14159))},
509+
nil,
510+
nil,
511+
bsonrwtest.WriteDocumentEnd,
512+
nil,
513+
},
514+
},
515+
},
475516
{
476517
"ProxyEncodeValue",
477518
ValueEncoderFunc(dve.ProxyEncodeValue),
@@ -998,6 +1039,15 @@ func (tvm testValueMarshaler) MarshalBSONValue() (bsontype.Type, []byte, error)
9981039
return tvm.t, tvm.buf, tvm.err
9991040
}
10001041

1042+
type testMarshaler struct {
1043+
buf []byte
1044+
err error
1045+
}
1046+
1047+
func (tvm testMarshaler) MarshalBSON() ([]byte, error) {
1048+
return tvm.buf, tvm.err
1049+
}
1050+
10011051
type testProxy struct {
10021052
ret interface{}
10031053
err error

bson/bsoncodec/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,6 @@ var tJSONNumber = reflect.TypeOf(json.Number(""))
5757

5858
var tValueMarshaler = reflect.TypeOf((*ValueMarshaler)(nil)).Elem()
5959
var tValueUnmarshaler = reflect.TypeOf((*ValueUnmarshaler)(nil)).Elem()
60+
var tMarshaler = reflect.TypeOf((*Marshaler)(nil)).Elem()
61+
var tUnmarshaler = reflect.TypeOf((*Unmarshaler)(nil)).Elem()
6062
var tProxy = reflect.TypeOf((*Proxy)(nil)).Elem()

0 commit comments

Comments
 (0)