| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package cel_test |
| 16 |
|
| 17 |
import ( |
| 18 |
"sort" |
| 19 |
"strings" |
| 20 |
"testing" |
| 21 |
|
| 22 |
"github.com/google/cel-go/cel" |
| 23 |
"github.com/google/cel-go/common/ast" |
| 24 |
"github.com/google/cel-go/ext" |
| 25 |
|
| 26 |
"google.golang.org/protobuf/encoding/prototext" |
| 27 |
"google.golang.org/protobuf/proto" |
| 28 |
|
| 29 |
proto3pb "github.com/google/cel-go/test/proto3pb" |
| 30 |
|
| 31 |
exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1" |
| 32 |
) |
| 33 |
|
| 34 |
func TestStaticOptimizerUpdateExpr(t *testing.T) { |
| 35 |
expr := `has(a.b)` |
| 36 |
inlined := `[x, y].filter(i, i.size() > 0)[0].z` |
| 37 |
|
| 38 |
e := optimizerEnv(t) |
| 39 |
exprAST, iss := e.Compile(expr) |
| 40 |
if iss.Err() != nil { |
| 41 |
t.Fatalf("Compile() failed: %v", iss.Err()) |
| 42 |
} |
| 43 |
|
| 44 |
inlinedAST, iss := e.Compile(inlined) |
| 45 |
if iss.Err() != nil { |
| 46 |
t.Fatalf("Compile() failed: %v", iss.Err()) |
| 47 |
} |
| 48 |
opt, err := cel.NewStaticOptimizer(&testOptimizer{t: t, inlineExpr: inlinedAST.NativeRep()}) |
| 49 |
if err != nil { |
| 50 |
t.Fatalf("NewStaticOptimizer() failed: %v", err) |
| 51 |
} |
| 52 |
optAST, iss := opt.Optimize(e, exprAST) |
| 53 |
if iss.Err() != nil { |
| 54 |
t.Fatalf("Optimize() generated an invalid AST: %v", iss.Err()) |
| 55 |
} |
| 56 |
optString, err := cel.AstToString(optAST) |
| 57 |
if err != nil { |
| 58 |
t.Fatalf("cel.AstToString() failed: %v", err) |
| 59 |
} |
| 60 |
sourceInfo := optAST.NativeRep().SourceInfo() |
| 61 |
sourceInfoPB, err := ast.SourceInfoToProto(sourceInfo) |
| 62 |
if err != nil { |
| 63 |
t.Fatalf("cel.AstToCheckedExpr() failed: %v", err) |
| 64 |
} |
| 65 |
sourceInfoPB.Positions = nil |
| 66 |
wantTextPB := ` |
| 67 |
location: "<input>" |
| 68 |
line_offsets: 9 |
| 69 |
macro_calls: { |
| 70 |
key: 1 |
| 71 |
value: { |
| 72 |
call_expr: { |
| 73 |
function: "has" |
| 74 |
args: { |
| 75 |
id: 24 |
| 76 |
select_expr: { |
| 77 |
operand: { |
| 78 |
id: 2 |
| 79 |
call_expr: { |
| 80 |
function: "_[_]" |
| 81 |
args: { |
| 82 |
id: 3 |
| 83 |
} |
| 84 |
args: { |
| 85 |
id: 20 |
| 86 |
const_expr: { |
| 87 |
int64_value: 0 |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
field: "z" |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
macro_calls: { |
| 99 |
key: 3 |
| 100 |
value: { |
| 101 |
call_expr: { |
| 102 |
target: { |
| 103 |
id: 4 |
| 104 |
list_expr: { |
| 105 |
elements: { |
| 106 |
id: 5 |
| 107 |
ident_expr: { |
| 108 |
name: "x" |
| 109 |
} |
| 110 |
} |
| 111 |
elements: { |
| 112 |
id: 6 |
| 113 |
ident_expr: { |
| 114 |
name: "y" |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
function: "filter" |
| 120 |
args: { |
| 121 |
id: 17 |
| 122 |
ident_expr: { |
| 123 |
name: "i" |
| 124 |
} |
| 125 |
} |
| 126 |
args: { |
| 127 |
id: 10 |
| 128 |
call_expr: { |
| 129 |
function: "_>_" |
| 130 |
args: { |
| 131 |
id: 11 |
| 132 |
call_expr: { |
| 133 |
target: { |
| 134 |
id: 12 |
| 135 |
ident_expr: { |
| 136 |
name: "i" |
| 137 |
} |
| 138 |
} |
| 139 |
function: "size" |
| 140 |
} |
| 141 |
} |
| 142 |
args: { |
| 143 |
id: 13 |
| 144 |
const_expr: { |
| 145 |
int64_value: 0 |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
` |
| 154 |
var wantSourceInfoPB exprpb.SourceInfo |
| 155 |
if err := prototext.Unmarshal([]byte(wantTextPB), &wantSourceInfoPB); err != nil { |
| 156 |
t.Fatalf("prototext.Unmarshal() failed: %v", err) |
| 157 |
} |
| 158 |
if !proto.Equal(&wantSourceInfoPB, sourceInfoPB) { |
| 159 |
t.Errorf("got source info: %s, wanted %s", prototext.Format(sourceInfoPB), wantTextPB) |
| 160 |
} |
| 161 |
expected := `has([x, y].filter(i, i.size() > 0)[0].z)` |
| 162 |
if expected != optString { |
| 163 |
t.Errorf("inlined got %q, wanted %q", optString, expected) |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
func TestStaticOptimizerNewAST(t *testing.T) { |
| 168 |
tests := []string{ |
| 169 |
`[3, 2, 1]`, |
| 170 |
`[1, 2, 3].all(i, i != 0)`, |
| 171 |
`cel.bind(m, {"a": 1, "b": 2}, m.filter(k, m[k] > 1))`, |
| 172 |
} |
| 173 |
for _, tst := range tests { |
| 174 |
tc := tst |
| 175 |
t.Run(tc, func(t *testing.T) { |
| 176 |
e := optimizerEnv(t) |
| 177 |
exprAST, iss := e.Compile(tc) |
| 178 |
if iss.Err() != nil { |
| 179 |
t.Fatalf("Compile(%q) failed: %v", tc, iss.Err()) |
| 180 |
} |
| 181 |
opt, err := cel.NewStaticOptimizer(&identityOptimizer{t: t}) |
| 182 |
if err != nil { |
| 183 |
t.Fatalf("NewStaticOptimizer() failed: %v", err) |
| 184 |
} |
| 185 |
optAST, iss := opt.Optimize(e, exprAST) |
| 186 |
if iss.Err() != nil { |
| 187 |
t.Fatalf("Optimize() generated an invalid AST: %v", iss.Err()) |
| 188 |
} |
| 189 |
optString, err := cel.AstToString(optAST) |
| 190 |
if err != nil { |
| 191 |
t.Fatalf("cel.AstToString() failed: %v", err) |
| 192 |
} |
| 193 |
if tc != optString { |
| 194 |
t.Errorf("identity optimizer got %q, wanted %q", optString, tc) |
| 195 |
} |
| 196 |
}) |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
func TestOptimizeWithSource(t *testing.T) { |
| 201 |
initial := `has(a.b)` |
| 202 |
replacement := `x["a"]` |
| 203 |
e := optimizerEnv(t) |
| 204 |
initialAST, iss := e.Compile(initial) |
| 205 |
if iss.Err() != nil { |
| 206 |
t.Fatalf("Compile(%q) failed: %v", initial, iss.Err()) |
| 207 |
} |
| 208 |
replacementAST, iss := e.Compile(replacement) |
| 209 |
if iss.Err() != nil { |
| 210 |
t.Fatalf("Compile(%q) failed: %v", replacement, iss.Err()) |
| 211 |
} |
| 212 |
|
| 213 |
opt, err := cel.NewStaticOptimizer( |
| 214 |
&replaceOptimizer{t: t, targetAST: replacementAST.NativeRep()}, |
| 215 |
cel.OptimizeWithSource(replacementAST.Source()), |
| 216 |
) |
| 217 |
if err != nil { |
| 218 |
t.Fatalf("NewStaticOptimizer() failed: %v", err) |
| 219 |
} |
| 220 |
optAST, iss := opt.Optimize(e, initialAST) |
| 221 |
if iss.Err() != nil { |
| 222 |
t.Fatalf("Optimize() returned an error: %v", iss.Err()) |
| 223 |
} |
| 224 |
|
| 225 |
if optAST.Source().Content() != replacement { |
| 226 |
t.Errorf("got source content %q, wanted %q", optAST.Source().Content(), replacement) |
| 227 |
} |
| 228 |
sourceInfoPB, err := ast.SourceInfoToProto(optAST.NativeRep().SourceInfo()) |
| 229 |
if err != nil { |
| 230 |
t.Fatalf("cel.AstToCheckedExpr() failed: %v", err) |
| 231 |
} |
| 232 |
wantTextPB := ` |
| 233 |
location: "<input>" |
| 234 |
line_offsets: 7 |
| 235 |
positions: { |
| 236 |
key: 1 |
| 237 |
value: 1 |
| 238 |
} |
| 239 |
positions: { |
| 240 |
key: 2 |
| 241 |
value: 0 |
| 242 |
} |
| 243 |
positions: { |
| 244 |
key: 3 |
| 245 |
value: 2 |
| 246 |
} |
| 247 |
` |
| 248 |
var wantSourceInfoPB exprpb.SourceInfo |
| 249 |
if err := prototext.Unmarshal([]byte(wantTextPB), &wantSourceInfoPB); err != nil { |
| 250 |
t.Fatalf("prototext.Unmarshal() failed: %v", err) |
| 251 |
} |
| 252 |
if !proto.Equal(&wantSourceInfoPB, sourceInfoPB) { |
| 253 |
t.Errorf("got source info: %s, wanted %s", prototext.Format(sourceInfoPB), wantTextPB) |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
func TestStaticOptimizerNilAST(t *testing.T) { |
| 258 |
env := optimizerEnv(t) |
| 259 |
opt, err := cel.NewStaticOptimizer(&identityOptimizer{t: t}) |
| 260 |
if err != nil { |
| 261 |
t.Fatalf("NewStaticOptimizer() failed: %v", err) |
| 262 |
} |
| 263 |
optAST, iss := opt.Optimize(env, nil) |
| 264 |
if iss.Err() == nil || !strings.Contains(iss.Err().Error(), "unexpected unspecified type") { |
| 265 |
t.Errorf("opt.Optimize(env, nil) got (%v, %v), wanted unexpected unspecified type", optAST, iss) |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
type identityOptimizer struct { |
| 270 |
t *testing.T |
| 271 |
} |
| 272 |
|
| 273 |
func (opt *identityOptimizer) Optimize(ctx *cel.OptimizerContext, a *ast.AST) *ast.AST { |
| 274 |
opt.t.Helper() |
| 275 |
|
| 276 |
|
| 277 |
main := ctx.CopyASTAndMetadata(a) |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
return ctx.NewAST(main) |
| 282 |
} |
| 283 |
|
| 284 |
type testOptimizer struct { |
| 285 |
t *testing.T |
| 286 |
inlineExpr *ast.AST |
| 287 |
} |
| 288 |
|
| 289 |
func (opt *testOptimizer) Optimize(ctx *cel.OptimizerContext, a *ast.AST) *ast.AST { |
| 290 |
opt.t.Helper() |
| 291 |
copy := ctx.CopyASTAndMetadata(opt.inlineExpr) |
| 292 |
origID := a.Expr().ID() |
| 293 |
presenceTest, hasMacro := ctx.NewHasMacro(origID, copy) |
| 294 |
macroKeys := getMacroKeys(ctx.MacroCalls()) |
| 295 |
if len(macroKeys) != 2 { |
| 296 |
opt.t.Errorf("Got %v macro calls, wanted 2", macroKeys) |
| 297 |
} |
| 298 |
ctx.UpdateExpr(a.Expr(), presenceTest) |
| 299 |
ctx.SetMacroCall(origID, hasMacro) |
| 300 |
return ctx.NewAST(a.Expr()) |
| 301 |
} |
| 302 |
|
| 303 |
type replaceOptimizer struct { |
| 304 |
t *testing.T |
| 305 |
targetAST *ast.AST |
| 306 |
} |
| 307 |
|
| 308 |
func (opt *replaceOptimizer) Optimize(ctx *cel.OptimizerContext, a *ast.AST) *ast.AST { |
| 309 |
opt.t.Helper() |
| 310 |
copy := ctx.CopyASTAndMetadata(opt.targetAST) |
| 311 |
return ctx.NewAST(copy) |
| 312 |
} |
| 313 |
|
| 314 |
func getMacroKeys(macroCalls map[int64]ast.Expr) []int { |
| 315 |
keys := []int{} |
| 316 |
for k := range macroCalls { |
| 317 |
keys = append(keys, int(k)) |
| 318 |
} |
| 319 |
sort.Ints(keys) |
| 320 |
return keys |
| 321 |
} |
| 322 |
|
| 323 |
func optimizerEnv(t *testing.T) *cel.Env { |
| 324 |
t.Helper() |
| 325 |
opts := []cel.EnvOption{ |
| 326 |
cel.Types(&proto3pb.TestAllTypes{}), |
| 327 |
cel.OptionalTypes(), |
| 328 |
cel.EnableMacroCallTracking(), |
| 329 |
ext.Bindings(), |
| 330 |
cel.Variable("a", cel.MapType(cel.StringType, cel.StringType)), |
| 331 |
cel.Variable("x", cel.MapType(cel.StringType, cel.StringType)), |
| 332 |
cel.Variable("y", cel.MapType(cel.StringType, cel.StringType)), |
| 333 |
} |
| 334 |
e, err := cel.NewEnv(opts...) |
| 335 |
if err != nil { |
| 336 |
t.Fatalf("NewEnv() failed: %v", err) |
| 337 |
} |
| 338 |
return e |
| 339 |
} |