...
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
17
18 type gridFSBucketOptions struct {
19 *options.BucketOptions
20 }
21
22 var _ bson.Unmarshaler = (*gridFSBucketOptions)(nil)
23
24 func (bo *gridFSBucketOptions) UnmarshalBSON(data []byte) error {
25 var temp struct {
26 Name *string `bson:"name"`
27 ChunkSize *int32 `bson:"chunkSizeBytes"`
28 RC *readConcern `bson:"readConcern"`
29 RP *ReadPreference `bson:"readPreference"`
30 WC *writeConcern `bson:"writeConcern"`
31 Extra map[string]interface{} `bson:",inline"`
32 }
33 if err := bson.Unmarshal(data, &temp); err != nil {
34 return fmt.Errorf("error unmarshalling to temporary gridFSBucketOptions object: %w", err)
35 }
36 if len(temp.Extra) > 0 {
37 return fmt.Errorf("unrecognized fields for gridFSBucketOptions: %v", mapKeys(temp.Extra))
38 }
39
40 bo.BucketOptions = options.GridFSBucket()
41 if temp.Name != nil {
42 bo.SetName(*temp.Name)
43 }
44 if temp.ChunkSize != nil {
45 bo.SetChunkSizeBytes(*temp.ChunkSize)
46 }
47 if temp.RC != nil {
48 bo.SetReadConcern(temp.RC.toReadConcernOption())
49 }
50 if temp.RP != nil {
51 rp, err := temp.RP.ToReadPrefOption()
52 if err != nil {
53 return fmt.Errorf("error parsing read preference document: %w", err)
54 }
55 bo.SetReadPreference(rp)
56 }
57 if temp.WC != nil {
58 wc, err := temp.WC.toWriteConcernOption()
59 if err != nil {
60 return fmt.Errorf("error parsing write concern document: %w", err)
61 }
62 bo.SetWriteConcern(wc)
63 }
64 return nil
65 }
66
View as plain text