-
Notifications
You must be signed in to change notification settings - Fork 2
/
node.go
77 lines (58 loc) · 1.19 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
package envh
type node struct {
children []*node
key string
value string
hasValue bool
}
func newNode() *node {
return &node{children: []*node{}}
}
func (n *node) findAllNodesByKey(key string, withValue bool) *[]*node {
results := []*node{}
nodes := n.children
for {
carry := []*node{}
for _, node := range nodes {
if node.key == key {
if withValue && node.hasValue || !withValue {
results = append(results, node)
}
}
carry = append(carry, node.children...)
}
nodes = carry
if len(carry) == 0 {
return &results
}
}
}
func (n *node) findNodeByKeyChain(keyChain *[]string) (*node, bool) {
if len(*keyChain) == 0 {
return nil, false
}
current := n
for _, key := range *keyChain {
node, exists := current.findNodeByKey(key)
if !exists {
return nil, false
}
current = node
}
return current, true
}
func (n *node) findNodeByKey(key string) (*node, bool) {
for _, child := range n.children {
if child.key == key {
return child, true
}
}
return nil, false
}
func (n *node) appendNode(child *node) bool {
if _, ok := n.findNodeByKey(child.key); ok {
return false
}
n.children = append(n.children, child)
return true
}