@@ -25,7 +25,6 @@ import (
25
25
"strings"
26
26
27
27
"github.com/google/cel-go/cel"
28
- "github.com/google/cel-go/checker/decls"
29
28
"github.com/google/cel-go/common/types"
30
29
"github.com/google/cel-go/common/types/ref"
31
30
"github.com/google/cel-go/common/types/traits"
@@ -54,6 +53,9 @@ func newCELProgram(expr string) (cel.Program, error) {
54
53
if issues != nil && issues .Err () != nil {
55
54
return nil , fmt .Errorf ("expression %v check failed: %w" , expr , issues .Err ())
56
55
}
56
+ if checked .OutputType () != types .BoolType {
57
+ return nil , fmt .Errorf ("invalid expression output type %v" , checked .OutputType ())
58
+ }
57
59
58
60
prg , err := env .Program (checked , cel .EvalOptions (cel .OptOptimize ), cel .InterruptCheckFrequency (100 ))
59
61
if err != nil {
@@ -94,26 +96,19 @@ func newCELEvaluator(expr string, req *http.Request) (resourcePredicate, error)
94
96
return nil , fmt .Errorf ("expression %v failed to evaluate: %w" , expr , err )
95
97
}
96
98
97
- v , ok := out .(types.Bool )
98
- if ! ok {
99
- return nil , fmt .Errorf ("expression %q did not return a boolean value" , expr )
100
- }
101
-
102
- result := v .Value ().(bool )
99
+ result := out .Value ().(bool )
103
100
104
101
return & result , nil
105
102
}, nil
106
103
}
107
104
108
105
func makeCELEnv () (* cel.Env , error ) {
109
- mapStrDyn := decls .NewMapType (decls .String , decls .Dyn )
110
106
return cel .NewEnv (
111
107
celext .Strings (),
112
108
notifications (),
113
- cel .Declarations (
114
- decls .NewVar ("resource" , mapStrDyn ),
115
- decls .NewVar ("request" , mapStrDyn ),
116
- ))
109
+ cel .Variable ("resource" , cel .ObjectType ("google.protobuf.Struct" )),
110
+ cel .Variable ("request" , cel .ObjectType ("google.protobuf.Struct" )),
111
+ )
117
112
}
118
113
119
114
func isJSONContent (r * http.Request ) bool {
@@ -132,17 +127,10 @@ func isJSONContent(r *http.Request) bool {
132
127
}
133
128
134
129
func notifications () cel.EnvOption {
135
- r , err := types .NewRegistry ()
136
- if err != nil {
137
- panic (err ) // TODO: Do something better?
138
- }
139
-
140
- return cel .Lib (& notificationsLib {registry : r })
130
+ return cel .Lib (& notificationsLib {})
141
131
}
142
132
143
- type notificationsLib struct {
144
- registry * types.Registry
145
- }
133
+ type notificationsLib struct {}
146
134
147
135
// LibraryName implements the SingletonLibrary interface method.
148
136
func (* notificationsLib ) LibraryName () string {
@@ -151,13 +139,13 @@ func (*notificationsLib) LibraryName() string {
151
139
152
140
// CompileOptions implements the Library interface method.
153
141
func (l * notificationsLib ) CompileOptions () []cel.EnvOption {
154
- listStrDyn := cel .ListType (cel .DynType )
142
+ listDyn := cel .ListType (cel .DynType )
155
143
opts := []cel.EnvOption {
156
144
cel .Function ("first" ,
157
- cel .MemberOverload ("first_list" , []* cel.Type {listStrDyn }, cel .DynType ,
145
+ cel .MemberOverload ("first_list" , []* cel.Type {listDyn }, cel .DynType ,
158
146
cel .UnaryBinding (listFirst ))),
159
147
cel .Function ("last" ,
160
- cel .MemberOverload ("last_list" , []* cel.Type {listStrDyn }, cel .DynType ,
148
+ cel .MemberOverload ("last_list" , []* cel.Type {listDyn }, cel .DynType ,
161
149
cel .UnaryBinding (listLast ))),
162
150
}
163
151
0 commit comments