You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

collectionoptions.go 2.5 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. package options
  7. import (
  8. "go.mongodb.org/mongo-driver/bson/bsoncodec"
  9. "go.mongodb.org/mongo-driver/mongo/readconcern"
  10. "go.mongodb.org/mongo-driver/mongo/readpref"
  11. "go.mongodb.org/mongo-driver/mongo/writeconcern"
  12. )
  13. // CollectionOptions represent all possible options to configure a Collection.
  14. type CollectionOptions struct {
  15. ReadConcern *readconcern.ReadConcern // The read concern for operations in the collection.
  16. WriteConcern *writeconcern.WriteConcern // The write concern for operations in the collection.
  17. ReadPreference *readpref.ReadPref // The read preference for operations in the collection.
  18. Registry *bsoncodec.Registry // The registry to be used to construct BSON encoders and decoders for the collection.
  19. }
  20. // Collection creates a new CollectionOptions instance
  21. func Collection() *CollectionOptions {
  22. return &CollectionOptions{}
  23. }
  24. // SetReadConcern sets the read concern for the collection.
  25. func (c *CollectionOptions) SetReadConcern(rc *readconcern.ReadConcern) *CollectionOptions {
  26. c.ReadConcern = rc
  27. return c
  28. }
  29. // SetWriteConcern sets the write concern for the collection.
  30. func (c *CollectionOptions) SetWriteConcern(wc *writeconcern.WriteConcern) *CollectionOptions {
  31. c.WriteConcern = wc
  32. return c
  33. }
  34. // SetReadPreference sets the read preference for the collection.
  35. func (c *CollectionOptions) SetReadPreference(rp *readpref.ReadPref) *CollectionOptions {
  36. c.ReadPreference = rp
  37. return c
  38. }
  39. // SetRegistry sets the bsoncodec Registry for the collection.
  40. func (c *CollectionOptions) SetRegistry(r *bsoncodec.Registry) *CollectionOptions {
  41. c.Registry = r
  42. return c
  43. }
  44. // MergeCollectionOptions combines the *CollectionOptions arguments into a single *CollectionOptions in a last one wins
  45. // fashion.
  46. func MergeCollectionOptions(opts ...*CollectionOptions) *CollectionOptions {
  47. c := Collection()
  48. for _, opt := range opts {
  49. if opt == nil {
  50. continue
  51. }
  52. if opt.ReadConcern != nil {
  53. c.ReadConcern = opt.ReadConcern
  54. }
  55. if opt.WriteConcern != nil {
  56. c.WriteConcern = opt.WriteConcern
  57. }
  58. if opt.ReadPreference != nil {
  59. c.ReadPreference = opt.ReadPreference
  60. }
  61. if opt.Registry != nil {
  62. c.Registry = opt.Registry
  63. }
  64. }
  65. return c
  66. }