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
7package options
8
9import (
10	"go.mongodb.org/mongo-driver/bson/bsoncodec"
11	"go.mongodb.org/mongo-driver/mongo/readconcern"
12	"go.mongodb.org/mongo-driver/mongo/readpref"
13	"go.mongodb.org/mongo-driver/mongo/writeconcern"
14)
15
16// CollectionOptions represents options that can be used to configure a Collection.
17type CollectionOptions struct {
18	// The read concern to use for operations executed on the Collection. The default value is nil, which means that
19	// the read concern of the database used to configure the Collection will be used.
20	ReadConcern *readconcern.ReadConcern
21
22	// The write concern to use for operations executed on the Collection. The default value is nil, which means that
23	// the write concern of the database used to configure the Collection will be used.
24	WriteConcern *writeconcern.WriteConcern
25
26	// The read preference to use for operations executed on the Collection. The default value is nil, which means that
27	// the read preference of the database used to configure the Collection will be used.
28	ReadPreference *readpref.ReadPref
29
30	// The BSON registry to marshal and unmarshal documents for operations executed on the Collection. The default value
31	// is nil, which means that the registry of the database used to configure the Collection will be used.
32	Registry *bsoncodec.Registry
33}
34
35// Collection creates a new CollectionOptions instance.
36func Collection() *CollectionOptions {
37	return &CollectionOptions{}
38}
39
40// SetReadConcern sets the value for the ReadConcern field.
41func (c *CollectionOptions) SetReadConcern(rc *readconcern.ReadConcern) *CollectionOptions {
42	c.ReadConcern = rc
43	return c
44}
45
46// SetWriteConcern sets the value for the WriteConcern field.
47func (c *CollectionOptions) SetWriteConcern(wc *writeconcern.WriteConcern) *CollectionOptions {
48	c.WriteConcern = wc
49	return c
50}
51
52// SetReadPreference sets the value for the ReadPreference field.
53func (c *CollectionOptions) SetReadPreference(rp *readpref.ReadPref) *CollectionOptions {
54	c.ReadPreference = rp
55	return c
56}
57
58// SetRegistry sets the value for the Registry field.
59func (c *CollectionOptions) SetRegistry(r *bsoncodec.Registry) *CollectionOptions {
60	c.Registry = r
61	return c
62}
63
64// MergeCollectionOptions combines the given CollectionOptions instances into a single *CollectionOptions in a
65// last-one-wins fashion.
66func MergeCollectionOptions(opts ...*CollectionOptions) *CollectionOptions {
67	c := Collection()
68
69	for _, opt := range opts {
70		if opt == nil {
71			continue
72		}
73		if opt.ReadConcern != nil {
74			c.ReadConcern = opt.ReadConcern
75		}
76		if opt.WriteConcern != nil {
77			c.WriteConcern = opt.WriteConcern
78		}
79		if opt.ReadPreference != nil {
80			c.ReadPreference = opt.ReadPreference
81		}
82		if opt.Registry != nil {
83			c.Registry = opt.Registry
84		}
85	}
86
87	return c
88}
89