-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtree.go
47 lines (39 loc) · 934 Bytes
/
tree.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
package lunar
// Node is a tree node
type Node struct {
Name string
Value string // value only makes sense in leaf
Children []*Node
}
// AddChildren adds children node if it's not existing
func (node *Node) AddChildren(children ...*Node) {
for _, child := range children {
if child != nil && node.GetChild(child.Name) == nil {
node.Children = append(node.Children, child)
}
}
}
// GetChild gets a child by its name
func (node *Node) GetChild(name string) *Node {
for _, child := range node.Children {
if child.Name == name {
return child
}
}
return nil
}
// IsLeaf returns true if the node is a leaf
func (node *Node) IsLeaf() bool {
return len(node.Children) == 0
}
// ToMap converts to map
func (node *Node) ToMap() interface{} {
if node.IsLeaf() {
return node.Value
}
m := make(map[string]interface{})
for _, child := range node.Children {
m[child.Name] = child.ToMap()
}
return m
}