forked from mikefarah/yq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_navigator.go
201 lines (172 loc) · 4.9 KB
/
data_navigator.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
package main
import (
"fmt"
"sort"
"strconv"
"gopkg.in/yaml.v2"
)
func entryInSlice(context yaml.MapSlice, key interface{}) *yaml.MapItem {
for idx := range context {
var entry = &context[idx]
if entry.Key == key {
return entry
}
}
return nil
}
func writeMap(context interface{}, paths []string, value interface{}) yaml.MapSlice {
log.Debugf("writeMap for %v for %v with value %v\n", paths, context, value)
var mapSlice yaml.MapSlice
switch context.(type) {
case yaml.MapSlice:
mapSlice = context.(yaml.MapSlice)
default:
mapSlice = make(yaml.MapSlice, 0)
}
if len(paths) == 0 {
return mapSlice
}
child := entryInSlice(mapSlice, paths[0])
if child == nil {
newChild := yaml.MapItem{Key: paths[0]}
mapSlice = append(mapSlice, newChild)
child = entryInSlice(mapSlice, paths[0])
log.Debugf("\tAppended child at %v for mapSlice %v\n", paths[0], mapSlice)
}
log.Debugf("\tchild.Value %v\n", child.Value)
remainingPaths := paths[1:]
child.Value = updatedChildValue(child.Value, remainingPaths, value)
log.Debugf("\tReturning mapSlice %v\n", mapSlice)
return mapSlice
}
func updatedChildValue(child interface{}, remainingPaths []string, value interface{}) interface{} {
if len(remainingPaths) == 0 {
return value
}
_, nextIndexErr := strconv.ParseInt(remainingPaths[0], 10, 64)
if nextIndexErr != nil && remainingPaths[0] != "+" {
// must be a map
return writeMap(child, remainingPaths, value)
}
// must be an array
return writeArray(child, remainingPaths, value)
}
func writeArray(context interface{}, paths []string, value interface{}) []interface{} {
log.Debugf("writeArray for %v for %v with value %v\n", paths, context, value)
var array []interface{}
switch context.(type) {
case []interface{}:
array = context.([]interface{})
default:
array = make([]interface{}, 0)
}
if len(paths) == 0 {
return array
}
log.Debugf("\tarray %v\n", array)
rawIndex := paths[0]
var index int64
// the append array indicator
if rawIndex == "+" {
index = int64(len(array))
} else {
index, _ = strconv.ParseInt(rawIndex, 10, 64)
}
// writeArray is only called by updatedChildValue which handles parsing the
// index, as such this renders this dead code.
// if err != nil {
// return array, fmt.Errorf("Error accessing array: %v", err)
// }
for index >= int64(len(array)) {
array = append(array, nil)
}
currentChild := array[index]
log.Debugf("\tcurrentChild %v\n", currentChild)
remainingPaths := paths[1:]
array[index] = updatedChildValue(currentChild, remainingPaths, value)
log.Debugf("\tReturning array %v\n", array)
return array
}
func readMap(context yaml.MapSlice, head string, tail []string) (interface{}, error) {
if head == "*" {
return readMapSplat(context, tail)
}
var value interface{}
entry := entryInSlice(context, head)
if entry != nil {
value = entry.Value
}
return calculateValue(value, tail)
}
func readMapSplat(context yaml.MapSlice, tail []string) (interface{}, error) {
var newArray = make([]interface{}, len(context))
var i = 0
for _, entry := range context {
if len(tail) > 0 {
val, err := recurse(entry.Value, tail[0], tail[1:])
if err != nil {
return nil, err
}
newArray[i] = val
} else {
newArray[i] = entry.Value
}
i++
}
return newArray, nil
}
func recurse(value interface{}, head string, tail []string) (interface{}, error) {
switch value.(type) {
case []interface{}:
if head == "*" {
return readArraySplat(value.([]interface{}), tail)
}
index, err := strconv.ParseInt(head, 10, 64)
if err != nil {
return nil, fmt.Errorf("Error accessing array: %v", err)
}
return readArray(value.([]interface{}), index, tail)
case yaml.MapSlice:
return readMap(value.(yaml.MapSlice), head, tail)
default:
return nil, nil
}
}
func readArray(array []interface{}, head int64, tail []string) (interface{}, error) {
if head >= int64(len(array)) {
return nil, nil
}
value := array[head]
return calculateValue(value, tail)
}
func readArraySplat(array []interface{}, tail []string) (interface{}, error) {
var newArray = make([]interface{}, len(array))
for index, value := range array {
val, err := calculateValue(value, tail)
if err != nil {
return nil, err
}
newArray[index] = val
}
return newArray, nil
}
func calculateValue(value interface{}, tail []string) (interface{}, error) {
if len(tail) > 0 {
return recurse(value, tail[0], tail[1:])
}
return value, nil
}
func mapToMapSlice(data map[interface{}]interface{}) yaml.MapSlice {
var mapSlice yaml.MapSlice
for k, v := range data {
if mv, ok := v.(map[interface{}]interface{}); ok {
v = mapToMapSlice(mv)
}
item := yaml.MapItem{Key: k, Value: v}
mapSlice = append(mapSlice, item)
}
// because the parsing of the yaml was done via a map the order will be inconsistent
// apply order to allow a consistent output
sort.SliceStable(mapSlice, func(i, j int) bool { return mapSlice[i].Key.(string) < mapSlice[j].Key.(string) })
return mapSlice
}