Skip to content

Commit

Permalink
GODRIVER-2443 Create a unique type for distinct results
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvasquez committed Apr 30, 2024
1 parent 2726cf3 commit 0d80ea4
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 163 deletions.
2 changes: 1 addition & 1 deletion internal/integration/client_side_encryption_prose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1901,7 +1901,7 @@ func TestClientSideEncryptionProse(t *testing.T) {
return defKeyID
}

var validateAddKeyAltName = func(mt *mtest.T, cse *cseProseTest, res *mongo.SingleResult[bson.Raw], expected ...string) {
var validateAddKeyAltName = func(mt *mtest.T, cse *cseProseTest, res *mongo.SingleResult, expected ...string) {
assert.Nil(mt, res.Err(), "error adding key alt name: %v", res.Err())

resbytes, err := res.Raw()
Expand Down
24 changes: 12 additions & 12 deletions internal/integration/crud_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ func executeFind(mt *mtest.T, sess mongo.Session, args bson.Raw) (*mongo.Cursor,
return mt.Coll.Find(context.Background(), filter, opts)
}

func executeRunCommand(mt *mtest.T, sess mongo.Session, args bson.Raw) *mongo.SingleResult[bson.Raw] {
func executeRunCommand(mt *mtest.T, sess mongo.Session, args bson.Raw) *mongo.SingleResult {
mt.Helper()

cmd := emptyDoc
Expand All @@ -431,7 +431,7 @@ func executeRunCommand(mt *mtest.T, sess mongo.Session, args bson.Raw) *mongo.Si
}

if sess != nil {
var sr *mongo.SingleResult[bson.Raw]
var sr *mongo.SingleResult
_ = mongo.WithSession(context.Background(), sess, func(sc mongo.SessionContext) error {
sr = mt.DB.RunCommand(sc, cmd, opts)
return nil
Expand Down Expand Up @@ -557,7 +557,7 @@ func executeListDatabases(mt *mtest.T, sess mongo.Session, args bson.Raw) (mongo
return mt.Client.ListDatabases(context.Background(), filter)
}

func executeFindOne(mt *mtest.T, sess mongo.Session, args bson.Raw) *mongo.SingleResult[bson.Raw] {
func executeFindOne(mt *mtest.T, sess mongo.Session, args bson.Raw) *mongo.SingleResult {
mt.Helper()

filter := emptyDoc
Expand All @@ -575,7 +575,7 @@ func executeFindOne(mt *mtest.T, sess mongo.Session, args bson.Raw) *mongo.Singl
}

if sess != nil {
var res *mongo.SingleResult[bson.Raw]
var res *mongo.SingleResult
_ = mongo.WithSession(context.Background(), sess, func(sc mongo.SessionContext) error {
res = mt.Coll.FindOne(sc, filter)
return nil
Expand Down Expand Up @@ -627,7 +627,7 @@ func executeDistinct(mt *mtest.T, sess mongo.Session, args bson.Raw) (bson.RawAr
}
}

var res *mongo.SingleResult[bson.RawArray]
var res *mongo.DistinctResult
if sess != nil {
err := mongo.WithSession(context.Background(), sess, func(sc mongo.SessionContext) error {
res = mt.Coll.Distinct(sc, fieldName, filter, opts)
Expand All @@ -645,7 +645,7 @@ func executeDistinct(mt *mtest.T, sess mongo.Session, args bson.Raw) (bson.RawAr
return res.Raw()
}

func executeFindOneAndDelete(mt *mtest.T, sess mongo.Session, args bson.Raw) *mongo.SingleResult[bson.Raw] {
func executeFindOneAndDelete(mt *mtest.T, sess mongo.Session, args bson.Raw) *mongo.SingleResult {
mt.Helper()

filter := emptyDoc
Expand Down Expand Up @@ -674,7 +674,7 @@ func executeFindOneAndDelete(mt *mtest.T, sess mongo.Session, args bson.Raw) *mo
}

if sess != nil {
var res *mongo.SingleResult[bson.Raw]
var res *mongo.SingleResult
_ = mongo.WithSession(context.Background(), sess, func(sc mongo.SessionContext) error {
res = mt.Coll.FindOneAndDelete(sc, filter, opts)
return nil
Expand All @@ -684,7 +684,7 @@ func executeFindOneAndDelete(mt *mtest.T, sess mongo.Session, args bson.Raw) *mo
return mt.Coll.FindOneAndDelete(context.Background(), filter, opts)
}

func executeFindOneAndUpdate(mt *mtest.T, sess mongo.Session, args bson.Raw) *mongo.SingleResult[bson.Raw] {
func executeFindOneAndUpdate(mt *mtest.T, sess mongo.Session, args bson.Raw) *mongo.SingleResult {
mt.Helper()

filter := emptyDoc
Expand Down Expand Up @@ -731,7 +731,7 @@ func executeFindOneAndUpdate(mt *mtest.T, sess mongo.Session, args bson.Raw) *mo
}

if sess != nil {
var res *mongo.SingleResult[bson.Raw]
var res *mongo.SingleResult
_ = mongo.WithSession(context.Background(), sess, func(sc mongo.SessionContext) error {
res = mt.Coll.FindOneAndUpdate(sc, filter, update, opts)
return nil
Expand All @@ -741,7 +741,7 @@ func executeFindOneAndUpdate(mt *mtest.T, sess mongo.Session, args bson.Raw) *mo
return mt.Coll.FindOneAndUpdate(context.Background(), filter, update, opts)
}

func executeFindOneAndReplace(mt *mtest.T, sess mongo.Session, args bson.Raw) *mongo.SingleResult[bson.Raw] {
func executeFindOneAndReplace(mt *mtest.T, sess mongo.Session, args bson.Raw) *mongo.SingleResult {
mt.Helper()

filter := emptyDoc
Expand Down Expand Up @@ -784,7 +784,7 @@ func executeFindOneAndReplace(mt *mtest.T, sess mongo.Session, args bson.Raw) *m
}

if sess != nil {
var res *mongo.SingleResult[bson.Raw]
var res *mongo.SingleResult
_ = mongo.WithSession(context.Background(), sess, func(sc mongo.SessionContext) error {
res = mt.Coll.FindOneAndReplace(sc, filter, replacement, opts)
return nil
Expand Down Expand Up @@ -1648,7 +1648,7 @@ func verifyCursorResult(mt *mtest.T, cur *mongo.Cursor, result interface{}) {

func verifySingleResult(
mt *mtest.T,
actualResult *mongo.SingleResult[bson.Raw],
actualResult *mongo.SingleResult,
expectedResult interface{},
) {
mt.Helper()
Expand Down
4 changes: 2 additions & 2 deletions internal/integration/mock_find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

// finder is an object that implements FindOne and Find.
type finder interface {
FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) *mongo.SingleResult[bson.Raw]
FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) *mongo.SingleResult
Find(context.Context, interface{}, ...*options.FindOptions) (*mongo.Cursor, error)
}

Expand All @@ -31,7 +31,7 @@ type mockFinder struct {
}

// FindOne mocks a findOne operation using NewSingleResultFromDocument.
func (mf *mockFinder) FindOne(_ context.Context, _ interface{}, _ ...*options.FindOneOptions) *mongo.SingleResult[bson.Raw] {
func (mf *mockFinder) FindOne(_ context.Context, _ interface{}, _ ...*options.FindOneOptions) *mongo.SingleResult {
return mongo.NewSingleResultFromDocument(mf.docs[0], mf.err, mf.registry)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/integration/sessions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,9 +619,9 @@ func extractReturnError(returnValues []reflect.Value) error {
switch converted := errVal.Interface().(type) {
case error:
return converted
case *mongo.SingleResult[bson.Raw]:
case *mongo.SingleResult:
return converted.Err()
case *mongo.SingleResult[bson.RawArray]:
case *mongo.DistinctResult:
return converted.Err()
default:
return nil
Expand Down
8 changes: 4 additions & 4 deletions mongo/client_encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (ce *ClientEncryption) CreateEncryptedCollection(ctx context.Context,

// AddKeyAltName adds a keyAltName to the keyAltNames array of the key document in the key vault collection with the
// given UUID (BSON binary subtype 0x04). Returns the previous version of the key document.
func (ce *ClientEncryption) AddKeyAltName(ctx context.Context, id bson.Binary, keyAltName string) *SingleResult[bson.Raw] {
func (ce *ClientEncryption) AddKeyAltName(ctx context.Context, id bson.Binary, keyAltName string) *SingleResult {
filter := bsoncore.NewDocumentBuilder().AppendBinary("_id", id.Subtype, id.Data).Build()
keyAltNameDoc := bsoncore.NewDocumentBuilder().AppendString("keyAltNames", keyAltName).Build()
update := bsoncore.NewDocumentBuilder().AppendDocument("$addToSet", keyAltNameDoc).Build()
Expand Down Expand Up @@ -333,14 +333,14 @@ func (ce *ClientEncryption) DeleteKey(ctx context.Context, id bson.Binary) (*Del
}

// GetKeyByAltName returns a key document in the key vault collection with the given keyAltName.
func (ce *ClientEncryption) GetKeyByAltName(ctx context.Context, keyAltName string) *SingleResult[bson.Raw] {
func (ce *ClientEncryption) GetKeyByAltName(ctx context.Context, keyAltName string) *SingleResult {
filter := bsoncore.NewDocumentBuilder().AppendString("keyAltNames", keyAltName).Build()
return ce.keyVaultColl.FindOne(ctx, filter)
}

// GetKey finds a single key document with the given UUID (BSON binary subtype 0x04). Returns the result of the
// internal find() operation on the key vault collection.
func (ce *ClientEncryption) GetKey(ctx context.Context, id bson.Binary) *SingleResult[bson.Raw] {
func (ce *ClientEncryption) GetKey(ctx context.Context, id bson.Binary) *SingleResult {
filter := bsoncore.NewDocumentBuilder().AppendBinary("_id", id.Subtype, id.Data).Build()
return ce.keyVaultColl.FindOne(ctx, filter)
}
Expand All @@ -353,7 +353,7 @@ func (ce *ClientEncryption) GetKeys(ctx context.Context) (*Cursor, error) {

// RemoveKeyAltName removes a keyAltName from the keyAltNames array of the key document in the key vault collection with
// the given UUID (BSON binary subtype 0x04). Returns the previous version of the key document.
func (ce *ClientEncryption) RemoveKeyAltName(ctx context.Context, id bson.Binary, keyAltName string) *SingleResult[bson.Raw] {
func (ce *ClientEncryption) RemoveKeyAltName(ctx context.Context, id bson.Binary, keyAltName string) *SingleResult {
filter := bsoncore.NewDocumentBuilder().AppendBinary("_id", id.Subtype, id.Data).Build()
update := bson.A{bson.D{{"$set", bson.D{{"keyAltNames", bson.D{{"$cond", bson.A{bson.D{{"$eq",
bson.A{"$keyAltNames", bson.A{keyAltName}}}}, "$$REMOVE", bson.D{{"$filter",
Expand Down

0 comments on commit 0d80ea4

Please sign in to comment.