/
milestone.go
63 lines (55 loc) · 1.11 KB
/
milestone.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
package authenticationflow
// Milestone is a marker.
// The designed use case is to find out whether a particular milestone exists
// in the flow, or any of its subflows.
type Milestone interface {
Milestone()
}
func FindMilestone[T Milestone](w *Flow) (T, bool) {
var t T
found := false
err := TraverseFlow(Traverser{
NodeSimple: func(nodeSimple NodeSimple, _ *Flow) error {
if m, ok := nodeSimple.(T); ok {
t = m
found = true
}
return nil
},
Intent: func(intent Intent, w *Flow) error {
if m, ok := intent.(T); ok {
t = m
found = true
}
return nil
},
}, w)
if err != nil {
return *new(T), false
}
if !found {
return *new(T), false
}
return t, true
}
func FindAllMilestones[T Milestone](w *Flow) []T {
var ts []T
err := TraverseFlow(Traverser{
NodeSimple: func(nodeSimple NodeSimple, _ *Flow) error {
if m, ok := nodeSimple.(T); ok {
ts = append(ts, m)
}
return nil
},
Intent: func(intent Intent, w *Flow) error {
if m, ok := intent.(T); ok {
ts = append(ts, m)
}
return nil
},
}, w)
if err != nil {
panic(err)
}
return ts
}