-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathiteration.go
330 lines (301 loc) · 9.36 KB
/
iteration.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* Copyright 2022- IBM Inc. All rights reserved
* SPDX-License-Identifier: Apache2.0
*/
package controllers
///////////////////////////////////////////////////////////////////
//
// iteration.go
// This module is to facilitate on generating a list of spec object
//
// There are three main function.
// - GetInitCombination: return combination of base spec object
// - GetAllCombination: return all combinations of all iteration items in a list form
// - UpdateValue: return new modified spec object regarding a new value at a specified location
//
///////////////////////////////////////////////////////////////////
import (
"fmt"
"reflect"
"strconv"
"strings"
cpev1 "github.com/IBM/cpe-operator/api/v1"
)
type IterationKeyType int
const (
VALUE IterationKeyType = iota
LIST
MAP
NULL_VALUE_STR = "nil"
)
type IterationHandler struct{}
func (it *IterationHandler) getKeyType(key string) IterationKeyType {
if !strings.Contains(key, "[") {
return VALUE
}
if strings.Contains(key, "=") {
return MAP
}
return LIST
}
func (it *IterationHandler) splitBracket(key string) (string, string) {
start := strings.Index(key, "[")
end := strings.Index(key, "]")
return key[0:start], key[start+1 : end]
}
func (it *IterationHandler) getValue(nextKey string, object map[string]interface{}) (IterationKeyType, interface{}, string, int) {
keyType := it.getKeyType(nextKey)
var nextObject interface{}
var keyName string
var indexStr string
var ok bool
indexInt := -1
switch keyType {
case VALUE:
keyName = nextKey
if nextObject, ok = object[nextKey]; !ok {
nextObject = nil
}
case LIST:
keyName, indexStr = it.splitBracket(nextKey)
if _, ok = object[keyName]; !ok {
object[keyName] = []interface{}{}
}
objectList := object[keyName].([]interface{})
indexInt, _ = strconv.Atoi(indexStr)
if indexInt < len(objectList) {
nextObject = objectList[indexInt]
} else {
nextObject = nil
}
case MAP:
keyName, indexStr = it.splitBracket(nextKey)
subMapIndex := strings.Split(indexStr, "=")
if _, ok = object[keyName]; !ok {
object[keyName] = []interface{}{}
}
subMaps := object[keyName].([]interface{})
nextObject = nil
indexInt = 0
for _, subMap := range subMaps {
key := subMapIndex[0]
if value, ok := subMap.(map[string]interface{})[key]; ok && value.(string) == subMapIndex[1] {
nextObject = subMap
break
}
indexInt += 1
}
default:
nextObject = nil
}
return keyType, nextObject, keyName, indexInt
}
// Update Value
func (it *IterationHandler) getValueInterface(reflectType reflect.Type, value string) interface{} {
if reflectType == nil {
return value
}
var valueInterface interface{}
if reflectType.Kind() == reflect.Int64 {
valueInterface, _ = strconv.ParseInt(value, 10, 64)
} else if reflectType.Kind() == reflect.Float64 {
valueInterface, _ = strconv.ParseFloat(value, 64)
} else if reflectType.Kind() == reflect.Bool {
valueInterface, _ = strconv.ParseBool(value)
} else {
valueInterface = value
}
return valueInterface
}
func (it *IterationHandler) updateNextValue(object map[string]interface{}, keys []string, curIndex int, valueToSet string) map[string]interface{} {
curKey := keys[curIndex]
keyType, nextObject, keyName, indexInt := it.getValue(curKey, object)
if nextObject == nil {
// last valueToSet
if curIndex == len(keys)-1 {
var objectList []interface{}
switch keyType {
case VALUE:
reflectType := reflect.TypeOf(object[curKey])
valueInterface := it.getValueInterface(reflectType, valueToSet)
object[curKey] = valueInterface
case LIST:
objectList = object[keyName].([]interface{})
reflectType := reflect.TypeOf(objectList).Elem()
valueInterface := it.getValueInterface(reflectType, valueToSet)
object[keyName] = append(objectList, valueInterface)
case MAP:
objectList = object[keyName].([]interface{})
newObject := make(map[string]interface{})
_, indexStr := it.splitBracket(curKey)
subMapIndex := strings.Split(indexStr, "=")
newObject[subMapIndex[0]] = subMapIndex[1]
valueKey := keys[curIndex+1]
reflectType := reflect.TypeOf(newObject[valueKey])
valueInterface := it.getValueInterface(reflectType, valueToSet)
newObject[valueKey] = valueInterface
object[keyName] = append(objectList, newObject)
}
} else {
// not last value but no value before
switch keyType {
case VALUE:
nextObject = make(map[string]interface{})
case LIST:
nextObject = []interface{}{}
case MAP:
nextObject = make(map[string]interface{})
_, indexStr := it.splitBracket(curKey)
subMapIndex := strings.Split(indexStr, "=")
reflectType := reflect.TypeOf(subMapIndex[1])
valueInterface := it.getValueInterface(reflectType, subMapIndex[1])
nextObject.(map[string]interface{})[subMapIndex[0]] = valueInterface
}
}
}
if nextObject != nil {
if reflect.TypeOf(nextObject).Kind() == reflect.Map {
subObject := it.updateNextValue(nextObject.(map[string]interface{}), keys, curIndex+1, valueToSet)
switch keyType {
case VALUE:
object[keyName] = subObject
case LIST:
objectArr := object[keyName].([]interface{})
if indexInt == len(objectArr) {
object[keyName] = []interface{}{subObject}
} else {
object[keyName].([]interface{})[indexInt] = subObject
}
case MAP:
objectArr := object[keyName].([]interface{})
if indexInt >= len(objectArr) {
object[keyName] = append(objectArr, subObject)
} else {
object[keyName].([]interface{})[indexInt] = subObject
}
}
} else {
reflectType := reflect.TypeOf(object[keyName])
if reflectType == reflect.TypeOf([]interface{}{}) {
objectArr := object[keyName].([]interface{})
valueInterface := it.getValueInterface(reflect.TypeOf(objectArr[0]), valueToSet)
if indexInt >= len(objectArr) {
object[keyName] = append(objectArr, valueInterface)
} else {
object[keyName].([]interface{})[indexInt] = valueInterface
}
} else {
valueInterface := it.getValueInterface(reflectType, valueToSet)
object[keyName] = valueInterface
}
}
}
return object
}
func (it *IterationHandler) getTokens(location string) [][]string {
var tokens [][]string
compositeSplit := strings.Split(location, ";")
for _, split := range compositeSplit {
token := it.getToken(split[1:])
tokens = append(tokens, token)
}
return tokens
}
func (it *IterationHandler) getToken(location string) []string {
var token []string
simpleSplit := strings.Split(location, ".")
opened := false
quoteValue := ""
for _, split := range simpleSplit {
if !opened && split[0] == '(' {
opened = true
split = split[1:len(split)]
quoteValue = split
} else if opened {
if split[len(split)-1] == ')' {
opened = false
split = split[0 : len(split)-1]
quoteValue += "." + split
token = append(token, quoteValue)
} else {
quoteValue += "." + split
}
} else {
token = append(token, split)
}
}
return token
}
func (it *IterationHandler) UpdateValue(baseObject map[string]interface{}, location string, value string) map[string]interface{} {
if value == NULL_VALUE_STR {
return baseObject
}
//locationSplits := strings.Split(location[1:], ".")
locationSplitsArr := it.getTokens(location)
values := strings.Split(value, ";")
var modifiedObject map[string]interface{}
for index, locationSplits := range locationSplitsArr {
if index < len(values) {
modifiedObject = it.updateNextValue(baseObject, locationSplits, 0, values[index])
}
}
return modifiedObject
}
// Get All Combination
func (it *IterationHandler) nextCombination(itr []cpev1.IterationItem, curLayer int, prevList []map[string]string) []map[string]string {
if curLayer == len(itr) {
return prevList
}
var newList []map[string]string
for _, value := range itr[curLayer].Values {
if len(prevList) == 0 {
newItem := make(map[string]string)
newItem[itr[curLayer].Name] = value
newList = append(newList, newItem)
} else {
for _, prevItem := range prevList {
newItem := make(map[string]string)
for prevK, prevV := range prevItem {
newItem[prevK] = prevV
}
newItem[itr[curLayer].Name] = value
newList = append(newList, newItem)
}
}
}
return it.nextCombination(itr, curLayer+1, newList)
}
func (it *IterationHandler) GetAllCombination(itr []cpev1.IterationItem) []map[string]string {
var prevList []map[string]string
return it.nextCombination(itr, 0, prevList)
}
// Get Init Combination
func (it *IterationHandler) deeperValue(object map[string]interface{}, keys []string, curIndex int) interface{} {
curKey := keys[curIndex]
_, nextObject, keyName, indexInt := it.getValue(curKey, object)
if nextObject == nil {
return NULL_VALUE_STR
}
if reflect.TypeOf(nextObject).Kind() == reflect.Map {
return it.deeperValue(nextObject.(map[string]interface{}), keys, curIndex+1)
}
reflectType := reflect.TypeOf(object[keyName])
if reflectType == reflect.TypeOf([]interface{}{}) {
objectArr := object[keyName].([]interface{})
return objectArr[indexInt]
}
return object[keyName]
}
func (it *IterationHandler) GetValue(initObject map[string]interface{}, location string) []string {
// locationSplits := strings.Split(location[1:], ".")
locationSplitsArr := it.getTokens(location)
var values []string
for _, locationSplits := range locationSplitsArr {
value := it.deeperValue(initObject, locationSplits, 0)
if value == "" { // cannot find
return nil
}
values = append(values, fmt.Sprintf("%v", value))
}
return values
}