Skip to content

Commit 5083299

Browse files
GODRIVER-1953 Remove bsonx from production code paths. (#893)
Co-authored-by: Luis Nieto <[email protected]>
1 parent 89bf72b commit 5083299

File tree

8 files changed

+43
-174
lines changed

8 files changed

+43
-174
lines changed

examples/documentation_examples/examples_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ import (
1717
"time"
1818

1919
"github.com/stretchr/testify/require"
20+
"go.mongodb.org/mongo-driver/bson"
2021
"go.mongodb.org/mongo-driver/examples/documentation_examples"
2122
"go.mongodb.org/mongo-driver/internal/testutil"
2223
"go.mongodb.org/mongo-driver/mongo"
2324
"go.mongodb.org/mongo-driver/mongo/description"
2425
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
2526
"go.mongodb.org/mongo-driver/mongo/options"
26-
"go.mongodb.org/mongo-driver/x/bsonx"
2727
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
2828
"go.mongodb.org/mongo-driver/x/mongo/driver/topology"
2929
)
@@ -174,7 +174,7 @@ func TestCausalConsistencyExamples(t *testing.T) {
174174
func getServerVersion(ctx context.Context, client *mongo.Client) (string, error) {
175175
serverStatus, err := client.Database("admin").RunCommand(
176176
ctx,
177-
bsonx.Doc{{"serverStatus", bsonx.Int32(1)}},
177+
bson.D{{"serverStatus", 1}},
178178
).DecodeBytes()
179179
if err != nil {
180180
return "", err

mongo/client_options_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313
"testing"
1414

1515
"github.com/stretchr/testify/require"
16+
"go.mongodb.org/mongo-driver/bson"
1617
"go.mongodb.org/mongo-driver/internal/testutil"
1718
"go.mongodb.org/mongo-driver/mongo/options"
18-
"go.mongodb.org/mongo-driver/x/bsonx"
1919
)
2020

2121
func TestClientOptions_CustomDialer(t *testing.T) {
@@ -27,7 +27,7 @@ func TestClientOptions_CustomDialer(t *testing.T) {
2727
require.NoError(t, err)
2828
err = client.Connect(context.Background())
2929
require.NoError(t, err)
30-
_, err = client.ListDatabases(context.Background(), bsonx.Doc{})
30+
_, err = client.ListDatabases(context.Background(), bson.D{})
3131
require.NoError(t, err)
3232
got := atomic.LoadInt32(&td.called)
3333
if got < 1 {

mongo/database.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"go.mongodb.org/mongo-driver/mongo/readconcern"
1919
"go.mongodb.org/mongo-driver/mongo/readpref"
2020
"go.mongodb.org/mongo-driver/mongo/writeconcern"
21-
"go.mongodb.org/mongo-driver/x/bsonx"
2221
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
2322
"go.mongodb.org/mongo-driver/x/mongo/driver"
2423
"go.mongodb.org/mongo-driver/x/mongo/driver/operation"
@@ -439,19 +438,13 @@ func (db *Database) ListCollectionNames(ctx context.Context, filter interface{},
439438

440439
names := make([]string, 0)
441440
for res.Next(ctx) {
442-
next := &bsonx.Doc{}
443-
err = res.Decode(next)
441+
elem, err := res.Current.LookupErr("name")
444442
if err != nil {
445443
return nil, err
446444
}
447445

448-
elem, err := next.LookupErr("name")
449-
if err != nil {
450-
return nil, err
451-
}
452-
453-
if elem.Type() != bson.TypeString {
454-
return nil, fmt.Errorf("incorrect type for 'name'. got %v. want %v", elem.Type(), bson.TypeString)
446+
if elem.Type != bson.TypeString {
447+
return nil, fmt.Errorf("incorrect type for 'name'. got %v. want %v", elem.Type, bson.TypeString)
455448
}
456449

457450
elemName := elem.StringValue()

mongo/gridfs/bucket.go

Lines changed: 16 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"go.mongodb.org/mongo-driver/mongo/readconcern"
2222
"go.mongodb.org/mongo-driver/mongo/readpref"
2323
"go.mongodb.org/mongo-driver/mongo/writeconcern"
24-
"go.mongodb.org/mongo-driver/x/bsonx"
2524
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
2625
)
2726

@@ -59,7 +58,7 @@ type Bucket struct {
5958
// Upload contains options to upload a file to a bucket.
6059
type Upload struct {
6160
chunkSize int32
62-
metadata bsonx.Doc
61+
metadata bson.D
6362
}
6463

6564
// NewBucket creates a GridFS bucket.
@@ -186,12 +185,8 @@ func (b *Bucket) UploadFromStreamWithID(fileID interface{}, filename string, sou
186185

187186
// OpenDownloadStream creates a stream from which the contents of the file can be read.
188187
func (b *Bucket) OpenDownloadStream(fileID interface{}) (*DownloadStream, error) {
189-
id, err := convertFileID(fileID)
190-
if err != nil {
191-
return nil, err
192-
}
193-
return b.openDownloadStream(bsonx.Doc{
194-
{"_id", id},
188+
return b.openDownloadStream(bson.D{
189+
{"_id", fileID},
195190
})
196191
}
197192

@@ -224,9 +219,9 @@ func (b *Bucket) OpenDownloadStreamByName(filename string, opts ...*options.Name
224219
numSkip = (-1 * numSkip) - 1
225220
}
226221

227-
findOpts := options.Find().SetSkip(int64(numSkip)).SetSort(bsonx.Doc{{"uploadDate", bsonx.Int32(sortOrder)}})
222+
findOpts := options.Find().SetSkip(int64(numSkip)).SetSort(bson.D{{"uploadDate", sortOrder}})
228223

229-
return b.openDownloadStream(bsonx.Doc{{"filename", bsonx.String(filename)}}, findOpts)
224+
return b.openDownloadStream(bson.D{{"filename", filename}}, findOpts)
230225
}
231226

232227
// DownloadToStreamByName downloads the file with the given name to the given io.Writer.
@@ -254,11 +249,7 @@ func (b *Bucket) Delete(fileID interface{}) error {
254249
defer cancel()
255250
}
256251

257-
id, err := convertFileID(fileID)
258-
if err != nil {
259-
return err
260-
}
261-
res, err := b.filesColl.DeleteOne(ctx, bsonx.Doc{{"_id", id}})
252+
res, err := b.filesColl.DeleteOne(ctx, bson.D{{"_id", fileID}})
262253
if err == nil && res.DeletedCount == 0 {
263254
err = ErrFileNotFound
264255
}
@@ -317,13 +308,9 @@ func (b *Bucket) Rename(fileID interface{}, newFilename string) error {
317308
defer cancel()
318309
}
319310

320-
id, err := convertFileID(fileID)
321-
if err != nil {
322-
return err
323-
}
324311
res, err := b.filesColl.UpdateOne(ctx,
325-
bsonx.Doc{{"_id", id}},
326-
bsonx.Doc{{"$set", bsonx.Document(bsonx.Doc{{"filename", bsonx.String(newFilename)}})}},
312+
bson.D{{"_id", fileID}},
313+
bson.D{{"$set", bson.D{{"filename", newFilename}}}},
327314
)
328315
if err != nil {
329316
return err
@@ -426,11 +413,7 @@ func (b *Bucket) downloadToStream(ds *DownloadStream, stream io.Writer) (int64,
426413
}
427414

428415
func (b *Bucket) deleteChunks(ctx context.Context, fileID interface{}) error {
429-
id, err := convertFileID(fileID)
430-
if err != nil {
431-
return err
432-
}
433-
_, err = b.chunksColl.DeleteMany(ctx, bsonx.Doc{{"files_id", id}})
416+
_, err := b.chunksColl.DeleteMany(ctx, bson.D{{"files_id", fileID}})
434417
return err
435418
}
436419

@@ -449,13 +432,9 @@ func (b *Bucket) findFile(ctx context.Context, filter interface{}, opts ...*opti
449432
}
450433

451434
func (b *Bucket) findChunks(ctx context.Context, fileID interface{}) (*mongo.Cursor, error) {
452-
id, err := convertFileID(fileID)
453-
if err != nil {
454-
return nil, err
455-
}
456435
chunksCursor, err := b.chunksColl.Find(ctx,
457-
bsonx.Doc{{"files_id", id}},
458-
options.Find().SetSort(bsonx.Doc{{"n", bsonx.Int32(1)}})) // sort by chunk index
436+
bson.D{{"files_id", fileID}},
437+
options.Find().SetSort(bson.D{{"n", 1}})) // sort by chunk index
459438
if err != nil {
460439
return nil, err
461440
}
@@ -550,7 +529,7 @@ func (b *Bucket) createIndexes(ctx context.Context) error {
550529
return err
551530
}
552531

553-
docRes := cloned.FindOne(ctx, bsonx.Doc{}, options.FindOne().SetProjection(bsonx.Doc{{"_id", bsonx.Int32(1)}}))
532+
docRes := cloned.FindOne(ctx, bson.D{}, options.FindOne().SetProjection(bson.D{{"_id", 1}}))
554533

555534
_, err = docRes.DecodeBytes()
556535
if err != mongo.ErrNoDocuments {
@@ -617,31 +596,13 @@ func (b *Bucket) parseUploadOptions(opts ...*options.UploadOptions) (*Upload, er
617596
if err != nil {
618597
return nil, err
619598
}
620-
doc, err := bsonx.ReadDoc(raw)
621-
if err != nil {
622-
return nil, err
599+
var doc bson.D
600+
unMarErr := bson.UnmarshalWithRegistry(uo.Registry, raw, &doc)
601+
if unMarErr != nil {
602+
return nil, unMarErr
623603
}
624604
upload.metadata = doc
625605
}
626606

627607
return upload, nil
628608
}
629-
630-
type _convertFileID struct {
631-
ID interface{} `bson:"_id"`
632-
}
633-
634-
func convertFileID(fileID interface{}) (bsonx.Val, error) {
635-
id := _convertFileID{
636-
ID: fileID,
637-
}
638-
639-
b, err := bson.Marshal(id)
640-
if err != nil {
641-
return bsonx.Val{}, err
642-
}
643-
val := bsoncore.Document(b).Lookup("_id")
644-
var res bsonx.Val
645-
err = res.UnmarshalBSONValue(val.Type, val.Data)
646-
return res, err
647-
}

mongo/gridfs/upload_stream.go

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import (
1414

1515
"math"
1616

17+
"go.mongodb.org/mongo-driver/bson"
1718
"go.mongodb.org/mongo-driver/bson/primitive"
1819
"go.mongodb.org/mongo-driver/mongo"
19-
"go.mongodb.org/mongo-driver/x/bsonx"
2020
)
2121

2222
// UploadBufferSize is the size in bytes of one stream batch. Chunks will be written to the db after the sum of chunk
@@ -137,11 +137,7 @@ func (us *UploadStream) Abort() error {
137137
defer cancel()
138138
}
139139

140-
id, err := convertFileID(us.FileID)
141-
if err != nil {
142-
return err
143-
}
144-
_, err = us.chunksColl.DeleteMany(ctx, bsonx.Doc{{"files_id", id}})
140+
_, err := us.chunksColl.DeleteMany(ctx, bson.D{{"files_id", us.FileID}})
145141
if err != nil {
146142
return err
147143
}
@@ -163,10 +159,6 @@ func (us *UploadStream) uploadChunks(ctx context.Context, uploadPartial bool) er
163159

164160
docs := make([]interface{}, numChunks)
165161

166-
id, err := convertFileID(us.FileID)
167-
if err != nil {
168-
return err
169-
}
170162
begChunkIndex := us.chunkIndex
171163
for i := 0; i < us.bufferIndex; i += int(us.chunkSize) {
172164
endIndex := i + int(us.chunkSize)
@@ -178,17 +170,17 @@ func (us *UploadStream) uploadChunks(ctx context.Context, uploadPartial bool) er
178170
endIndex = us.bufferIndex
179171
}
180172
chunkData := us.buffer[i:endIndex]
181-
docs[us.chunkIndex-begChunkIndex] = bsonx.Doc{
182-
{"_id", bsonx.ObjectID(primitive.NewObjectID())},
183-
{"files_id", id},
184-
{"n", bsonx.Int32(int32(us.chunkIndex))},
185-
{"data", bsonx.Binary(0x00, chunkData)},
173+
docs[us.chunkIndex-begChunkIndex] = bson.D{
174+
{"_id", primitive.NewObjectID()},
175+
{"files_id", us.FileID},
176+
{"n", int32(us.chunkIndex)},
177+
{"data", primitive.Binary{Subtype: 0x00, Data: chunkData}},
186178
}
187179
us.chunkIndex++
188180
us.fileLen += int64(len(chunkData))
189181
}
190182

191-
_, err = us.chunksColl.InsertMany(ctx, docs)
183+
_, err := us.chunksColl.InsertMany(ctx, docs)
192184
if err != nil {
193185
return err
194186
}
@@ -203,23 +195,19 @@ func (us *UploadStream) uploadChunks(ctx context.Context, uploadPartial bool) er
203195
}
204196

205197
func (us *UploadStream) createFilesCollDoc(ctx context.Context) error {
206-
id, err := convertFileID(us.FileID)
207-
if err != nil {
208-
return err
209-
}
210-
doc := bsonx.Doc{
211-
{"_id", id},
212-
{"length", bsonx.Int64(us.fileLen)},
213-
{"chunkSize", bsonx.Int32(us.chunkSize)},
214-
{"uploadDate", bsonx.DateTime(time.Now().UnixNano() / int64(time.Millisecond))},
215-
{"filename", bsonx.String(us.filename)},
198+
doc := bson.D{
199+
{"_id", us.FileID},
200+
{"length", us.fileLen},
201+
{"chunkSize", us.chunkSize},
202+
{"uploadDate", primitive.DateTime(time.Now().UnixNano() / int64(time.Millisecond))},
203+
{"filename", us.filename},
216204
}
217205

218206
if us.metadata != nil {
219-
doc = append(doc, bsonx.Elem{"metadata", bsonx.Document(us.metadata)})
207+
doc = append(doc, bson.E{"metadata", us.metadata})
220208
}
221209

222-
_, err = us.filesColl.InsertOne(ctx, doc)
210+
_, err := us.filesColl.InsertOne(ctx, doc)
223211
if err != nil {
224212
return err
225213
}

mongo/main_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import (
1212
"testing"
1313

1414
"github.com/google/go-cmp/cmp"
15+
"go.mongodb.org/mongo-driver/bson"
1516
"go.mongodb.org/mongo-driver/bson/bsoncodec"
1617
"go.mongodb.org/mongo-driver/internal/testutil/assert"
1718
"go.mongodb.org/mongo-driver/mongo/readconcern"
1819
"go.mongodb.org/mongo-driver/mongo/readpref"
1920
"go.mongodb.org/mongo-driver/mongo/writeconcern"
20-
"go.mongodb.org/mongo-driver/x/bsonx"
21+
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
2122
)
2223

2324
func TestMain(m *testing.M) {
@@ -32,8 +33,8 @@ func TestMain(m *testing.M) {
3233
assert.RegisterOpts(reflect.TypeOf(&readconcern.ReadConcern{}), cmp.AllowUnexported(readconcern.ReadConcern{}))
3334
assert.RegisterOpts(reflect.TypeOf(&writeconcern.WriteConcern{}), cmp.AllowUnexported(writeconcern.WriteConcern{}))
3435
assert.RegisterOpts(reflect.TypeOf(&readpref.ReadPref{}), cmp.AllowUnexported(readpref.ReadPref{}))
35-
assert.RegisterOpts(reflect.TypeOf(bsonx.Doc{}), cmp.AllowUnexported(bsonx.Elem{}, bsonx.Val{}))
36-
assert.RegisterOpts(reflect.TypeOf(bsonx.Arr{}), cmp.AllowUnexported(bsonx.Val{}))
36+
assert.RegisterOpts(reflect.TypeOf(bson.D{}), cmp.AllowUnexported(bson.E{}, bsoncore.Value{}))
37+
assert.RegisterOpts(reflect.TypeOf(bson.A{}), cmp.AllowUnexported(bsoncore.Value{}))
3738

3839
os.Exit(m.Run())
3940
}

mongo/mongo.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,6 @@ func transformAndEnsureID(registry *bsoncodec.Registry, val interface{}) (bsonco
123123
return doc, id, nil
124124
}
125125

126-
func transformDocument(registry *bsoncodec.Registry, val interface{}) (bsonx.Doc, error) {
127-
if doc, ok := val.(bsonx.Doc); ok {
128-
return doc.Copy(), nil
129-
}
130-
b, err := transformBsoncoreDocument(registry, val, true, "document")
131-
if err != nil {
132-
return nil, err
133-
}
134-
return bsonx.ReadDoc(b)
135-
}
136-
137126
func transformBsoncoreDocument(registry *bsoncodec.Registry, val interface{}, mapAllowed bool, paramName string) (bsoncore.Document, error) {
138127
if registry == nil {
139128
registry = bson.DefaultRegistry

0 commit comments

Comments
 (0)