-
Notifications
You must be signed in to change notification settings - Fork 379
Expand file tree
/
Copy pathbsonutil.go
More file actions
534 lines (472 loc) · 14.2 KB
/
bsonutil.go
File metadata and controls
534 lines (472 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
// Copyright (C) MongoDB, Inc. 2014-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Package bsonutil provides utilities for processing BSON data.
package bsonutil
import (
"bytes"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"reflect"
"strconv"
"time"
"github.com/mongodb/mongo-tools/common/json"
"github.com/mongodb/mongo-tools/common/util"
errors2 "github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
var ErrNoSuchField = errors.New("no such field")
// IsEqual marshals two documents to raw BSON and compares them.
func IsEqual(left, right bson.D) (bool, error) {
leftBytes, err := bson.Marshal(left)
if err != nil {
return false, err
}
rightBytes, err := bson.Marshal(right)
if err != nil {
return false, err
}
return bytes.Compare(leftBytes, rightBytes) == 0, nil
}
// ConvertLegacyExtJSONDocumentToBSON iterates through the document map and converts JSON
// values to their corresponding BSON values. It also replaces any extended JSON
// type value (e.g. $date) with the corresponding BSON type.
func ConvertLegacyExtJSONDocumentToBSON(doc map[string]interface{}) error {
for key, jsonValue := range doc {
var bsonValue interface{}
var err error
switch v := jsonValue.(type) {
case map[string]interface{}, bson.D: // subdocument
bsonValue, err = ParseSpecialKeys(v)
default:
bsonValue, err = ConvertLegacyExtJSONValueToBSON(v)
}
if err != nil {
return err
}
doc[key] = bsonValue
}
return nil
}
// GetExtendedBsonD iterates through the document and returns a bson.D that adds type
// information for each key in document.
func GetExtendedBsonD(doc bson.D) (bson.D, error) {
var err error
var bsonDoc bson.D
for _, docElem := range doc {
var bsonValue interface{}
switch v := docElem.Value.(type) {
case map[string]interface{}, bson.D: // subdocument
bsonValue, err = ParseSpecialKeys(v)
default:
bsonValue, err = ConvertLegacyExtJSONValueToBSON(v)
}
if err != nil {
return nil, err
}
bsonDoc = append(bsonDoc, bson.E{
Key: docElem.Key,
Value: bsonValue,
})
}
return bsonDoc, nil
}
// FindValueByKey returns the value of keyName in document. If keyName is not found
// in the top-level of the document, ErrNoSuchField is returned as the error.
func FindValueByKey(keyName string, document *bson.D) (interface{}, error) {
for _, key := range *document {
if key.Key == keyName {
return key.Value, nil
}
}
return nil, ErrNoSuchField
}
// FindStringValueByKey returns the value of keyName in document as a String.
// Returns an error if keyName is not found in the top-level of the document,
// or if it is found but its value is not a string.
func FindStringValueByKey(keyName string, document *bson.D) (string, error) {
value, err := FindValueByKey(keyName, document)
if err != nil {
return "", err
}
str, ok := value.(string)
if !ok {
return "", fmt.Errorf("field present, but is not a string: %v", value)
}
return str, nil
}
// FindIntByKey returns the value of keyName in the document as an int for
// either int32 or int64 underlying type.
func FindIntByKey(keyName string, document *bson.D) (int, error) {
raw, err := FindValueByKey(keyName, document)
if err != nil {
return 0, err
}
switch x := raw.(type) {
case int32:
return int(x), nil
case int64:
return int(x), nil
case int:
return x, nil
default:
return 0, fmt.Errorf("field '%s' is not an integer type", keyName)
}
}
// FindSubdocumentByKey returns the value of keyName in document as a document.
// Returns an error if keyName is not found in the top-level of the document,
// or if it is found but its value is not a document.
func FindSubdocumentByKey(keyName string, document *bson.D) (bson.D, error) {
value, err := FindValueByKey(keyName, document)
if err != nil {
return bson.D{}, err
}
doc, ok := value.(bson.D)
if !ok {
return bson.D{}, fmt.Errorf("field '%s' is not a document", keyName)
}
return doc, nil
}
// RemoveKey removes the given key. Returns the removed value and true if the
// key was found.
func RemoveKey(key string, document *bson.D) (interface{}, bool) {
if document == nil {
return nil, false
}
doc := *document
for i, elem := range doc {
if elem.Key == key {
// Remove this key.
*document = append(doc[:i], doc[i+1:]...)
return elem.Value, true
}
}
return nil, false
}
// ParseSpecialKeys takes a JSON document and inspects it for any extended JSON
// type (e.g $numberLong) and replaces any such values with the corresponding
// BSON type. (uses legacy extJSON parser).
func ParseSpecialKeys(special interface{}) (interface{}, error) {
// first ensure we are using a correct document type
var doc map[string]interface{}
switch v := special.(type) {
case bson.D:
doc = v.Map()
case map[string]interface{}:
doc = v
default:
return nil, fmt.Errorf("%v (type %T) is not valid input to ParseSpecialKeys", special, special)
}
// check document to see if it is special
switch len(doc) {
case 1: // document has a single field
if jsonValue, ok := doc["$date"]; ok {
switch v := jsonValue.(type) {
case string:
return util.FormatDate(v)
case bson.D:
asMap := v.Map()
if jsonValue, ok := asMap["$numberLong"]; ok {
n, err := parseNumberLongField(jsonValue)
if err != nil {
return nil, err
}
return time.Unix(n/1e3, n%1e3*1e6), err
}
return nil, errors.New("expected $numberLong field in $date")
case map[string]interface{}:
if jsonValue, ok := v["$numberLong"]; ok {
n, err := parseNumberLongField(jsonValue)
if err != nil {
return nil, err
}
return time.Unix(n/1e3, n%1e3*1e6), err
}
return nil, errors.New("expected $numberLong field in $date")
case json.Number:
n, err := v.Int64()
return time.Unix(n/1e3, n%1e3*1e6), err
case float64:
n := int64(v)
return time.Unix(n/1e3, n%1e3*1e6), nil
case int32:
n := int64(v)
return time.Unix(n/1e3, n%1e3*1e6), nil
case int64:
return time.Unix(v/1e3, v%1e3*1e6), nil
case json.ISODate:
return v, nil
default:
return nil, errors.New("invalid type for $date field")
}
}
if jsonValue, ok := doc["$code"]; ok {
switch v := jsonValue.(type) {
case string:
return primitive.JavaScript(v), nil
default:
return nil, errors.New("expected $code field to have string value")
}
}
if jsonValue, ok := doc["$oid"]; ok {
switch v := jsonValue.(type) {
case string:
return primitive.ObjectIDFromHex(v)
default:
return nil, errors.New("expected $oid field to have string value")
}
}
if jsonValue, ok := doc["$numberLong"]; ok {
return parseNumberLongField(jsonValue)
}
if jsonValue, ok := doc["$numberInt"]; ok {
switch v := jsonValue.(type) {
case string:
// all of decimal, hex, and octal are supported here
n, err := strconv.ParseInt(v, 0, 32)
return int32(n), err
default:
return nil, errors.New("expected $numberInt field to have string value")
}
}
if jsonValue, ok := doc["$timestamp"]; ok {
ts := json.Timestamp{}
var tsDoc map[string]interface{}
switch internalDoc := jsonValue.(type) {
case map[string]interface{}:
tsDoc = internalDoc
case bson.D:
tsDoc = internalDoc.Map()
default:
return nil, errors.New("expected $timestamp key to have internal document")
}
if seconds, ok := tsDoc["t"]; ok {
if asUint32, err := util.ToUInt32(seconds); err == nil {
ts.Seconds = asUint32
} else {
return nil, errors.New("expected $timestamp 't' field to be a numeric type")
}
} else {
return nil, errors.New("expected $timestamp to have 't' field")
}
if inc, ok := tsDoc["i"]; ok {
if asUint32, err := util.ToUInt32(inc); err == nil {
ts.Increment = asUint32
} else {
return nil, errors.New("expected $timestamp 'i' field to be a numeric type")
}
} else {
return nil, errors.New("expected $timestamp to have 'i' field")
}
// see BSON spec for details on the bit fiddling here
return primitive.Timestamp{T: ts.Seconds, I: ts.Increment}, nil
}
if jsonValue, ok := doc["$numberDecimal"]; ok {
switch v := jsonValue.(type) {
case string:
return primitive.ParseDecimal128(v)
default:
return nil, errors.New("expected $numberDecimal field to have string value")
}
}
if _, ok := doc["$undefined"]; ok {
return primitive.Undefined{}, nil
}
if _, ok := doc["$maxKey"]; ok {
return primitive.MaxKey{}, nil
}
if _, ok := doc["$minKey"]; ok {
return primitive.MinKey{}, nil
}
case 2: // document has two fields
if jsonValue, ok := doc["$code"]; ok {
code := primitive.CodeWithScope{}
switch v := jsonValue.(type) {
case string:
code.Code = primitive.JavaScript(v)
default:
return nil, errors.New("expected $code field to have string value")
}
if jsonValue, ok = doc["$scope"]; ok {
switch v2 := jsonValue.(type) {
case map[string]interface{}, bson.D:
x, err := ParseSpecialKeys(v2)
if err != nil {
return nil, err
}
code.Scope = x
return code, nil
default:
return nil, errors.New("expected $scope field to contain map")
}
} else {
return nil, errors.New("expected $scope field with $code field")
}
}
if jsonValue, ok := doc["$regex"]; ok {
regex := primitive.Regex{}
switch pattern := jsonValue.(type) {
case string:
regex.Pattern = pattern
default:
return nil, errors.New("expected $regex field to have string value")
}
if jsonValue, ok = doc["$options"]; !ok {
return nil, errors.New("expected $options field with $regex field")
}
switch options := jsonValue.(type) {
case string:
regex.Options = options
default:
return nil, errors.New("expected $options field to have string value")
}
// Validate regular expression options
for i := range regex.Options {
switch o := regex.Options[i]; o {
default:
return nil, fmt.Errorf("invalid regular expression option '%v'", o)
case 'g', 'i', 'm', 's': // allowed
}
}
return regex, nil
}
if jsonValue, ok := doc["$binary"]; ok {
binary := primitive.Binary{}
switch data := jsonValue.(type) {
case string:
bytes, err := base64.StdEncoding.DecodeString(data)
if err != nil {
return nil, err
}
binary.Data = bytes
default:
return nil, errors.New("expected $binary field to have string value")
}
if jsonValue, ok = doc["$type"]; !ok {
return nil, errors.New("expected $type field with $binary field")
}
switch typ := jsonValue.(type) {
case string:
kind, err := hex.DecodeString(typ)
if err != nil {
return nil, err
} else if len(kind) != 1 {
return nil, errors.New("expected single byte (as hexadecimal string) for $type field")
}
binary.Subtype = kind[0]
default:
return nil, errors.New("expected $type field to have string value")
}
return binary, nil
}
}
// nothing matched, so we recurse deeper
switch v := special.(type) {
case bson.D:
return GetExtendedBsonD(v)
case map[string]interface{}:
return ConvertLegacyExtJSONValueToBSON(v)
default:
return nil, fmt.Errorf("%v (type %T) is not valid input to ParseSpecialKeys", special, special)
}
}
// ParseLegacyExtJSONValue takes any value generated by the json package and returns a
// BSON version of that value.
func ParseLegacyExtJSONValue(jsonValue interface{}) (interface{}, error) {
switch v := jsonValue.(type) {
case map[string]interface{}, bson.D: // subdocument
return ParseSpecialKeys(v)
default:
return ConvertLegacyExtJSONValueToBSON(v)
}
}
func parseNumberLongField(jsonValue interface{}) (int64, error) {
switch v := jsonValue.(type) {
case string:
// all of decimal, hex, and octal are supported here
return strconv.ParseInt(v, 0, 64)
default:
return 0, errors.New("expected $numberLong field to have string value")
}
}
func Bson2Float64(data interface{}) (float64, bool) {
switch v := data.(type) {
case int32:
return float64(v), true
case int64:
return float64(v), true
case float64:
return v, true
case primitive.Decimal128:
if bi, _, err := v.BigInt(); err == nil {
intVal := bi.Int64()
return float64(intVal), true
}
return 0, false
}
return 0, false
}
// MtoD converts a bson.M to a bson.D.
func MtoD(m bson.M) bson.D {
doc := make(bson.D, 0, len(m))
for key, value := range m {
doc = append(doc, bson.E{key, value})
}
return doc
}
// MarshalExtJSONReversible is a wrapper around bson.MarshalExtJSON function,
// but would return an error if it cannot be reversed by bson.UnmarshalExtJSON.
//
// It is preferred to be used in mongodump to avoid generating un-reversible ext JSON.
func MarshalExtJSONReversible(
val interface{},
canonical bool,
escapeHTML bool,
) ([]byte, error) {
jsonBytes, err := bson.MarshalExtJSON(val, canonical, escapeHTML)
if err != nil {
return nil, err
}
reversedVal := reflect.New(reflect.TypeOf(val)).Elem().Interface()
if unmarshalErr := bson.UnmarshalExtJSON(jsonBytes, canonical, &reversedVal); unmarshalErr != nil {
return nil, errors2.Wrap(unmarshalErr, "marshal is not reversible")
}
return jsonBytes, nil
}
// MarshalExtJSONWithBSONRoundtripConsistency is a wrapper around bson.MarshalExtJSON
// which also validates that BSON objects that are marshaled to ExtJSON objects
// return a consistent BSON object when unmarshaled.
func MarshalExtJSONWithBSONRoundtripConsistency(
val interface{},
canonical bool,
escapeHTML bool,
) ([]byte, error) {
jsonBytes, err := MarshalExtJSONReversible(val, canonical, escapeHTML)
if err != nil {
return nil, err
}
originalBSON, err := bson.Marshal(val)
if err != nil {
return nil, fmt.Errorf("could not marshal into BSON")
}
reversedVal := reflect.New(reflect.TypeOf(val)).Elem().Interface()
err = bson.UnmarshalExtJSON(jsonBytes, canonical, &reversedVal)
if err != nil {
return nil, err
}
reversedBSON, err := bson.Marshal(reversedVal)
if err != nil {
return nil, fmt.Errorf("could not marshal into BSON")
}
if !bytes.Equal(originalBSON, reversedBSON) {
return nil, fmt.Errorf(
"marshaling BSON to ExtJSON and back resulted in discrepancies",
)
}
return jsonBytes, nil
}