...
1
2
3
4
5
6
7 package unified
8
9 import (
10 "fmt"
11
12 "go.mongodb.org/mongo-driver/bson"
13 "go.mongodb.org/mongo-driver/mongo/options"
14 )
15
16 type dbOrCollectionOptions struct {
17 DBOptions *options.DatabaseOptions
18 CollectionOptions *options.CollectionOptions
19 }
20
21 var _ bson.Unmarshaler = (*dbOrCollectionOptions)(nil)
22
23
24
25 func (d *dbOrCollectionOptions) UnmarshalBSON(data []byte) error {
26 var temp struct {
27 RC *readConcern `bson:"readConcern"`
28 RP *ReadPreference `bson:"readPreference"`
29 WC *writeConcern `bson:"writeConcern"`
30 Extra map[string]interface{} `bson:",inline"`
31 }
32 if err := bson.Unmarshal(data, &temp); err != nil {
33 return fmt.Errorf("error unmarshalling to temporary dbOrCollectionOptions object: %w", err)
34 }
35 if len(temp.Extra) > 0 {
36 return fmt.Errorf("unrecognized fields for dbOrCollectionOptions: %v", mapKeys(temp.Extra))
37 }
38
39 d.DBOptions = options.Database()
40 d.CollectionOptions = options.Collection()
41 if temp.RC != nil {
42 rc := temp.RC.toReadConcernOption()
43 d.DBOptions.SetReadConcern(rc)
44 d.CollectionOptions.SetReadConcern(rc)
45 }
46 if temp.RP != nil {
47 rp, err := temp.RP.ToReadPrefOption()
48 if err != nil {
49 return fmt.Errorf("error parsing read preference document: %w", err)
50 }
51
52 d.DBOptions.SetReadPreference(rp)
53 d.CollectionOptions.SetReadPreference(rp)
54 }
55 if temp.WC != nil {
56 wc, err := temp.WC.toWriteConcernOption()
57 if err != nil {
58 return fmt.Errorf("error parsing write concern document: %w", err)
59 }
60
61 d.DBOptions.SetWriteConcern(wc)
62 d.CollectionOptions.SetWriteConcern(wc)
63 }
64
65 return nil
66 }
67
View as plain text