This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
loader.go
72 lines (66 loc) · 1.55 KB
/
loader.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
package layer
import (
"context"
"errors"
)
type Loader func(context.Context, ...ID) (List, error)
type LoaderByScene func(context.Context, SceneID) (List, error)
var WalkerSkipChildren = errors.New("LAYER_WALKER_SKIP_CHILDREN")
func LoaderFrom(data []Layer) Loader {
return func(ctx context.Context, ids ...ID) (List, error) {
res := make([]*Layer, 0, len(ids))
for _, i := range ids {
found := false
for _, d := range data {
if i == d.ID() {
res = append(res, &d)
found = true
break
}
}
if !found {
res = append(res, nil)
}
}
return res, nil
}
}
func LoaderFromMap(data map[ID]Layer) Loader {
return func(ctx context.Context, ids ...ID) (List, error) {
res := make([]*Layer, 0, len(ids))
for _, i := range ids {
if d, ok := data[i]; ok {
res = append(res, &d)
} else {
res = append(res, nil)
}
}
return res, nil
}
}
func (l Loader) Walk(ctx context.Context, walker func(Layer, GroupList) error, init []ID) error {
var walk func(ids []ID, parents GroupList) error
walk = func(ids []ID, parents GroupList) error {
loaded, err := l(ctx, ids...)
if err != nil {
return err
}
for _, l := range loaded.Deref() {
if l == nil {
continue
}
if err := walker(l, parents); err == WalkerSkipChildren {
continue
} else if err != nil {
return err
}
if lg := ToLayerGroup(l); lg != nil && lg.Layers().LayerCount() > 0 {
if err := walk(lg.Layers().Layers(), append(parents, lg)); err != nil {
return err
}
}
}
return nil
}
return walk(init, nil)
}