-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
executable file
·228 lines (199 loc) · 5.01 KB
/
store.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
package utils
import (
"errors"
"strings"
)
// Adjust loads value from state if referenced
func Adjust(obj interface{}, state map[string]interface{}) interface{} {
switch v := obj.(type) {
case map[string]interface{}:
newObj := map[string]interface{}{}
for key, valTemp := range v {
newObj[key] = Adjust(valTemp, state)
}
return newObj
case []interface{}:
newArray := []interface{}{}
for _, valTemp := range v {
newArray = append(newArray, Adjust(valTemp, state))
}
return newArray
case string:
val, err := LoadValue(v, state)
if err == nil {
return Adjust(val, state)
}
return v
default:
return v
}
}
// LoadStringIfExists loads a value if its present else returns the same
func LoadStringIfExists(value string, state map[string]interface{}) string {
if temp, err := LoadValue(value, state); err == nil {
if tempString, ok := temp.(string); ok {
value = tempString
}
}
return value
}
// LoadValue loads a value from the state
func LoadValue(key string, state map[string]interface{}) (interface{}, error) {
if key == "" {
return nil, errors.New("Invalid key")
}
tempArray := splitVariable(key)
length := len(tempArray) - 1
if length == 0 {
return nil, errors.New("The variable does not map to internal state")
}
if tempArray[0] == "utils" {
function := tempArray[1]
pre := strings.IndexRune(function, '(')
post := strings.IndexRune(function, ')')
if strings.HasPrefix(function, "exists") {
_, err := LoadValue(function[pre+1:post], state)
return err == nil, nil
}
return nil, errors.New("Invalid utils operation")
}
scope, present := state[tempArray[0]]
if !present {
return nil, errors.New("Scope not present")
}
obj, ok := scope.(map[string]interface{})
if !ok {
return nil, errors.New("Invalid state object")
}
for index, k := range tempArray {
if index < 1 {
continue
}
if index == length {
if strings.HasSuffix(k, "]") {
pre := strings.IndexRune(k, '[')
post := strings.IndexRune(k, ']')
var err error
obj, err = convert(k[0:pre], obj)
if err != nil {
return nil, err
}
subVal, err := LoadValue(k[pre+1:post], state)
if err != nil {
return nil, err
}
subKey, ok := subVal.(string)
if !ok {
return nil, errors.New("Key not of type string")
}
value, present := obj[subKey]
if !present {
return nil, errors.New("Key not present in state - " + key)
}
return value, nil
}
value, present := obj[k]
if !present {
return nil, errors.New("Key not present in state - " + key)
}
return value, nil
}
if strings.Contains(k, "]") {
pre := strings.IndexRune(k, '[')
post := strings.IndexRune(k, ']')
var err error
obj, err = convert(k[0:pre], obj)
if err != nil {
return nil, err
}
subVal, err := LoadValue(k[pre+1:post], state)
if err != nil {
return nil, err
}
subKey, ok := subVal.(string)
if !ok {
return nil, errors.New("Key not of type string")
}
obj, err = convert(subKey, obj)
if err != nil {
return nil, err
}
continue
}
var err error
obj, err = convert(k, obj)
if err != nil {
return nil, err
}
}
return nil, errors.New("Key not found")
}
// LoadNumber loads a key as a float. Throws error
func LoadNumber(key interface{}, args map[string]interface{}) (float64, error) {
// Create a temporary copy of key
temp := key
// Load value from argument if key was string i.e. it points to a variable in the argument
if tempString, ok := key.(string); ok {
val, err := LoadValue(tempString, args)
if err != nil {
return 0, err
}
temp = val
}
switch v := temp.(type) {
case int64:
return float64(v), nil
case float64:
return v, nil
}
return 0, errors.New("Store: Cloud not load value")
}
// LoadBool loads a key as a float. Throws error
func LoadBool(key interface{}, args map[string]interface{}) (bool, error) {
// Create a temporary copy of key
temp := key
// Load value from argument if key was string i.e. it points to a variable in the argument
if tempString, ok := key.(string); ok {
val, err := LoadValue(tempString, args)
if err != nil {
return false, err
}
temp = val
}
if v, ok := temp.(bool); ok {
return v, nil
}
return false, errors.New("Store: Cloud not load value")
}
func convert(key string, obj map[string]interface{}) (map[string]interface{}, error) {
tempObj, present := obj[key]
if !present {
return nil, errors.New("Key not present in state (convert) - " + key)
}
conv, ok := tempObj.(map[string]interface{})
if !ok {
return nil, errors.New("Incorrect type")
}
return conv, nil
}
func splitVariable(key string) []string {
var inBracket bool
var lastIndex int
array := []string{}
for i, c := range key {
if c == '[' || c == '(' {
inBracket = true
} else if c == ']' || c == ')' {
inBracket = false
} else if c == '.' && !inBracket {
sub := key[lastIndex:i]
array = append(array, sub)
lastIndex = i + 1
}
if i == len(key)-1 {
sub := key[lastIndex : i+1]
array = append(array, sub)
}
}
return array
}