-
Notifications
You must be signed in to change notification settings - Fork 152
/
regexp.go
215 lines (201 loc) · 7.96 KB
/
regexp.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
package regexp
import (
"context"
"regexp"
"github.com/influxdata/flux"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/semantic"
"github.com/influxdata/flux/values"
)
var SpecialFns map[string]values.Function
func init() {
SpecialFns = map[string]values.Function{
"compile": values.NewFunction(
"compile",
semantic.NewFunctionPolyType(semantic.FunctionPolySignature{
Parameters: map[string]semantic.PolyType{"v": semantic.String},
Required: semantic.LabelSet{"v"},
Return: semantic.Regexp,
}),
func(ctx context.Context, args values.Object) (values.Value, error) {
v, ok := args.Get("v")
if !ok {
return nil, errors.New(codes.Invalid, "missing argument v")
}
if v.Type().Nature() == semantic.String {
re, err := regexp.Compile(v.Str())
if err != nil {
return nil, err
}
return values.NewRegexp(re), err
}
return nil, errors.Newf(codes.Invalid, "cannot convert argument v of type %v to Regex", v.Type().Nature())
},
false,
),
"quoteMeta": values.NewFunction(
"quoteMeta",
semantic.NewFunctionPolyType(semantic.FunctionPolySignature{
Parameters: map[string]semantic.PolyType{"v": semantic.String},
Required: semantic.LabelSet{"v"},
Return: semantic.String,
}),
func(ctx context.Context, args values.Object) (values.Value, error) {
v, ok := args.Get("v")
if !ok {
return nil, errors.New(codes.Invalid, "missing argument v")
}
if v.Type().Nature() == semantic.String {
value := regexp.QuoteMeta(v.Str())
return values.NewString(value), nil
}
return nil, errors.Newf(codes.Invalid, "cannot escape all regular expression metacharacters inside argument v of type %v", v.Type().Nature())
},
false,
),
"findString": values.NewFunction(
"findString",
semantic.NewFunctionPolyType(semantic.FunctionPolySignature{
Parameters: map[string]semantic.PolyType{"r": semantic.Regexp, "v": semantic.String},
Required: semantic.LabelSet{"r", "v"},
Return: semantic.String,
}),
func(ctx context.Context, args values.Object) (values.Value, error) {
v, ok := args.Get("v")
r, okk := args.Get("r")
if !ok || !okk {
return nil, errors.New(codes.Invalid, "missing argument")
}
if v.Type().Nature() == semantic.String && r.Type().Nature() == semantic.Regexp {
value := r.Regexp().FindString(v.Str())
return values.NewString(value), nil
}
return nil, errors.Newf(codes.Invalid, "cannot execute function containing argument r of type %v and argument v of type %v", r.Type().Nature(), v.Type().Nature())
},
false,
),
"findStringIndex": values.NewFunction(
"findStringIndex",
semantic.NewFunctionPolyType(semantic.FunctionPolySignature{
Parameters: map[string]semantic.PolyType{"r": semantic.Regexp, "v": semantic.String},
Required: semantic.LabelSet{"r", "v"},
Return: semantic.Array,
}),
func(ctx context.Context, args values.Object) (values.Value, error) {
v, ok := args.Get("v")
r, okk := args.Get("r")
if !ok || !okk {
return nil, errors.New(codes.Invalid, "missing argument")
}
if v.Type().Nature() == semantic.String && r.Type().Nature() == semantic.Regexp {
value := r.Regexp().FindStringIndex(v.Str())
arr := values.NewArray(semantic.Int)
for _, z := range value {
arr.Append(values.NewInt(int64(z)))
}
return arr, nil
}
return nil, errors.Newf(codes.Invalid, "cannot execute function containing argument r of type %v and argument v of type %v", r.Type().Nature(), v.Type().Nature())
},
false,
),
"matchRegexpString": values.NewFunction(
"matchRegexpString",
semantic.NewFunctionPolyType(semantic.FunctionPolySignature{
Parameters: map[string]semantic.PolyType{"r": semantic.Regexp, "v": semantic.String},
Required: semantic.LabelSet{"r", "v"},
Return: semantic.Bool,
}),
func(ctx context.Context, args values.Object) (values.Value, error) {
v, ok := args.Get("v")
r, okk := args.Get("r")
if !ok || !okk {
return nil, errors.New(codes.Invalid, "missing argument")
}
if v.Type().Nature() == semantic.String && r.Type().Nature() == semantic.Regexp {
value := r.Regexp().MatchString(v.Str())
return values.NewBool(value), nil
}
return nil, errors.Newf(codes.Invalid, "cannot execute function containing argument r of type %v and argument v of type %v", r.Type().Nature(), v.Type().Nature())
},
false,
),
"replaceAllString": values.NewFunction(
"replaceAllString",
semantic.NewFunctionPolyType(semantic.FunctionPolySignature{
Parameters: map[string]semantic.PolyType{"r": semantic.Regexp, "v": semantic.String, "t": semantic.String},
Required: semantic.LabelSet{"r", "v", "t"},
Return: semantic.String,
}),
func(ctx context.Context, args values.Object) (values.Value, error) {
r, ok := args.Get("r")
v, okk := args.Get("v")
t, okkk := args.Get("t")
if !ok || !okk || !okkk {
return nil, errors.New(codes.Invalid, "missing argument")
}
if v.Type().Nature() == semantic.String && t.Type().Nature() == semantic.String && r.Type().Nature() == semantic.Regexp {
value := r.Regexp().ReplaceAllString(v.Str(), t.Str())
return values.NewString(value), nil
}
return nil, errors.Newf(codes.Invalid, "cannot execute function containing argument r of type %v, argument v of type %v, and argument t of type %v", r.Type().Nature(), v.Type().Nature(), t.Type().Nature())
},
false,
),
"splitRegexp": values.NewFunction(
"splitRegexp",
semantic.NewFunctionPolyType(semantic.FunctionPolySignature{
Parameters: map[string]semantic.PolyType{"r": semantic.Regexp, "v": semantic.String, "i": semantic.Int},
Required: semantic.LabelSet{"r", "v", "i"},
Return: semantic.Array,
}),
func(ctx context.Context, args values.Object) (values.Value, error) {
r, ok := args.Get("r")
v, okk := args.Get("v")
i, okkk := args.Get("i")
if !ok || !okk || !okkk {
return nil, errors.New(codes.Invalid, "missing argument")
}
if v.Type().Nature() == semantic.String && i.Type().Nature() == semantic.Int && r.Type().Nature() == semantic.Regexp {
value := r.Regexp().Split(v.Str(), int(i.Int()))
arr := values.NewArray(semantic.String)
for _, z := range value {
arr.Append(values.NewString(z))
}
return arr, nil
}
return nil, errors.Newf(codes.Invalid, "cannot execute function containing argument r of type %v, argument v of type %v, and argument i of type %v", r.Type().Nature(), v.Type().Nature(), i.Type().Nature())
},
false,
),
"getString": values.NewFunction(
"getString",
semantic.NewFunctionPolyType(semantic.FunctionPolySignature{
Parameters: map[string]semantic.PolyType{"r": semantic.Regexp},
Required: semantic.LabelSet{"r"},
Return: semantic.String,
}),
func(ctx context.Context, args values.Object) (values.Value, error) {
r, ok := args.Get("r")
if !ok {
return nil, errors.New(codes.Invalid, "missing argument")
}
if r.Type().Nature() == semantic.Regexp {
value := r.Regexp().String()
return values.NewString(value), nil
}
return nil, errors.Newf(codes.Invalid, "cannot execute function containing argument r of type %v", r.Type().Nature())
},
false,
),
}
flux.RegisterPackageValue("regexp", "compile", SpecialFns["compile"])
flux.RegisterPackageValue("regexp", "quoteMeta", SpecialFns["quoteMeta"])
flux.RegisterPackageValue("regexp", "findString", SpecialFns["findString"])
flux.RegisterPackageValue("regexp", "findStringIndex", SpecialFns["findStringIndex"])
flux.RegisterPackageValue("regexp", "matchRegexpString", SpecialFns["matchRegexpString"])
flux.RegisterPackageValue("regexp", "replaceAllString", SpecialFns["replaceAllString"])
flux.RegisterPackageValue("regexp", "splitRegexp", SpecialFns["splitRegexp"])
flux.RegisterPackageValue("regexp", "getString", SpecialFns["getString"])
}