-
Notifications
You must be signed in to change notification settings - Fork 18
/
node.go
196 lines (165 loc) · 4.21 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
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
192
193
194
195
196
package node
import (
"fmt"
"reflect"
"strings"
)
type Node struct {
Name string
Value interface{}
Parent *Node
Children []*Node
Line int
Column int
}
type MapValue struct{}
type ListValue struct{}
type ScalarValue struct {
Value string
}
func (node *Node) ValueTypeAsString() string {
switch node.Value.(type) {
case *MapValue:
return "map"
case *ListValue:
return "list"
case *ScalarValue:
return "scalar"
default:
return "unknown"
}
}
func (node *Node) ValueIsEmpty() bool {
switch value := node.Value.(type) {
case *MapValue, *ListValue:
return len(node.Children) == 0
case *ScalarValue:
return value.Value == ""
default:
return false
}
}
func (node *Node) IsMap() bool {
_, isMap := node.Value.(*MapValue)
return isMap
}
func (node *Node) String() string {
switch value := node.Value.(type) {
case *MapValue:
var children []string
for _, child := range node.Children {
children = append(children, fmt.Sprintf("%s=%s", child.Name, child.String()))
}
return fmt.Sprintf("MapValue(%s)", strings.Join(children, ", "))
case *ListValue:
var children []string
for _, child := range node.Children {
children = append(children, child.String())
}
return fmt.Sprintf("ListValue(%s)", strings.Join(children, ", "))
case *ScalarValue:
return fmt.Sprintf("ScalarValue(%s)", value.Value)
default:
return "UnknownNodeType()"
}
}
func (node *Node) CopyWithParent(parent *Node) *Node {
result := &Node{
Name: node.Name,
Value: node.Value,
Parent: parent,
Line: node.Line,
Column: node.Column,
}
for _, child := range node.Children {
result.Children = append(result.Children, child.CopyWithParent(result))
}
return result
}
func (node *Node) Deduplicate() {
// Split children into two groups
seen := map[string]*Node{}
var unique, duplicate []*Node
for _, child := range node.Children {
if _, ok := seen[child.Name]; ok {
duplicate = append(duplicate, child)
} else {
unique = append(unique, child)
seen[child.Name] = child
}
}
// Merge children from the duplicate group into their unique counterparts
// with recursive descent
for _, duplicateChild := range duplicate {
duplicateChild.Deduplicate()
uniqueChild := seen[duplicateChild.Name]
uniqueChild.MergeFrom(duplicateChild)
}
node.Children = unique
}
func (node *Node) MergeListOfMapsToSingleMap() {
_, ok := node.Value.(*ListValue)
if !ok {
return
}
var virtualNode Node
for _, child := range node.Children {
if _, ok := child.Value.(*MapValue); !ok {
return
}
virtualNode.MergeFrom(child)
}
node.Children = virtualNode.Children
// Rewrite parents from virtualNode to node
for _, child := range virtualNode.Children {
child.Parent = node
}
// This is now a map
node.Value = &MapValue{}
}
func (node *Node) MergeFrom(other *Node) {
node.Name = other.Name
node.Line = other.Line
node.Column = other.Column
// Special treatment for environment variables since they can also be represented as a list of maps
if node.Name == "env" || node.Name == "environment" {
node.MergeListOfMapsToSingleMap()
other.MergeListOfMapsToSingleMap()
}
if reflect.TypeOf(node.Value) != reflect.TypeOf(other.Value) {
node.Value = other.Value
// Simply overwrite node's children with other's children
node.Children = node.Children[:0]
for _, child := range other.Children {
node.Children = append(node.Children, child.CopyWithParent(node))
}
return
}
switch other.Value.(type) {
case *MapValue:
existingChildren := map[string]*Node{}
for _, child := range node.Children {
existingChildren[child.Name] = child
}
for _, otherChild := range other.Children {
existingChild, ok := existingChildren[otherChild.Name]
if ok {
existingChild.MergeFrom(otherChild)
} else {
node.Children = append(node.Children, otherChild.CopyWithParent(node))
}
}
case *ListValue:
for i, otherChild := range other.Children {
if i < len(node.Children) {
// We have a counterpart child, do a merge
node.Children[i].MergeFrom(otherChild)
} else {
// They have more children that we do, simply append them one by one
node.Children = append(node.Children, otherChild.CopyWithParent(node))
}
}
case *ScalarValue:
node.Value = other.Value
}
}