Don Newton | 379ae25 | 2019-04-01 12:17:06 -0400 | [diff] [blame^] | 1 | // Copyright (C) MongoDB, Inc. 2017-present. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | // not use this file except in compliance with the License. You may obtain |
| 5 | // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | |
| 7 | package options |
| 8 | |
| 9 | import ( |
| 10 | "github.com/mongodb/mongo-go-driver/bson/bsoncodec" |
| 11 | "github.com/mongodb/mongo-go-driver/mongo/readconcern" |
| 12 | "github.com/mongodb/mongo-go-driver/mongo/readpref" |
| 13 | "github.com/mongodb/mongo-go-driver/mongo/writeconcern" |
| 14 | ) |
| 15 | |
| 16 | // CollectionOptions represent all possible options to configure a Collection. |
| 17 | type CollectionOptions struct { |
| 18 | ReadConcern *readconcern.ReadConcern // The read concern for operations in the collection. |
| 19 | WriteConcern *writeconcern.WriteConcern // The write concern for operations in the collection. |
| 20 | ReadPreference *readpref.ReadPref // The read preference for operations in the collection. |
| 21 | Registry *bsoncodec.Registry // The registry to be used to construct BSON encoders and decoders for the collection. |
| 22 | } |
| 23 | |
| 24 | // Collection creates a new CollectionOptions instance |
| 25 | func Collection() *CollectionOptions { |
| 26 | return &CollectionOptions{} |
| 27 | } |
| 28 | |
| 29 | // SetReadConcern sets the read concern for the collection. |
| 30 | func (c *CollectionOptions) SetReadConcern(rc *readconcern.ReadConcern) *CollectionOptions { |
| 31 | c.ReadConcern = rc |
| 32 | return c |
| 33 | } |
| 34 | |
| 35 | // SetWriteConcern sets the write concern for the collection. |
| 36 | func (c *CollectionOptions) SetWriteConcern(wc *writeconcern.WriteConcern) *CollectionOptions { |
| 37 | c.WriteConcern = wc |
| 38 | return c |
| 39 | } |
| 40 | |
| 41 | // SetReadPreference sets the read preference for the collection. |
| 42 | func (c *CollectionOptions) SetReadPreference(rp *readpref.ReadPref) *CollectionOptions { |
| 43 | c.ReadPreference = rp |
| 44 | return c |
| 45 | } |
| 46 | |
| 47 | // SetRegistry sets the bsoncodec Registry for the collection. |
| 48 | func (c *CollectionOptions) SetRegistry(r *bsoncodec.Registry) *CollectionOptions { |
| 49 | c.Registry = r |
| 50 | return c |
| 51 | } |
| 52 | |
| 53 | // MergeCollectionOptions combines the *CollectionOptions arguments into a single *CollectionOptions in a last one wins |
| 54 | // fashion. |
| 55 | func MergeCollectionOptions(opts ...*CollectionOptions) *CollectionOptions { |
| 56 | c := Collection() |
| 57 | |
| 58 | for _, opt := range opts { |
| 59 | if opt == nil { |
| 60 | continue |
| 61 | } |
| 62 | if opt.ReadConcern != nil { |
| 63 | c.ReadConcern = opt.ReadConcern |
| 64 | } |
| 65 | if opt.WriteConcern != nil { |
| 66 | c.WriteConcern = opt.WriteConcern |
| 67 | } |
| 68 | if opt.ReadPreference != nil { |
| 69 | c.ReadPreference = opt.ReadPreference |
| 70 | } |
| 71 | if opt.Registry != nil { |
| 72 | c.Registry = opt.Registry |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return c |
| 77 | } |