Skip to content

Commit 6c8f481

Browse files
author
Divjot Arora
committed
Move Decimal128 and ObjectID types to bson/primitive.
GODRIVER-661 Change-Id: Iddce6140d275fc8217719f522521dc7cb1731cf9
1 parent bce51d3 commit 6c8f481

Some content is hidden

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

48 files changed

+438
-483
lines changed

benchmark/bson_types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
package benchmark
88

9-
import "github.com/mongodb/mongo-go-driver/bson/objectid"
9+
import "github.com/mongodb/mongo-go-driver/bson/primitive"
1010

1111
type flatBSONTags struct {
12-
ID objectid.ObjectID `bson:"_id"`
12+
ID primitive.ObjectID `bson:"_id"`
1313

1414
AA int64 `bson:"AAgSNVyBb"`
1515
AI bool `bson:"aicoMxZq"`
@@ -233,7 +233,7 @@ type flatBSON struct {
233233
XxvXmHiQ int
234234
YDHWnEXV string
235235
ZmtEJFSO string
236-
ID objectid.ObjectID `bson:"_id"`
236+
ID primitive.ObjectID `bson:"_id"`
237237
AhFCBmqT int64
238238
AicoMxZq bool
239239
BkuaZWRT int64

bson/bsoncodec/bsoncodec_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"testing"
1212

1313
"github.com/mongodb/mongo-go-driver/bson/bsonrw"
14-
"github.com/mongodb/mongo-go-driver/bson/decimal"
14+
"github.com/mongodb/mongo-go-driver/bson/primitive"
1515
)
1616

1717
func noerr(t *testing.T, err error) {
@@ -38,7 +38,7 @@ func compareErrors(err1, err2 error) bool {
3838
return true
3939
}
4040

41-
func compareDecimal128(d1, d2 decimal.Decimal128) bool {
41+
func compareDecimal128(d1, d2 primitive.Decimal128) bool {
4242
d1H, d1L := d1.GetBytes()
4343
d2H, d2L := d2.GetBytes()
4444

bson/bsoncodec/default_value_decoders.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ import (
1818

1919
"github.com/mongodb/mongo-go-driver/bson/bsonrw"
2020
"github.com/mongodb/mongo-go-driver/bson/bsontype"
21-
"github.com/mongodb/mongo-go-driver/bson/decimal"
22-
"github.com/mongodb/mongo-go-driver/bson/objectid"
21+
"github.com/mongodb/mongo-go-driver/bson/primitive"
2322
)
2423

2524
var defaultValueDecoders DefaultValueDecoders
@@ -433,15 +432,15 @@ func (dvd DefaultValueDecoders) StringDecodeValue(dctx DecodeContext, vr bsonrw.
433432
return nil
434433
}
435434

436-
// ObjectIDDecodeValue is the ValueDecoderFunc for objectid.ObjectID.
435+
// ObjectIDDecodeValue is the ValueDecoderFunc for primitive.ObjectID.
437436
func (dvd DefaultValueDecoders) ObjectIDDecodeValue(dc DecodeContext, vr bsonrw.ValueReader, i interface{}) error {
438437
if vr.Type() != bsontype.ObjectID {
439438
return fmt.Errorf("cannot decode %v into an ObjectID", vr.Type())
440439
}
441440

442-
target, ok := i.(*objectid.ObjectID)
441+
target, ok := i.(*primitive.ObjectID)
443442
if !ok || target == nil {
444-
return ValueDecoderError{Name: "ObjectIDDecodeValue", Types: []interface{}{(*objectid.ObjectID)(nil)}, Received: i}
443+
return ValueDecoderError{Name: "ObjectIDDecodeValue", Types: []interface{}{(*primitive.ObjectID)(nil)}, Received: i}
445444
}
446445

447446
oid, err := vr.ReadObjectID()
@@ -453,15 +452,15 @@ func (dvd DefaultValueDecoders) ObjectIDDecodeValue(dc DecodeContext, vr bsonrw.
453452
return nil
454453
}
455454

456-
// Decimal128DecodeValue is the ValueDecoderFunc for decimal.Decimal128.
455+
// Decimal128DecodeValue is the ValueDecoderFunc for primitive.Decimal128.
457456
func (dvd DefaultValueDecoders) Decimal128DecodeValue(dctx DecodeContext, vr bsonrw.ValueReader, i interface{}) error {
458457
if vr.Type() != bsontype.Decimal128 {
459-
return fmt.Errorf("cannot decode %v into a decimal.Decimal128", vr.Type())
458+
return fmt.Errorf("cannot decode %v into a primitive.Decimal128", vr.Type())
460459
}
461460

462-
target, ok := i.(*decimal.Decimal128)
461+
target, ok := i.(*primitive.Decimal128)
463462
if !ok || target == nil {
464-
return ValueDecoderError{Name: "Decimal128DecodeValue", Types: []interface{}{(*decimal.Decimal128)(nil)}, Received: i}
463+
return ValueDecoderError{Name: "Decimal128DecodeValue", Types: []interface{}{(*primitive.Decimal128)(nil)}, Received: i}
465464
}
466465

467466
d128, err := vr.ReadDecimal128()
@@ -799,9 +798,9 @@ func (dvd DefaultValueDecoders) EmptyInterfaceDecodeValue(dc DecodeContext, vr b
799798
rtype = tInt64
800799
fn = func() { *target = *(val.(*int64)) }
801800
case bsontype.Decimal128:
802-
val = new(decimal.Decimal128)
801+
val = new(primitive.Decimal128)
803802
rtype = tDecimal
804-
fn = func() { *target = *(val.(*decimal.Decimal128)) }
803+
fn = func() { *target = *(val.(*primitive.Decimal128)) }
805804
default:
806805
return fmt.Errorf("Type %s is not a valid BSON type and has no default Go type to decode into", vr.Type())
807806
}

bson/bsoncodec/default_value_decoders_test.go

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ import (
2121
"github.com/mongodb/mongo-go-driver/bson/bsonrw"
2222
"github.com/mongodb/mongo-go-driver/bson/bsonrw/bsonrwtest"
2323
"github.com/mongodb/mongo-go-driver/bson/bsontype"
24-
"github.com/mongodb/mongo-go-driver/bson/decimal"
25-
"github.com/mongodb/mongo-go-driver/bson/objectid"
24+
"github.com/mongodb/mongo-go-driver/bson/primitive"
2625
"github.com/mongodb/mongo-go-driver/x/bsonx/bsoncore"
2726
)
2827

@@ -50,7 +49,7 @@ func TestDefaultValueDecoders(t *testing.T) {
5049
intAllowedDecodeTypes := []interface{}{(*int8)(nil), (*int16)(nil), (*int32)(nil), (*int64)(nil), (*int)(nil)}
5150
uintAllowedDecodeTypes := []interface{}{(*uint8)(nil), (*uint16)(nil), (*uint32)(nil), (*uint64)(nil), (*uint)(nil)}
5251
now := time.Now().Truncate(time.Millisecond)
53-
d128 := decimal.NewDecimal128(12345, 67890)
52+
d128 := primitive.NewDecimal128(12345, 67890)
5453
var ptrPtrValueUnmarshaler **testValueUnmarshaler
5554
var ptrPtrUnmarshaler **testUnmarshaler
5655

@@ -809,31 +808,31 @@ func TestDefaultValueDecoders(t *testing.T) {
809808
nil,
810809
&bsonrwtest.ValueReaderWriter{BSONType: bsontype.ObjectID},
811810
bsonrwtest.Nothing,
812-
ValueDecoderError{Name: "ObjectIDDecodeValue", Types: []interface{}{(*objectid.ObjectID)(nil)}, Received: &wrong},
811+
ValueDecoderError{Name: "ObjectIDDecodeValue", Types: []interface{}{(*primitive.ObjectID)(nil)}, Received: &wrong},
813812
},
814813
{
815814
"type not objectID",
816-
objectid.ObjectID{},
815+
primitive.ObjectID{},
817816
nil,
818817
&bsonrwtest.ValueReaderWriter{BSONType: bsontype.String},
819818
bsonrwtest.Nothing,
820819
fmt.Errorf("cannot decode %v into an ObjectID", bsontype.String),
821820
},
822821
{
823822
"ReadObjectID Error",
824-
objectid.ObjectID{},
823+
primitive.ObjectID{},
825824
nil,
826825
&bsonrwtest.ValueReaderWriter{BSONType: bsontype.ObjectID, Err: errors.New("roid error"), ErrAfter: bsonrwtest.ReadObjectID},
827826
bsonrwtest.ReadObjectID,
828827
errors.New("roid error"),
829828
},
830829
{
831830
"success",
832-
objectid.ObjectID{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C},
831+
primitive.ObjectID{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C},
833832
nil,
834833
&bsonrwtest.ValueReaderWriter{
835834
BSONType: bsontype.ObjectID,
836-
Return: objectid.ObjectID{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C},
835+
Return: primitive.ObjectID{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C},
837836
},
838837
bsonrwtest.ReadObjectID,
839838
nil,
@@ -850,19 +849,19 @@ func TestDefaultValueDecoders(t *testing.T) {
850849
nil,
851850
&bsonrwtest.ValueReaderWriter{BSONType: bsontype.Decimal128},
852851
bsonrwtest.Nothing,
853-
ValueDecoderError{Name: "Decimal128DecodeValue", Types: []interface{}{(*decimal.Decimal128)(nil)}, Received: &wrong},
852+
ValueDecoderError{Name: "Decimal128DecodeValue", Types: []interface{}{(*primitive.Decimal128)(nil)}, Received: &wrong},
854853
},
855854
{
856855
"type not decimal128",
857-
decimal.Decimal128{},
856+
primitive.Decimal128{},
858857
nil,
859858
&bsonrwtest.ValueReaderWriter{BSONType: bsontype.String},
860859
bsonrwtest.Nothing,
861-
fmt.Errorf("cannot decode %v into a decimal.Decimal128", bsontype.String),
860+
fmt.Errorf("cannot decode %v into a primitive.Decimal128", bsontype.String),
862861
},
863862
{
864863
"ReadDecimal128 Error",
865-
decimal.Decimal128{},
864+
primitive.Decimal128{},
866865
nil,
867866
&bsonrwtest.ValueReaderWriter{BSONType: bsontype.Decimal128, Err: errors.New("rd128 error"), ErrAfter: bsonrwtest.ReadDecimal128},
868867
bsonrwtest.ReadDecimal128,
@@ -1293,8 +1292,8 @@ func TestDefaultValueDecoders(t *testing.T) {
12931292
})
12941293

12951294
t.Run("success path", func(t *testing.T) {
1296-
oid := objectid.New()
1297-
oids := []objectid.ObjectID{objectid.New(), objectid.New(), objectid.New()}
1295+
oid := primitive.NewObjectID()
1296+
oids := []primitive.ObjectID{primitive.NewObjectID(), primitive.NewObjectID(), primitive.NewObjectID()}
12981297
var str = new(string)
12991298
*str = "bar"
13001299
now := time.Now().Truncate(time.Millisecond)
@@ -1303,7 +1302,7 @@ func TestDefaultValueDecoders(t *testing.T) {
13031302
t.Errorf("Error parsing URL: %v", err)
13041303
t.FailNow()
13051304
}
1306-
decimal128, err := decimal.ParseDecimal128("1.5e10")
1305+
decimal128, err := primitive.ParseDecimal128("1.5e10")
13071306
if err != nil {
13081307
t.Errorf("Error parsing decimal128: %v", err)
13091308
t.FailNow()
@@ -1327,8 +1326,8 @@ func TestDefaultValueDecoders(t *testing.T) {
13271326
nil,
13281327
},
13291328
{
1330-
"map[string]objectid.ObjectID",
1331-
map[string]objectid.ObjectID{"foo": oid},
1329+
"map[string]primitive.ObjectID",
1330+
map[string]primitive.ObjectID{"foo": oid},
13321331
func() []byte {
13331332
idx, doc := bsoncore.AppendDocumentStart(nil)
13341333
doc = bsoncore.AppendObjectIDElement(doc, "foo", oid)
@@ -1348,8 +1347,8 @@ func TestDefaultValueDecoders(t *testing.T) {
13481347
nil,
13491348
},
13501349
{
1351-
"map[string][]objectid.ObjectID",
1352-
map[string][]objectid.ObjectID{"Z": oids},
1350+
"map[string][]primitive.ObjectID",
1351+
map[string][]primitive.ObjectID{"Z": oids},
13531352
buildDocumentArray(func(doc []byte) []byte {
13541353
doc = bsoncore.AppendObjectIDElement(doc, "0", oids[0])
13551354
doc = bsoncore.AppendObjectIDElement(doc, "1", oids[1])
@@ -1384,8 +1383,8 @@ func TestDefaultValueDecoders(t *testing.T) {
13841383
nil,
13851384
},
13861385
{
1387-
"map[string][]decimal.Decimal128",
1388-
map[string][]decimal.Decimal128{"Z": {decimal128}},
1386+
"map[string][]primitive.Decimal128",
1387+
map[string][]primitive.Decimal128{"Z": {decimal128}},
13891388
buildDocumentArray(func(doc []byte) []byte {
13901389
return bsoncore.AppendDecimal128Element(doc, "0", decimal128)
13911390
}),
@@ -1511,13 +1510,13 @@ func TestDefaultValueDecoders(t *testing.T) {
15111510
L struct {
15121511
M string
15131512
}
1514-
Q objectid.ObjectID
1513+
Q primitive.ObjectID
15151514
T []struct{}
15161515
Y json.Number
15171516
Z time.Time
15181517
AA json.Number
15191518
AB *url.URL
1520-
AC decimal.Decimal128
1519+
AC primitive.Decimal128
15211520
AD *time.Time
15221521
AE *testValueUnmarshaler
15231522
}{
@@ -1590,15 +1589,15 @@ func TestDefaultValueDecoders(t *testing.T) {
15901589
M string
15911590
}
15921591
N [][]string
1593-
R []objectid.ObjectID
1592+
R []primitive.ObjectID
15941593
T []struct{}
15951594
W []map[string]struct{}
15961595
X []map[string]struct{}
15971596
Y []map[string]struct{}
15981597
Z []time.Time
15991598
AA []json.Number
16001599
AB []*url.URL
1601-
AC []decimal.Decimal128
1600+
AC []primitive.Decimal128
16021601
AD []*time.Time
16031602
AE []*testValueUnmarshaler
16041603
}{
@@ -1628,7 +1627,7 @@ func TestDefaultValueDecoders(t *testing.T) {
16281627
Z: []time.Time{now, now},
16291628
AA: []json.Number{json.Number("5"), json.Number("10.1")},
16301629
AB: []*url.URL{murl},
1631-
AC: []decimal.Decimal128{decimal128},
1630+
AC: []primitive.Decimal128{decimal128},
16321631
AD: []*time.Time{&now, &now},
16331632
AE: []*testValueUnmarshaler{
16341633
{t: bsontype.String, val: bsoncore.AppendString(nil, "hello")},

bson/bsoncodec/default_value_encoders.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ import (
1717

1818
"github.com/mongodb/mongo-go-driver/bson/bsonrw"
1919
"github.com/mongodb/mongo-go-driver/bson/bsontype"
20-
"github.com/mongodb/mongo-go-driver/bson/decimal"
21-
"github.com/mongodb/mongo-go-driver/bson/objectid"
20+
"github.com/mongodb/mongo-go-driver/bson/primitive"
2221
)
2322

2423
var defaultValueEncoders DefaultValueEncoders
@@ -208,43 +207,43 @@ func (dve DefaultValueEncoders) StringEncodeValue(ectx EncodeContext, vw bsonrw.
208207
return vw.WriteString(val.String())
209208
}
210209

211-
// ObjectIDEncodeValue is the ValueEncoderFunc for objectid.ObjectID.
210+
// ObjectIDEncodeValue is the ValueEncoderFunc for primitive.ObjectID.
212211
func (dve DefaultValueEncoders) ObjectIDEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, i interface{}) error {
213-
var oid objectid.ObjectID
212+
var oid primitive.ObjectID
214213
switch t := i.(type) {
215-
case objectid.ObjectID:
214+
case primitive.ObjectID:
216215
oid = t
217-
case *objectid.ObjectID:
216+
case *primitive.ObjectID:
218217
if t == nil {
219218
return vw.WriteNull()
220219
}
221220
oid = *t
222221
default:
223222
return ValueEncoderError{
224223
Name: "ObjectIDEncodeValue",
225-
Types: []interface{}{objectid.ObjectID{}, (*objectid.ObjectID)(nil)},
224+
Types: []interface{}{primitive.ObjectID{}, (*primitive.ObjectID)(nil)},
226225
Received: i,
227226
}
228227
}
229228

230229
return vw.WriteObjectID(oid)
231230
}
232231

233-
// Decimal128EncodeValue is the ValueEncoderFunc for decimal.Decimal128.
232+
// Decimal128EncodeValue is the ValueEncoderFunc for primitive.Decimal128.
234233
func (dve DefaultValueEncoders) Decimal128EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, i interface{}) error {
235-
var d128 decimal.Decimal128
234+
var d128 primitive.Decimal128
236235
switch t := i.(type) {
237-
case decimal.Decimal128:
236+
case primitive.Decimal128:
238237
d128 = t
239-
case *decimal.Decimal128:
238+
case *primitive.Decimal128:
240239
if t == nil {
241240
return vw.WriteNull()
242241
}
243242
d128 = *t
244243
default:
245244
return ValueEncoderError{
246245
Name: "Decimal128EncodeValue",
247-
Types: []interface{}{decimal.Decimal128{}, (*decimal.Decimal128)(nil)},
246+
Types: []interface{}{primitive.Decimal128{}, (*primitive.Decimal128)(nil)},
248247
Received: i,
249248
}
250249
}

0 commit comments

Comments
 (0)