/
datastore.go
executable file
·244 lines (205 loc) · 5.25 KB
/
datastore.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
package datastore
import (
"fmt"
"math"
"regexp"
"strconv"
"strings"
"sync"
"github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
)
type Datastore struct {
storage map[string]interface{} // custom storage
responseJson []string // store the responses
logDatastore bool
lock *sync.Mutex
}
func NewStore(logDatastore bool) *Datastore {
ds := Datastore{}
ds.storage = make(map[string]interface{}, 0)
ds.responseJson = make([]string, 0)
ds.logDatastore = logDatastore
ds.lock = &sync.Mutex{}
return &ds
}
type DatastoreKeyNotFoundError struct {
error string
}
func (data DatastoreKeyNotFoundError) Error() string {
return data.error
}
type DatastoreIndexOutOfBoundsError struct {
error string
}
func (data DatastoreIndexOutOfBoundsError) Error() string {
return data.error
}
type DatastoreIndexError struct {
error string
}
func (data DatastoreIndexError) Error() string {
return data.error
}
// SetWithQjson stores the given response driven by a map key => qjson
func (ds *Datastore) SetWithQjson(jsonResponse string, storeResponse map[string]string) error {
for k, qv := range storeResponse {
setEmpty := false
if len(qv) > 0 && qv[0] == '!' {
setEmpty = true
qv = qv[1:]
}
qValue := gjson.Get(jsonResponse, qv)
if qValue.Value() == nil {
if ds.logDatastore {
logrus.Tracef("'%s' was not found in '%s'", qv, jsonResponse)
}
// Remove value from datastore
if setEmpty {
ds.Delete(k)
}
continue
}
err := ds.Set(k, qValue.Value())
if err != nil {
return err
}
}
return nil
}
func (ds *Datastore) Delete(k string) {
delete(ds.storage, k)
}
// We store the response
func (ds *Datastore) UpdateLastResponse(s string) {
ds.responseJson[len(ds.responseJson)-1] = s
}
// We store the response
func (ds *Datastore) AppendResponse(s string) {
ds.responseJson = append(ds.responseJson, s)
}
func (ds *Datastore) SetMap(smap map[string]interface{}) error {
for k, v := range smap {
err := ds.Set(k, v)
if err != nil {
return err
}
}
return nil
}
func (ds *Datastore) Set(index string, value interface{}) error {
var dsMapRegex = regexp.MustCompile(`^(.*?)\[(.+?)\]$`)
//typeswitch for checking if float is actually int
switch t := value.(type) {
case float64:
if math.Mod(t, 1.0) == 0 {
//is int
value = int(t)
}
}
ds.lock.Lock()
defer ds.lock.Unlock()
//Slice in datastore
if strings.HasSuffix(index, "[]") {
// do a push to an array
use_index := index[:len(index)-2]
_, ok := ds.storage[use_index]
if !ok {
ds.storage[use_index] = make([]interface{}, 0)
}
s, ok := ds.storage[use_index].([]interface{})
if !ok {
tmp := ds.storage[use_index]
ds.storage[use_index] = make([]interface{}, 0)
s = ds.storage[use_index].([]interface{})
if tmp != nil {
ds.storage[use_index] = append(s, tmp)
}
}
ds.storage[use_index] = append(s, value)
} else if rego := dsMapRegex.FindStringSubmatch(index); len(rego) > 0 {
// do a push to an array
use_index := rego[1]
_, ok := ds.storage[use_index]
if !ok {
ds.storage[use_index] = make(map[string]interface{}, 0)
}
s, ok := ds.storage[use_index].(map[string]interface{})
if !ok {
ds.storage[use_index] = make(map[string]interface{}, 0)
s = ds.storage[use_index].(map[string]interface{})
}
s[rego[2]] = value
ds.storage[use_index] = s
} else {
ds.storage[index] = value
}
if ds.logDatastore {
logrus.Tracef("Set datastore[\"%s\"]=%#v", index, value)
}
return nil
}
func (ds Datastore) Get(index string) (res interface{}, err error) {
// strings are evalulated as int, so
// that we can support "-<int>" notations
if index == "-" {
// return the entire custom store
return ds.storage, nil
}
var dsMapRegex = regexp.MustCompile(`^(.*?)\[(.+?)\]$`)
if rego := dsMapRegex.FindStringSubmatch(index); len(rego) > 0 {
//we have a map or slice
useIndex := rego[1]
mapIndex := rego[2]
tmpRes, ok := ds.storage[useIndex]
if !ok {
logrus.Errorf("datastore: key: %s not found.", useIndex)
return "", DatastoreKeyNotFoundError{error: fmt.Sprintf("datastore: key: %s not found.", useIndex)}
}
tmpResMap, ok := tmpRes.(map[string]interface{})
if ok {
//We have a map
mapVal, ok := tmpResMap[mapIndex]
if !ok {
//Value not found in map, so return empty string
return "", nil
} else {
return mapVal, nil
}
}
tmpResSlice, ok := tmpRes.([]interface{})
if ok {
//We have a slice
sliceIdx, err := strconv.Atoi(mapIndex)
if err != nil {
return "", DatastoreIndexError{error: fmt.Sprintf("datastore: could not convert key to int: %s", mapIndex)}
}
if sliceIdx < 0 {
sliceIdx = len(tmpResSlice) + sliceIdx
}
if len(tmpResSlice) <= sliceIdx || sliceIdx < 0 {
return "", nil
} else {
return tmpResSlice[sliceIdx], nil
}
}
} else {
idx, err := strconv.Atoi(index)
if err == nil {
if idx < 0 {
idx = idx + len(ds.responseJson)
}
if idx >= len(ds.responseJson) || idx < 0 {
// index out of range
return "", DatastoreIndexOutOfBoundsError{error: fmt.Sprintf("datastore.Get: idx out of range: %d, current length: %d", idx, len(ds.responseJson))}
}
return ds.responseJson[idx], nil
}
var ok bool
res, ok = ds.storage[index]
if ok {
return res, nil
}
}
return "", nil
}