/
bson_decode.go
622 lines (574 loc) · 14.4 KB
/
bson_decode.go
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
// Copyright 2010 Gary Burd
//
// 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
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package mongo
import (
"bytes"
"math"
"os"
"reflect"
"runtime"
)
var ErrEOD = os.NewError("bson: unexpected end of data when parsing BSON")
// DecodeConvertError is returned when decoder cannot convert BSON value to the
// target type.
type DecodeConvertError struct {
kind int
t reflect.Type
}
func (e *DecodeConvertError) String() string {
return "bson: could not decode " + kindName(e.kind) + " to " + e.t.String()
}
// DecodeTypeError is returned when the deocoder encounters an unknown type in
// the input.
type DecodeTypeError struct {
kind int
}
func (e *DecodeTypeError) String() string {
return "bson: could not decode " + kindName(e.kind)
}
// Deocde decodes BSON data to value v.
//
// Decode traverses the value v recursively. Decode uese the inverse of the
// encodings supported by Encode, allocating maps, slices and pointers as
// needed. The following conversions from BSON types to GO types are supported:
//
// BSON -> Go
// 32-bit integer -> int, int32, int64, float32, float64, bool
// 64-bit integer -> int64, int, int32, float32, float64, bool
// Array -> []interface{}, other slice types
// Binary -> []byte
// Boolean -> bool
// Datetime -> mongo.Datetime, int64
// Document -> map[string]interface{}, struct types
// Double -> float64, float32, int, int32, int64
// MinValue, MaxValue -> mongo.MinMax
// ObjectID -> mongo.ObjectID
// Symbol -> mongo.Symbol, string
// Timestamp -> mongo.Timestamp, int64
// string -> string
//
// If a number overflows the target type or the BSON value cannot be converted
// to the target type, then the decoding completes the best it can and an error
// is returned.
//
// To decode a BSON value into a nil interface value, the first type listed in
// the right hand column of the table above is used.
func Decode(data []byte, v interface{}) (err os.Error) {
defer func() {
if r := recover(); r != nil {
if _, ok := r.(runtime.Error); ok {
panic(r)
}
err = r.(os.Error)
}
}()
d := decodeState{data: data}
pv, ok := reflect.NewValue(v).(*reflect.PtrValue)
if !ok || pv.IsNil() {
return os.NewError("bson: decode arg must be pointer")
}
d.decodeValue(kindDocument, pv.Elem())
return d.savedError
}
// decodeState represents the state while decoding a JSON value.
type decodeState struct {
data []byte
offset int // read offset in data
savedError os.Error
}
// abort by panicking with err.
func (d *decodeState) abort(err os.Error) {
panic(err)
}
// saveError saves the first err it is called with, for reporting at the end of
// Decode.
func (d *decodeState) saveError(err os.Error) {
if d.savedError == nil {
d.savedError = err
}
}
// saveErrorAndSkip skips the value and saves a conversion error.
func (d *decodeState) saveErrorAndSkip(kind int, t reflect.Type) {
d.skipValue(kind)
if d.savedError == nil {
d.savedError = &DecodeConvertError{kind, t}
}
}
func (d *decodeState) beginDoc() int {
offset := d.offset
offset += int(wire.Uint32(d.scanSlice(4)))
return offset
}
func (d *decodeState) endDoc(offset int) {
if d.offset != offset {
d.abort(os.NewError("bson: doc length wrong"))
}
}
func (d *decodeState) scanSlice(n int) []byte {
if n+d.offset > len(d.data) {
d.abort(ErrEOD)
}
p := d.data[d.offset : d.offset+n]
d.offset += n
return p
}
func (d *decodeState) scanKindName() (int, []byte) {
if d.offset >= len(d.data) {
d.abort(ErrEOD)
}
kind := int(d.data[d.offset])
d.offset += 1
if kind == 0 {
return 0, nil
}
n := bytes.IndexByte(d.data[d.offset:], 0)
if n < 0 {
d.abort(ErrEOD)
}
p := d.data[d.offset : d.offset+n]
d.offset = d.offset + n + 1
return kind, p
}
func (d *decodeState) scanFloat() float64 {
return math.Float64frombits(wire.Uint64(d.scanSlice(8)))
}
func (d *decodeState) scanString() string {
n := int(wire.Uint32(d.scanSlice(4)))
s := string(d.scanSlice(n - 1))
d.offset += 1 // skip null terminator
return s
}
func (d *decodeState) scanBinary() ([]byte, int) {
n := int(wire.Uint32(d.scanSlice(4)))
subtype := int(d.scanSlice(1)[0])
return d.scanSlice(n), subtype
}
func (d *decodeState) scanObjectId() []byte {
return d.scanSlice(12)
}
func (d *decodeState) scanBool() bool {
b := d.scanSlice(1)[0]
if b == 0 {
return false
}
return true
}
func (d *decodeState) scanInt32() int32 {
return int32(wire.Uint32(d.scanSlice(4)))
}
func (d *decodeState) scanInt64() int64 {
return int64(wire.Uint64(d.scanSlice(8)))
}
func (d *decodeState) decodeValue(kind int, v reflect.Value) {
v = d.indirect(v)
t := v.Type()
decoder, ok := typeDecoder[t]
if !ok {
decoder, ok = kindDecoder[t.Kind()]
if !ok {
d.saveErrorAndSkip(kind, v.Type())
return
}
}
decoder(d, kind, v)
}
// indirect walks down v allocating pointers as needed, until it gets to a
// non-pointer.
func (d *decodeState) indirect(v reflect.Value) reflect.Value {
for {
/*
if iv, ok := v.(*reflect.InterfaceValue); ok && !iv.IsNil() {
v = iv.Elem()
continue
}
*/
pv, ok := v.(*reflect.PtrValue)
if !ok {
break
}
if pv.IsNil() {
pv.PointTo(reflect.MakeZero(pv.Type().(*reflect.PtrType).Elem()))
}
v = pv.Elem()
}
return v
}
func decodeFloat(d *decodeState, kind int, value reflect.Value) {
var f float64
switch kind {
default:
d.saveErrorAndSkip(kind, value.Type())
return
case kindFloat:
f = d.scanFloat()
case kindInt64:
f = float64(d.scanInt64())
case kindInt32:
f = float64(d.scanInt32())
}
v := value.(*reflect.FloatValue)
if v.Overflow(f) {
d.saveError(&DecodeConvertError{kind, v.Type()})
return
}
v.Set(f)
}
func decodeInt(d *decodeState, kind int, value reflect.Value) {
var n int64
switch kind {
default:
d.saveErrorAndSkip(kind, value.Type())
return
case kindInt64, kindTimestamp, kindDateTime:
n = d.scanInt64()
case kindInt32:
n = int64(d.scanInt32())
case kindFloat:
n = int64(d.scanFloat())
}
v := value.(*reflect.IntValue)
if v.Overflow(n) {
d.saveError(&DecodeConvertError{kind, v.Type()})
return
}
v.Set(n)
}
func decodeTimestamp(d *decodeState, kind int, value reflect.Value) {
switch kind {
default:
d.saveErrorAndSkip(kind, value.Type())
return
case kindInt64, kindTimestamp:
decodeInt(d, kindInt64, value)
}
}
func decodeDateTime(d *decodeState, kind int, value reflect.Value) {
switch kind {
default:
d.saveErrorAndSkip(kind, value.Type())
return
case kindInt64, kindDateTime:
decodeInt(d, kindInt64, value)
}
}
func decodeString(d *decodeState, kind int, value reflect.Value) {
var s string
switch kind {
default:
d.saveErrorAndSkip(kind, value.Type())
return
case kindString, kindSymbol, kindCode:
s = d.scanString()
}
value.(*reflect.StringValue).Set(s)
}
func decodeObjectId(d *decodeState, kind int, value reflect.Value) {
var p []byte
switch kind {
default:
d.saveErrorAndSkip(kind, value.Type())
return
case kindObjectId:
p = d.scanSlice(12)
}
reflect.Copy(value.(reflect.ArrayOrSliceValue), reflect.NewValue(p).(reflect.ArrayOrSliceValue))
}
func decodeBSONData(d *decodeState, kind int, value reflect.Value) {
start := d.offset
d.skipValue(kind)
bd := BSONData{Kind: kind, Data: make([]byte, d.offset-start)}
copy(bd.Data, d.data[start:d.offset])
value.SetValue(reflect.NewValue(bd))
}
func decodeByteSlice(d *decodeState, kind int, value reflect.Value) {
var p []byte
switch kind {
default:
d.saveErrorAndSkip(kind, value.Type())
return
case kindBinary:
p, _ = d.scanBinary()
}
v := value.(*reflect.SliceValue)
if v.IsNil() || v.Cap() < len(p) {
v.Set(reflect.MakeSlice(v.Type().(*reflect.SliceType), len(p), len(p)))
} else {
v.SetLen(len(p))
}
reflect.Copy(v, reflect.NewValue(p).(reflect.ArrayOrSliceValue))
}
func decodeBool(d *decodeState, kind int, value reflect.Value) {
var b bool
switch kind {
default:
d.saveErrorAndSkip(kind, value.Type())
return
case kindBool:
b = d.scanBool()
case kindInt32:
b = d.scanInt32() != 0
case kindInt64:
b = d.scanInt64() != 0
}
value.(*reflect.BoolValue).Set(b)
}
func decodeMinMax(d *decodeState, kind int, value reflect.Value) {
var n int64
switch kind {
default:
d.saveError(&DecodeConvertError{kind, value.Type()})
return
case kindMaxValue:
n = 1
case kindMinValue:
n = -1
}
value.(*reflect.IntValue).Set(n)
}
func decodeMapStringInterface(d *decodeState, kind int, value reflect.Value) {
if kind != kindDocument {
d.saveErrorAndSkip(kind, value.Type())
}
v := value.(*reflect.MapValue)
if v.IsNil() {
t := v.Type().(*reflect.MapType)
v.SetValue(reflect.MakeMap(t))
}
m := v.Interface().(map[string]interface{})
offset := d.beginDoc()
for {
kind, name := d.scanKindName()
if kind == 0 {
break
}
m[string(name)] = d.decodeValueInterface(kind)
}
d.endDoc(offset)
}
func decodeMap(d *decodeState, kind int, value reflect.Value) {
t := value.Type().(*reflect.MapType)
if t.Key().Kind() != reflect.String || kind != kindDocument {
d.saveErrorAndSkip(kind, value.Type())
return
}
v := value.(*reflect.MapValue)
if v.IsNil() {
v.SetValue(reflect.MakeMap(t))
}
offset := d.beginDoc()
for {
kind, name := d.scanKindName()
if kind == 0 {
break
}
subv := reflect.MakeZero(t.Elem())
d.decodeValue(kind, subv)
v.SetElem(reflect.NewValue(string(name)), subv)
}
d.endDoc(offset)
}
func decodeSlice(d *decodeState, kind int, value reflect.Value) {
if kind != kindArray {
d.saveErrorAndSkip(kind, value.Type())
return
}
v := value.(*reflect.SliceValue)
t := v.Type().(*reflect.SliceType)
offset := d.beginDoc()
i := 0
for {
kind, _ := d.scanKindName()
if kind == 0 {
break
}
if i >= v.Cap() {
newcap := v.Cap() + v.Cap()/2
if newcap < 4 {
newcap = 4
}
newv := reflect.MakeSlice(t, v.Len(), newcap)
reflect.Copy(newv, v)
v.Set(newv)
}
if i >= v.Len() {
v.SetLen(i + 1)
}
d.decodeValue(kind, v.Elem(i))
i += 1
}
d.endDoc(offset)
}
func decodeArray(d *decodeState, kind int, value reflect.Value) {
if kind != kindArray {
d.saveErrorAndSkip(kind, value.Type())
return
}
v := value.(*reflect.ArrayValue)
offset := d.beginDoc()
i := 0
for {
kind, _ := d.scanKindName()
if kind == 0 {
break
}
if i < v.Len() {
d.decodeValue(kind, v.Elem(i))
} else {
d.skipValue(kind)
}
i += 1
}
d.endDoc(offset)
}
func decodeStruct(d *decodeState, kind int, value reflect.Value) {
v := value.(*reflect.StructValue)
t := v.Type().(*reflect.StructType)
m := compileStruct(t)
offset := d.beginDoc()
for {
kind, name := d.scanKindName()
if kind == 0 {
break
}
if kind == kindNull {
continue
}
if f, ok := m[string(name)]; ok {
d.decodeValue(kind, v.FieldByIndex(f.Index))
} else {
d.skipValue(kind)
}
}
d.endDoc(offset)
}
func decodeInterface(d *decodeState, kind int, value reflect.Value) {
value.(*reflect.InterfaceValue).Set(reflect.NewValue(d.decodeValueInterface(kind)))
}
func (d *decodeState) decodeValueInterface(kind int) interface{} {
switch kind {
case kindFloat:
return d.scanFloat()
case kindString:
return d.scanString()
case kindDocument:
m := make(map[string]interface{})
offset := d.beginDoc()
for {
kind, name := d.scanKindName()
if kind == 0 {
break
}
m[string(name)] = d.decodeValueInterface(kind)
}
d.endDoc(offset)
return m
case kindArray:
var a []interface{}
offset := d.beginDoc()
for {
kind, _ := d.scanKindName()
if kind == 0 {
break
}
a = append(a, d.decodeValueInterface(kind))
}
d.endDoc(offset)
return a
case kindBinary:
p, _ := d.scanBinary()
newp := make([]byte, len(p))
copy(newp, p)
return newp
case kindObjectId:
p := d.scanSlice(12)
oid := ObjectId{}
copy(oid[:], p)
return oid
case kindBool:
return d.scanBool()
case kindDateTime:
return DateTime(d.scanInt64())
case kindNull:
return nil
case kindSymbol:
return Symbol(d.scanString())
case kindInt32:
return int(d.scanInt32())
case kindTimestamp:
return Timestamp(d.scanInt64())
case kindInt64:
return d.scanInt64()
case kindMinValue:
return MinValue
case kindMaxValue:
return MaxValue
default:
d.abort(&DecodeTypeError{kind})
}
return nil
}
func (d *decodeState) skipValue(kind int) {
switch kind {
case kindString, kindSymbol:
n := int(d.scanInt32())
d.offset += n
case kindDocument, kindArray:
n := int(d.scanInt32())
d.offset += n - 4
case kindBinary:
n := int(d.scanInt32())
d.offset += n + 1
case kindObjectId:
d.offset += 12
case kindBool:
d.offset += 1
case kindDateTime, kindTimestamp, kindInt64, kindFloat:
d.offset += 8
case kindInt32:
d.offset += 4
case kindMinValue, kindMaxValue, kindNull:
d.offset += 0
default:
d.abort(&DecodeTypeError{kind})
}
}
type decoderFunc func(e *decodeState, kind int, v reflect.Value)
var kindDecoder map[reflect.Kind]decoderFunc
var typeDecoder map[reflect.Type]decoderFunc
func init() {
kindDecoder = map[reflect.Kind]decoderFunc{
reflect.Bool: decodeBool,
reflect.Float32: decodeFloat,
reflect.Float64: decodeFloat,
reflect.Int32: decodeInt,
reflect.Int64: decodeInt,
reflect.Int: decodeInt,
reflect.Interface: decodeInterface,
reflect.Map: decodeMap,
reflect.String: decodeString,
reflect.Struct: decodeStruct,
reflect.Slice: decodeSlice,
reflect.Array: decodeArray,
}
typeDecoder = map[reflect.Type]decoderFunc{
reflect.Typeof(BSONData{}): decodeBSONData,
reflect.Typeof(DateTime(0)): decodeDateTime,
reflect.Typeof(MinMax(0)): decodeMinMax,
reflect.Typeof(ObjectId{}): decodeObjectId,
reflect.Typeof(Symbol("")): decodeString,
reflect.Typeof(Timestamp(0)): decodeTimestamp,
reflect.Typeof([]byte{}): decodeByteSlice,
reflect.Typeof(make(map[string]interface{})): decodeMapStringInterface,
}
}