-
Notifications
You must be signed in to change notification settings - Fork 6
/
operations.go
406 lines (345 loc) · 9.57 KB
/
operations.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
394
395
396
397
398
399
400
401
402
403
404
405
406
package transform
import (
"errors"
"fmt"
"html"
"regexp"
"strconv"
"strings"
"time"
jsonpath "github.com/GannettDigital/PaesslerAG_jsonpath"
"github.com/microcosm-cc/bluemonday"
)
var durationRe = regexp.MustCompile(`^([\d]*?):?([\d]*):([\d]*)$`)
// duration is a transformOperation which changes from a string duration like
// "MM:SS" to a number of seconds as an integer.
type duration struct {
re *regexp.Regexp
}
func (c *duration) init(args map[string]string) error {
c.re = durationRe
return nil
}
func (c *duration) transform(raw interface{}) (interface{}, error) {
if array, ok := raw.([]interface{}); ok && len(array) == 1 {
raw = array[0]
}
if raw == nil {
return 0, nil
}
in, ok := raw.(string)
if !ok {
return nil, errors.New("duration only supports strings")
}
matches := c.re.FindStringSubmatch(in)
if matches == nil || len(matches) != 4 {
return nil, errors.New("duration transform input did not match 'MM:SS' or 'HH:MM:SS'")
}
hours, err := strconv.Atoi(matches[1])
if err != nil && matches[1] != "" {
return nil, err
}
minutes, err := strconv.Atoi(matches[2])
if err != nil && matches[2] != "" {
return nil, err
}
seconds, err := strconv.Atoi(matches[3])
if err != nil && matches[3] != "" {
return nil, err
}
minutes += 60 * hours
seconds += 60 * minutes
return seconds, nil
}
// changeCase is a transformOperation which changes the case of strings.
type changeCase struct {
args map[string]string
}
func (c *changeCase) init(args map[string]string) error {
if err := requiredArgs([]string{"to"}, args); err != nil {
return err
}
value := args["to"]
if value != "lower" && value != "upper" {
return errors.New("the argument 'to' is required and must be either 'lower' or 'upper'")
}
c.args = args
return nil
}
func (c *changeCase) transform(raw interface{}) (interface{}, error) {
in, ok := raw.(string)
if !ok {
return nil, errors.New("changeCase only supports strings")
}
switch c.args["to"] {
case "lower":
return strings.ToLower(in), nil
case "upper":
return strings.ToUpper(in), nil
}
return nil, errors.New("unknown error in changeCase")
}
// inverse is a transformOperation which flips the value of a boolean.
type inverse struct {
args map[string]string
}
func (i *inverse) init(args map[string]string) error {
return nil
}
func (i *inverse) transform(raw interface{}) (interface{}, error) {
in, ok := raw.(bool)
if !ok {
return nil, errors.New("inverse only supports booleans")
}
return !in, nil
}
// max is a transformOperation which retrieves a field from the maximum item in
// an array. The maxiumum item is determined by comparing values in a defined
// number field on the array items.
type max struct {
args map[string]string
}
func (m *max) init(args map[string]string) error {
if err := requiredArgs([]string{"by", "return"}, args); err != nil {
return err
}
m.args = args
return nil
}
func (m *max) transform(in interface{}) (interface{}, error) {
inArray, ok := in.([]interface{})
if !ok {
return nil, errors.New("input must be an array")
}
byArg := strings.Replace(m.args["by"], "@", "$", 1)
returnArg := strings.Replace(m.args["return"], "@", "$", 1)
var largest float64
var largestIndex int
for i, item := range inArray {
byRaw, err := jsonpath.Get(byArg, item)
if err != nil {
return nil, fmt.Errorf("failed extracting 'by' field: %v", err)
}
by, ok := byRaw.(float64)
if !ok {
byInt, ok := byRaw.(int)
if !ok {
return nil, errors.New("by field is not a number")
}
by = float64(byInt)
}
if by > largest {
largest = by
largestIndex = i
}
}
rawReturn, err := jsonpath.Get(returnArg, inArray[largestIndex])
if err != nil {
return nil, fmt.Errorf("failed extracting 'return' field: %v", err)
}
return rawReturn, nil
}
// replace is a transformOperation which performs a regex based find/replace on
// a string value.
type replace struct {
args map[string]string
regex *regexp.Regexp
}
func (r *replace) init(args map[string]string) error {
if err := requiredArgs([]string{"regex", "new"}, args); err != nil {
return err
}
re, err := regexp.Compile(args["regex"])
if err != nil {
return fmt.Errorf("failed to parse regex %q: %v", args["regex"], err)
}
r.regex = re
r.args = args
return nil
}
func (r *replace) transform(raw interface{}) (interface{}, error) {
if r.regex == nil {
return nil, errors.New("init was not run")
}
in, ok := raw.(string)
if !ok {
return nil, errors.New("replace only supports strings")
}
return r.regex.ReplaceAllString(in, r.args["new"]), nil
}
// split is a transformOperation which splits a string based on a given split string.
type split struct {
args map[string]string
}
func (s *split) init(args map[string]string) error {
if err := requiredArgs([]string{"on"}, args); err != nil {
return err
}
s.args = args
return nil
}
func (s *split) transform(raw interface{}) (interface{}, error) {
in, ok := raw.(string)
if !ok {
return nil, errors.New("split only supports strings")
}
// An empty string input should result in an empty array.
// strings.Split() will return []string{""} instead of an empty array.
// https://play.golang.org/p/8ySv_t37haN
if in == "" {
return []interface{}{}, nil
}
splits := strings.Split(in, s.args["on"])
// Return []interface{} to avoid messing up type casts later in the process
var interfaceSplits []interface{}
for _, s := range splits {
interfaceSplits = append(interfaceSplits, s)
}
return interfaceSplits, nil
}
// timeParse is a transformOperation which formats a date string into the layout.
type timeParse struct {
args map[string]string
}
func (t *timeParse) init(args map[string]string) error {
if err := requiredArgs([]string{"format", "layout"}, args); err != nil {
return err
}
t.args = args
return nil
}
func (t *timeParse) transform(raw interface{}) (interface{}, error) {
in, ok := raw.(string)
if !ok {
return nil, errors.New("timeParse only supports strings")
}
parsedTime, err := time.Parse(t.args["format"], in)
if err != nil {
return nil, fmt.Errorf("time could not be parsed using supplied format")
}
return parsedTime.Format(t.args["layout"]), nil
}
// currentTime is a transformOperation which returns the current time in a
// specified format.
type currentTime struct {
args map[string]string
}
func (c *currentTime) init(args map[string]string) error {
if err := requiredArgs([]string{"format"}, args); err != nil {
return err
}
c.args = args
return nil
}
func (c *currentTime) transform(_ interface{}) (interface{}, error) {
timeFmt := c.args["format"]
switch c.args["format"] {
case "RFC3339":
timeFmt = time.RFC3339
}
return time.Now().Format(timeFmt), nil
}
// toCamelCase is a transformOperation which converts strings with dashes to camelCase.
type toCamelCase struct {
args map[string]string
}
func (c *toCamelCase) init(args map[string]string) error {
if err := requiredArgs([]string{"delimiter"}, args); err != nil {
return err
}
c.args = args
return nil
}
func (c *toCamelCase) transform(raw interface{}) (interface{}, error) {
in, ok := raw.(string)
if !ok {
return nil, errors.New("toCamelCase only supports input of type string")
}
arr := strings.Split(in, c.args["delimiter"])
for i, cap := range arr {
if i == 0 {
arr[0] = strings.ToLower(cap)
continue
}
arr[i] = strings.Title(cap)
}
return strings.Join(arr, ""), nil
}
// removeHTML is a transformOperation which removes all html from a string.
type removeHTML struct {
args map[string]string
}
func (c *removeHTML) init(args map[string]string) error {
return nil
}
func (c *removeHTML) transform(raw interface{}) (interface{}, error) {
in, ok := raw.(string)
if !ok {
return nil, errors.New("removeHTML only supports input of type string")
}
p := bluemonday.NewPolicy().AddSpaceWhenStrippingTag(true)
sanitized := p.Sanitize(in)
s := strings.ReplaceAll(strings.TrimSpace(sanitized), " ", " ")
return html.UnescapeString(s), nil
}
// convertToFloat64 is a transformOperation which converts various types to float64.
type convertToFloat64 struct {
args map[string]string
}
func (c *convertToFloat64) init(args map[string]string) error {
return nil
}
func (c *convertToFloat64) transform(raw interface{}) (interface{}, error) {
switch in := raw.(type) {
case string:
return strconv.ParseFloat(in, 64)
case int:
return float64(in), nil
case float64:
return in, nil
default:
return nil, fmt.Errorf("convertToFloat64 only supports strings, int, and float64, raw type: %T", raw)
}
}
// convertToInt64 is a transformOperation which converts various types to int64.
type convertToInt64 struct {
args map[string]string
}
func (c *convertToInt64) init(args map[string]string) error {
return nil
}
func (c *convertToInt64) transform(raw interface{}) (interface{}, error) {
switch in := raw.(type) {
case string:
return strconv.ParseInt(in, 10, 64)
case float32:
return int64(in), nil
case float64:
return int64(in), nil
case int:
return int64(in), nil
case int8:
return int64(in), nil
case int16:
return int64(in), nil
case int32:
return int64(in), nil
case int64:
return int64(in), nil
default:
return nil, fmt.Errorf("convertToInt64 only supports strings, int, and float64, raw type: %T", raw)
}
}
// requiredArgs checks the given args map to make sure it contains the required args
// and only the required args.
func requiredArgs(required []string, args map[string]string) error {
if len(args) != len(required) {
return fmt.Errorf("expected args %v but got %d args", required, len(args))
}
for _, arg := range required {
if _, ok := args[arg]; !ok {
return fmt.Errorf("argument %q is required", arg)
}
}
return nil
}