forked from mongodb/mongo-go-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unmarshal.go
68 lines (56 loc) · 1.91 KB
/
unmarshal.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
package bson
import "sync"
// This pool is used to keep the allocations of Decoders down. This is only used for the Marshal*
// methods and is not consumable from outside of this package. The Encoders retrieved from this pool
// must have both Reset and SetRegistry called on them.
var decPool = sync.Pool{
New: func() interface{} {
return new(Decoderv2)
},
}
// Unmarshalv2 parses the BSON-encoded data and stores the result in the value
// pointed to by val. If val is nil or not a pointer, Unmarshal returns
// InvalidUnmarshalError.
func Unmarshalv2(data []byte, val interface{}) error {
return UnmarshalWithRegistry(defaultRegistry, data, val)
}
// UnmarshalWithRegistry parses the BSON-encoded data using Registry r and
// stores the result in the value pointed to by val. If val is nil or not
// a pointer, UnmarshalWithRegistry returns InvalidUnmarshalError.
func UnmarshalWithRegistry(r *Registry, data []byte, val interface{}) error {
vr := newValueReader(data)
dec := decPool.Get().(*Decoderv2)
defer decPool.Put(dec)
err := dec.Reset(vr)
if err != nil {
return err
}
err = dec.SetRegistry(r)
if err != nil {
return err
}
return dec.Decode(val)
}
// UnmarshalDocumentv2 parses the *Document and stores the result in the value pointed to by val. If
// val is nil or not a pointer, UnmarshalDocument returns InvalidUnmarshalError.
func UnmarshalDocumentv2(d *Document, val interface{}) error {
return UnmarshalDocumentWithRegistry(defaultRegistry, d, val)
}
// UnmarshalDocumentWithRegistry behaves the same as UnmarshalDocument but uses r as the *Registry.
func UnmarshalDocumentWithRegistry(r *Registry, d *Document, val interface{}) error {
dvr, err := NewDocumentValueReader(d)
if err != nil {
return err
}
dec := decPool.Get().(*Decoderv2)
defer decPool.Put(dec)
err = dec.Reset(dvr)
if err != nil {
return err
}
err = dec.SetRegistry(r)
if err != nil {
return err
}
return dec.Decode(val)
}