-
Notifications
You must be signed in to change notification settings - Fork 20
/
wrappers.go
334 lines (291 loc) · 10.3 KB
/
wrappers.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
package functions
import (
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/excellent/types"
)
// NumArgsCheck wraps an XFunc and checks the number of args
func NumArgsCheck(num int, f types.XFunc) types.XFunc {
return MinAndMaxArgsCheck(num, num, f)
}
// MinArgsCheck wraps an XFunc and checks the minimum number of args
func MinArgsCheck(min int, f types.XFunc) types.XFunc {
return MinAndMaxArgsCheck(min, -1, f)
}
// MinAndMaxArgsCheck wraps an XFunc and checks the number of args
func MinAndMaxArgsCheck(min int, max int, f types.XFunc) types.XFunc {
return func(env envs.Environment, args ...types.XValue) types.XValue {
if min == max {
// function requires a fixed number of arguments
if len(args) != min {
return types.NewXErrorf("need %d argument(s), got %d", min, len(args))
}
} else if max < 0 {
// function requires a minimum number of arguments
if len(args) < min {
return types.NewXErrorf("need at least %d argument(s), got %d", min, len(args))
}
} else {
// function requires the given range of arguments
if len(args) < min || len(args) > max {
return types.NewXErrorf("need %d to %d argument(s), got %d", min, max, len(args))
}
}
return f(env, args...)
}
}
// NoArgFunction creates an XFunc from a no-arg function
func NoArgFunction(f func(envs.Environment) types.XValue) types.XFunc {
return NumArgsCheck(0, func(env envs.Environment, args ...types.XValue) types.XValue {
return f(env)
})
}
// OneArgFunction creates an XFunc from a single-arg function
func OneArgFunction(f func(envs.Environment, types.XValue) types.XValue) types.XFunc {
return NumArgsCheck(1, func(env envs.Environment, args ...types.XValue) types.XValue {
return f(env, args[0])
})
}
// TwoArgFunction creates an XFunc from a two-arg function
func TwoArgFunction(f func(envs.Environment, types.XValue, types.XValue) types.XValue) types.XFunc {
return NumArgsCheck(2, func(env envs.Environment, args ...types.XValue) types.XValue {
return f(env, args[0], args[1])
})
}
// ThreeArgFunction creates an XFunc from a three-arg function
func ThreeArgFunction(f func(envs.Environment, types.XValue, types.XValue, types.XValue) types.XValue) types.XFunc {
return NumArgsCheck(3, func(env envs.Environment, args ...types.XValue) types.XValue {
return f(env, args[0], args[1], args[2])
})
}
// OneTextFunction creates an XFunc from a function that takes a single text arg
func OneTextFunction(f func(envs.Environment, types.XText) types.XValue) types.XFunc {
return NumArgsCheck(1, func(env envs.Environment, args ...types.XValue) types.XValue {
str, xerr := types.ToXText(env, args[0])
if xerr != nil {
return xerr
}
return f(env, str)
})
}
// TwoTextFunction creates an XFunc from a function that takes two text args
func TwoTextFunction(f func(envs.Environment, types.XText, types.XText) types.XValue) types.XFunc {
return NumArgsCheck(2, func(env envs.Environment, args ...types.XValue) types.XValue {
str1, xerr := types.ToXText(env, args[0])
if xerr != nil {
return xerr
}
str2, xerr := types.ToXText(env, args[1])
if xerr != nil {
return xerr
}
return f(env, str1, str2)
})
}
// TextAndNumberFunction creates an XFunc from a function that takes a text and a number arg
func TextAndNumberFunction(f func(envs.Environment, types.XText, types.XNumber) types.XValue) types.XFunc {
return NumArgsCheck(2, func(env envs.Environment, args ...types.XValue) types.XValue {
str, xerr := types.ToXText(env, args[0])
if xerr != nil {
return xerr
}
num, xerr := types.ToXNumber(env, args[1])
if xerr != nil {
return xerr
}
return f(env, str, num)
})
}
// TextAndIntegerFunction creates an XFunc from a function that takes a text and an integer arg
func TextAndIntegerFunction(f func(envs.Environment, types.XText, int) types.XValue) types.XFunc {
return NumArgsCheck(2, func(env envs.Environment, args ...types.XValue) types.XValue {
str, xerr := types.ToXText(env, args[0])
if xerr != nil {
return xerr
}
num, xerr := types.ToInteger(env, args[1])
if xerr != nil {
return xerr
}
return f(env, str, num)
})
}
// TextAndOptionalTextFunction creates an XFunc from a function that takes either one or two text args
func TextAndOptionalTextFunction(f func(envs.Environment, types.XText, types.XText) types.XValue, defaultVal types.XText) types.XFunc {
return MinAndMaxArgsCheck(1, 2, func(env envs.Environment, args ...types.XValue) types.XValue {
str1, xerr := types.ToXText(env, args[0])
if xerr != nil {
return xerr
}
str2 := defaultVal
if len(args) == 2 {
str2, xerr = types.ToXText(env, args[1])
if xerr != nil {
return xerr
}
}
return f(env, str1, str2)
})
}
// ThreeIntegerFunction creates an XFunc from a function that takes a text and an integer arg
func ThreeIntegerFunction(f func(envs.Environment, int, int, int) types.XValue) types.XFunc {
return NumArgsCheck(3, func(env envs.Environment, args ...types.XValue) types.XValue {
num1, xerr := types.ToInteger(env, args[0])
if xerr != nil {
return xerr
}
num2, xerr := types.ToInteger(env, args[1])
if xerr != nil {
return xerr
}
num3, xerr := types.ToInteger(env, args[2])
if xerr != nil {
return xerr
}
return f(env, num1, num2, num3)
})
}
// TextAndDateFunction creates an XFunc from a function that takes a text and a date arg
func TextAndDateFunction(f func(envs.Environment, types.XText, types.XDateTime) types.XValue) types.XFunc {
return NumArgsCheck(2, func(env envs.Environment, args ...types.XValue) types.XValue {
str, xerr := types.ToXText(env, args[0])
if xerr != nil {
return xerr
}
date, xerr := types.ToXDateTime(env, args[1])
if xerr != nil {
return xerr
}
return f(env, str, date)
})
}
// InitialTextFunction creates an XFunc from a function that takes an initial text arg followed by other args
func InitialTextFunction(minOtherArgs int, maxOtherArgs int, f func(envs.Environment, types.XText, ...types.XValue) types.XValue) types.XFunc {
return MinAndMaxArgsCheck(minOtherArgs+1, maxOtherArgs+1, func(env envs.Environment, args ...types.XValue) types.XValue {
str, xerr := types.ToXText(env, args[0])
if xerr != nil {
return xerr
}
return f(env, str, args[1:]...)
})
}
// OneNumberFunction creates an XFunc from a single number function
func OneNumberFunction(f func(envs.Environment, types.XNumber) types.XValue) types.XFunc {
return NumArgsCheck(1, func(env envs.Environment, args ...types.XValue) types.XValue {
num, xerr := types.ToXNumber(env, args[0])
if xerr != nil {
return xerr
}
return f(env, num)
})
}
// OneNumberAndOptionalIntegerFunction creates an XFunc from a function that takes a number and an optional integer
func OneNumberAndOptionalIntegerFunction(f func(envs.Environment, types.XNumber, int) types.XValue, defaultVal int) types.XFunc {
return MinAndMaxArgsCheck(1, 2, func(env envs.Environment, args ...types.XValue) types.XValue {
num, xerr := types.ToXNumber(env, args[0])
if xerr != nil {
return xerr
}
intVal := defaultVal
if len(args) == 2 {
intVal, xerr = types.ToInteger(env, args[1])
if xerr != nil {
return xerr
}
}
return f(env, num, intVal)
})
}
// TwoNumberFunction creates an XFunc from a function that takes two numbers
func TwoNumberFunction(f func(envs.Environment, types.XNumber, types.XNumber) types.XValue) types.XFunc {
return NumArgsCheck(2, func(env envs.Environment, args ...types.XValue) types.XValue {
num1, xerr := types.ToXNumber(env, args[0])
if xerr != nil {
return xerr
}
num2, xerr := types.ToXNumber(env, args[1])
if xerr != nil {
return xerr
}
return f(env, num1, num2)
})
}
// OneDateFunction creates an XFunc from a single date function
func OneDateFunction(f func(envs.Environment, types.XDate) types.XValue) types.XFunc {
return NumArgsCheck(1, func(env envs.Environment, args ...types.XValue) types.XValue {
date, xerr := types.ToXDate(env, args[0])
if xerr != nil {
return xerr
}
return f(env, date)
})
}
// OneDateTimeFunction creates an XFunc from a single datetime function
func OneDateTimeFunction(f func(envs.Environment, types.XDateTime) types.XValue) types.XFunc {
return NumArgsCheck(1, func(env envs.Environment, args ...types.XValue) types.XValue {
date, xerr := types.ToXDateTime(env, args[0])
if xerr != nil {
return xerr
}
return f(env, date)
})
}
// ObjectTextAndNumberFunction creates an XFunc from a function that takes an object, text and a number
func ObjectTextAndNumberFunction(f func(envs.Environment, *types.XObject, types.XText, types.XNumber) types.XValue) types.XFunc {
return NumArgsCheck(3, func(env envs.Environment, args ...types.XValue) types.XValue {
object, xerr := types.ToXObject(env, args[0])
if xerr != nil {
return xerr
}
text, xerr := types.ToXText(env, args[1])
if xerr != nil {
return xerr
}
num, xerr := types.ToXNumber(env, args[2])
if xerr != nil {
return xerr
}
return f(env, object, text, num)
})
}
// ObjectAndTextsFunction creates an XFunc from a function that takes an object and any number of text values
func ObjectAndTextsFunction(f func(envs.Environment, *types.XObject, ...types.XText) types.XValue) types.XFunc {
return MinArgsCheck(2, func(env envs.Environment, args ...types.XValue) types.XValue {
object, xerr := types.ToXObject(env, args[0])
if xerr != nil {
return xerr
}
texts := make([]types.XText, len(args)-1)
for i, arg := range args[1:] {
text, xerr := types.ToXText(env, arg)
if xerr != nil {
return xerr
}
texts[i] = text
}
return f(env, object, texts...)
})
}
// OneArrayFunction creates an XFunc from a single array function
func OneArrayFunction(f func(envs.Environment, *types.XArray) types.XValue) types.XFunc {
return NumArgsCheck(1, func(env envs.Environment, args ...types.XValue) types.XValue {
array, xerr := types.ToXArray(env, args[0])
if xerr != nil {
return xerr
}
return f(env, array)
})
}
// TwoArrayFunction creates an XFunc from a function that takes two arrays
func TwoArrayFunction(f func(envs.Environment, *types.XArray, *types.XArray) types.XValue) types.XFunc {
return NumArgsCheck(2, func(env envs.Environment, args ...types.XValue) types.XValue {
array1, xerr := types.ToXArray(env, args[0])
if xerr != nil {
return xerr
}
array2, xerr := types.ToXArray(env, args[1])
if xerr != nil {
return xerr
}
return f(env, array1, array2)
})
}