...
1
2
3
4
5
6
7 package mongo
8
9 import (
10 "fmt"
11
12 "go.mongodb.org/mongo-driver/bson"
13 "go.mongodb.org/mongo-driver/bson/primitive"
14 "go.mongodb.org/mongo-driver/x/mongo/driver/operation"
15 )
16
17
18 type BulkWriteResult struct {
19
20 InsertedCount int64
21
22
23 MatchedCount int64
24
25
26 ModifiedCount int64
27
28
29 DeletedCount int64
30
31
32 UpsertedCount int64
33
34
35 UpsertedIDs map[int64]interface{}
36 }
37
38
39 type InsertOneResult struct {
40
41 InsertedID interface{}
42 }
43
44
45 type InsertManyResult struct {
46
47 InsertedIDs []interface{}
48 }
49
50
51
52
53 type DeleteResult struct {
54 DeletedCount int64 `bson:"n"`
55 }
56
57
58
59 type RewrapManyDataKeyResult struct {
60 *BulkWriteResult
61 }
62
63
64 type ListDatabasesResult struct {
65
66 Databases []DatabaseSpecification
67
68
69
70 TotalSize int64
71 }
72
73 func newListDatabasesResultFromOperation(res operation.ListDatabasesResult) ListDatabasesResult {
74 var ldr ListDatabasesResult
75 ldr.Databases = make([]DatabaseSpecification, 0, len(res.Databases))
76 for _, spec := range res.Databases {
77 ldr.Databases = append(
78 ldr.Databases,
79 DatabaseSpecification{Name: spec.Name, SizeOnDisk: spec.SizeOnDisk, Empty: spec.Empty},
80 )
81 }
82 ldr.TotalSize = res.TotalSize
83 return ldr
84 }
85
86
87 type DatabaseSpecification struct {
88 Name string
89 SizeOnDisk int64
90 Empty bool
91 }
92
93
94 type UpdateResult struct {
95 MatchedCount int64
96 ModifiedCount int64
97 UpsertedCount int64
98 UpsertedID interface{}
99 }
100
101
102
103
104
105 func (result *UpdateResult) UnmarshalBSON(b []byte) error {
106
107 elems, err := bson.Raw(b).Elements()
108 if err != nil {
109 return err
110 }
111
112 for _, elem := range elems {
113 switch elem.Key() {
114 case "n":
115 switch elem.Value().Type {
116 case bson.TypeInt32:
117 result.MatchedCount = int64(elem.Value().Int32())
118 case bson.TypeInt64:
119 result.MatchedCount = elem.Value().Int64()
120 default:
121 return fmt.Errorf("Received invalid type for n, should be Int32 or Int64, received %s", elem.Value().Type)
122 }
123 case "nModified":
124 switch elem.Value().Type {
125 case bson.TypeInt32:
126 result.ModifiedCount = int64(elem.Value().Int32())
127 case bson.TypeInt64:
128 result.ModifiedCount = elem.Value().Int64()
129 default:
130 return fmt.Errorf("Received invalid type for nModified, should be Int32 or Int64, received %s", elem.Value().Type)
131 }
132 case "upserted":
133 switch elem.Value().Type {
134 case bson.TypeArray:
135 e, err := elem.Value().Array().IndexErr(0)
136 if err != nil {
137 break
138 }
139 if e.Value().Type != bson.TypeEmbeddedDocument {
140 break
141 }
142 var d struct {
143 ID interface{} `bson:"_id"`
144 }
145 err = bson.Unmarshal(e.Value().Document(), &d)
146 if err != nil {
147 return err
148 }
149 result.UpsertedID = d.ID
150 default:
151 return fmt.Errorf("Received invalid type for upserted, should be Array, received %s", elem.Value().Type)
152 }
153 }
154 }
155
156 return nil
157 }
158
159
160
161 type IndexSpecification struct {
162
163 Name string
164
165
166 Namespace string
167
168
169 KeysDocument bson.Raw
170
171
172 Version int32
173
174
175
176 ExpireAfterSeconds *int32
177
178
179
180 Sparse *bool
181
182
183
184 Unique *bool
185
186
187 Clustered *bool
188 }
189
190 var _ bson.Unmarshaler = (*IndexSpecification)(nil)
191
192 type unmarshalIndexSpecification struct {
193 Name string `bson:"name"`
194 Namespace string `bson:"ns"`
195 KeysDocument bson.Raw `bson:"key"`
196 Version int32 `bson:"v"`
197 ExpireAfterSeconds *int32 `bson:"expireAfterSeconds"`
198 Sparse *bool `bson:"sparse"`
199 Unique *bool `bson:"unique"`
200 Clustered *bool `bson:"clustered"`
201 }
202
203
204
205
206 func (i *IndexSpecification) UnmarshalBSON(data []byte) error {
207 var temp unmarshalIndexSpecification
208 if err := bson.Unmarshal(data, &temp); err != nil {
209 return err
210 }
211
212 i.Name = temp.Name
213 i.Namespace = temp.Namespace
214 i.KeysDocument = temp.KeysDocument
215 i.Version = temp.Version
216 i.ExpireAfterSeconds = temp.ExpireAfterSeconds
217 i.Sparse = temp.Sparse
218 i.Unique = temp.Unique
219 i.Clustered = temp.Clustered
220 return nil
221 }
222
223
224
225 type CollectionSpecification struct {
226
227 Name string
228
229
230 Type string
231
232
233 ReadOnly bool
234
235
236
237 UUID *primitive.Binary
238
239
240 Options bson.Raw
241
242
243
244 IDIndex *IndexSpecification
245 }
246
247 var _ bson.Unmarshaler = (*CollectionSpecification)(nil)
248
249
250
251 type unmarshalCollectionSpecification struct {
252 Name string `bson:"name"`
253 Type string `bson:"type"`
254 Info *struct {
255 ReadOnly bool `bson:"readOnly"`
256 UUID *primitive.Binary `bson:"uuid"`
257 } `bson:"info"`
258 Options bson.Raw `bson:"options"`
259 IDIndex *IndexSpecification `bson:"idIndex"`
260 }
261
262
263
264
265
266 func (cs *CollectionSpecification) UnmarshalBSON(data []byte) error {
267 var temp unmarshalCollectionSpecification
268 if err := bson.Unmarshal(data, &temp); err != nil {
269 return err
270 }
271
272 cs.Name = temp.Name
273 cs.Type = temp.Type
274 if cs.Type == "" {
275
276
277 cs.Type = "collection"
278 }
279 if temp.Info != nil {
280 cs.ReadOnly = temp.Info.ReadOnly
281 cs.UUID = temp.Info.UUID
282 }
283 cs.Options = temp.Options
284 cs.IDIndex = temp.IDIndex
285 return nil
286 }
287
View as plain text