/
eager_load.go
409 lines (352 loc) · 11.5 KB
/
eager_load.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
package queries
import (
"context"
"database/sql"
"reflect"
"strings"
"github.com/friendsofgo/errors"
"github.com/volatiletech/sqlboiler/v4/boil"
"github.com/volatiletech/strmangle"
)
type loadRelationshipState struct {
ctx context.Context
exec boil.Executor
loaded map[string]struct{}
toLoad []string
mods map[string]Applicator
}
func (l loadRelationshipState) hasLoaded(depth int) bool {
_, ok := l.loaded[l.buildKey(depth)]
return ok
}
func (l loadRelationshipState) setLoaded(depth int) {
l.loaded[l.buildKey(depth)] = struct{}{}
}
func (l loadRelationshipState) buildKey(depth int) string {
buf := strmangle.GetBuffer()
for i, piece := range l.toLoad[:depth+1] {
if i != 0 {
buf.WriteByte('.')
}
buf.WriteString(piece)
}
str := buf.String()
strmangle.PutBuffer(buf)
return str
}
// eagerLoad loads all of the model's relationships
//
// toLoad should look like:
// []string{"Relationship", "Relationship.NestedRelationship"} ... etc
// obj should be one of:
// *[]*struct or *struct
// bkind should reflect what kind of thing it is above
func eagerLoad(ctx context.Context, exec boil.Executor, toLoad []string, mods map[string]Applicator, obj interface{}, bkind bindKind) error {
state := loadRelationshipState{
ctx: ctx, // defiant to the end, I know this is frowned upon
exec: exec,
loaded: map[string]struct{}{},
mods: mods,
}
for _, toLoad := range toLoad {
state.toLoad = strings.Split(toLoad, ".")
if err := state.loadRelationships(0, obj, bkind); err != nil {
return err
}
}
return nil
}
// loadRelationships dynamically calls the template generated eager load
// functions of the form:
//
// func (t *TableR) LoadRelationshipName(exec Executor, singular bool, obj interface{})
//
// The arguments to this function are:
// - t is not considered here, and is always passed nil. The function exists on a loaded
// struct to avoid a circular dependency with boil, and the receiver is ignored.
// - exec is used to perform additional queries that might be required for loading the relationships.
// - bkind is passed in to identify whether or not this was a single object
// or a slice that must be loaded into.
// - obj is the object or slice of objects, always of the type *obj or *[]*obj as per bind.
//
// We start with a normal select before eager loading anything: select * from a;
// Then we start eager loading things, it can be represented by a DAG
// a1, a2 select id, a_id from b where id in (a1, a2)
// / | \
// b1 b2 b3 select id, b_id from c where id in (b2, b3, b4)
// / | \ \
// c1 c2 c3 c4
//
// That's to say that we descend the graph of relationships, and at each level
// we gather all the things up we want to load into, load them, and then move
// to the next level of the graph.
func (l loadRelationshipState) loadRelationships(depth int, obj interface{}, bkind bindKind) error {
typ := reflect.TypeOf(obj).Elem()
if bkind == kindPtrSliceStruct {
typ = typ.Elem().Elem()
}
loadingFrom := reflect.ValueOf(obj)
if loadingFrom.IsNil() {
return nil
}
if !l.hasLoaded(depth) {
if err := l.callLoadFunction(depth, loadingFrom, typ, bkind); err != nil {
return err
}
}
// Check if we can stop
if depth+1 >= len(l.toLoad) {
return nil
}
// *[]*struct -> []*struct
// *struct -> struct
loadingFrom = reflect.Indirect(loadingFrom)
// If it's singular we can just immediately call without looping
if bkind == kindStruct {
return l.loadRelationshipsRecurse(depth, loadingFrom)
}
// If we were an empty slice to begin with, bail, probably a useless check
if loadingFrom.Len() == 0 {
return nil
}
// Collect eagerly loaded things to send into next eager load call
slice, nextBKind, err := collectLoaded(l.toLoad[depth], loadingFrom)
if err != nil {
return err
}
// If we could collect nothing we're done
if slice.Len() == 0 {
return nil
}
ptr := reflect.New(slice.Type())
ptr.Elem().Set(slice)
return l.loadRelationships(depth+1, ptr.Interface(), nextBKind)
}
// callLoadFunction finds the loader struct, finds the method that we need
// to call and calls it.
func (l loadRelationshipState) callLoadFunction(depth int, loadingFrom reflect.Value, typ reflect.Type, bkind bindKind) error {
current := l.toLoad[depth]
ln, found := typ.FieldByName(loaderStructName)
// It's possible a Loaders struct doesn't exist on the struct.
if !found {
return errors.Errorf("attempted to load %s but no L struct was found", current)
}
// Attempt to find the LoadRelationshipName function
loadMethod, found := ln.Type.MethodByName(loadMethodPrefix + current)
if !found {
return errors.Errorf("could not find %s%s method for eager loading", loadMethodPrefix, current)
}
ctxArg := reflect.ValueOf(l.ctx)
// Hack to allow nil executors
execArg := reflect.ValueOf(l.exec)
if !execArg.IsValid() {
execArg = reflect.ValueOf((*sql.DB)(nil))
}
// Get a loader instance from anything we have, *struct, or *[]*struct
val := reflect.Indirect(loadingFrom)
if bkind == kindPtrSliceStruct {
if val.Len() == 0 {
return nil
}
val = val.Index(0)
if val.IsNil() {
return nil
}
val = reflect.Indirect(val)
}
methodArgs := make([]reflect.Value, 0, 5)
methodArgs = append(methodArgs, val.FieldByName(loaderStructName))
if ctxArg.IsValid() {
methodArgs = append(methodArgs, ctxArg)
}
methodArgs = append(methodArgs, execArg, reflect.ValueOf(bkind == kindStruct), loadingFrom)
if mods, ok := l.mods[l.buildKey(depth)]; ok {
methodArgs = append(methodArgs, reflect.ValueOf(mods))
} else {
methodArgs = append(methodArgs, applicatorSentinelVal)
}
ret := loadMethod.Func.Call(methodArgs)
if intf := ret[0].Interface(); intf != nil {
return errors.Wrapf(intf.(error), "failed to eager load %s", current)
}
l.setLoaded(depth)
return nil
}
// loadRelationshipsRecurse is a helper function for taking a reflect.Value and
// Basically calls loadRelationships with: obj.R.EagerLoadedObj
// Called with an obj of *struct
func (l loadRelationshipState) loadRelationshipsRecurse(depth int, obj reflect.Value) error {
key := l.toLoad[depth]
r, err := findRelationshipStruct(obj)
if err != nil {
return errors.Wrapf(err, "failed to append loaded %s", key)
}
loadedObject := reflect.Indirect(r).FieldByName(key)
if loadedObject.IsNil() {
return nil
}
bkind := kindStruct
if derefed := reflect.Indirect(loadedObject); derefed.Kind() != reflect.Struct {
bkind = kindPtrSliceStruct
// Convert away any helper slice types
// elemType is *elem (from []*elem or helperSliceType)
// sliceType is *[]*elem
elemType := derefed.Type().Elem()
sliceType := reflect.PtrTo(reflect.SliceOf(elemType))
loadedObject = loadedObject.Addr().Convert(sliceType)
}
return l.loadRelationships(depth+1, loadedObject.Interface(), bkind)
}
// collectLoaded traverses the next level of the graph and picks up all
// the values that we need for the next eager load query.
//
// For example when loadingFrom is [parent1, parent2]
//
// parent1 -> child1
// \-> child2
// parent2 -> child3
//
// This should return [child1, child2, child3]
func collectLoaded(key string, loadingFrom reflect.Value) (reflect.Value, bindKind, error) {
// Pull the first one so we can get the types out of it in order to
// create the proper type of slice.
current := reflect.Indirect(loadingFrom.Index(0))
lnFrom := loadingFrom.Len()
r, err := findRelationshipStruct(current)
if err != nil {
return reflect.Value{}, 0, errors.Wrapf(err, "failed to collect loaded %s", key)
}
loadedObject := reflect.Indirect(r).FieldByName(key)
loadedType := loadedObject.Type() // Should be *obj or []*obj
bkind := kindPtrSliceStruct
if loadedType.Elem().Kind() == reflect.Struct {
bkind = kindStruct
loadedType = reflect.SliceOf(loadedType)
} else {
// Ensure that we get rid of all the helper "XSlice" types
loadedType = reflect.SliceOf(loadedType.Elem())
}
collection := reflect.MakeSlice(loadedType, 0, 0)
i := 0
for {
switch bkind {
case kindStruct:
if !loadedObject.IsNil() {
collection = reflect.Append(collection, loadedObject)
}
case kindPtrSliceStruct:
collection = reflect.AppendSlice(collection, loadedObject)
}
i++
if i >= lnFrom {
break
}
current = reflect.Indirect(loadingFrom.Index(i))
r, err = findRelationshipStruct(current)
if err != nil {
return reflect.Value{}, 0, errors.Wrapf(err, "failed to collect loaded %s", key)
}
loadedObject = reflect.Indirect(r).FieldByName(key)
}
return collection, kindPtrSliceStruct, nil
}
func findRelationshipStruct(obj reflect.Value) (reflect.Value, error) {
relationshipStruct := obj.FieldByName(relationshipStructName)
if !relationshipStruct.IsValid() {
return reflect.Value{}, errors.New("relationship struct was invalid")
} else if relationshipStruct.IsNil() {
return reflect.Value{}, errors.New("relationship struct was nil")
}
return relationshipStruct, nil
}
var (
applicatorSentinel Applicator
applicatorSentinelVal = reflect.ValueOf(&applicatorSentinel).Elem()
)
// SetFromEmbeddedStruct sets `to` value from embedded struct
// of the `from` struct or slice of structs.
// Expects `to` and `from` to be a pair of pre-allocated **struct or *[]*struct.
// Returns false if types do not match.
func SetFromEmbeddedStruct(to interface{}, from interface{}) bool {
toPtrVal := reflect.ValueOf(to)
fromPtrVal := reflect.ValueOf(from)
if toPtrVal.Kind() != reflect.Ptr || fromPtrVal.Kind() != reflect.Ptr {
return false
}
toStructTyp, ok := singularStructType(to)
if !ok {
return false
}
fromStructTyp, ok := singularStructType(from)
if !ok {
return false
}
fieldNum, ok := embeddedStructFieldNum(fromStructTyp, toStructTyp)
if !ok {
return false
}
toVal := toPtrVal.Elem()
if toVal.Kind() == reflect.Interface {
toVal = reflect.ValueOf(toVal.Interface())
}
fromVal := fromPtrVal.Elem()
if fromVal.Kind() == reflect.Interface {
fromVal = reflect.ValueOf(fromVal.Interface())
}
if toVal.Kind() == reflect.Ptr && toVal.Elem().Kind() == reflect.Struct &&
fromVal.Kind() == reflect.Ptr && fromVal.Elem().Kind() == reflect.Struct {
toVal.Set(fromVal.Elem().Field(fieldNum).Addr())
return true
}
toKind := toPtrVal.Type().Elem().Kind()
fromKind := fromPtrVal.Type().Elem().Kind()
if toKind == reflect.Slice && fromKind == reflect.Slice {
toSlice := reflect.MakeSlice(toVal.Type(), fromVal.Len(), fromVal.Len())
for i := 0; i < fromVal.Len(); i++ {
toSlice.Index(i).Set(fromVal.Index(i).Elem().Field(fieldNum).Addr())
}
toVal.Set(toSlice)
return true
}
return false
}
// singularStructType returns singular struct type
// from **struct or *[]*struct types.
// Used for Load* methods during binding.
func singularStructType(obj interface{}) (reflect.Type, bool) {
val := reflect.Indirect(reflect.ValueOf(obj))
if val.Kind() == reflect.Interface {
val = reflect.ValueOf(val.Interface())
}
typ := val.Type()
inSlice := false
SWITCH:
switch typ.Kind() {
case reflect.Ptr:
typ = typ.Elem()
goto SWITCH
case reflect.Slice:
if inSlice {
// Slices inside other slices are not supported
return nil, false
}
inSlice = true
typ = typ.Elem()
goto SWITCH
case reflect.Struct:
return typ, true
default:
return nil, false
}
}
// embeddedStructFieldNum returns the index of embedded struct field of type `emb` inside `obj` struct.
func embeddedStructFieldNum(obj reflect.Type, emb reflect.Type) (int, bool) {
for i := 0; i < obj.NumField(); i++ {
v := obj.Field(i)
if v.Type.Kind() == reflect.Struct &&
v.Anonymous && v.Type == emb {
return i, true
}
}
return 0, false
}