-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
59 lines (47 loc) · 1.1 KB
/
utils.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
package graph
import (
"context"
"errors"
)
type (
// used to provide human readable diagnostic output
DebugFunc func(string, ...any)
// calls F on current node, selected and err results from S
DebugSelector[T Direction] struct {
F DebugFunc
S Selector[T]
}
// calls F on current node, error from V
DebugVisitor struct {
F DebugFunc
V Visitor
}
// provides current node to every Visitor in slice
VisitorArray []Visitor
)
func (v VisitorArray) Visit(ctx context.Context, node Node) error {
var errs []error
for i := range v {
if err := v[i].Visit(ctx, node); err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
}
func (d DebugVisitor) Visit(ctx context.Context, node Node) error {
d.F("visiting node %s", node)
err := d.V.Visit(ctx, node)
if err != nil {
d.F("error on visit to %s: %s", node, err)
}
return err
}
func (d DebugSelector[T]) Select(g Directed, n Node) ([]T, error) {
d.F("selecting from %s", n)
selected, err := d.S.Select(g, n)
if err != nil {
d.F("error while selecting: %s", err)
}
d.F("selected %+v", selected)
return selected, err
}