I need to define these interfaces to mock official mongo driver
type MgCollection interface {
FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) *mongo.SingleResult
// Other methods
}
type MgDatabase interface {
Collection(name string, opts ...*options.CollectionOptions) MgCollection
// Other methods
}
In mongo driver package there are two structs mongo.Collection and mongo.Database with these methods
func (coll *Collection) FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) *SingleResult {
// Method code
}
func (db *Database) Collection(name string, opts ...*options.CollectionOptions) *Collection {
// Method code
}
The struct *mongo.Collection implement MgCollection correctly, so this code compiles without error
var col mgdriver.MgCollection
col = &mongo.Collection{}
col.FindOne(ctx, nil, nil)
But The struct *mongo.Database not implement MgDatabase, so I when I write something like this:
var db mgdriver.MgDatabase
db = &mongo.Database{}
db.Collection("Test", nil)
Compiler show this error:
cannot use &mongo.Database literal (type *mongo.Database) as type mgdriver.MgDatabase in assignment: *mongo.Database does not implement mgdriver.MgDatabase (wrong type for Collection method) have Collection(string, ...*options.CollectionOptions) *mongo.Collection want Collection(string, ...*options.CollectionOptions) mgdriver.MgCollection
Both mongo.Collection and mongo.Database are in the official package and I cannot change any code in that package. so how can I change my interfaces to mock official mongo driver correctly?