Skip to content

GODRIVER-1953 Refactor to (almost) not use bsonx package #692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/documentation_examples/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import (
"time"

"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/examples/documentation_examples"
"go.mongodb.org/mongo-driver/internal/testutil"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/description"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx"
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
"go.mongodb.org/mongo-driver/x/mongo/driver/topology"
)
Expand Down Expand Up @@ -103,7 +103,7 @@ func TestChangeStreamExamples(t *testing.T) {
func getServerVersion(ctx context.Context, client *mongo.Client) (string, error) {
serverStatus, err := client.Database("admin").RunCommand(
ctx,
bsonx.Doc{{"serverStatus", bsonx.Int32(1)}},
bson.D{{"serverStatus", 1}},
).DecodeBytes()
if err != nil {
return "", err
Expand Down
4 changes: 2 additions & 2 deletions mongo/client_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
"testing"

"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/internal/testutil"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx"
)

func TestClientOptions_CustomDialer(t *testing.T) {
Expand All @@ -27,7 +27,7 @@ func TestClientOptions_CustomDialer(t *testing.T) {
require.NoError(t, err)
err = client.Connect(context.Background())
require.NoError(t, err)
_, err = client.ListDatabases(context.Background(), bsonx.Doc{})
_, err = client.ListDatabases(context.Background(), bson.D{})
require.NoError(t, err)
got := atomic.LoadInt32(&td.called)
if got < 1 {
Expand Down
7 changes: 3 additions & 4 deletions mongo/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"go.mongodb.org/mongo-driver/mongo/readconcern"
"go.mongodb.org/mongo-driver/mongo/readpref"
"go.mongodb.org/mongo-driver/mongo/writeconcern"
"go.mongodb.org/mongo-driver/x/bsonx"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
"go.mongodb.org/mongo-driver/x/mongo/driver"
"go.mongodb.org/mongo-driver/x/mongo/driver/operation"
Expand Down Expand Up @@ -427,7 +426,7 @@ func (db *Database) ListCollectionNames(ctx context.Context, filter interface{},

names := make([]string, 0)
for res.Next(ctx) {
next := &bsonx.Doc{}
next := &bsoncore.Document{}
err = res.Decode(next)
if err != nil {
return nil, err
Expand All @@ -438,8 +437,8 @@ func (db *Database) ListCollectionNames(ctx context.Context, filter interface{},
return nil, err
}

if elem.Type() != bson.TypeString {
return nil, fmt.Errorf("incorrect type for 'name'. got %v. want %v", elem.Type(), bson.TypeString)
if elem.Type != bson.TypeString {
return nil, fmt.Errorf("incorrect type for 'name'. got %v. want %v", elem.Type, bson.TypeString)
}

elemName := elem.StringValue()
Expand Down
31 changes: 16 additions & 15 deletions mongo/gridfs/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type Bucket struct {
// Upload contains options to upload a file to a bucket.
type Upload struct {
chunkSize int32
metadata bsonx.Doc
metadata bson.D
}

// NewBucket creates a GridFS bucket.
Expand Down Expand Up @@ -190,7 +190,7 @@ func (b *Bucket) OpenDownloadStream(fileID interface{}) (*DownloadStream, error)
if err != nil {
return nil, err
}
return b.openDownloadStream(bsonx.Doc{
return b.openDownloadStream(bson.D{
{"_id", id},
})
}
Expand Down Expand Up @@ -224,9 +224,9 @@ func (b *Bucket) OpenDownloadStreamByName(filename string, opts ...*options.Name
numSkip = (-1 * numSkip) - 1
}

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

return b.openDownloadStream(bsonx.Doc{{"filename", bsonx.String(filename)}}, findOpts)
return b.openDownloadStream(bson.D{{"filename", filename}}, findOpts)
}

// DownloadToStreamByName downloads the file with the given name to the given io.Writer.
Expand Down Expand Up @@ -258,7 +258,7 @@ func (b *Bucket) Delete(fileID interface{}) error {
if err != nil {
return err
}
res, err := b.filesColl.DeleteOne(ctx, bsonx.Doc{{"_id", id}})
res, err := b.filesColl.DeleteOne(ctx, bson.D{{"_id", id}})
if err == nil && res.DeletedCount == 0 {
err = ErrFileNotFound
}
Expand Down Expand Up @@ -322,8 +322,8 @@ func (b *Bucket) Rename(fileID interface{}, newFilename string) error {
return err
}
res, err := b.filesColl.UpdateOne(ctx,
bsonx.Doc{{"_id", id}},
bsonx.Doc{{"$set", bsonx.Document(bsonx.Doc{{"filename", bsonx.String(newFilename)}})}},
bson.D{{"_id", id}},
bson.D{{"$set", bson.D{{"filename", newFilename}}}},
)
if err != nil {
return err
Expand Down Expand Up @@ -430,7 +430,7 @@ func (b *Bucket) deleteChunks(ctx context.Context, fileID interface{}) error {
if err != nil {
return err
}
_, err = b.chunksColl.DeleteMany(ctx, bsonx.Doc{{"files_id", id}})
_, err = b.chunksColl.DeleteMany(ctx, bson.D{{"files_id", id}})
return err
}

Expand All @@ -454,8 +454,8 @@ func (b *Bucket) findChunks(ctx context.Context, fileID interface{}) (*mongo.Cur
return nil, err
}
chunksCursor, err := b.chunksColl.Find(ctx,
bsonx.Doc{{"files_id", id}},
options.Find().SetSort(bsonx.Doc{{"n", bsonx.Int32(1)}})) // sort by chunk index
bson.D{{"files_id", id}},
options.Find().SetSort(bson.D{{"n", 1}})) // sort by chunk index
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -550,7 +550,7 @@ func (b *Bucket) createIndexes(ctx context.Context) error {
return err
}

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

_, err = docRes.DecodeBytes()
if err != mongo.ErrNoDocuments {
Expand Down Expand Up @@ -617,11 +617,12 @@ func (b *Bucket) parseUploadOptions(opts ...*options.UploadOptions) (*Upload, er
if err != nil {
return nil, err
}
doc, err := bsonx.ReadDoc(raw)
if err != nil {
return nil, err
doc := &bson.D{}
unMarErr := bson.UnmarshalWithRegistry(uo.Registry, raw, doc)
if unMarErr != nil {
return nil, unMarErr
}
upload.metadata = doc
upload.metadata = *doc
}

return upload, nil
Expand Down
20 changes: 11 additions & 9 deletions mongo/gridfs/upload_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"math"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
Expand Down Expand Up @@ -141,7 +142,7 @@ func (us *UploadStream) Abort() error {
if err != nil {
return err
}
_, err = us.chunksColl.DeleteMany(ctx, bsonx.Doc{{"files_id", id}})
_, err = us.chunksColl.DeleteMany(ctx, bson.D{{"files_id", id}})
if err != nil {
return err
}
Expand Down Expand Up @@ -178,11 +179,12 @@ func (us *UploadStream) uploadChunks(ctx context.Context, uploadPartial bool) er
endIndex = us.bufferIndex
}
chunkData := us.buffer[i:endIndex]
docs[us.chunkIndex-begChunkIndex] = bsonx.Doc{
{"_id", bsonx.ObjectID(primitive.NewObjectID())},
docs[us.chunkIndex-begChunkIndex] = bson.D{
{"_id", primitive.NewObjectID()},
{"files_id", id},
{"n", bsonx.Int32(int32(us.chunkIndex))},
{"n", int32(us.chunkIndex)},
{"data", bsonx.Binary(0x00, chunkData)},
// {"data", bsoncore.BuildDocumentValue(chunkData)},
}
us.chunkIndex++
us.fileLen += int64(len(chunkData))
Expand All @@ -207,16 +209,16 @@ func (us *UploadStream) createFilesCollDoc(ctx context.Context) error {
if err != nil {
return err
}
doc := bsonx.Doc{
doc := bson.D{
{"_id", id},
{"length", bsonx.Int64(us.fileLen)},
{"chunkSize", bsonx.Int32(us.chunkSize)},
{"length", us.fileLen},
{"chunkSize", us.chunkSize},
{"uploadDate", bsonx.DateTime(time.Now().UnixNano() / int64(time.Millisecond))},
{"filename", bsonx.String(us.filename)},
{"filename", us.filename},
}

if us.metadata != nil {
doc = append(doc, bsonx.Elem{"metadata", bsonx.Document(us.metadata)})
doc = append(doc, bson.E{"metadata", us.metadata})
}

_, err = us.filesColl.InsertOne(ctx, doc)
Expand Down
7 changes: 4 additions & 3 deletions mongo/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsoncodec"
"go.mongodb.org/mongo-driver/internal/testutil/assert"
"go.mongodb.org/mongo-driver/mongo/readconcern"
"go.mongodb.org/mongo-driver/mongo/readpref"
"go.mongodb.org/mongo-driver/mongo/writeconcern"
"go.mongodb.org/mongo-driver/x/bsonx"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)

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

os.Exit(m.Run())
}
53 changes: 25 additions & 28 deletions mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"strings"

"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"

"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -81,8 +80,6 @@ func transformAndEnsureID(registry *bsoncodec.Registry, val interface{}) (bsonco
switch tt := val.(type) {
case nil:
return nil, nil, ErrNilDocument
case bsonx.Doc:
val = tt.Copy()
case []byte:
// Slight optimization so we'll just use MarshalBSON and not go through the codec machinery.
val = bson.Raw(tt)
Expand Down Expand Up @@ -123,16 +120,16 @@ func transformAndEnsureID(registry *bsoncodec.Registry, val interface{}) (bsonco
return doc, id, nil
}

func transformDocument(registry *bsoncodec.Registry, val interface{}) (bsonx.Doc, error) {
if doc, ok := val.(bsonx.Doc); ok {
return doc.Copy(), nil
}
b, err := transformBsoncoreDocument(registry, val, true, "document")
if err != nil {
return nil, err
}
return bsonx.ReadDoc(b)
}
// func transformDocument(registry *bsoncodec.Registry, val interface{}) (bsoncore.Document, error) {
// // if doc, ok := val.(bsonx.Doc); ok {
// // return doc.Copy(), nil
// // }
// b, err := transformBsoncoreDocument(registry, val, true, "document")
// if err != nil {
// return nil, err
// }
// return b, nil
// }

func transformBsoncoreDocument(registry *bsoncodec.Registry, val interface{}, mapAllowed bool, paramName string) (bsoncore.Document, error) {
if registry == nil {
Expand Down Expand Up @@ -161,20 +158,20 @@ func transformBsoncoreDocument(registry *bsoncodec.Registry, val interface{}, ma
return b, nil
}

func ensureID(d bsonx.Doc) (bsonx.Doc, interface{}) {
var id interface{}

elem, err := d.LookupElementErr("_id")
switch err.(type) {
case nil:
id = elem
default:
oid := primitive.NewObjectID()
d = append(d, bsonx.Elem{"_id", bsonx.ObjectID(oid)})
id = oid
}
return d, id
}
// func ensureID(d bsonx.Doc) (bsonx.Doc, interface{}) {
// var id interface{}

// elem, err := d.LookupElementErr("_id")
// switch err.(type) {
// case nil:
// id = elem
// default:
// oid := primitive.NewObjectID()
// d = append(d, bsonx.Elem{"_id", bsonx.ObjectID(oid)})
// id = oid
// }
// return d, id
// }

func ensureDollarKey(doc bsoncore.Document) error {
firstElem, err := doc.IndexErr(0)
Expand Down Expand Up @@ -257,7 +254,7 @@ func transformUpdateValue(registry *bsoncodec.Registry, update interface{}, doll
switch t := update.(type) {
case nil:
return u, ErrNilDocument
case primitive.D, bsonx.Doc:
case primitive.D:
u.Type = bsontype.EmbeddedDocument
u.Data, err = transformBsoncoreDocument(registry, update, true, "update")
if err != nil {
Expand Down
Loading