-
Type: Improvement
-
Resolution: Duplicate
-
Priority: Unknown
-
None
-
Affects Version/s: 1.5.2, 1.5.3
-
Component/s: Options & Configuration
-
None
-
Not Needed
-
- Would be part of the auto generation Go docs.
- May need to ensure any other documentation is up to date.
While playing around with `mongo.readconcern` and `mongo.writeconcern` I noticed the API is different between the two modules. It would be nice if they aligned.
Currently, in order to obtain the best practise causal consistency, you have to use:
import ( "go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/readconcern" "go.mongodb.org/mongo-driver/mongo/writeconcern" ) func main() { opts := options.Session(). SetCausalConsistency(true). SetDefaultReadConcern(readconcern.Majority()). SetDefaultWriteConcern(writeconcern.New(writeconcern.WMajority())) }
First step I tried:
import ( "go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/readconcern" "go.mongodb.org/mongo-driver/mongo/writeconcern" ) func main() { opts := options.Session(). SetCausalConsistency(true). SetDefaultReadConcern(readconcern.Majority()). SetDefaultWriteConcern(writeconcern.WMajority()) }
Which fails as `WMajority()` returns a `writeconcern.Option`. Which has to be passed into New.
So the second thing I tried to make the API look consistent was:
import ( "go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/readconcern" "go.mongodb.org/mongo-driver/mongo/writeconcern" ) func main() { opts := options.Session(). SetCausalConsistency(true). SetDefaultReadConcern(readconcern.New(readconcern.Majority())). SetDefaultWriteConcern(writeconcern.New(writeconcern.WMajority())) }
Which naturally fails, as `readconcern.Majority()` returns a `*ReadConcern` rather than a `readconcern.Option`.
So you end up having to do this:
import ( "go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/readconcern" "go.mongodb.org/mongo-driver/mongo/writeconcern" ) func main() { opts := options.Session(). SetCausalConsistency(true). SetDefaultReadConcern(readconcern.New(readconcern.Level("majority"))). SetDefaultWriteConcern(writeconcern.New(writeconcern.WMajority())) }
So it would be nice to be able to have `func Majority() *WriteConcern {}` being a function, like in `readconcern` that directly returns a `*Writeconcern` for API consistency/better developer experiance:
import ( "go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/readconcern" "go.mongodb.org/mongo-driver/mongo/writeconcern" ) func main() { opts := options.Session(). SetCausalConsistency(true). SetDefaultReadConcern(readconcern.Majority()). SetDefaultWriteConcern(writeconcern.Majority()) }
- duplicates
-
GODRIVER-964 make option type construction consistent with the options package (e.g. readpref, readconcern, writeconcern)
- Closed