-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnode.go
116 lines (94 loc) · 1.96 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
116
package mui
import (
"encoding/json"
"fmt"
)
// Node - MUI Lang node represantation
type Node struct {
Name string `json:"name"`
Content string `json:"content"`
Props []*Prop `json:"props"`
Parent *Node `json:"-"`
Children []*Node `json:"children"`
}
// NewNode -
func NewNode(name string) *Node {
return &Node{Name: name}
}
// GetName -
func (node *Node) GetName() string {
return node.Name
}
// SetName -
func (node *Node) SetName(name string) {
node.Name = name
}
// GetParent -
func (node *Node) GetParent() *Node {
return node.Parent
}
// SetParent -
func (node *Node) SetParent(parent *Node) {
node.Parent = parent
}
// GetContent -
func (node *Node) GetContent() string {
return node.Content
}
// SetContent -
func (node *Node) SetContent(content string) {
node.Content = content
}
// GetProps -
func (node *Node) GetProps() []*Prop {
return node.Props
}
// GetProps -
func (node *Node) GetProp(propName string) string {
for _, prop := range node.Props {
if prop.Key == propName {
return prop.Value
}
}
return "nil"
}
// SetProps -
func (node *Node) SetProps(props []*Prop) {
node.Props = props
}
// AddProp -
func (node *Node) AddProp(prop *Prop) {
node.Props = append(node.Props, prop)
}
// AddProps -
func (node *Node) AddProps(props ...*Prop) {
node.Props = append(node.Props, props...)
}
// GetChildren -
func (node *Node) GetChildren() []*Node {
return node.Children
}
// SetChildren -
func (node *Node) SetChildren(children []*Node) {
node.Children = children
}
// AddChild -
func (node *Node) AddChild(child *Node) {
child.SetParent(node)
node.Children = append(node.Children, child)
}
// AddChildren -
func (node *Node) AddChildren(children ...*Node) {
for _, child := range children {
child.Parent = node
}
node.Children = append(node.Children, children...)
}
func (node *Node) AsJSON() string {
res, err := json.MarshalIndent(node, "", " ")
if err != nil {
fmt.Println(err)
return "{}"
}
return string(res)
}