forked from alash3al/sqler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macro.go
294 lines (240 loc) · 5.62 KB
/
macro.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
package main
import (
"encoding/json"
"fmt"
"strings"
"github.com/alash3al/go-color"
"github.com/go-resty/resty/v2"
"github.com/jmoiron/sqlx"
)
// Macro - a macro configuration
type Macro struct {
Methods []string
Include []string
Validators map[string]string
Authorizer string
Bind map[string]string
Exec string
Aggregate []string
Transformer string
Trigger struct {
Webhook string
Macro string
}
Cron string
name string
manager *Manager
}
// Call - executes the macro
func (m *Macro) Call(input map[string]interface{}) (interface{}, error) {
ok, err := m.authorize(input)
if err != nil {
return err.Error(), err
}
if !ok {
return errAuthorizationError.Error(), errAuthorizationError
}
invalid, err := m.validate(input)
if err != nil {
return err.Error(), err
} else if len(invalid) > 0 {
return invalid, errValidationError
}
if err := m.runIncludes(input); err != nil {
return err.Error(), err
}
var out interface{}
if len(m.Aggregate) > 0 {
out, err = m.aggregate(input)
if err != nil {
return err.Error(), err
}
} else {
out, err = m.execSQLQuery(strings.Split(strings.TrimSpace(m.Exec), *flagSQLSeparator), input)
if err != nil {
return err.Error(), err
}
}
out, err = m.transform(out)
if err != nil {
return err.Error(), err
}
go (func() {
if m.Trigger.Webhook != "" {
_, err := resty.New().R().SetDoNotParseResponse(true).SetHeader("Content-Type", "application/json").SetBody(map[string]interface{}{
"payload": out,
}).Post(m.Trigger.Webhook)
if err != nil {
color.Red("[X]- " + err.Error())
}
}
if subm := m.manager.Get(m.Trigger.Macro); subm != nil {
_, err := subm.Call(map[string]interface{}{
"payload": out,
})
if err != nil {
color.Red("[X]- " + err.Error())
}
}
})()
return out, nil
}
// execSQLQuery - execute the specified sql query
func (m *Macro) execSQLQuery(sqls []string, input map[string]interface{}) (interface{}, error) {
args, err := m.buildBind(input)
if err != nil {
return nil, err
}
conn, err := sqlx.Open(*flagDBDriver, *flagDBDSN)
if err != nil {
return nil, err
}
defer conn.Close()
for i, sql := range sqls {
if strings.TrimSpace(sql) == "" {
sqls = append(sqls[0:i], sqls[i+1:]...)
}
}
for _, sql := range sqls[0 : len(sqls)-1] {
sql = strings.TrimSpace(sql)
if "" == sql {
continue
}
if _, err := conn.NamedExec(sql, args); err != nil {
return nil, err
}
}
rows, err := conn.NamedQuery(sqls[len(sqls)-1], args)
if err != nil {
return nil, err
}
defer rows.Close()
ret := []map[string]interface{}{}
for rows.Next() {
row, err := m.scanSQLRow(rows)
if err != nil {
continue
}
ret = append(ret, row)
}
return interface{}(ret), nil
}
// scanSQLRow - scan a row from the specified rows
func (m *Macro) scanSQLRow(rows *sqlx.Rows) (map[string]interface{}, error) {
row := make(map[string]interface{})
if err := rows.MapScan(row); err != nil {
return nil, err
}
for k, v := range row {
if nil == v {
continue
}
switch v.(type) {
case []uint8:
v = []byte(v.([]uint8))
default:
v, _ = json.Marshal(v)
}
var d interface{}
if nil == json.Unmarshal(v.([]byte), &d) {
row[k] = d
} else {
row[k] = string(v.([]byte))
}
}
return row, nil
}
// transform - run the transformer function
func (m *Macro) transform(data interface{}) (interface{}, error) {
transformer := strings.TrimSpace(m.Transformer)
if transformer == "" {
return data, nil
}
vm := initJSVM(map[string]interface{}{"$result": data})
v, err := vm.RunString(transformer)
if err != nil {
return nil, err
}
return v.Export(), nil
}
// authorize - run the authorizer function
func (m *Macro) authorize(input map[string]interface{}) (bool, error) {
authorizer := strings.TrimSpace(m.Authorizer)
if authorizer == "" {
return true, nil
}
var execError error
vm := initJSVM(map[string]interface{}{"$input": input})
val, err := vm.RunString(m.Authorizer)
if err != nil {
return false, err
}
if execError != nil {
return false, execError
}
return val.ToBoolean(), nil
}
// aggregate - run the aggregators
func (m *Macro) aggregate(input map[string]interface{}) (map[string]interface{}, error) {
ret := map[string]interface{}{}
for _, k := range m.Aggregate {
macro := m.manager.Get(k)
if nil == macro {
err := fmt.Errorf("unknown macro %s", k)
return nil, err
}
out, err := macro.Call(input)
if err != nil {
return nil, err
}
ret[k] = out
}
return ret, nil
}
// validate - validate the input aginst the rules
func (m *Macro) validate(input map[string]interface{}) (ret []string, err error) {
if len(m.Validators) < 1 {
return nil, nil
}
vm := initJSVM(map[string]interface{}{"$input": input})
for k, src := range m.Validators {
val, err := vm.RunString(src)
if err != nil {
return nil, err
}
if !val.ToBoolean() {
ret = append(ret, k)
}
}
return ret, err
}
// buildBind - build the bind vars
func (m *Macro) buildBind(input map[string]interface{}) (map[string]interface{}, error) {
if len(m.Bind) < 1 {
return nil, nil
}
vm := initJSVM(map[string]interface{}{"$input": input})
ret := map[string]interface{}{}
for k, src := range m.Bind {
val, err := vm.RunString(src)
if err != nil {
return nil, err
}
ret[k] = val.Export()
}
return ret, nil
}
// runIncludes - run the include function
func (m *Macro) runIncludes(input map[string]interface{}) error {
for _, name := range m.Include {
macro := m.manager.Get(name)
if nil == macro {
return fmt.Errorf("macro %s not found", name)
}
_, err := macro.Call(input)
if err != nil {
return err
}
}
return nil
}