-
Notifications
You must be signed in to change notification settings - Fork 0
/
variables.go
393 lines (367 loc) · 8.54 KB
/
variables.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package xtpl
import (
"fmt"
"reflect"
"strconv"
"strings"
)
func newVarName() (varName string) {
return "$_XR_" + strconv.FormatInt(serialID.Next(), 10)
}
type xVarCollection struct {
source map[string]interface{}
overSource map[string]interface{}
variables map[string]*xVar
keyList []string
}
func (xvc *xVarCollection) len() int {
return len(xvc.keyList)
}
func (xvc *xVarCollection) keys() []string {
return xvc.keyList
}
func (xvc *xVarCollection) getMultiVar(fields []string) *xVar {
var res *xVar
res = xvc.getVar(fields[0])
for i := 1; i < len(fields); i++ {
field := fields[i]
if strings.HasPrefix(field, "$") {
field = xvc.getVar(field).toString()
}
res = res.Collection.getVar(field)
}
return res
}
func (xvc *xVarCollection) getVar(varName string) *xVar {
if xvc != nil {
varName = strings.TrimLeft(varName, "$")
if value, ok := xvc.variables[varName]; ok {
return value
}
if value, ok := xvc.overSource[varName]; ok {
return xvc.toVar(varName, value)
}
if value, ok := xvc.source[varName]; ok {
return xvc.toVar(varName, value)
}
}
return &xVar{}
}
func (xvc *xVarCollection) setVar(varName string, value interface{}) {
varName = strings.TrimLeft(varName, "$")
if _, ok := xvc.overSource[varName]; ok {
delete(xvc.variables, varName)
} else if _, ok := xvc.source[varName]; ok {
delete(xvc.variables, varName)
} else {
xvc.keyList = append(xvc.keyList, varName)
}
xvc.overSource[varName] = value
}
func (xvc *xVarCollection) toVar(varName string, value interface{}) *xVar {
var xv = xVarInit(varName, value)
xvc.variables[varName] = xv
return xv
}
type varType uint8
const (
_ varType = iota
varTypeBool
varTypeInt
varTypeFloat
varTypeString
varTypeSlice
varTypeMap
varTypeInterface
varTypeFunc
)
type xVar struct {
vType varType
valueBool bool
valueInt int64
valueFloat float64
valueString string
valueInterface interface{}
valueFunc reflect.Value
Collection *xVarCollection
}
func xVarInit(name string, value interface{}) *xVar {
var xv = &xVar{}
xv.Collection = &xVarCollection{
source: map[string]interface{}{},
overSource: map[string]interface{}{},
variables: map[string]*xVar{},
}
var valueOf = reflect.ValueOf(value)
var valueKind = valueOf.Kind()
switch valueKind {
case reflect.Bool:
xv.vType = varTypeBool
xv.valueBool = value.(bool)
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64:
xv.vType = varTypeInt
switch valueKind {
case reflect.Int:
xv.valueInt = int64(value.(int))
case reflect.Int8:
xv.valueInt = int64(value.(int8))
case reflect.Int16:
xv.valueInt = int64(value.(int16))
case reflect.Int32:
xv.valueInt = int64(value.(int32))
case reflect.Int64:
xv.valueInt = value.(int64)
case reflect.Uint:
xv.valueInt = int64(value.(uint))
case reflect.Uint8:
xv.valueInt = int64(value.(uint8))
case reflect.Uint16:
xv.valueInt = int64(value.(uint16))
case reflect.Uint32:
xv.valueInt = int64(value.(uint32))
case reflect.Uint64:
xv.valueInt = int64(value.(uint64))
}
case reflect.Float32,
reflect.Float64:
xv.vType = varTypeFloat
switch valueKind {
case reflect.Float32:
xv.valueFloat = float64(value.(float32))
case reflect.Float64:
xv.valueFloat = value.(float64)
}
case reflect.String:
s := fmt.Sprintf("%s", value)
if strings.HasPrefix(s, "0") && s != "0" {
xv.vType = varTypeString
xv.valueString = s
} else if n, err := strconv.ParseFloat(s, 64); err == nil {
xv.vType = varTypeFloat
xv.valueFloat = n
} else {
xv.vType = varTypeString
xv.valueString = s
}
case reflect.Array,
reflect.Slice:
xv.vType = varTypeSlice
for i := 0; i < valueOf.Len(); i++ {
xv.Collection.setVar(strconv.Itoa(i), valueOf.Index(i).Interface())
}
case reflect.Map:
xv.vType = varTypeMap
for _, key := range valueOf.MapKeys() {
xv.Collection.setVar(key.String(), valueOf.MapIndex(key).Interface())
}
case reflect.Struct:
xv.vType = varTypeMap
if valueOf.IsValid() {
for i := 0; i < valueOf.NumField(); i++ {
field := valueOf.Field(i)
if field.IsValid() && field.CanInterface() {
xv.Collection.setVar(valueOf.Type().Field(i).Name, field.Interface())
}
}
for i := 0; i < valueOf.NumMethod(); i++ {
method := valueOf.Method(i)
if method.IsValid() && method.CanInterface() {
xv.Collection.setVar(valueOf.Type().Method(i).Name, method.Interface())
}
}
}
case reflect.Ptr:
if valueOf.Elem().IsValid() && valueOf.Elem().CanInterface() {
xv = xVarInit(name, valueOf.Elem().Interface())
}
case reflect.Func:
xv.vType = varTypeFunc
xv.valueFunc = valueOf
case reflect.Interface:
xv.vType = varTypeInterface
}
xv.valueInterface = value
return xv
}
func (xv *xVar) toBool() bool {
switch xv.vType {
case varTypeBool:
return xv.valueBool
case varTypeInt:
return xv.valueInt != 0
case varTypeFloat:
return xv.valueFloat != 0
case varTypeString:
b, _ := strconv.ParseBool(xv.valueString)
return b
case varTypeSlice:
return xv.Collection.len() > 0
case varTypeMap:
return xv.Collection.len() > 0
case varTypeInterface:
return xv.valueInterface != nil
case varTypeFunc:
return !xv.valueFunc.IsNil()
}
return false
}
func (xv *xVar) toInt() int64 {
switch xv.vType {
case varTypeBool:
if xv.valueBool {
return 1
}
return 0
case varTypeInt:
return xv.valueInt
case varTypeFloat:
return int64(xv.valueFloat)
case varTypeString:
i, _ := strconv.ParseInt(xv.valueString, 10, 64)
return i
case varTypeSlice:
return int64(xv.Collection.len())
case varTypeMap:
return int64(xv.Collection.len())
case varTypeInterface:
if xv.valueInterface != nil {
return 1
}
return 0
case varTypeFunc:
if !xv.valueFunc.IsNil() {
return 1
}
return 0
}
return 0
}
func (xv *xVar) toFloat() float64 {
switch xv.vType {
case varTypeBool:
if xv.valueBool {
return 1
}
return 0
case varTypeInt:
return float64(xv.valueInt)
case varTypeFloat:
return xv.valueFloat
case varTypeString:
f, _ := strconv.ParseFloat(xv.valueString, 64)
return f
case varTypeSlice:
return float64(xv.Collection.len())
case varTypeMap:
return float64(xv.Collection.len())
case varTypeInterface:
if xv.valueInterface != nil {
return 1
}
return 0
case varTypeFunc:
if !xv.valueFunc.IsNil() {
return 1
}
return 0
}
return 0
}
func (xv *xVar) toString() string {
switch xv.vType {
case varTypeBool:
return strconv.FormatBool(xv.valueBool)
case varTypeInt:
return strconv.FormatInt(xv.valueInt, 10)
case varTypeFloat:
return strconv.FormatFloat(xv.valueFloat, 'f', -1, 64)
case varTypeString:
return xv.valueString
case varTypeSlice:
return fmt.Sprintf("%s", xv.valueInterface)
case varTypeMap:
return fmt.Sprintf("%s", xv.valueInterface)
case varTypeInterface:
return fmt.Sprintf("%s", xv.valueInterface)
case varTypeFunc:
return fmt.Sprintf("%s", xv.valueInterface)
}
return ""
}
func (xv *xVar) toBytes() []byte {
return []byte(xv.toString())
}
func (xv *xVar) toSlice() []*xVar {
switch xv.vType {
case varTypeSlice,
varTypeMap,
varTypeInterface:
var s = make([]*xVar, xv.Collection.len())
for i, key := range xv.Collection.keys() {
s[i] = xv.Collection.getVar(key)
}
return s
}
return nil
}
func (xv *xVar) toSliceWithInterface() []interface{} {
switch xv.vType {
case varTypeSlice,
varTypeMap,
varTypeInterface:
var s = make([]interface{}, xv.Collection.len())
for i, key := range xv.Collection.keys() {
s[i] = xv.Collection.getVar(key).toInterface()
}
return s
}
return nil
}
func (xv *xVar) toMap() map[string]*xVar {
switch xv.vType {
case varTypeSlice,
varTypeMap,
varTypeInterface:
var m = make(map[string]*xVar)
for _, key := range xv.Collection.keys() {
m[key] = xv.Collection.getVar(key)
}
return m
}
return nil
}
func (xv *xVar) toMapWithInterface() map[string]interface{} {
switch xv.vType {
case varTypeSlice,
varTypeMap,
varTypeInterface:
var m = make(map[string]interface{})
for _, key := range xv.Collection.keys() {
m[key] = xv.Collection.getVar(key).toInterface()
}
return m
}
return nil
}
func (xv *xVar) toInterface() interface{} {
return xv.valueInterface
}
func (xv *xVar) toFunc() (call func(in []reflect.Value) []reflect.Value, arguments []reflect.Kind) {
switch xv.vType {
case varTypeFunc:
call = xv.valueFunc.Call
for i := 0; i < xv.valueFunc.Type().NumIn(); i++ {
arguments = append(arguments, xv.valueFunc.Type().In(i).Kind())
}
}
return
}