Repository navigation
Expand file tree
/
Copy pathtree.go
More file actions
206 lines (177 loc) · 5.87 KB
/
Copy pathtree.go
File metadata and controls
206 lines (177 loc) · 5.87 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Package tree provides functions for performing operations related
// to tree data structures.
//
// A website's page structure can be easily depicted as a tree, so these
// functions are used for storing pages in a tree structure as well as
// reading pages from it.
//
// The package exclusively uses terminology for tree data structures:
// https://en.wikipedia.org/wiki/Tree_(data_structure)#Terminology
package tree
import (
"errors"
"fmt"
)
var (
// ErrInvalidPath indicates that a path is not formally valid.
ErrInvalidPath = errors.New("path is not a valid tree path")
// ErrEdgeNotFound indicates that an edge cannot be found.
ErrEdgeNotFound = errors.New("edge does not exist")
)
// Node represents a tree node which contains a value and has zero or
// more child nodes connected via edges.
type Node interface {
// Children should return a map of child nodes. The map keys are
// expected to be edge names that link the node to its child.
Children() map[string]Node
// InitChild should initialize a new child node that is linked
// to the current node via the given edge name.
//
// This implies that a node initialized with InitChild can be
// loaded with Children afterwards, and is available under the
// edge name passed to CreateChild.
InitChild(edge string)
// CreateChild should create a new child node that is linked to
// the current node via the given edge name. Its behavior should
// be analogous to InitChild.
CreateChild(edge string, child Node)
}
// CreateNode stores a node under a given tree path. It follows the
// path starting from the root node and creates all required children
// using Node.CreateChild if they don't exist yet.
//
// Passing an invalid tree path to CreateNode will lead to undefined
// behavior. Check the path using IsValidPath first.
func CreateNode(path string, root Node, node Node) error {
if !IsValidPath(path) {
return fmt.Errorf("create node %s: %w", path, ErrInvalidPath)
}
if IsRootPath(path) {
return nil
}
n := root
edges := Edges(path)
for i, edge := range edges {
if _, exists := n.Children()[edge]; !exists {
// If the current edge is the last one of the tree path, this
// is the edge where the node has to be created.
if i == len(edges)-1 {
n.CreateChild(edge, node)
return nil
}
n.InitChild(edge)
}
n = n.Children()[edge]
}
return nil
}
// ResolveOrInitNode resolves the node under the given tree path or, if
// it doesn't exist yet, initializes a new node and returns that new node.
//
// This is convenient in situations where you would create a new node if
// it does not exist yet or load it from the tree if it does exist - but
// without the need to try ResolveNode first and call CreateNode if the
// node cannot be resolved.
//
// ResolveOrInitNode creates the entire path including all edges.
func ResolveOrInitNode(path string, root Node) (Node, error) {
if !IsValidPath(path) {
return nil, fmt.Errorf("create node %s: %w", path, ErrInvalidPath)
}
if IsRootPath(path) {
return root, nil
}
n := root
for _, edge := range Edges(path) {
if _, exists := n.Children()[edge]; !exists {
n.InitChild(edge)
}
n = n.Children()[edge]
}
return n, nil
}
// ResolveNode follows the given path starting from the root node and
// traverses all child nodes until the last edge is reached.
//
// Returns the node linked to the last edge in the path or an error if
// it cannot be resolved. Passing an invalid tree path to ResolveNode
// will lead to undefined behavior. Check the path using IsValidPath
// first.
func ResolveNode(path string, root Node) (Node, error) {
if !IsValidPath(path) {
return nil, fmt.Errorf("resolve node %s: %w", path, ErrInvalidPath)
}
if IsRootPath(path) {
return root, nil
}
n := root
for _, edge := range Edges(path) {
// Stop traversing the tree when an edge cannot be found.
if _, exists := n.Children()[edge]; !exists {
return nil, fmt.
Errorf("resolve node %s: edge %s: %w", path, edge, ErrEdgeNotFound)
}
n = n.Children()[edge]
}
return n, nil
}
// Walk traverses all nodes in a tree, starting from the root route.
// For each node, walkFn will be invoked with the current node and the
// tree path for that node.
//
// maxDepth is counted starting from 0, which represents the root
// node. Set maxDepth to -1 to walk down the entire tree.
//
// As soon as an error arises in one of the walkFns, the error is handed
// up to the caller.
func Walk(root Node, walkFn func(path string, node Node) error, maxDepth int) error {
return walkNode(root, walkFn, maxDepth, 0, RootPath)
}
func walkNode(node Node, walkFn func(path string, node Node) error, maxDepth, curDepth int, curPath string) error {
if maxDepth != -1 && curDepth-1 == maxDepth {
return nil
}
curDepth++
if err := walkFn(curPath, node); err != nil {
return err
}
for edge, child := range node.Children() {
if err := walkNode(child, walkFn, maxDepth, curDepth, concat(curPath, edge)); err != nil {
return err
}
}
return nil
}
func concat(path, edge string) string {
if path == RootPath {
return path + edge
}
return path + delimiter + edge
}
// WalkPath walks all nodes within a tree path and invokes the given
// walkFn on each node.
//
// In contrast to Walk, WalkPath does not invoke the walkFn for
// sibling nodes with the same depth because they're not within the
// tree path. If you desire to take sibling nodes into account, use
// Walk with the depth of your path instead.
func WalkPath(path string, root Node, walkFn func(node Node) error) error {
if !IsValidPath(path) {
return fmt.Errorf("create node %s: %w", path, ErrInvalidPath)
}
n := root
if err := walkFn(n); err != nil {
return err
}
for _, edge := range Edges(path) {
if _, exists := n.Children()[edge]; !exists {
return fmt.
Errorf("resolve node %s: edge %s: %w", path, edge, ErrEdgeNotFound)
}
n = n.Children()[edge]
if err := walkFn(n); err != nil {
return err
}
}
return nil
}