-
Notifications
You must be signed in to change notification settings - Fork 8
/
node.go
115 lines (96 loc) · 1.91 KB
/
node.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
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
package gtree
import (
"fmt"
"io/fs"
"path"
"strings"
)
// Node is main struct for gtree.
type Node struct {
name string
hierarchy uint
index uint
brnch branch
parent *Node
children []*Node
}
type branch struct {
value string
path string
}
func newNode(name string, hierarchy, index uint) *Node {
return &Node{
name: name,
hierarchy: hierarchy,
index: index,
}
}
func (n *Node) setParent(parent *Node) {
n.parent = parent
}
func (n *Node) addChild(child *Node) {
n.children = append(n.children, child)
}
func (n *Node) hasChild() bool {
return len(n.children) > 0
}
func (n *Node) findChildByText(text string) *Node {
for _, child := range n.children {
if text == child.name {
return child
}
}
return nil
}
func (n *Node) isDirectlyUnder(node *Node) bool {
if node == nil {
return false
}
return n.hierarchy == node.hierarchy+1
}
func (n *Node) isLastOfHierarchy() bool {
if n.parent == nil {
return false
}
lastIdx := len(n.parent.children) - 1
return n.index == n.parent.children[lastIdx].index
}
const (
rootHierarchyNum uint = 1
)
func (n *Node) isRoot() bool {
return n.hierarchy == rootHierarchyNum
}
func (n *Node) setBranch(branchs ...string) {
ret := ""
for _, v := range branchs {
ret += v
}
n.brnch.value = ret
}
func (n *Node) branch() string {
return n.brnch.value
}
func (n *Node) setPath(paths ...string) {
n.brnch.path = path.Join(paths...)
}
func (n *Node) validatePath() error {
invalidChars := "/" // NOTE: ディレクトリ名に含めてはまずそうなものをここに追加する
if strings.ContainsAny(n.name, invalidChars) {
return fmt.Errorf("invalid node name: %s", n.name)
}
if !fs.ValidPath(n.path()) {
return fmt.Errorf("invalid path: %s", n.path())
}
return nil
}
func (n *Node) path() string {
if n.isRoot() {
return n.name
}
return n.brnch.path
}
func (n *Node) clean() {
n.setBranch("")
n.setPath("")
}