Skip to content

Commit 3fe726e

Browse files
committedDec 23, 2020
20+ functions
1 parent 425a3a9 commit 3fe726e

10 files changed

+1016
-1426
lines changed
 

‎.bumpversion.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 0.0.9
2+
current_version = 0.0.10
33
commit = False
44
tag = False
55

‎Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version := "0.0.9"
1+
version := "0.0.10"
22

33
.DEFAULT_GOAL := help
44

‎README.md

+28-1,261
Large diffs are not rendered by default.

‎decision.go

+1-89
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ package trigger
22

33
import (
44
"github.com/google/cel-go/cel"
5-
"github.com/google/cel-go/checker/decls"
6-
"github.com/google/cel-go/interpreter/functions"
75
"github.com/pkg/errors"
8-
expr "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
96
)
107

118
var (
@@ -15,7 +12,6 @@ var (
1512

1613
// Decision is used to evaluate boolean expressions
1714
type Decision struct {
18-
e *cel.Env
1915
program cel.Program
2016
expression string
2117
}
@@ -25,95 +21,11 @@ func NewDecision(expression string) (*Decision, error) {
2521
if expression == "" {
2622
return nil, ErrEmptyExpressions
2723
}
28-
e, err := cel.NewEnv(
29-
cel.Declarations(
30-
decls.NewVar("this", decls.NewMapType(decls.String, decls.Any)),
31-
decls.NewFunction("now",
32-
decls.NewOverload(
33-
"now",
34-
[]*expr.Type{},
35-
decls.Int,
36-
),
37-
),
38-
decls.NewFunction("uuid",
39-
decls.NewOverload(
40-
"uuid",
41-
[]*expr.Type{},
42-
decls.String,
43-
),
44-
),
45-
decls.NewFunction("sha1",
46-
decls.NewOverload(
47-
"sha1",
48-
[]*expr.Type{decls.String},
49-
decls.String,
50-
),
51-
),
52-
decls.NewFunction("sha256",
53-
decls.NewOverload(
54-
"sha256",
55-
[]*expr.Type{decls.String},
56-
decls.String,
57-
),
58-
),
59-
decls.NewFunction("base64Decode",
60-
decls.NewOverload(
61-
"base64Decode",
62-
[]*expr.Type{decls.String},
63-
decls.String,
64-
),
65-
),
66-
decls.NewFunction("base64Encode",
67-
decls.NewOverload(
68-
"base64Encode",
69-
[]*expr.Type{decls.String},
70-
decls.String,
71-
),
72-
),
73-
),
74-
)
75-
if err != nil {
76-
return nil, err
77-
}
78-
if expression == "" {
79-
return nil, errors.New("empty expression")
80-
}
81-
ast, iss := e.Compile(expression)
82-
if iss.Err() != nil {
83-
return nil, iss.Err()
84-
}
85-
program, err := e.Program(ast,
86-
cel.Functions(
87-
&functions.Overload{
88-
Operator: "now",
89-
Function: defaultFuncMap["now"],
90-
},
91-
&functions.Overload{
92-
Operator: "sha256",
93-
Function: defaultFuncMap["sha256"],
94-
},
95-
&functions.Overload{
96-
Operator: "base64Encode",
97-
Function: defaultFuncMap["base64Encode"],
98-
},
99-
&functions.Overload{
100-
Operator: "base64Decode",
101-
Function: defaultFuncMap["base64Decode"],
102-
},
103-
&functions.Overload{
104-
Operator: "uuid",
105-
Function: defaultFuncMap["uuid"],
106-
},
107-
&functions.Overload{
108-
Operator: "sha1",
109-
Function: defaultFuncMap["sha1"],
110-
}),
111-
)
24+
program, err := globalEnv.Program(expression)
11225
if err != nil {
11326
return nil, err
11427
}
11528
return &Decision{
116-
e: e,
11729
program: program,
11830
expression: expression,
11931
}, nil

‎env.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package trigger
2+
3+
import (
4+
"fmt"
5+
"github.com/google/cel-go/cel"
6+
"github.com/google/cel-go/checker/decls"
7+
"github.com/google/cel-go/interpreter/functions"
8+
"github.com/graphikDB/generic"
9+
expr "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
10+
"os"
11+
"time"
12+
)
13+
14+
func init() {
15+
var err error
16+
var declarations = []*expr.Decl{
17+
decls.NewVar("this", decls.NewMapType(decls.String, decls.Any)),
18+
}
19+
for _, function := range Functions {
20+
declarations = append(declarations, function.decl)
21+
}
22+
env, err := cel.NewEnv(cel.Declarations(declarations...))
23+
if err != nil {
24+
fmt.Println(err.Error())
25+
os.Exit(1)
26+
}
27+
globalEnv = &environment{
28+
env: env,
29+
cache: generic.NewCache(1 * time.Minute),
30+
}
31+
}
32+
33+
type environment struct {
34+
env *cel.Env
35+
cache *generic.Cache
36+
}
37+
38+
func (e *environment) Program(expression string) (cel.Program, error) {
39+
if val, ok := e.cache.Get(expression); ok {
40+
if program, ok := val.(cel.Program); ok {
41+
return program, nil
42+
}
43+
}
44+
ast, iss := globalEnv.env.Compile(expression)
45+
if iss.Err() != nil {
46+
return nil, iss.Err()
47+
}
48+
var overloads []*functions.Overload
49+
for _, function := range Functions {
50+
overloads = append(overloads, function.overload)
51+
}
52+
program, err := globalEnv.env.Program(
53+
ast,
54+
cel.Functions(overloads...),
55+
)
56+
if err != nil {
57+
return nil, err
58+
}
59+
e.cache.Set(expression, program, 5*time.Minute)
60+
return program, nil
61+
}
62+
63+
var globalEnv *environment

‎eval_test.go

+308-3
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ func ExampleNewTrigger() {
6868
{
6969
'admin': true,
7070
'updated_at': now(),
71-
'email_hash': sha1(this.email)
71+
'email_sha1': sha1("this.email"),
72+
'email_sha3': sha3("this.email"),
73+
'email_sha256': sha256("this.email")
7274
}
7375
`)
7476
if err != nil {
@@ -84,6 +86,309 @@ func ExampleNewTrigger() {
8486
fmt.Println(err.Error())
8587
return
8688
}
87-
fmt.Println(data["admin"], data["updated_at"].(int64) > 0, data["email_hash"])
88-
// Output: true true 6fd706dd2d151c2bf79218a2acd764a7d3eed7e3
89+
fmt.Println(data["admin"], data["updated_at"].(int64) > 0, data["email_sha1"], data["email_sha3"], data["email_sha256"])
90+
// Output: true true bbd5d1877fc1db4e1dc12fbd39dd0989cf422be5 1ec04699856dcbef0f32413a71b6c8a1228de6663f46159f0084b0ecbccb7a8ca3e7928028650ad318f2d52e2ed5b9edecfc46c088557e5fa640f94c3fec8c46 2fee51920dc7672e5c66b328a4b4fff0382c4552f893f7a92747a213085855dd
8991
}
92+
93+
func TestDecision_Eval(t *testing.T) {
94+
type fields struct {
95+
expression string
96+
}
97+
type args struct {
98+
data map[string]interface{}
99+
}
100+
tests := []struct {
101+
name string
102+
fields fields
103+
args args
104+
wantErr bool
105+
}{
106+
{
107+
name: "hello world equality",
108+
fields: fields{
109+
expression: "this.text == 'hello world'",
110+
},
111+
args: args{
112+
data: map[string]interface{}{
113+
"text": "hello world",
114+
},
115+
},
116+
wantErr: false,
117+
},
118+
{
119+
name: "hello world inequlity",
120+
fields: fields{
121+
expression: "this.text != 'hello world'",
122+
},
123+
args: args{
124+
data: map[string]interface{}{
125+
"text": "hello world",
126+
},
127+
},
128+
wantErr: true,
129+
},
130+
{
131+
name: "hello world sha1",
132+
fields: fields{
133+
expression: "sha1(this.text) != 'hello world'",
134+
},
135+
args: args{
136+
data: map[string]interface{}{
137+
"text": "hello world",
138+
},
139+
},
140+
wantErr: false,
141+
},
142+
{
143+
name: "hello world sha3",
144+
fields: fields{
145+
expression: "sha3(this.text) != 'hello world'",
146+
},
147+
args: args{
148+
data: map[string]interface{}{
149+
"text": "hello world",
150+
},
151+
},
152+
wantErr: false,
153+
},
154+
{
155+
name: "hello world sha256",
156+
fields: fields{
157+
expression: "sha256(this.text) != 'hello world'",
158+
},
159+
args: args{
160+
data: map[string]interface{}{
161+
"text": "hello world",
162+
},
163+
},
164+
wantErr: false,
165+
},
166+
{
167+
name: "hello world base64Encode",
168+
fields: fields{
169+
expression: "base64Encode(this.text) == 'aGVsbG8gd29ybGQ='",
170+
},
171+
args: args{
172+
data: map[string]interface{}{
173+
"text": "hello world",
174+
},
175+
},
176+
wantErr: false,
177+
},
178+
{
179+
name: "hello world base64Decode",
180+
fields: fields{
181+
expression: "base64Decode(this.text) == 'hello world'",
182+
},
183+
args: args{
184+
data: map[string]interface{}{
185+
"text": "aGVsbG8gd29ybGQ=",
186+
},
187+
},
188+
wantErr: false,
189+
},
190+
{
191+
name: "hello world jsonDecode",
192+
fields: fields{
193+
expression: "jsonDecode(this.text).text == 'hello world'",
194+
},
195+
args: args{
196+
data: map[string]interface{}{
197+
"text": `{ "text": "hello world"}`,
198+
},
199+
},
200+
wantErr: false,
201+
},
202+
{
203+
name: "hello world includes",
204+
fields: fields{
205+
expression: "includes(this.text, 'hello world')",
206+
},
207+
args: args{
208+
data: map[string]interface{}{
209+
"text": []string{"hello world"},
210+
},
211+
},
212+
wantErr: false,
213+
},
214+
{
215+
name: "1993 includes",
216+
fields: fields{
217+
expression: "includes(this.dob, 1993)",
218+
},
219+
args: args{
220+
data: map[string]interface{}{
221+
"dob": []int64{1993},
222+
},
223+
},
224+
wantErr: false,
225+
},
226+
{
227+
name: "hello world replace",
228+
fields: fields{
229+
expression: "replace(this.text, ' ', '') == 'helloworld'",
230+
},
231+
args: args{
232+
data: map[string]interface{}{
233+
"text": "hello world",
234+
},
235+
},
236+
wantErr: false,
237+
},
238+
{
239+
name: "hello world join",
240+
fields: fields{
241+
expression: "join(this.text, ' ') == 'hello world'",
242+
},
243+
args: args{
244+
data: map[string]interface{}{
245+
"text": []string{"hello", "world"},
246+
},
247+
},
248+
wantErr: false,
249+
},
250+
{
251+
name: "hello world titleCase",
252+
fields: fields{
253+
expression: "titleCase(this.text) == 'Hello World'",
254+
},
255+
args: args{
256+
data: map[string]interface{}{
257+
"text": "hello world",
258+
},
259+
},
260+
wantErr: false,
261+
},
262+
{
263+
name: "hello world split",
264+
fields: fields{
265+
expression: "includes(split(this.text, ' '), 'hello')",
266+
},
267+
args: args{
268+
data: map[string]interface{}{
269+
"text": "hello world",
270+
},
271+
},
272+
wantErr: false,
273+
},
274+
{
275+
name: "denver to la",
276+
fields: fields{
277+
expression: "int(geoDistance(this.denver, this.los_angelas)) > 1336367 && int(geoDistance(this.denver, this.los_angelas)) < 1536367",
278+
},
279+
args: args{
280+
data: map[string]interface{}{
281+
"denver": []float64{39.739235, -104.990250},
282+
"los_angelas": []float64{34.052235, -118.243683},
283+
},
284+
},
285+
wantErr: false,
286+
},
287+
{
288+
name: "render hello world",
289+
fields: fields{
290+
expression: "render(this.text, this.data) == 'hello world'",
291+
},
292+
args: args{
293+
data: map[string]interface{}{
294+
"text": "{{ .text }}",
295+
"data": map[string]interface{}{
296+
"text": "hello world",
297+
},
298+
},
299+
},
300+
wantErr: false,
301+
},
302+
{
303+
name: "parseClaims",
304+
fields: fields{
305+
expression: "parseClaims(this.jwt).name == 'John Doe'",
306+
},
307+
args: args{
308+
data: map[string]interface{}{
309+
"jwt": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
310+
},
311+
},
312+
wantErr: false,
313+
},
314+
{
315+
name: "typeOf",
316+
fields: fields{
317+
expression: "typeOf(this.jwt) == 'string'",
318+
},
319+
args: args{
320+
data: map[string]interface{}{
321+
"jwt": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
322+
},
323+
},
324+
wantErr: false,
325+
},
326+
}
327+
for _, tt := range tests {
328+
t.Run(tt.name, func(t *testing.T) {
329+
decision, err := trigger.NewDecision(tt.fields.expression)
330+
if err != nil && !tt.wantErr {
331+
t.Errorf("Eval() error = %v, wantErr %v", err, tt.wantErr)
332+
return
333+
}
334+
if decision != nil {
335+
if err := decision.Eval(tt.args.data); (err != nil) != tt.wantErr {
336+
if (err != nil) != tt.wantErr {
337+
t.Errorf("Eval() error = %v, wantErr %v", err, tt.wantErr)
338+
return
339+
}
340+
}
341+
}
342+
})
343+
}
344+
}
345+
346+
//
347+
//func TestTrigger_Trigger(t1 *testing.T) {
348+
// type fields struct {
349+
// decision *trigger.Decision
350+
// expression string
351+
// }
352+
// type args struct {
353+
// data map[string]interface{}
354+
// }
355+
// tests := []struct {
356+
// name string
357+
// fields fields
358+
// args args
359+
// want map[string]interface{}
360+
// wantErr bool
361+
// }{
362+
// {
363+
// name: "",
364+
// fields: fields{
365+
// decision: nil,
366+
// expression: "",
367+
// },
368+
// args: args{
369+
// data: map[string]interface{}{},
370+
// },
371+
// want: nil,
372+
// wantErr: false,
373+
// },
374+
// }
375+
// for _, tt := range tests {
376+
// t1.Run(tt.name, func(t1 *testing.T) {
377+
// t, err := trigger.NewTrigger(tt.fields.decision, tt.fields.expression)
378+
// if (err != nil) != tt.wantErr {
379+
// t1.Errorf("Trigger() error = %v, wantErr %v", err, tt.wantErr)
380+
// return
381+
// }
382+
// if t != nil {
383+
// got, err := t.Trigger(tt.args.data)
384+
// if (err != nil) != tt.wantErr {
385+
// t1.Errorf("Trigger() error = %v, wantErr %v", err, tt.wantErr)
386+
// return
387+
// }
388+
// if !reflect.DeepEqual(got, tt.want) {
389+
// t1.Errorf("Trigger() got = %v, want %v", got, tt.want)
390+
// }
391+
// }
392+
// })
393+
// }
394+
//}

‎functions.go

+590-26
Large diffs are not rendered by default.

‎go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ go 1.15
55
require (
66
github.com/google/cel-go v0.6.1-0.20201210004405-3ea8bd382b11
77
github.com/google/uuid v1.1.2
8+
github.com/graphikDB/generic v0.0.1
9+
github.com/paulmach/orb v0.1.7
810
github.com/pkg/errors v0.9.1
11+
github.com/spf13/cast v1.3.1
12+
github.com/thoas/go-funk v0.7.0
13+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
914
google.golang.org/genproto v0.0.0-20201102152239-715cce707fb0
1015
)

‎go.sum

+18
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
77
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
88
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
99
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
10+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
11+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1012
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
1113
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
1214
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
1315
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
16+
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
1417
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
1518
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
1619
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@@ -34,16 +37,29 @@ github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w=
3437
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
3538
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
3639
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
40+
github.com/graphikDB/generic v0.0.1 h1:0NIlU/Hk50dx+tCh5KgyZ+1l0IWgcOkb32fKX+j7WQE=
41+
github.com/graphikDB/generic v0.0.1/go.mod h1:3Dji4QoogUaekuR3a+qDFRAmut1CUtsMkhVkW0oGAiw=
42+
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
43+
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
44+
github.com/paulmach/orb v0.1.7 h1:Lwv10ANhqpTH3Kw5qow4YpSW5RLAx67nNGgbJpv/GC0=
45+
github.com/paulmach/orb v0.1.7/go.mod h1:qakeIafyxF4NlRIgpXp3awhLCNuqhl3lyNpkWws2BNQ=
3746
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
3847
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
3948
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4049
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
4150
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
51+
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
52+
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
4253
github.com/stoewer/go-strcase v1.2.0 h1:Z2iHWqGXH00XYgqDmNgQbIBxf3wrNq0F3feEy0ainaU=
4354
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
4455
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
56+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
57+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
4558
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
4659
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
60+
github.com/thoas/go-funk v0.7.0 h1:GmirKrs6j6zJbhJIficOsz2aAI7700KsU/5YrdHRM1Y=
61+
github.com/thoas/go-funk v0.7.0/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q=
62+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
4763
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
4864
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
4965
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
@@ -60,11 +76,13 @@ golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJ
6076
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
6177
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
6278
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
79+
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So=
6380
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
6481
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
6582
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
6683
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
6784
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
85+
golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
6886
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
6987
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
7088
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=

‎trigger.go

+1-45
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@ package trigger
22

33
import (
44
"github.com/google/cel-go/cel"
5-
"github.com/google/cel-go/checker/decls"
65
"github.com/google/cel-go/common/types/ref"
7-
"github.com/google/cel-go/interpreter/functions"
86
"github.com/pkg/errors"
9-
expr "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
107
)
118

129
// Trigger creates values as map[string]interface{} if it's decisider returns no errors against a Mapper
1310
type Trigger struct {
14-
e *cel.Env
1511
decision *Decision
1612
program cel.Program
1713
expression string
@@ -22,51 +18,11 @@ func NewTrigger(decision *Decision, triggerExpression string) (*Trigger, error)
2218
if triggerExpression == "" {
2319
return nil, ErrEmptyExpressions
2420
}
25-
e, err := cel.NewEnv(
26-
cel.Declarations(
27-
decls.NewVar("this", decls.NewMapType(decls.String, decls.Any)),
28-
decls.NewFunction("now",
29-
decls.NewOverload(
30-
"now",
31-
[]*expr.Type{},
32-
decls.Int,
33-
),
34-
),
35-
decls.NewFunction("sha1",
36-
decls.NewOverload(
37-
"sha1_string",
38-
[]*expr.Type{decls.String},
39-
decls.String,
40-
),
41-
),
42-
),
43-
)
44-
if err != nil {
45-
return nil, err
46-
}
47-
ast, iss := e.Compile(triggerExpression)
48-
if iss.Err() != nil {
49-
return nil, iss.Err()
50-
}
51-
program, err := e.Program(
52-
ast,
53-
cel.Functions(
54-
&functions.Overload{
55-
Operator: "now",
56-
Function: defaultFuncMap["now"],
57-
},
58-
&functions.Overload{
59-
Operator: "sha1_string",
60-
Unary: func(value ref.Val) ref.Val {
61-
return defaultFuncMap["sha1"](value)
62-
},
63-
}),
64-
)
21+
program, err := globalEnv.Program(triggerExpression)
6522
if err != nil {
6623
return nil, err
6724
}
6825
return &Trigger{
69-
e: e,
7026
decision: decision,
7127
program: program,
7228
expression: triggerExpression,

0 commit comments

Comments
 (0)
Please sign in to comment.