-
Notifications
You must be signed in to change notification settings - Fork 9
/
resolver.go
257 lines (226 loc) · 6.67 KB
/
resolver.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
package firestorm
import (
"cloud.google.com/go/firestore"
"context"
"reflect"
"strings"
)
type entityMap = map[string]interface{}
type refSet map[string]*firestore.DocumentRef
type resolveFunc func(m entityMap, ref *firestore.DocumentRef)
// AllEntities loads all paths on the struct see: SetLoadPaths
const AllEntities = "ALL"
var refType = reflect.TypeOf((*firestore.DocumentRef)(nil))
var entityType = reflect.TypeOf((entityMap)(nil))
type refCollector struct {
r *resolver
targetsToResolve map[string][]resolveFunc // func that ads the result to the target
refs refSet // refs to resolve
nfRefs map[string]*firestore.DocumentRef // not found refs
}
func (r *resolver) NewRefCollector() *refCollector {
return &refCollector{r, make(map[string][]resolveFunc), make(refSet), make(map[string]*firestore.DocumentRef)}
}
func (c *refCollector) Append(m entityMap, key string, ref *firestore.DocumentRef) {
if e, ok := c.r.loaded[ref.Path]; ok {
// it should be safe to modify although I think the spec is ambiguous
// see: https://github.com/golang/go/issues/9926
m[key] = e
} else {
resolveFunc := func(childM entityMap, childRef *firestore.DocumentRef) {
m[key] = childM
}
c.targetsToResolve[ref.Path] = append(c.targetsToResolve[ref.Path], resolveFunc)
c.refs[ref.Path] = ref
}
}
func (c *refCollector) AppendSlice(m entityMap, key string, refs []*firestore.DocumentRef) {
targetSlice := make([]entityMap, len(refs))
// it should be safe to modify although I think the spec is ambiguous
// see: https://github.com/golang/go/issues/9926
m[key] = targetSlice
for i, ref := range refs {
if e, ok := c.r.loaded[ref.Path]; ok {
targetSlice[i] = e
} else {
index := i // save index in closure
resolveFunc := func(childM entityMap, childRef *firestore.DocumentRef) {
targetSlice[index] = childM
}
c.targetsToResolve[ref.Path] = append(c.targetsToResolve[ref.Path], resolveFunc)
c.refs[ref.Path] = ref
}
}
}
// resolves the elements matching the ref and removes them
func (c *refCollector) resolve(m entityMap, ref *firestore.DocumentRef) {
if targets, ok := c.targetsToResolve[ref.Path]; ok {
for _, target := range targets {
target(m, ref)
}
}
}
func (c *refCollector) getRefs() []*firestore.DocumentRef {
result := make([]*firestore.DocumentRef, 0, len(c.refs))
for _, v := range c.refs {
result = append(result, v)
}
return result
}
func (c *refCollector) AppendNotResolved(ref *firestore.DocumentRef) {
c.nfRefs[ref.Path] = ref
}
func (c *refCollector) getErrors() error {
if len(c.nfRefs) > 0 {
return newNotFoundError(c.nfRefs)
}
return nil
}
type resolver struct {
fsc *FSClient
resolved map[string]entityMap
loaded map[string]entityMap
paths []string
}
func newResolver(fsc *FSClient, paths ...string) *resolver {
return &resolver{fsc, make(map[string]entityMap), make(map[string]entityMap), paths}
}
func (r *resolver) ResolveCacheRef(ctx context.Context, crefs []cacheRef) ([]entityMap, error) {
result := make([]entityMap, len(crefs))
r.Loaded(crefs)
col := r.NewRefCollector()
for i, cref := range crefs {
m := cref.GetResult()
if len(m) != 0 {
r.resolveEntity(m, cref.Ref, col, r.paths...)
result[i] = m
} else {
col.AppendNotResolved(cref.Ref)
}
}
if err := r.resolveChildren(ctx, col, r.paths...); err != nil {
return nil, err
}
return result, col.getErrors()
}
func (r *resolver) ResolveDocs(ctx context.Context, docs []*firestore.DocumentSnapshot) ([]entityMap, error) {
result := make([]entityMap, len(docs))
col := r.NewRefCollector()
for i, doc := range docs {
if doc.Exists() {
m := doc.Data()
r.resolveEntity(m, doc.Ref, col, r.paths...)
result[i] = m
} else {
col.AppendNotResolved(doc.Ref)
}
}
if err := r.resolveChildren(ctx, col, r.paths...); err != nil {
return nil, err
}
return result, col.getErrors()
}
func (r *resolver) resolveChildren(ctx context.Context, col *refCollector, paths ...string) error {
// base case stop recursion when no more children are present
refs := col.getRefs()
if len(refs) == 0 {
return nil
}
// cut off the first path in the paths list
nextPaths := make([]string, 0, len(paths))
for _, v := range paths {
split := strings.Split(v, ".")
if len(split) > 1 {
nextPaths = append(nextPaths, split[1])
}
}
// now query the DB
crefs, err := r.fsc.getCachedEntities(ctx, refs)
if err != nil {
return err
}
r.Loaded(crefs)
childCol := r.NewRefCollector()
for _, cref := range crefs {
result := cref.GetResult()
if len(result) == 0 { // add not found refs
col.AppendNotResolved(cref.Ref)
continue
}
r.resolveEntity(result, cref.Ref, childCol, nextPaths...)
col.resolve(result, cref.Ref)
}
return r.resolveChildren(ctx, childCol, nextPaths...)
}
func (r *resolver) resolveEntity(m entityMap, ref *firestore.DocumentRef, col *refCollector, paths ...string) {
// only resolve it once
if ref != nil {
if _, ok := r.resolved[ref.Path]; ok {
return
}
r.resolved[ref.Path] = m
m[r.fsc.IDKey] = ref.ID
//m["createtime"] = doc.CreateTime
//m["updatetime"] = doc.UpdateTime
//m["readtime"] = doc.ReadTime
}
for k, v := range m {
switch val := v.(type) {
case *firestore.DocumentRef:
if r.contains(k, paths...) {
col.Append(m, k, val)
} else {
delete(m, k)
}
default:
valOf := reflect.ValueOf(v)
switch valOf.Kind() {
case reflect.Map:
if valOf.Len() > 0 && valOf.Type() == entityType {
r.resolveEntity(v.(entityMap), nil, col, paths...)
}
case reflect.Slice:
if valOf.Len() > 0 {
first := valOf.Index(0)
// from firestore the type of slice is interface
if first.Kind() == reflect.Interface {
first = first.Elem()
}
//fmt.Printf("kind: %v type: %v \n", first.Kind(), first.Type())
if first.Kind() == reflect.Map {
for i := 0; i < valOf.Len(); i++ {
r.resolveEntity(valOf.Index(i).Interface().(entityMap), nil, col, paths...)
}
} else if first.Type() == refType {
if !r.contains(k, paths...) {
delete(m, k)
continue
}
refs := make([]*firestore.DocumentRef, valOf.Len())
for i := 0; i < valOf.Len(); i++ {
fromEmlPtr := valOf.Index(i)
refs[i] = fromEmlPtr.Interface().(*firestore.DocumentRef)
}
col.AppendSlice(m, k, refs)
}
}
}
}
}
}
func (r *resolver) contains(find string, paths ...string) bool {
if find == r.fsc.ParentKey {
return true
}
for _, a := range paths {
if a == AllEntities || strings.Index(a, find) == 0 {
return true
}
}
return false
}
func (r *resolver) Loaded(refs []cacheRef) {
for _, v := range refs {
r.loaded[v.Ref.Path] = v.GetResult()
}
}