-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseVariables.go
273 lines (221 loc) · 7 KB
/
parseVariables.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
package templateManager
/*
Functions dedicated to the parsing of variables embedded in templates (as strings) into their actual, strongly typed forms
*/
import (
"strings"
"strconv"
)
// Determines a variable's (likely) basic type from a string representation of it
func getVariableType(value string, b ...string) (string, any) {
bias := ""
if len(b) > 0 {
bias = b[0]
}
if strings.HasPrefix(value, "{") && strings.HasSuffix(value, "}") {
return "map", nil
} else if strings.HasPrefix(value, "[") && strings.HasSuffix(value, "]") {
return "slice", nil
} else if val, err := strconv.Atoi(value); bias != "float" && err == nil {
return "int", val
} else if val, err := strconv.ParseFloat(value, 64); err == nil {
return "float", val
} else if val, err := strconv.ParseBool(strings.ToLower(value)); err == nil {
return "bool", val
}
return "string", value
}
// Generic helper to parse supported map type combinations from a map of strings to a map of their actual types
func parseVariableMap[K string|int|float64|bool, V string|int|float64|bool](values map[string]string) map[K]V {
tmp := map[K]V{}
for key, val := range values {
_, key := getVariableType(key)
_, val := getVariableType(val)
tmp[key.(K)] = val.(V)
}
return tmp
}
// Generic helper to parse supported slice types from a slice of strings to a slice of their actual type
func parseVariableSlice[V string|int|float64|bool](values []string) []V {
t := ""
var emptyV V
var emptyVInterface any = emptyV
if _, ok := emptyVInterface.(int); ok {
t = "int"
} else if _, ok := emptyVInterface.(float64); ok {
t = "float"
}
tmp := []V{}
for _, val := range values {
_, v := getVariableType(val, t)
tmp = append(tmp, v.(V))
}
return tmp
}
// Generic helper to parse supported nested slice types from a slice of strings to a slice of their actual type
func parseNestedVariableSlice[V string|int|float64|bool](values []string) [][]V {
t := ""
var emptyV V
var emptyVInterface any = emptyV
if _, ok := emptyVInterface.(int); ok {
t = "int"
} else if _, ok := emptyVInterface.(float64); ok {
t = "float"
}
tmp := [][]V{}
for _, val := range values {
sub := []V{}
tmp2 := prepareSlice(val)
for _, subval := range tmp2 {
_, v := getVariableType(subval, t)
sub = append(sub, v.(V))
}
tmp = append(tmp, sub)
}
return tmp
}
// Parses a string representation of a map into string values ready for type detection
func prepareMap(value string) map[string]string {
value = value[1:len(value) - 1]
value = value + ","
m := make(map[string]string)
if regexps["findStringStringMap"].MatchString(value) {
matches := regexps["findStringStringMap"].FindAllStringSubmatch(value, -1)
for _, match := range matches {
m[match[1]] = match[2]
}
return m
}
if regexps["findStringNumericMap"].MatchString(value) {
matches := regexps["findStringNumericMap"].FindAllStringSubmatch(value, -1)
for _, match := range matches {
m[match[1]] = match[2]
}
return m
}
if regexps["findStringBoolMap"].MatchString(value) {
matches := regexps["findStringBoolMap"].FindAllStringSubmatch(value, -1)
for _, match := range matches {
m[match[1]] = strings.ToLower(match[2])
}
return m
}
if regexps["findNumericStringMap"].MatchString(value) {
matches := regexps["findNumericStringMap"].FindAllStringSubmatch(value, -1)
for _, match := range matches {
m[match[1]] = match[2]
}
return m
}
if regexps["findNumericNumericMap"].MatchString(value) {
matches := regexps["findNumericNumericMap"].FindAllStringSubmatch(value, -1)
for _, match := range matches {
m[match[1]] = match[2]
}
return m
}
if regexps["findNumericBoolMap"].MatchString(value) {
matches := regexps["findNumericBoolMap"].FindAllStringSubmatch(value, -1)
for _, match := range matches {
m[match[1]] = strings.ToLower(match[2])
}
return m
}
if regexps["findBoolStringMap"].MatchString(value) {
matches := regexps["findBoolStringMap"].FindAllStringSubmatch(value, -1)
for _, match := range matches {
m[strings.ToLower(match[1])] = match[2]
}
return m
}
if regexps["findBoolNumericMap"].MatchString(value) {
matches := regexps["findBoolNumericMap"].FindAllStringSubmatch(value, -1)
for _, match := range matches {
m[strings.ToLower(match[1])] = match[2]
}
return m
}
if regexps["findBoolBoolMap"].MatchString(value) {
matches := regexps["findBoolBoolMap"].FindAllStringSubmatch(value, -1)
for _, match := range matches {
m[strings.ToLower(match[1])] = strings.ToLower(match[2])
}
return m
}
/*
if regexps["findSliceMap"].MatchString(value) {
matches := regexps["findSliceMap"].FindAllStringSubmatch(value, -1)
for _, match := range matches {
m[match[1]] = match[2]
}
}
*/
return m
}
// Parses a string representation of a slice of booleans into a slice of strings ready for type detection
func prepareBooleanSlice(value string) []string {
value = value + ","
slice := []string{}
if regexps["findBooleanSlice"].MatchString(value) {
matches := regexps["findBooleanSlice"].FindAllStringSubmatch(value, -1)
for _, match := range matches {
slice = append(slice, strings.ToLower(match[1]))
}
}
return slice
}
// Parses a string representation of a slice of numbers into a slice of strings ready for type detection
func prepareNumericSlice(value string) []string {
value = value + ","
slice := []string{}
if regexps["findNumericSlice"].MatchString(value) {
matches := regexps["findNumericSlice"].FindAllStringSubmatch(value, -1)
for _, match := range matches {
slice = append(slice, match[1])
}
}
return slice
}
// Parses a string representation of a slice into a slice of string values ready for type detection
func prepareSlice(value string) []string {
value = value[1:len(value) - 1]
var values = []string{}
if value[0:1] == "[" {
values = prepareSliceSlice(value)
} else if strings.Contains(value, `"`) || strings.Contains(value, `'`) || strings.Contains(value, "`") {
values = prepareStringSlice(value)
} else {
values = prepareNumericSlice(value)
}
if len(values) == 0 {
values = prepareBooleanSlice(value)
}
return values
}
// Parses a string representation of a slice of slices into a slice of strings ready for type detection
func prepareSliceSlice(value string) []string {
value = value + ","
slice := []string{}
if regexps["findSliceSlice"].MatchString(value) {
matches := regexps["findSliceSlice"].FindAllStringSubmatch(value, -1)
for _, match := range matches {
slice = append(slice, match[1])
}
}
return slice
}
// Parses a string representation of a slice of strings into an actual slice of strings ready for type detection
func prepareStringSlice(value string) []string {
value = value + ","
slice := []string{}
if regexps["findStringSlice"].MatchString(value) {
matches := regexps["findStringSlice"].FindAllStringSubmatch(value, -1)
for _, match := range matches {
val := strings.Replace(match[1], "\\`", "`", -1)
val = strings.Replace(val, `\"`, `"`, -1)
val = strings.Replace(val, "\\'", "'", -1)
slice = append(slice, val)
}
}
return slice
}