Skip to content

Commit

Permalink
GODRIVER-2962 Remove setters from Command.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewdale committed Sep 20, 2023
1 parent 4b0c121 commit ff57da9
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 219 deletions.
19 changes: 14 additions & 5 deletions internal/integtest/integtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,13 @@ func MonitoredTopology(t *testing.T, dbName string, monitor *event.CommandMonito
} else {
_ = monitoredTopology.Connect()

err = operation.NewCommand(bsoncore.BuildDocument(nil, bsoncore.AppendInt32Element(nil, "dropDatabase", 1))).
Database(dbName).ServerSelector(description.WriteSelector()).Deployment(monitoredTopology).Execute(context.Background())

op := &operation.Command{
Command: bsoncore.BuildDocument(nil, bsoncore.AppendInt32Element(nil, "dropDatabase", 1)),
Database: dbName,
Selector: description.WriteSelector(),
Deployment: monitoredTopology,
}
err = op.Execute(context.Background())
require.NoError(t, err)
}

Expand All @@ -125,8 +129,13 @@ func Topology(t *testing.T) *topology.Topology {
} else {
_ = liveTopology.Connect()

err = operation.NewCommand(bsoncore.BuildDocument(nil, bsoncore.AppendInt32Element(nil, "dropDatabase", 1))).
Database(DBName(t)).ServerSelector(description.WriteSelector()).Deployment(liveTopology).Execute(context.Background())
op := &operation.Command{
Command: bsoncore.BuildDocument(nil, bsoncore.AppendInt32Element(nil, "dropDatabase", 1)),
Database: DBName(t),
Selector: description.WriteSelector(),
Deployment: liveTopology,
}
err = op.Execute(context.Background())
require.NoError(t, err)
}
})
Expand Down
33 changes: 23 additions & 10 deletions mongo/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,19 +180,32 @@ func (db *Database) processRunCommand(ctx context.Context, cmd interface{},

cursorOpts.MarshalValueEncoderFn = newEncoderFn(db.bsonOpts, db.registry)

op = operation.NewCursorCommand(runCmdDoc, cursorOpts)
op = &operation.Command{
Command: runCmdDoc,
CreateCursor: true,
CursorOpts: cursorOpts,
}
default:
op = operation.NewCommand(runCmdDoc)
op = &operation.Command{
Command: runCmdDoc,
}
}

// TODO(GODRIVER-2649): ReadConcern(db.readConcern) will not actually pass the database's
// read concern. Remove this note once readConcern is correctly passed to the operation
// level.
return op.Session(sess).CommandMonitor(db.client.monitor).
ServerSelector(readSelect).ClusterClock(db.client.clock).
Database(db.name).Deployment(db.client.deployment).ReadConcern(db.readConcern).
Crypt(db.client.cryptFLE).ReadPreference(ro.ReadPreference).ServerAPI(db.client.serverAPI).
Timeout(db.client.timeout).Logger(db.client.logger), sess, nil
// TODO(GODRIVER-2649): Set read concern on the operation once the Command
// TODO type actually supports it.
op.Session = sess
op.Monitor = db.client.monitor
op.Selector = readSelect
op.Clock = db.client.clock
op.Database = db.name
op.Deployment = db.client.deployment
op.Crypt = db.client.cryptFLE
op.ReadPreference = ro.ReadPreference
op.ServerAPI = db.client.serverAPI
op.Timeout = db.client.timeout
op.Logger = db.client.logger

return op, sess, nil
}

// RunCommand executes the given command against the database. This function does not obey the Database's read
Expand Down
24 changes: 14 additions & 10 deletions x/mongo/driver/auth/mongodbcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ func (a *MongoDBCRAuthenticator) Auth(ctx context.Context, cfg *Config) error {
}

doc := bsoncore.BuildDocumentFromElements(nil, bsoncore.AppendInt32Element(nil, "getnonce", 1))
cmd := operation.NewCommand(doc).
Database(db).
Deployment(driver.SingleConnectionDeployment{cfg.Connection}).
ClusterClock(cfg.ClusterClock).
ServerAPI(cfg.ServerAPI)
cmd := &operation.Command{
Command: doc,
Database: db,
Deployment: driver.SingleConnectionDeployment{cfg.Connection},
Clock: cfg.ClusterClock,
ServerAPI: cfg.ServerAPI,
}
err := cmd.Execute(ctx)
if err != nil {
return newError(err, MONGODBCR)
Expand All @@ -84,11 +86,13 @@ func (a *MongoDBCRAuthenticator) Auth(ctx context.Context, cfg *Config) error {
bsoncore.AppendStringElement(nil, "nonce", getNonceResult.Nonce),
bsoncore.AppendStringElement(nil, "key", a.createKey(getNonceResult.Nonce)),
)
cmd = operation.NewCommand(doc).
Database(db).
Deployment(driver.SingleConnectionDeployment{cfg.Connection}).
ClusterClock(cfg.ClusterClock).
ServerAPI(cfg.ServerAPI)
cmd = &operation.Command{
Command: doc,
Database: db,
Deployment: driver.SingleConnectionDeployment{cfg.Connection},
Clock: cfg.ClusterClock,
ServerAPI: cfg.ServerAPI,
}
err = cmd.Execute(ctx)
if err != nil {
return newError(err, MONGODBCR)
Expand Down
24 changes: 14 additions & 10 deletions x/mongo/driver/auth/sasl.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,13 @@ func (sc *saslConversation) Finish(ctx context.Context, cfg *Config, firstRespon
bsoncore.AppendInt32Element(nil, "conversationId", int32(cid)),
bsoncore.AppendBinaryElement(nil, "payload", 0x00, payload),
)
saslContinueCmd := operation.NewCommand(doc).
Database(sc.source).
Deployment(driver.SingleConnectionDeployment{cfg.Connection}).
ClusterClock(cfg.ClusterClock).
ServerAPI(cfg.ServerAPI)
saslContinueCmd := &operation.Command{
Command: doc,
Database: sc.source,
Deployment: driver.SingleConnectionDeployment{C: cfg.Connection},
Clock: cfg.ClusterClock,
ServerAPI: cfg.ServerAPI,
}

err = saslContinueCmd.Execute(ctx)
if err != nil {
Expand All @@ -161,11 +163,13 @@ func ConductSaslConversation(ctx context.Context, cfg *Config, authSource string
if err != nil {
return newError(err, conversation.mechanism)
}
saslStartCmd := operation.NewCommand(saslStartDoc).
Database(authSource).
Deployment(driver.SingleConnectionDeployment{cfg.Connection}).
ClusterClock(cfg.ClusterClock).
ServerAPI(cfg.ServerAPI)
saslStartCmd := &operation.Command{
Command: saslStartDoc,
Database: authSource,
Deployment: driver.SingleConnectionDeployment{C: cfg.Connection},
Clock: cfg.ClusterClock,
ServerAPI: cfg.ServerAPI,
}
if err := saslStartCmd.Execute(ctx); err != nil {
return newError(err, conversation.mechanism)
}
Expand Down
13 changes: 7 additions & 6 deletions x/mongo/driver/auth/x509.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ func (a *MongoDBX509Authenticator) CreateSpeculativeConversation() (SpeculativeC
// Auth authenticates the provided connection by conducting an X509 authentication conversation.
func (a *MongoDBX509Authenticator) Auth(ctx context.Context, cfg *Config) error {
requestDoc := createFirstX509Message()
authCmd := operation.
NewCommand(requestDoc).
Database("$external").
Deployment(driver.SingleConnectionDeployment{cfg.Connection}).
ClusterClock(cfg.ClusterClock).
ServerAPI(cfg.ServerAPI)
authCmd := &operation.Command{
Command: requestDoc,
Database: "$external",
Deployment: driver.SingleConnectionDeployment{C: cfg.Connection},
Clock: cfg.ClusterClock,
ServerAPI: cfg.ServerAPI,
}
err := authCmd.Execute(ctx)
if err != nil {
return newAuthError("round trip error", err)
Expand Down
8 changes: 5 additions & 3 deletions x/mongo/driver/integration/compressor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ func TestCompression(t *testing.T) {
bsoncore.BuildDocument(nil, bsoncore.AppendStringElement(nil, "name", "compression_test")),
)

cmd := operation.NewCommand(bsoncore.BuildDocument(nil, bsoncore.AppendInt32Element(nil, "serverStatus", 1))).
Deployment(integtest.Topology(t)).
Database(integtest.DBName(t))
cmd := &operation.Command{
Command: bsoncore.BuildDocument(nil, bsoncore.AppendInt32Element(nil, "serverStatus", 1)),
Deployment: integtest.Topology(t),
Database: integtest.DBName(t),
}

ctx := context.Background()
err := cmd.Execute(ctx)
Expand Down
17 changes: 12 additions & 5 deletions x/mongo/driver/integration/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,25 @@ func addCompressorToURI(uri string) string {

// runCommand runs an arbitrary command on a given database of target server
func runCommand(s driver.Server, db string, cmd bsoncore.Document) (bsoncore.Document, error) {
op := operation.NewCommand(cmd).
Database(db).Deployment(driver.SingleServerDeployment{Server: s})
op := &operation.Command{
Command: cmd,
Database: db,
Deployment: driver.SingleServerDeployment{Server: s},
}
err := op.Execute(context.Background())
res := op.Result()
return res, err
}

// dropCollection drops the collection in the test cluster.
func dropCollection(t *testing.T, dbname, colname string) {
err := operation.NewCommand(bsoncore.BuildDocument(nil, bsoncore.AppendStringElement(nil, "drop", colname))).
Database(dbname).ServerSelector(description.WriteSelector()).Deployment(integtest.Topology(t)).
Execute(context.Background())
op := &operation.Command{
Command: bsoncore.BuildDocument(nil, bsoncore.AppendStringElement(nil, "drop", colname)),
Database: dbname,
Selector: description.WriteSelector(),
Deployment: integtest.Topology(t),
}
err := op.Execute(context.Background())
if de, ok := err.(driver.Error); err != nil && !(ok && de.NamespaceNotFound()) {
require.NoError(t, err)
}
Expand Down
Loading

0 comments on commit ff57da9

Please sign in to comment.