forked from stelligent/mu
/
map.go
245 lines (212 loc) · 5.66 KB
/
map.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package common
import (
"fmt"
"reflect"
)
type mergeOperation func(destVal reflect.Value, srcVal reflect.Value) (bool, reflect.Value)
func appendOperation(destVal reflect.Value, srcVal reflect.Value) (bool, reflect.Value) {
switch srcVal.Kind() {
case reflect.Slice, reflect.Array:
if destVal.IsValid() {
return true, reflect.AppendSlice(destVal, srcVal)
}
case reflect.Map:
if destVal.IsValid() {
MapApply(destVal.Interface(), srcVal.Interface())
return true, destVal
}
}
return true, srcVal
}
func replaceOperation(destVal reflect.Value, srcVal reflect.Value) (bool, reflect.Value) {
if srcVal.Kind() == reflect.Map {
newVal := valueOfInterface(srcVal.MapIndex(reflect.ValueOf("Fn::Replace")))
return newVal.IsValid(), newVal
}
return false, reflect.Value{}
}
func spliceOperation(destVal reflect.Value, srcVal reflect.Value) (bool, reflect.Value) {
if srcVal.Kind() == reflect.Map {
newVal := valueOfInterface(srcVal.MapIndex(reflect.ValueOf("Fn::Splice")))
if newVal.IsValid() && newVal.Len() == 3 {
start := int(newVal.Index(0).Elem().Int())
mergeCount := int(newVal.Index(1).Elem().Int())
newSlice := newVal.Index(2).Elem()
// if no existing value, just use the new value
if !destVal.IsValid() {
return true, newSlice
}
if start > destVal.Len() {
start = destVal.Len()
}
// initialize with elements from before the splice
returnVal := reflect.MakeSlice(destVal.Type(), 0, destVal.Len()+srcVal.Len())
returnVal = reflect.AppendSlice(returnVal, destVal.Slice(0, start))
// merge the overlapping elements
for i := 0; i < newSlice.Len(); i++ {
if i < mergeCount && (start+i) < destVal.Len() {
// merge
_, mergedElement := appendOperation(valueOfInterface(destVal.Index(start+i)), valueOfInterface(newSlice.Index(i)))
returnVal = reflect.Append(returnVal, mergedElement)
} else {
// append
returnVal = reflect.Append(returnVal, newSlice.Index(i))
}
}
// append any existing element from after the splice
if start+mergeCount < destVal.Len() {
returnVal = reflect.AppendSlice(returnVal, destVal.Slice(start+mergeCount, destVal.Len()))
}
return true, returnVal
}
}
return false, reflect.Value{}
}
// MapApply recursively applies map values from source to destination
func MapApply(dest, src interface{}) {
srcMap := reflect.ValueOf(src)
if srcMap.Kind() != reflect.Map {
return
}
destMap := reflect.ValueOf(dest)
if destMap.Kind() != reflect.Map {
return
}
for _, key := range srcMap.MapKeys() {
srcVal := valueOfInterface(srcMap.MapIndex(key))
destVal := valueOfInterface(destMap.MapIndex(key))
for _, op := range []mergeOperation{spliceOperation, replaceOperation, appendOperation} {
handled, response := op(destVal, srcVal)
if handled {
destMap.SetMapIndex(key, response)
break
}
}
}
}
func valueOfInterface(v reflect.Value) reflect.Value {
if !v.IsValid() {
return v
}
return reflect.ValueOf(v.Interface())
}
// ConvertMapI2MapS walks the given dynamic object recursively, and
// converts maps with interface{} key type to maps with string key type.
// This function comes handy if you want to marshal a dynamic object into
// JSON where maps with interface{} key type are not allowed.
//
// Recursion is implemented into values of the following types:
// -map[interface{}]interface{}
// -map[string]interface{}
// -[]interface{}
//
// When converting map[interface{}]interface{} to map[string]interface{},
// fmt.Sprint() with default formatting is used to convert the key to a string key.
func ConvertMapI2MapS(v interface{}) interface{} {
switch x := v.(type) {
case map[interface{}]interface{}:
m := map[string]interface{}{}
for k, v2 := range x {
switch k2 := k.(type) {
case string: // Fast check if it's already a string
m[k2] = ConvertMapI2MapS(v2)
default:
m[fmt.Sprint(k)] = ConvertMapI2MapS(v2)
}
}
v = m
case []interface{}:
for i, v2 := range x {
x[i] = ConvertMapI2MapS(v2)
}
case map[string]interface{}:
for k, v2 := range x {
x[k] = ConvertMapI2MapS(v2)
}
case int:
return int64(v.(int))
case uint:
return int64(v.(uint))
case uint8:
return int64(v.(uint8))
case uint16:
return int64(v.(uint16))
case uint32:
return int64(v.(uint32))
case uint64:
return int64(v.(uint64))
}
return v
}
// MapGet returns a value denoted by the path.
//
// If path is empty or nil, v is returned.
func MapGet(v interface{}, path ...interface{}) interface{} {
for _, el := range path {
switch node := v.(type) {
case map[string]interface{}:
key, ok := el.(string)
if !ok {
return nil
}
v, ok = node[key]
if !ok {
return nil
}
case map[interface{}]interface{}:
var ok bool
v, ok = node[el]
if !ok {
return nil
}
case []interface{}:
idx, ok := el.(int)
if !ok {
return nil
}
if idx < 0 || idx >= len(node) {
return nil
}
v = node[idx]
default:
return nil
}
}
return v
}
// MapGetSlice returns a value denoted by the path.
//
// If path is empty or nil, v is returned.
func MapGetSlice(v interface{}, path ...interface{}) []interface{} {
v = MapGet(v, path...)
if v == nil {
return nil
}
s, ok := v.([]interface{})
if !ok {
return nil
}
return s
}
// MapGetString returns a string value denoted by the path.
//
// If path is empty or nil, v is returned as a string.
func MapGetString(v interface{}, path ...interface{}) string {
v = MapGet(v, path...)
if v == nil {
return ""
}
s, ok := v.(string)
if !ok {
return ""
}
return s
}
// MapClone returns a map copy
func MapClone(m map[string]string) map[string]string {
rtn := make(map[string]string)
for k, v := range m {
rtn[k] = v
}
return rtn
}