forked from stephenafamo/bob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload.go
62 lines (51 loc) · 1.55 KB
/
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
package bob
import (
"context"
"github.com/stephenafamo/scan"
)
type (
// Loadable is an object that has loaders
// if a query implements this interface, the loaders are called
// after executing the query
Loadable interface {
GetLoaders() []Loader
}
// Loader is an object that is called after the main query is performed
// when called from [Exec], retrieved is nil
// when called from [One], retrieved is the retrieved object
// when called from [All], retrieved is a slice retrieved objects
// this is used for loading relationships
Loader interface {
Load(ctx context.Context, exec Executor, retrieved any) error
}
)
// Load is an embeddable struct that enables Preloading and AfterLoading
type Load[Q any] struct {
loadContext context.Context
loadFuncs []Loader
preloadMapperMods []scan.MapperMod
}
// GetLoadContext
func (l *Load[Q]) GetLoadContext() context.Context {
return l.loadContext
}
// SetLoadContext
func (l *Load[Q]) SetLoadContext(ctx context.Context) {
l.loadContext = ctx
}
// GetMapperMods implements the [MapperModder] interface
func (l *Load[Q]) GetMapperMods() []scan.MapperMod {
return l.preloadMapperMods
}
// AppendMapperMod adds to the query's mapper mods
func (l *Load[Q]) AppendMapperMod(f scan.MapperMod) {
l.preloadMapperMods = append(l.preloadMapperMods, f)
}
// GetLoaders implements the [Loadable] interface
func (l *Load[Q]) GetLoaders() []Loader {
return l.loadFuncs
}
// AppendLoader add to the query's loaders
func (l *Load[Q]) AppendLoader(f ...Loader) {
l.loadFuncs = append(l.loadFuncs, f...)
}