-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtree_handler_programmably.go
More file actions
191 lines (161 loc) · 5.33 KB
/
tree_handler_programmably.go
File metadata and controls
191 lines (161 loc) · 5.33 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
//go:build !tinywasm
package gtree
import (
"errors"
"io"
"iter"
)
var (
idxCounter = newCounter()
)
// OutputFromRoot outputs tree to w.
// This function requires node generated by NewRoot function.
func OutputFromRoot(w io.Writer, root *Node, options ...Option) error {
if err := validateTreeRoot(root); err != nil {
return err
}
idxCounter.reset()
cfg := newConfig(options)
return initializeTree(cfg).outputProgrammably(w, root, cfg)
}
// MkdirFromRoot makes directories.
// This function requires node generated by NewRoot function.
func MkdirFromRoot(root *Node, options ...Option) error {
if err := validateTreeRoot(root); err != nil {
return err
}
idxCounter.reset()
cfg := newConfig(options)
return initializeTree(cfg).mkdirProgrammably(root, cfg)
}
// VerifyFromRoot verifies directory.
// This function requires node generated by NewRoot function.
func VerifyFromRoot(root *Node, options ...Option) error {
if err := validateTreeRoot(root); err != nil {
return err
}
idxCounter.reset()
cfg := newConfig(options)
return initializeTree(cfg).verifyProgrammably(root, cfg)
}
// WalkFromRoot executes user-defined function while traversing tree structure recursively.
// This function requires node generated by NewRoot function.
func WalkFromRoot(root *Node, callback func(*WalkerNode) error, options ...Option) error {
if err := validateTreeRoot(root); err != nil {
return err
}
idxCounter.reset()
cfg := newConfig(options)
return initializeTree(cfg).walkProgrammably(root, callback, cfg)
}
// WalkIterFromRoot returns each node resulting from recursively traversing tree structure.
// This function requires node generated by NewRoot function.
func WalkIterFromRoot(root *Node, options ...Option) iter.Seq2[*WalkerNode, error] {
return func(yield func(*WalkerNode, error) bool) {
if err := validateTreeRoot(root); err != nil {
yield(nil, err)
return
}
idxCounter.reset()
cfg := newConfig(options)
cfg.massive = false // treePipeline で walkIterProgrammably は未実装で、treeSimple のを使わせてもらうため
walkIter := initializeTree(cfg).walkIterProgrammably(root, cfg)
next, stop := iter.Pull2(walkIter)
defer stop()
for {
wn, err, ok := next()
if !ok {
return
}
if err != nil {
yield(nil, err)
return
}
if !yield(wn, nil) {
return
}
}
}
}
// NewRoot creates a starting node for building tree.
func NewRoot(text string, options ...NodeOption) *Node {
return newNode(text, rootHierarchyNum, idxCounter.next(), options...)
}
// Add adds a node and returns an instance of it.
// If a node with the same text already exists in the same hierarchy of the tree, that node will be returned.
// However, if the WithDuplicationAllowed function is specified for the NewRoot function, it will create and return a new node instance even if a node with the same text exists at the same hierarchy.
func (parent *Node) Add(text string) *Node {
if !parent.allowDuplicates {
if child := parent.findChildByText(text); child != nil {
return child
}
}
nodeOptions := []NodeOption{}
if parent.allowDuplicates {
nodeOptions = append(nodeOptions, WithDuplicationAllowed())
}
current := newNode(text, parent.hierarchy+1, idxCounter.next(), nodeOptions...)
current.setParent(parent)
parent.addChild(current)
return current
}
var (
// ErrNilNode is returned if the argument *gtree.Node of OutputFromRoot / MkdirFromRoot / VerifyFromRoot / WalkFromRoot / WalkIterFromRoot function is nill.
ErrNilNode = errors.New("nil node")
// ErrNotRoot is returned if the argument *gtree.Node of OutputFromRoot / MkdirFromRoot / VerifyFromRoot / WalkFromRoot / WalkIterFromRoot function is not root of the tree.
ErrNotRoot = errors.New("not root node")
)
func validateTreeRoot(root *Node) error {
if root == nil {
return ErrNilNode
}
if !root.isRoot() {
return ErrNotRoot
}
return nil
}
// OutputProgrammably outputs tree to w.
// This function requires node generated by NewRoot function.
//
// Deprecated: Call OutputFromRoot.
//
//go:fix inline
func OutputProgrammably(w io.Writer, root *Node, options ...Option) error {
return OutputFromRoot(w, root, options...)
}
// MkdirProgrammably makes directories.
// This function requires node generated by NewRoot function.
//
// Deprecated: Call MkdirFromRoot.
//
//go:fix inline
func MkdirProgrammably(root *Node, options ...Option) error {
return MkdirFromRoot(root, options...)
}
// VerifyProgrammably verifies directory.
// This function requires node generated by NewRoot function.
//
// Deprecated: Call VerifyFromRoot.
//
//go:fix inline
func VerifyProgrammably(root *Node, options ...Option) error {
return VerifyFromRoot(root, options...)
}
// WalkProgrammably executes user-defined function while traversing tree structure recursively.
// This function requires node generated by NewRoot function.
//
// Deprecated: Call WalkFromRoot.
//
//go:fix inline
func WalkProgrammably(root *Node, callback func(*WalkerNode) error, options ...Option) error {
return WalkFromRoot(root, callback, options...)
}
// WalkIterProgrammably returns each node resulting from recursively traversing tree structure.
// This function requires node generated by NewRoot function.
//
// Deprecated: Call WalkIterFromRoot.
//
//go:fix inline
func WalkIterProgrammably(root *Node, options ...Option) iter.Seq2[*WalkerNode, error] {
return WalkIterFromRoot(root, options...)
}