-
Notifications
You must be signed in to change notification settings - Fork 6
/
operations.go
310 lines (259 loc) · 7.28 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
package transform
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/PaesslerAG/jsonpath"
)
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]
}
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")
}
splits := strings.Split(in, s.args["on"])
// Return []interface{} to avoid messing up type casts later in the process
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
}
// 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
}
// stringToInteger is a transformOperation which takes a string and converts it into an Int
type stringToInteger struct {
}
func (s *stringToInteger) init(args map[string]string) error {
return nil
}
func (s *stringToInteger) transform(raw interface{}) (interface{}, error) {
str, ok := raw.(string)
if !ok {
return nil, errors.New("stringToInteger only supports strings")
}
return strconv.Atoi(str)
}
// 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
}