forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.go
1515 lines (1277 loc) · 33.8 KB
/
functions.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package stateful
import (
"errors"
"fmt"
"math"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"github.com/dustin/go-humanize"
"github.com/influxdata/influxdb/influxql"
"github.com/influxdata/kapacitor/tick/ast"
)
// maxArgs is used to specify the largest number of arguments that a
// builtin function can accept.
// Increment this value if you create a builtin function with more than
// the current value of maxArgs.
const (
maxArgs = 4
)
type ErrMissingType struct {
Name string
Args []string
Scope []string
}
func (e ErrMissingType) Error() string {
s := "Cannot call function \"%s\" argument %s is missing, values in scope are [%s]"
if len(e.Args) > 1 {
s = "Cannot call function \"%s\" arguments %s are missing, values in scope are [%s]"
}
// remove missing values from scope
for _, a := range e.Args {
e.Scope = removeElement(e.Scope, a)
}
return fmt.Sprintf(s, e.Name, strings.Join(e.Args, ", "), strings.Join(e.Scope, ", "))
}
func removeElement(xs []string, el string) []string {
for i, x := range xs {
if x == el {
xs = append(xs[:i], xs[i+1:]...)
break
}
}
return xs
}
type ErrWrongFuncSignature struct {
Name string
DomainProvided Domain
ArgLiterals []string
Func Func
}
func (e ErrWrongFuncSignature) Error() string {
var argStringer fmt.Stringer = &argDomain{args: e.ArgLiterals, domain: e.DomainProvided}
if e.ArgLiterals == nil {
argStringer = e.DomainProvided
}
return fmt.Sprintf("Cannot call function \"%s\" with args %s, available signatures are %s.",
e.Name, argStringer, FuncDomains(e.Func))
}
type argDomain struct {
args []string
domain Domain
}
func (a *argDomain) String() string {
input := []string{}
for j, el := range a.args {
t := a.domain[j]
input = append(input, fmt.Sprintf("%s: %s", el, t))
}
return "(" + strings.Join(input, ",") + ")"
}
var ErrNotFloat = errors.New("value is not a float")
type Domain [maxArgs]ast.ValueType
func (d Domain) String() string {
input := []string{}
for _, el := range d {
if el == ast.InvalidType {
// Because inputs should be consecutive
break
}
input = append(input, el.String())
}
return "(" + strings.Join(input, ",") + ")"
}
type Domains []Domain
func (ds Domains) String() string {
input := []string{}
for _, d := range ds {
input = append(input, d.String())
}
return "[" + strings.Join(input, ", ") + "]"
}
// A callable function from within the expression
type Func interface {
Reset()
Call(...interface{}) (interface{}, error)
Signature() map[Domain]ast.ValueType
}
func FuncDomains(f Func) Domains {
ds := []Domain{}
for d := range f.Signature() {
ds = append(ds, d)
}
return ds
}
// Lookup for functions
type Funcs map[string]Func
var statelessFuncs Funcs
var builtinFuncs Funcs
func init() {
statelessFuncs = make(Funcs)
// Conversion functions
statelessFuncs["bool"] = boolean{}
statelessFuncs["int"] = integer{}
statelessFuncs["float"] = float{}
statelessFuncs["string"] = str{}
statelessFuncs["duration"] = duration{}
// Math functions
statelessFuncs["abs"] = newMath1("abs", math.Abs)
statelessFuncs["acos"] = newMath1("acos", math.Acos)
statelessFuncs["acosh"] = newMath1("acosh", math.Acosh)
statelessFuncs["asin"] = newMath1("asin", math.Asin)
statelessFuncs["asinh"] = newMath1("asinh", math.Asinh)
statelessFuncs["atan"] = newMath1("atan", math.Atan)
statelessFuncs["atan2"] = newMath2("atan2", math.Atan2)
statelessFuncs["atanh"] = newMath1("atanh", math.Atanh)
statelessFuncs["cbrt"] = newMath1("cbrt", math.Cbrt)
statelessFuncs["ceil"] = newMath1("ceil", math.Ceil)
statelessFuncs["cos"] = newMath1("cos", math.Cos)
statelessFuncs["cosh"] = newMath1("cosh", math.Cosh)
statelessFuncs["erf"] = newMath1("erf", math.Erf)
statelessFuncs["erfc"] = newMath1("erfc", math.Erfc)
statelessFuncs["exp"] = newMath1("exp", math.Exp)
statelessFuncs["exp2"] = newMath1("exp2", math.Exp2)
statelessFuncs["expm1"] = newMath1("expm1", math.Expm1)
statelessFuncs["floor"] = newMath1("floor", math.Floor)
statelessFuncs["gamma"] = newMath1("gamma", math.Gamma)
statelessFuncs["hypot"] = newMath2("hypot", math.Hypot)
statelessFuncs["j0"] = newMath1("j0", math.J0)
statelessFuncs["j1"] = newMath1("j1", math.J1)
statelessFuncs["jn"] = newMathIF("jn", math.Jn)
statelessFuncs["log"] = newMath1("log", math.Log)
statelessFuncs["log10"] = newMath1("log10", math.Log10)
statelessFuncs["log1p"] = newMath1("log1p", math.Log1p)
statelessFuncs["log2"] = newMath1("log2", math.Log2)
statelessFuncs["logb"] = newMath1("logb", math.Logb)
statelessFuncs["max"] = newMath2("max", math.Max)
statelessFuncs["min"] = newMath2("min", math.Min)
statelessFuncs["mod"] = newMath2("mod", math.Mod)
statelessFuncs["pow"] = newMath2("pow", math.Pow)
statelessFuncs["pow10"] = newMathI("pow10", math.Pow10)
statelessFuncs["sin"] = newMath1("sin", math.Sin)
statelessFuncs["sinh"] = newMath1("sinh", math.Sinh)
statelessFuncs["sqrt"] = newMath1("sqrt", math.Sqrt)
statelessFuncs["tan"] = newMath1("tan", math.Tan)
statelessFuncs["tanh"] = newMath1("tanh", math.Tanh)
statelessFuncs["trunc"] = newMath1("trunc", math.Trunc)
statelessFuncs["y0"] = newMath1("y0", math.Y0)
statelessFuncs["y1"] = newMath1("y1", math.Y1)
statelessFuncs["yn"] = newMathIF("yn", math.Yn)
// String functions
statelessFuncs["strContains"] = newString2Bool("strContains", strings.Contains)
statelessFuncs["strContainsAny"] = newString2Bool("strContainsAny", strings.ContainsAny)
statelessFuncs["strCount"] = newString2Int("strCount", strings.Count)
statelessFuncs["strHasPrefix"] = newString2Bool("strHasPrefix", strings.HasPrefix)
statelessFuncs["strHasSuffix"] = newString2Bool("strHasSuffix", strings.HasSuffix)
statelessFuncs["strIndex"] = newString2Int("strIndex", strings.Index)
statelessFuncs["strIndexAny"] = newString2Int("strIndexAny", strings.IndexAny)
statelessFuncs["strLastIndex"] = newString2Int("strLastIndex", strings.LastIndex)
statelessFuncs["strLastIndexAny"] = newString2Int("strLastIndexAny", strings.LastIndexAny)
statelessFuncs["strLength"] = strLength{}
statelessFuncs["strReplace"] = strReplace{}
statelessFuncs["strSubstring"] = strSubstring{}
statelessFuncs["strToLower"] = newString1String("strToLower", strings.ToLower)
statelessFuncs["strToUpper"] = newString1String("strToUpper", strings.ToUpper)
statelessFuncs["strTrim"] = newString2String("strTrim", strings.Trim)
statelessFuncs["strTrimLeft"] = newString2String("strTrimLeft", strings.TrimLeft)
statelessFuncs["strTrimPrefix"] = newString2String("strTrimPrefix", strings.TrimPrefix)
statelessFuncs["strTrimRight"] = newString2String("strTrimRight", strings.TrimRight)
statelessFuncs["strTrimSpace"] = newString1String("strTrimSpace", strings.TrimSpace)
statelessFuncs["strTrimSuffix"] = newString2String("strTrimSuffix", strings.TrimSuffix)
// Regex functions
statelessFuncs["regexReplace"] = regexReplace{}
// Missing functions
statelessFuncs["isPresent"] = isPresent{}
// Time functions
statelessFuncs["unixNano"] = unixNano{}
statelessFuncs["minute"] = minute{}
statelessFuncs["hour"] = hour{}
statelessFuncs["weekday"] = weekday{}
statelessFuncs["day"] = day{}
statelessFuncs["month"] = month{}
statelessFuncs["year"] = year{}
statelessFuncs["now"] = now{}
// Humanize functions
statelessFuncs["humanBytes"] = humanBytes{}
// Conditionals
statelessFuncs["if"] = ifFunc{}
// Create map of builtin functions after all functions have been added to statelessFuncs
builtinFuncs = NewFunctions()
}
// Return set of built-in Funcs
func NewFunctions() Funcs {
funcs := make(Funcs, len(statelessFuncs)+3)
for n, f := range statelessFuncs {
funcs[n] = f
}
// Statefull functions -- need new instance
funcs["sigma"] = &sigma{}
funcs["count"] = &count{}
funcs["spread"] = &spread{min: math.Inf(+1), max: math.Inf(-1)}
return funcs
}
type math1Func func(float64) float64
type math1 struct {
name string
f math1Func
}
func newMath1(name string, f math1Func) math1 {
return math1{
name: name,
f: f,
}
}
func (m math1) Call(args ...interface{}) (v interface{}, err error) {
if len(args) != 1 {
return 0, errors.New(m.name + " expects exactly one argument")
}
a0, ok := args[0].(float64)
if !ok {
err = fmt.Errorf("cannot pass %T to %s, must be float64", args[0], m.name)
return
}
v = m.f(a0)
return
}
var math1FuncSignature = map[Domain]ast.ValueType{}
// Initialize Math 1 Func Signature
func init() {
d := Domain{}
d[0] = ast.TFloat
math1FuncSignature[d] = ast.TFloat
}
func (m math1) Signature() map[Domain]ast.ValueType {
return math1FuncSignature
}
func (m math1) Reset() {}
type math2Func func(float64, float64) float64
type math2 struct {
name string
f math2Func
}
func newMath2(name string, f math2Func) math2 {
return math2{
name: name,
f: f,
}
}
func (m math2) Call(args ...interface{}) (v interface{}, err error) {
if len(args) != 2 {
return 0, errors.New(m.name + " expects exactly two arguments")
}
a0, ok := args[0].(float64)
if !ok {
err = fmt.Errorf("cannot pass %T to %s, must be float64", args[0], m.name)
return
}
a1, ok := args[1].(float64)
if !ok {
err = fmt.Errorf("cannot pass %T to %s, must be float64", args[1], m.name)
return
}
v = m.f(a0, a1)
return
}
var math2FuncSignature = map[Domain]ast.ValueType{}
// Initialize Math 2 Function Signature
func init() {
d := Domain{}
d[0] = ast.TFloat
d[1] = ast.TFloat
math2FuncSignature[d] = ast.TFloat
}
func (m math2) Signature() map[Domain]ast.ValueType {
return math2FuncSignature
}
func (m math2) Reset() {}
type mathIFunc func(int) float64
type mathI struct {
name string
f mathIFunc
}
func newMathI(name string, f mathIFunc) mathI {
return mathI{
name: name,
f: f,
}
}
func (m mathI) Call(args ...interface{}) (v interface{}, err error) {
if len(args) != 1 {
return 0, errors.New(m.name + " expects exactly two arguments")
}
a0, ok := args[0].(int64)
if !ok {
err = fmt.Errorf("cannot pass %T to %s, must be int64", args[0], m.name)
return
}
v = m.f(int(a0))
return
}
var mathIFuncSignature = map[Domain]ast.ValueType{}
// Initialize Math I Function Signature
func init() {
d := Domain{}
d[0] = ast.TInt
mathIFuncSignature[d] = ast.TFloat
}
func (m mathI) Signature() map[Domain]ast.ValueType {
return mathIFuncSignature
}
func (m mathI) Reset() {}
type mathIFFunc func(int, float64) float64
type mathIF struct {
name string
f mathIFFunc
}
func newMathIF(name string, f mathIFFunc) mathIF {
return mathIF{
name: name,
f: f,
}
}
func (m mathIF) Call(args ...interface{}) (v interface{}, err error) {
if len(args) != 2 {
return 0, errors.New(m.name + " expects exactly two arguments")
}
a0, ok := args[0].(int64)
if !ok {
err = fmt.Errorf("cannot pass %T to %s as first arg, must be int64", args[0], m.name)
return
}
a1, ok := args[1].(float64)
if !ok {
err = fmt.Errorf("cannot pass %T to %s as second arg, must be float64", args[1], m.name)
return
}
v = m.f(int(a0), a1)
return
}
var mathIFFuncSignature = map[Domain]ast.ValueType{}
// Initialize Math IF Function Signature
func init() {
d := Domain{}
d[0] = ast.TInt
d[1] = ast.TFloat
mathIFFuncSignature[d] = ast.TFloat
}
func (m mathIF) Signature() map[Domain]ast.ValueType {
return mathIFFuncSignature
}
func (m mathIF) Reset() {}
type string2BoolFunc func(string, string) bool
type string2Bool struct {
name string
f string2BoolFunc
}
func newString2Bool(name string, f string2BoolFunc) string2Bool {
return string2Bool{
name: name,
f: f,
}
}
func (m string2Bool) Call(args ...interface{}) (v interface{}, err error) {
if len(args) != 2 {
return 0, errors.New(m.name + " expects exactly two arguments")
}
a0, ok := args[0].(string)
if !ok {
err = fmt.Errorf("cannot pass %T as first arg to %s, must be string", args[0], m.name)
return
}
a1, ok := args[1].(string)
if !ok {
err = fmt.Errorf("cannot pass %T as second arg to %s, must be string", args[1], m.name)
return
}
v = m.f(a0, a1)
return
}
var string2BoolFuncSignature = map[Domain]ast.ValueType{}
// Initialize String 2 Bool Function Signature
func init() {
d := Domain{}
d[0] = ast.TString
d[1] = ast.TString
string2BoolFuncSignature[d] = ast.TBool
}
func (m string2Bool) Signature() map[Domain]ast.ValueType {
return string2BoolFuncSignature
}
func (m string2Bool) Reset() {}
type string2IntFunc func(string, string) int
type string2Int struct {
name string
f string2IntFunc
}
func newString2Int(name string, f string2IntFunc) string2Int {
return string2Int{
name: name,
f: f,
}
}
func (m string2Int) Call(args ...interface{}) (v interface{}, err error) {
if len(args) != 2 {
return 0, errors.New(m.name + " expects exactly two arguments")
}
a0, ok := args[0].(string)
if !ok {
err = fmt.Errorf("cannot pass %T as first arg to %s, must be string", args[0], m.name)
return
}
a1, ok := args[1].(string)
if !ok {
err = fmt.Errorf("cannot pass %T as second arg to %s, must be string", args[1], m.name)
return
}
v = int64(m.f(a0, a1))
return
}
var string2IntFuncSignature = map[Domain]ast.ValueType{}
// Initialize String 2 Int Function Signature
func init() {
d := Domain{}
d[0] = ast.TString
d[1] = ast.TString
string2IntFuncSignature[d] = ast.TInt
}
func (m string2Int) Signature() map[Domain]ast.ValueType {
return string2IntFuncSignature
}
func (m string2Int) Reset() {}
type string2StringFunc func(string, string) string
type string2String struct {
name string
f string2StringFunc
}
func newString2String(name string, f string2StringFunc) string2String {
return string2String{
name: name,
f: f,
}
}
func (m string2String) Call(args ...interface{}) (v interface{}, err error) {
if len(args) != 2 {
return 0, errors.New(m.name + " expects exactly two arguments")
}
a0, ok := args[0].(string)
if !ok {
err = fmt.Errorf("cannot pass %T as first arg to %s, must be string", args[0], m.name)
return
}
a1, ok := args[1].(string)
if !ok {
err = fmt.Errorf("cannot pass %T as second arg to %s, must be string", args[1], m.name)
return
}
v = m.f(a0, a1)
return
}
var string2StringFuncSignature = map[Domain]ast.ValueType{}
// Initialize String 2 String Function Signature
func init() {
d := Domain{}
d[0] = ast.TString
d[1] = ast.TString
string2StringFuncSignature[d] = ast.TString
}
func (m string2String) Signature() map[Domain]ast.ValueType {
return string2StringFuncSignature
}
func (m string2String) Reset() {}
type string1StringFunc func(string) string
type string1String struct {
name string
f string1StringFunc
}
func newString1String(name string, f string1StringFunc) string1String {
return string1String{
name: name,
f: f,
}
}
func (m string1String) Call(args ...interface{}) (v interface{}, err error) {
if len(args) != 1 {
return 0, errors.New(m.name + " expects exactly one argument")
}
a0, ok := args[0].(string)
if !ok {
err = fmt.Errorf("cannot pass %T as first arg to %s, must be string", args[0], m.name)
return
}
v = m.f(a0)
return
}
var string1StringFuncSignature = map[Domain]ast.ValueType{}
// Initialize String 1 String Function Signature
func init() {
d := Domain{}
d[0] = ast.TString
string1StringFuncSignature[d] = ast.TString
}
func (m string1String) Signature() map[Domain]ast.ValueType {
return string1StringFuncSignature
}
func (m string1String) Reset() {}
type strLength struct {
}
func (m strLength) Call(args ...interface{}) (v interface{}, err error) {
if len(args) != 1 {
return 0, errors.New("strLength expects exactly one argument")
}
str, ok := args[0].(string)
if !ok {
err = fmt.Errorf("cannot pass %T as first arg to strLength, must be string", args[0])
return
}
v = int64(len(str))
return
}
var strLengthFuncSignature = map[Domain]ast.ValueType{}
// Initialize String Length Function Signature
func init() {
d := Domain{}
d[0] = ast.TString
strLengthFuncSignature[d] = ast.TInt
}
func (strLength) Signature() map[Domain]ast.ValueType {
return strLengthFuncSignature
}
func (m strLength) Reset() {}
type strReplace struct {
}
func (m strReplace) Call(args ...interface{}) (v interface{}, err error) {
if len(args) != 4 {
return 0, errors.New("strReplace expects exactly four arguments")
}
str, ok := args[0].(string)
if !ok {
err = fmt.Errorf("cannot pass %T as first arg to strReplace, must be string", args[0])
return
}
old, ok := args[1].(string)
if !ok {
err = fmt.Errorf("cannot pass %T as second arg to strReplace, must be string", args[1])
return
}
new, ok := args[2].(string)
if !ok {
err = fmt.Errorf("cannot pass %T as third arg to strReplace, must be string", args[2])
return
}
n, ok := args[3].(int64)
if !ok {
err = fmt.Errorf("cannot pass %T as fourth arg to strReplace, must be int", args[3])
return
}
v = strings.Replace(str, old, new, int(n))
return
}
var strReplaceFuncSignature = map[Domain]ast.ValueType{}
// Initialize String Replace Function Signature
func init() {
d := Domain{}
d[0] = ast.TString
d[1] = ast.TString
d[2] = ast.TString
d[3] = ast.TInt
strReplaceFuncSignature[d] = ast.TString
}
func (strReplace) Signature() map[Domain]ast.ValueType {
return strReplaceFuncSignature
}
func (m strReplace) Reset() {}
type strSubstring struct {
}
func (m strSubstring) Call(args ...interface{}) (v interface{}, err error) {
if len(args) != 3 {
return 0, errors.New("strSubstring expects exactly three arguments")
}
str, ok := args[0].(string)
if !ok {
err = fmt.Errorf("cannot pass %T as first arg to strSubstring, must be string", args[0])
return
}
start, ok := args[1].(int64)
if !ok {
err = fmt.Errorf("cannot pass %T as second arg to strSubstring, must be int", args[1])
return
}
stop, ok := args[2].(int64)
if !ok {
err = fmt.Errorf("cannot pass %T as third arg to strSubstring, must be int", args[2])
return
}
if start < 0 {
return nil, fmt.Errorf("found negative index for strSubstring: %d", start)
}
if stop < 0 {
return nil, fmt.Errorf("found negative index for strSubstring: %d", stop)
}
if int(stop) >= len(str) {
return nil, fmt.Errorf("stop index too large for string in strSubstring: %d", stop)
}
v = str[start:stop]
return
}
var strSubstringFuncSignature = map[Domain]ast.ValueType{}
// Initialize String Substring Function Signature
func init() {
d := Domain{}
d[0] = ast.TString
d[1] = ast.TInt
d[2] = ast.TInt
strSubstringFuncSignature[d] = ast.TString
}
func (strSubstring) Signature() map[Domain]ast.ValueType {
return strSubstringFuncSignature
}
func (m strSubstring) Reset() {}
type regexReplace struct {
}
func (m regexReplace) Call(args ...interface{}) (v interface{}, err error) {
if len(args) != 3 {
return 0, errors.New("regexReplace expects exactly three arguments")
}
pattern, ok := args[0].(*regexp.Regexp)
if !ok {
err = fmt.Errorf("cannot pass %T as first arg to regexReplace, must be regex", args[0])
return
}
src, ok := args[1].(string)
if !ok {
err = fmt.Errorf("cannot pass %T as second arg to regexReplace, must be string", args[1])
return
}
repl, ok := args[2].(string)
if !ok {
err = fmt.Errorf("cannot pass %T as third arg to regexReplace, must be string", args[2])
return
}
v = pattern.ReplaceAllString(src, repl)
return
}
var regexReplaceFuncSignature = map[Domain]ast.ValueType{}
// Initialize Regex Replace Function Signature
func init() {
d := Domain{}
d[0] = ast.TRegex
d[1] = ast.TString
d[2] = ast.TString
regexReplaceFuncSignature[d] = ast.TString
}
func (regexReplace) Signature() map[Domain]ast.ValueType {
return regexReplaceFuncSignature
}
func (m regexReplace) Reset() {}
type boolean struct {
}
func (boolean) Reset() {
}
// Converts the value to a boolean
func (boolean) Call(args ...interface{}) (v interface{}, err error) {
if len(args) != 1 {
return 0, errors.New("bool expects exactly one argument")
}
switch a := args[0].(type) {
case bool:
v = a
case string:
v, err = strconv.ParseBool(a)
case int64:
switch a {
case 0:
v = false
case 1:
v = true
default:
err = fmt.Errorf("cannot convert %v to boolean", a)
}
case float64:
switch a {
case 0:
v = false
case 1:
v = true
default:
err = fmt.Errorf("cannot convert %v to boolean", a)
}
default:
err = fmt.Errorf("cannot convert %T to boolean", a)
}
return
}
var booleanFuncSignature = map[Domain]ast.ValueType{}
// Initialize Boolean Function Signature
func init() {
d := Domain{}
d[0] = ast.TBool
booleanFuncSignature[d] = ast.TBool
d[0] = ast.TString
booleanFuncSignature[d] = ast.TBool
d[0] = ast.TInt
booleanFuncSignature[d] = ast.TBool
d[0] = ast.TFloat
booleanFuncSignature[d] = ast.TBool
}
func (boolean) Signature() map[Domain]ast.ValueType {
return booleanFuncSignature
}
type integer struct {
}
func (integer) Reset() {
}
// Converts the value to a integer
func (integer) Call(args ...interface{}) (v interface{}, err error) {
if len(args) != 1 {
return 0, errors.New("int expects exactly one argument")
}
switch a := args[0].(type) {
case int64:
v = a
case float64:
v = int64(a)
case string:
v, err = strconv.ParseInt(a, 10, 64)
case bool:
if a {
v = int64(1)
} else {
v = int64(0)
}
case time.Duration:
v = int64(a)
default:
err = fmt.Errorf("cannot convert %T to integer", a)
}
return
}
var integerFuncSignature = map[Domain]ast.ValueType{}
// Initialize Integer Function Signature
func init() {
d := Domain{}
d[0] = ast.TBool
integerFuncSignature[d] = ast.TInt
d[0] = ast.TString
integerFuncSignature[d] = ast.TInt
d[0] = ast.TInt
integerFuncSignature[d] = ast.TInt
d[0] = ast.TFloat
integerFuncSignature[d] = ast.TInt
}
func (integer) Signature() map[Domain]ast.ValueType {
return integerFuncSignature
}
type float struct {
}
func (float) Reset() {
}
// Converts the value to a float
func (float) Call(args ...interface{}) (v interface{}, err error) {
if len(args) != 1 {
return 0, errors.New("float expects exactly one argument")
}
switch a := args[0].(type) {
case int64:
v = float64(a)
case float64:
v = a
case string:
v, err = strconv.ParseFloat(a, 64)
case bool:
if a {
v = float64(1)
} else {
v = float64(0)
}
default:
err = fmt.Errorf("cannot convert %T to float", a)
}
return
}
var floatFuncSignature = map[Domain]ast.ValueType{}
// Initialize Float Function Signature
func init() {
d := Domain{}
d[0] = ast.TBool
floatFuncSignature[d] = ast.TFloat
d[0] = ast.TString
floatFuncSignature[d] = ast.TFloat
d[0] = ast.TInt
floatFuncSignature[d] = ast.TFloat
d[0] = ast.TFloat
floatFuncSignature[d] = ast.TFloat
}
func (float) Signature() map[Domain]ast.ValueType {
return floatFuncSignature
}
type str struct {
}
func (str) Reset() {
}
// Converts the value to a str
func (str) Call(args ...interface{}) (v interface{}, err error) {
if len(args) != 1 {
return 0, errors.New("string expects exactly one argument")
}
switch a := args[0].(type) {
case int64:
v = strconv.FormatInt(a, 10)
case float64:
v = strconv.FormatFloat(a, 'f', -1, 64)
case bool:
v = strconv.FormatBool(a)
case time.Duration:
v = influxql.FormatDuration(a)
case string:
v = a
default:
err = fmt.Errorf("cannot convert %T to string", a)
}
return
}
var stringFuncSignature = map[Domain]ast.ValueType{}
// Initialize String Function Signature
func init() {
d := Domain{}
d[0] = ast.TBool
stringFuncSignature[d] = ast.TString
d[0] = ast.TString
stringFuncSignature[d] = ast.TString
d[0] = ast.TInt
stringFuncSignature[d] = ast.TString
d[0] = ast.TFloat
stringFuncSignature[d] = ast.TString
d[0] = ast.TDuration
stringFuncSignature[d] = ast.TString
}
func (str) Signature() map[Domain]ast.ValueType {
return stringFuncSignature
}
type duration struct {
}
func (duration) Reset() {