-
Notifications
You must be signed in to change notification settings - Fork 8
/
simple_tree_walker.go
71 lines (58 loc) · 1.74 KB
/
simple_tree_walker.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
//go:build !tinywasm
package gtree
// WalkerNode is used in user-defined function that can be executed with Walk/WalkProgrammably function.
type WalkerNode struct {
origin *Node
}
// Name returns name of node in completed tree structure.
func (wn *WalkerNode) Name() string {
return wn.origin.name
}
// Branch returns branch of node in completed tree structure.
func (wn *WalkerNode) Branch() string {
return wn.origin.branch()
}
// Row returns row of node in completed tree structure.
func (wn *WalkerNode) Row() string {
if !wn.origin.isRoot() {
return wn.origin.branch() + " " + wn.origin.name
}
return wn.origin.name
}
// Level returns level of node in completed tree structure.
func (wn *WalkerNode) Level() uint {
return wn.origin.hierarchy
}
// Path returns path of node in completed tree structure.
// Path is the path from the root node to this node.
// The separator is / in any OS execution environment.
func (wn *WalkerNode) Path() string {
return wn.origin.path()
}
// HasChild returns whether the node in completed tree structure has child nodes.
func (wn *WalkerNode) HasChild() bool {
return wn.origin.hasChild()
}
func newWalkerSimple() walkerSimple {
return &defaultWalkerSimple{}
}
type defaultWalkerSimple struct{}
func (dw *defaultWalkerSimple) walk(roots []*Node, callback func(*WalkerNode) error) error {
for _, root := range roots {
if err := dw.walkNode(root, callback); err != nil {
return err
}
}
return nil
}
func (dw *defaultWalkerSimple) walkNode(current *Node, callback func(*WalkerNode) error) error {
if err := callback(&WalkerNode{origin: current}); err != nil {
return err
}
for _, child := range current.children {
if err := dw.walkNode(child, callback); err != nil {
return err
}
}
return nil
}