-
Notifications
You must be signed in to change notification settings - Fork 2
/
decode.go
243 lines (225 loc) · 6.9 KB
/
decode.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
package lightspeed
import (
"fmt"
"log"
"os"
"reflect"
"strconv"
"strings"
"time"
)
type LightSpeedDecoder struct {
Table interface{} // struct that contains pointers to all the dependencies/stores
Dependencies map[string]string
StatementReferences map[int]interface{}
}
func NewLightSpeedDecoder(dependencies map[string]string, table interface{}) *LightSpeedDecoder {
if reflect.ValueOf(table).Kind() != reflect.Ptr {
return nil
}
return &LightSpeedDecoder{
Table: table,
Dependencies: dependencies,
StatementReferences: make(map[int]interface{}),
}
}
// pass the LightSpeedData.Steps to this function
func (ls *LightSpeedDecoder) Decode(data interface{}) interface{} {
s, ok := data.([]interface{})
if !ok {
return data
}
stepType := StepType(int(s[0].(float64)))
stepData := s[1:]
switch stepType {
case BLOCK:
for _, blockData := range stepData {
stepDataArr := blockData.([]interface{})
ls.Decode(stepDataArr)
}
case LOAD:
key, ok := stepData[0].(float64)
if !ok {
log.Println("[LOAD] failed to store key to float64")
return 0
}
shouldLoad, ok := ls.StatementReferences[int(key)]
if !ok {
log.Println("[LOAD] failed to fetch statement reference for key:", key)
return 0
}
return shouldLoad
case STORE:
retVal := ls.Decode(stepData[1])
ls.StatementReferences[int(stepData[0].(float64))] = retVal
case STORE_ARRAY:
key, ok := stepData[0].(float64)
if !ok {
log.Println(stepData...)
os.Exit(1)
}
shouldStore, ok := stepData[1].(float64)
if !ok {
log.Println(stepData...)
os.Exit(1)
}
ls.StatementReferences[int(key)] = int64(shouldStore)
ls.Decode(s[2:])
case CALL_STORED_PROCEDURE:
referenceName := stepData[0].(string)
ls.handleStoredProcedure(referenceName, stepData[1:])
case UNDEFINED:
return nil
case I64_FROM_STRING:
i64, err := strconv.ParseInt(stepData[0].(string), 10, 64)
if err != nil {
log.Println("[I64_FROM_STRING] failed to convert string to int64:", err.Error())
return 0
}
return i64
case IF:
statement := stepData[0]
result := ls.Decode(statement).(int64)
if result > 0 {
ls.Decode(stepData[1])
} else if len(stepData) >= 3 {
if stepData[2] != nil {
ls.Decode(stepData[2])
}
}
case NOT:
return ls.Decode(stepData[0]).(int64)
case NATIVE_OP_CURRENT_TIME:
return time.Now().UnixMilli()
case CALL_NATIVE_OPERATION:
log.Println("call native operation...", stepData)
return nil
case NATIVE_OP_MAP_CREATE:
return make(map[interface{}]interface{}, 0)
case NATIVE_OP_MAP_SET:
mapToUpdate, ok := ls.Decode(stepData[0]).(map[interface{}]interface{})
if !ok {
log.Println("failed to type assert map from statement references...")
return nil
}
mapKey := ls.Decode(stepData[1])
mapVal := ls.Decode(stepData[2])
mapToUpdate[mapKey] = mapVal
case LOGGER_LOG:
log.Println("[FB-LOGGER] Server log:", stepData[0])
return nil
case I64_ADD:
first := ls.Decode(stepData[0]).(int64)
second := ls.Decode(stepData[1]).(int64)
return first + second
default:
log.Println("got unknown step type:", stepType)
log.Println(stepData...)
log.Println(s...)
os.Exit(1)
}
return nil
}
func (ls *LightSpeedDecoder) handleStoredProcedure(referenceName string, data []interface{}) {
depReference, ok := ls.Dependencies[referenceName]
if !ok {
log.Println("Skipping dependency with reference name:",referenceName, data)
return
}
reflectedMs := reflect.ValueOf(ls.Table).Elem()
//log.Println(depReference)
depField := reflectedMs.FieldByName(depReference)
if !depField.IsValid() {
log.Println("Skipping dependency with reference name:",referenceName, data)
return
}
var err error
depFieldsType := depField.Type().Elem()
newDepInstance := reflect.New(depFieldsType).Elem()
for i := 0; i < depFieldsType.NumField(); i++ {
fieldInfo := depFieldsType.Field(i)
var index int
conditionField := fieldInfo.Tag.Get("conditionField")
if conditionField != "" {
indexChoices := fieldInfo.Tag.Get("indexes")
conditionVal := newDepInstance.FieldByName(conditionField)
index, err = ls.parseConditionIndex(conditionVal.Bool(), indexChoices)
if err != nil {
log.Println(fmt.Sprintf("failed to parse condition index in dependency %v for field %v", depFieldsType.Name(), fieldInfo.Name))
continue
}
} else {
index, _ = strconv.Atoi(fieldInfo.Tag.Get("index"))
}
if index > len(data)-1 {
log.Println(fmt.Sprintf("breaking handleStoredProcedure loop because the defined struct exceeds the length of the lightspeed data slice: (index=%d, sliceLen=%d, field=%s, dependency=%s)", index, len(data)-1, fieldInfo.Name, depFieldsType.Name()))
break
}
kind := fieldInfo.Type.Kind()
val := ls.Decode(data[index])
if val == nil { // skip setting field, because the index in the array was [9] which is undefined.
continue
}
valType := reflect.TypeOf(val)
switch kind {
case reflect.Int64:
i64, ok := val.(int64)
if !ok {
log.Println(fmt.Sprintf("failed to set int64 to %v in dependency %v for field %v (index=%d, actualType=%v)", val, depFieldsType.Name(), fieldInfo.Name, index, valType))
continue
}
newDepInstance.Field(i).SetInt(i64)
case reflect.String:
str, ok := val.(string)
if !ok {
log.Println(fmt.Sprintf("failed to set string to %v in dependency %v for field %v (index=%d, actualType=%v)", val, depFieldsType.Name(), fieldInfo.Name, index, valType))
continue
}
newDepInstance.Field(i).SetString(str)
case reflect.Interface:
if val == nil {
continue
}
log.Println(val)
newDepInstance.Field(i).Set(reflect.ValueOf(val))
case reflect.Bool:
boolean, ok := val.(bool)
if !ok {
log.Println(fmt.Sprintf("failed to set bool to %v in dependency %v for field %v (index=%d, actualType=%v)", val, depFieldsType.Name(), fieldInfo.Name, index, valType))
continue
}
newDepInstance.Field(i).SetBool(boolean)
case reflect.Int:
integer, ok := val.(int)
if !ok {
log.Println(fmt.Sprintf("failed to set int to %v in dependency %v for field %v (index=%d, actualType=%v)", val, depFieldsType.Name(), fieldInfo.Name, index, valType))
continue
}
newDepInstance.Field(i).SetInt(int64(integer))
case reflect.Float64:
floatVal, ok := val.(float64)
if !ok {
log.Println(fmt.Sprintf("failed to set float64 to %v in dependency %v for field %v (index=%d, actualType=%v)", val, depFieldsType.Name(), fieldInfo.Name, index, valType))
continue
}
newDepInstance.Field(i).SetFloat(floatVal)
default:
log.Println("invalid kind:", kind, val, valType)
os.Exit(1)
}
}
newSlice := reflect.Append(depField, newDepInstance)
depField.Set(newSlice)
}
// conditionVal ? trueIndex : falseIndex
func (ls *LightSpeedDecoder) parseConditionIndex(val bool, choices string) (int, error) {
indexes := strings.Split(choices, ",")
var index int
var err error
if val {
index, err = strconv.Atoi(indexes[0])
} else {
index, err = strconv.Atoi(indexes[1])
}
return index, err
}