-
Notifications
You must be signed in to change notification settings - Fork 19
/
context_walk.go
41 lines (35 loc) · 946 Bytes
/
context_walk.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
package tools
import (
"github.com/nyaruka/goflow/excellent/types"
)
// ContextWalk traverses the given context invoking the callback for each non-nil value
func ContextWalk(context *types.XObject, callback func(types.XValue)) {
contextWalk(context, callback)
}
// ContextWalkObjects traverses the given context invoking the callback for each object found
func ContextWalkObjects(context *types.XObject, callback func(*types.XObject)) {
contextWalk(context, func(v types.XValue) {
switch typed := v.(type) {
case *types.XObject:
callback(typed)
}
})
}
func contextWalk(v types.XValue, callback func(types.XValue)) {
if types.IsNil(v) {
return
}
callback(v)
switch typed := v.(type) {
case *types.XObject:
for _, p := range typed.Properties() {
c, _ := typed.Get(p)
contextWalk(c, callback)
}
case *types.XArray:
for i := 0; i < typed.Count(); i++ {
c := typed.Get(i)
contextWalk(c, callback)
}
}
}