-
Notifications
You must be signed in to change notification settings - Fork 18
/
node.go
222 lines (186 loc) · 4.9 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package node
import (
"errors"
"fmt"
"gopkg.in/yaml.v2"
"reflect"
"strings"
)
type Node struct {
Name string
Value interface{}
Parent *Node
Children []*Node
}
type MapValue struct{}
type ListValue struct{}
type ScalarValue struct {
Value string
}
var ErrNodeConversionFailed = errors.New("node conversion failed")
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,
}
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
// 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
}
}
func NewFromSlice(slice yaml.MapSlice) (*Node, error) {
return convert(nil, "root", slice)
}
func convert(parent *Node, name string, obj interface{}) (*Node, error) {
var result *Node
switch typedObj := obj.(type) {
case []interface{}:
result = &Node{
Name: name,
Value: &ListValue{},
Parent: parent,
}
for _, arrayItem := range typedObj {
listSubtree, err := convert(result, "", arrayItem)
if err != nil {
return nil, err
}
result.Children = append(result.Children, listSubtree)
}
case yaml.MapSlice:
result = &Node{
Name: name,
Value: &MapValue{},
Parent: parent,
}
for _, sliceItem := range typedObj {
// Apparently this is possible, so do the sanity check
key, ok := sliceItem.Key.(string)
if !ok {
return nil, fmt.Errorf("%w: map key is not a string", ErrNodeConversionFailed)
}
mapSubtree, err := convert(result, key, sliceItem.Value)
if err != nil {
return nil, err
}
result.Children = append(result.Children, mapSubtree)
}
default:
scalar := &ScalarValue{}
if typedObj != nil {
scalar.Value = fmt.Sprintf("%v", typedObj)
}
result = &Node{
Name: name,
Value: scalar,
Parent: parent,
}
}
return result, nil
}