diff --git a/examples/documentation_examples/examples_test.go b/examples/documentation_examples/examples_test.go index 0b962a714f..e07db65203 100644 --- a/examples/documentation_examples/examples_test.go +++ b/examples/documentation_examples/examples_test.go @@ -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" ) @@ -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 diff --git a/mongo/client_options_test.go b/mongo/client_options_test.go index 1fc4ef1102..6a85d4e78c 100644 --- a/mongo/client_options_test.go +++ b/mongo/client_options_test.go @@ -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) { @@ -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 { diff --git a/mongo/database.go b/mongo/database.go index 2078733443..3f8d00eb13 100644 --- a/mongo/database.go +++ b/mongo/database.go @@ -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" @@ -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 @@ -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() diff --git a/mongo/gridfs/bucket.go b/mongo/gridfs/bucket.go index 19b02264a5..bcb36d7cfc 100644 --- a/mongo/gridfs/bucket.go +++ b/mongo/gridfs/bucket.go @@ -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. @@ -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}, }) } @@ -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. @@ -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 } @@ -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 @@ -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 } @@ -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 } @@ -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 { @@ -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 diff --git a/mongo/gridfs/upload_stream.go b/mongo/gridfs/upload_stream.go index 93ea79aa7f..79809e43e3 100644 --- a/mongo/gridfs/upload_stream.go +++ b/mongo/gridfs/upload_stream.go @@ -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" @@ -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 } @@ -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)) @@ -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) diff --git a/mongo/main_test.go b/mongo/main_test.go index 38c6733526..c4ca209262 100644 --- a/mongo/main_test.go +++ b/mongo/main_test.go @@ -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) { @@ -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()) } diff --git a/mongo/mongo.go b/mongo/mongo.go index a0535877e0..802c050eea 100644 --- a/mongo/mongo.go +++ b/mongo/mongo.go @@ -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" @@ -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) @@ -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 { @@ -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) @@ -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 { diff --git a/mongo/mongo_test.go b/mongo/mongo_test.go index 8aa5a2c39d..7589ef76ac 100644 --- a/mongo/mongo_test.go +++ b/mongo/mongo_test.go @@ -16,59 +16,10 @@ import ( "go.mongodb.org/mongo-driver/bson/bsontype" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/internal/testutil/assert" - "go.mongodb.org/mongo-driver/x/bsonx" "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" ) func TestMongoHelpers(t *testing.T) { - t.Run("transform document", func(t *testing.T) { - testCases := []struct { - name string - document interface{} - want bsonx.Doc - err error - }{ - { - "bson.Marshaler", - bMarsh{bsonx.Doc{{"foo", bsonx.String("bar")}}}, - bsonx.Doc{{"foo", bsonx.String("bar")}}, - nil, - }, - { - "reflection", - reflectStruct{Foo: "bar"}, - bsonx.Doc{{"foo", bsonx.String("bar")}}, - nil, - }, - { - "reflection pointer", - &reflectStruct{Foo: "bar"}, - bsonx.Doc{{"foo", bsonx.String("bar")}}, - nil, - }, - { - "unsupported type", - []string{"foo", "bar"}, - nil, - MarshalError{ - Value: []string{"foo", "bar"}, - Err: errors.New("WriteArray can only write a Array while positioned on a Element or Value but is positioned on a TopLevel")}, - }, - { - "nil", - nil, - nil, - ErrNilDocument, - }, - } - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - got, err := transformDocument(bson.NewRegistryBuilder().Build(), tc.document) - assert.Equal(t, tc.err, err, "expected error %v, got %v", tc.err, err) - assert.Equal(t, tc.want, got, "expected document %v, got %v", tc.want, got) - }) - } - }) t.Run("transform and ensure ID", func(t *testing.T) { t.Run("newly added _id should be first element", func(t *testing.T) { doc := bson.D{{"foo", "bar"}, {"baz", "qux"}, {"hello", "world"}} @@ -398,11 +349,11 @@ func TestMongoHelpers(t *testing.T) { var _ bson.Marshaler = bMarsh{} type bMarsh struct { - bsonx.Doc + bson.D } func (b bMarsh) MarshalBSON() ([]byte, error) { - return b.Doc.MarshalBSON() + return bson.Marshal(b) } type reflectStruct struct {