Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-2034

API Consistency for read/write

    • Type: Icon: Improvement Improvement
    • Resolution: Duplicate
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 1.5.2, 1.5.3
    • 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())
      }

       

       

            Assignee:
            isabella.siu@mongodb.com Isabella Siu (Inactive)
            Reporter:
            matt@linc-ed.com Matt Hartstonge
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: