-
Notifications
You must be signed in to change notification settings - Fork 0
/
unmarshal.go
121 lines (101 loc) · 2.66 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
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
package birch
import (
"fmt"
"github.com/tychoish/fun/erc"
)
// DocumentMap is a map-based view of a document, used for
// key-lookups, and providing another option in document construction.
type DocumentMap map[string]*Element
// Copy returns a new map with the same values as the source map, but
// in a different map object that callers have ownership over.
func (dm DocumentMap) Copy() DocumentMap {
out := make(DocumentMap, len(dm))
for key := range dm {
out[key] = dm[key]
}
return out
}
// Validate checks the validity of each element in the DocumentMap,
// and ensures that every key in the map matches the key value of the
// element itself.
func (dm DocumentMap) Validate() error {
ec := &erc.Collector{}
for key := range dm {
elem := dm[key]
ekey := elem.Key()
erc.Whenf(ec, ekey != key, "map key %q not equal to element key %q", key, ekey)
if _, err := elem.Validate(); err != nil {
ec.Add(fmt.Errorf("for mapKey=%q, invalid element: %w", ekey, err))
}
}
return ec.Resolve()
}
func (dm DocumentMap) MarshalDocument() (*Document, error) {
if err := dm.Validate(); err != nil {
return nil, err
}
doc := DC.Make(len(dm))
for key := range dm {
doc.Append(dm[key])
}
return doc, nil
}
func (dm DocumentMap) UnmarshalDocument(in *Document) error {
iter := in.Iterator()
for iter.Next(iterCtx) {
elem := iter.Value()
if _, err := elem.Validate(); err != nil {
return err
}
dm[elem.Key()] = elem
}
return iter.Close()
}
func (dm DocumentMap) MarshalBSON() ([]byte, error) {
doc, err := dm.MarshalDocument()
if err != nil {
return nil, err
}
return doc.MarshalBSON()
}
func (dm DocumentMap) UnmarshalBSON(in []byte) error {
iter, err := Reader(in).Iterator()
if err != nil {
return err
}
for iter.Next(iterCtx) {
elem := iter.Value()
if _, err := elem.Validate(); err != nil {
return err
}
dm[elem.Key()] = elem
}
return nil
}
// Map returns a map-based view of the document. If a key appears in a
// document more than once only the first occurrence is
// included. birch.Document's cache the document map, so as logn as
// the document doesn't change rebuilding the map is cheap.
//
// The object returned contains pointers to the underlying elements of
// the document, but is a copy of the cached map, and multiple calls
// to Map() will return different maps.
func (d *Document) Map() DocumentMap {
d.refreshCache()
return d.cache.Copy()
}
func (d *Document) refreshCache() {
if d.cacheValid {
return
}
out := make(DocumentMap, len(d.elems))
for idx := range d.elems {
key := d.elems[idx].Key()
if _, ok := out[key]; ok {
continue
}
out[key] = d.elems[idx]
}
d.cache = out
d.cacheValid = true
}