Skip to content

Commit 8fa14fd

Browse files
committed
Remove remaining bsonx usage in production code; don't drop handling of bsonx.Doc
1 parent 3e03df1 commit 8fa14fd

File tree

5 files changed

+17
-99
lines changed

5 files changed

+17
-99
lines changed

mongo/database.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -438,13 +438,7 @@ func (db *Database) ListCollectionNames(ctx context.Context, filter interface{},
438438

439439
names := make([]string, 0)
440440
for res.Next(ctx) {
441-
next := &bsoncore.Document{}
442-
err = res.Decode(next)
443-
if err != nil {
444-
return nil, err
445-
}
446-
447-
elem, err := next.LookupErr("name")
441+
elem, err := res.Current.LookupErr("name")
448442
if err != nil {
449443
return nil, err
450444
}

mongo/gridfs/bucket.go

Lines changed: 5 additions & 45 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

@@ -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-
}
193188
return b.openDownloadStream(bson.D{
194-
{"_id", id},
189+
{"_id", fileID},
195190
})
196191
}
197192

@@ -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, bson.D{{"_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,12 +308,8 @@ 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-
bson.D{{"_id", id}},
312+
bson.D{{"_id", fileID}},
326313
bson.D{{"$set", bson.D{{"filename", newFilename}}}},
327314
)
328315
if err != nil {
@@ -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, bson.D{{"files_id", id}})
416+
_, err := b.chunksColl.DeleteMany(ctx, bson.D{{"files_id", fileID}})
434417
return err
435418
}
436419

@@ -449,12 +432,8 @@ 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-
bson.D{{"files_id", id}},
436+
bson.D{{"files_id", fileID}},
458437
options.Find().SetSort(bson.D{{"n", 1}})) // sort by chunk index
459438
if err != nil {
460439
return nil, err
@@ -627,22 +606,3 @@ func (b *Bucket) parseUploadOptions(opts ...*options.UploadOptions) (*Upload, er
627606

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

mongo/gridfs/upload_stream.go

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"go.mongodb.org/mongo-driver/bson"
1818
"go.mongodb.org/mongo-driver/bson/primitive"
1919
"go.mongodb.org/mongo-driver/mongo"
20-
"go.mongodb.org/mongo-driver/x/bsonx"
2120
)
2221

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

141-
id, err := convertFileID(us.FileID)
142-
if err != nil {
143-
return err
144-
}
145-
_, err = us.chunksColl.DeleteMany(ctx, bson.D{{"files_id", id}})
140+
_, err := us.chunksColl.DeleteMany(ctx, bson.D{{"files_id", us.FileID}})
146141
if err != nil {
147142
return err
148143
}
@@ -164,10 +159,6 @@ func (us *UploadStream) uploadChunks(ctx context.Context, uploadPartial bool) er
164159

165160
docs := make([]interface{}, numChunks)
166161

167-
id, err := convertFileID(us.FileID)
168-
if err != nil {
169-
return err
170-
}
171162
begChunkIndex := us.chunkIndex
172163
for i := 0; i < us.bufferIndex; i += int(us.chunkSize) {
173164
endIndex := i + int(us.chunkSize)
@@ -181,16 +172,15 @@ func (us *UploadStream) uploadChunks(ctx context.Context, uploadPartial bool) er
181172
chunkData := us.buffer[i:endIndex]
182173
docs[us.chunkIndex-begChunkIndex] = bson.D{
183174
{"_id", primitive.NewObjectID()},
184-
{"files_id", id},
175+
{"files_id", us.FileID},
185176
{"n", int32(us.chunkIndex)},
186-
{"data", bsonx.Binary(0x00, chunkData)},
187-
// {"data", bsoncore.BuildDocumentValue(chunkData)},
177+
{"data", primitive.Binary{Subtype: 0x00, Data: chunkData}},
188178
}
189179
us.chunkIndex++
190180
us.fileLen += int64(len(chunkData))
191181
}
192182

193-
_, err = us.chunksColl.InsertMany(ctx, docs)
183+
_, err := us.chunksColl.InsertMany(ctx, docs)
194184
if err != nil {
195185
return err
196186
}
@@ -205,23 +195,19 @@ func (us *UploadStream) uploadChunks(ctx context.Context, uploadPartial bool) er
205195
}
206196

207197
func (us *UploadStream) createFilesCollDoc(ctx context.Context) error {
208-
id, err := convertFileID(us.FileID)
209-
if err != nil {
210-
return err
211-
}
212198
doc := bson.D{
213-
{"_id", id},
199+
{"_id", us.FileID},
214200
{"length", us.fileLen},
215201
{"chunkSize", us.chunkSize},
216-
{"uploadDate", bsonx.DateTime(time.Now().UnixNano() / int64(time.Millisecond))},
202+
{"uploadDate", primitive.DateTime(time.Now().UnixNano() / int64(time.Millisecond))},
217203
{"filename", us.filename},
218204
}
219205

220206
if us.metadata != nil {
221207
doc = append(doc, bson.E{"metadata", us.metadata})
222208
}
223209

224-
_, err = us.filesColl.InsertOne(ctx, doc)
210+
_, err := us.filesColl.InsertOne(ctx, doc)
225211
if err != nil {
226212
return err
227213
}

mongo/mongo.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"strings"
1717

1818
"go.mongodb.org/mongo-driver/mongo/options"
19+
"go.mongodb.org/mongo-driver/x/bsonx"
1920
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
2021

2122
"go.mongodb.org/mongo-driver/bson"
@@ -80,6 +81,8 @@ func transformAndEnsureID(registry *bsoncodec.Registry, val interface{}) (bsonco
8081
switch tt := val.(type) {
8182
case nil:
8283
return nil, nil, ErrNilDocument
84+
case bsonx.Doc:
85+
val = tt.Copy()
8386
case []byte:
8487
// Slight optimization so we'll just use MarshalBSON and not go through the codec machinery.
8588
val = bson.Raw(tt)
@@ -120,17 +123,6 @@ func transformAndEnsureID(registry *bsoncodec.Registry, val interface{}) (bsonco
120123
return doc, id, nil
121124
}
122125

123-
// func transformDocument(registry *bsoncodec.Registry, val interface{}) (bsoncore.Document, error) {
124-
// // if doc, ok := val.(bsonx.Doc); ok {
125-
// // return doc.Copy(), nil
126-
// // }
127-
// b, err := transformBsoncoreDocument(registry, val, true, "document")
128-
// if err != nil {
129-
// return nil, err
130-
// }
131-
// return b, nil
132-
// }
133-
134126
func transformBsoncoreDocument(registry *bsoncodec.Registry, val interface{}, mapAllowed bool, paramName string) (bsoncore.Document, error) {
135127
if registry == nil {
136128
registry = bson.DefaultRegistry
@@ -273,7 +265,7 @@ func transformUpdateValue(registry *bsoncodec.Registry, update interface{}, doll
273265
switch t := update.(type) {
274266
case nil:
275267
return u, ErrNilDocument
276-
case primitive.D:
268+
case primitive.D, bsonx.Doc:
277269
u.Type = bsontype.EmbeddedDocument
278270
u.Data, err = transformBsoncoreDocument(registry, update, true, "update")
279271
if err != nil {

mongo/mongo_test.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -459,20 +459,6 @@ func TestMongoHelpers(t *testing.T) {
459459
})
460460
}
461461

462-
var _ bson.Marshaler = bMarsh{}
463-
464-
type bMarsh struct {
465-
bson.D
466-
}
467-
468-
func (b bMarsh) MarshalBSON() ([]byte, error) {
469-
return bson.Marshal(b)
470-
}
471-
472-
type reflectStruct struct {
473-
Foo string
474-
}
475-
476462
var _ bsoncodec.ValueMarshaler = bvMarsh{}
477463

478464
type bvMarsh struct {

0 commit comments

Comments
 (0)