| 1 | // Copyright 2023 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package cel_test |
| 16 | |
| 17 | import ( |
| 18 | "testing" |
| 19 | |
| 20 | "github.com/google/cel-go/cel" |
| 21 | |
| 22 | proto3pb "github.com/google/cel-go/test/proto3pb" |
| 23 | ) |
| 24 | |
| 25 | func TestInliningOptimizer(t *testing.T) { |
| 26 | type varExpr struct { |
| 27 | name string |
| 28 | alias string |
| 29 | t *cel.Type |
| 30 | expr string |
| 31 | } |
| 32 | tests := []struct { |
| 33 | expr string |
| 34 | vars []varExpr |
| 35 | inlined string |
| 36 | folded string |
| 37 | }{ |
| 38 | { |
| 39 | expr: `a || b`, |
| 40 | vars: []varExpr{ |
| 41 | { |
| 42 | name: "a", |
| 43 | t: cel.BoolType, |
| 44 | }, |
| 45 | { |
| 46 | name: "b", |
| 47 | alias: "bravo", |
| 48 | t: cel.BoolType, |
| 49 | expr: `'hello'.contains('lo')`, |
| 50 | }, |
| 51 | }, |
| 52 | inlined: `a || "hello".contains("lo")`, |
| 53 | folded: `true`, |
| 54 | }, |
| 55 | { |
| 56 | expr: `a + [a]`, |
| 57 | vars: []varExpr{ |
| 58 | { |
| 59 | name: "a", |
| 60 | alias: "alpha", |
| 61 | t: cel.DynType, |
| 62 | expr: `dyn([1, 2])`, |
| 63 | }, |
| 64 | }, |
| 65 | inlined: `cel.bind(alpha, dyn([1, 2]), alpha + [alpha])`, |
| 66 | folded: `[1, 2, [1, 2]]`, |
| 67 | }, |
| 68 | { |
| 69 | expr: `a && (a || b)`, |
| 70 | vars: []varExpr{ |
| 71 | { |
| 72 | name: "a", |
| 73 | alias: "alpha", |
| 74 | t: cel.BoolType, |
| 75 | expr: `'hello'.contains('lo')`, |
| 76 | }, |
| 77 | { |
| 78 | name: "b", |
| 79 | t: cel.BoolType, |
| 80 | }, |
| 81 | }, |
| 82 | inlined: `cel.bind(alpha, "hello".contains("lo"), alpha && (alpha || b))`, |
| 83 | folded: `true`, |
| 84 | }, |
| 85 | { |
| 86 | expr: `a && b && a`, |
| 87 | vars: []varExpr{ |
| 88 | { |
| 89 | name: "a", |
| 90 | alias: "alpha", |
| 91 | t: cel.BoolType, |
| 92 | expr: `'hello'.contains('lo')`, |
| 93 | }, |
| 94 | { |
| 95 | name: "b", |
| 96 | t: cel.BoolType, |
| 97 | }, |
| 98 | }, |
| 99 | inlined: `cel.bind(alpha, "hello".contains("lo"), alpha && b && alpha)`, |
| 100 | folded: `cel.bind(alpha, true, alpha && b && alpha)`, |
| 101 | }, |
| 102 | { |
| 103 | expr: `(c || d) || (a && (a || b))`, |
| 104 | vars: []varExpr{ |
| 105 | { |
| 106 | name: "a", |
| 107 | alias: "alpha", |
| 108 | t: cel.BoolType, |
| 109 | expr: `'hello'.contains('lo')`, |
| 110 | }, |
| 111 | { |
| 112 | name: "b", |
| 113 | t: cel.BoolType, |
| 114 | }, |
| 115 | { |
| 116 | name: "c", |
| 117 | t: cel.BoolType, |
| 118 | }, |
| 119 | { |
| 120 | name: "d", |
| 121 | t: cel.BoolType, |
| 122 | expr: "!false", |
| 123 | }, |
| 124 | }, |
| 125 | inlined: `c || !false || cel.bind(alpha, "hello".contains("lo"), alpha && (alpha || b))`, |
| 126 | folded: `true`, |
| 127 | }, |
| 128 | { |
| 129 | expr: `a && (a || b)`, |
| 130 | vars: []varExpr{ |
| 131 | { |
| 132 | name: "a", |
| 133 | t: cel.BoolType, |
| 134 | }, |
| 135 | { |
| 136 | name: "b", |
| 137 | alias: "bravo", |
| 138 | t: cel.BoolType, |
| 139 | expr: `'hello'.contains('lo')`, |
| 140 | }, |
| 141 | }, |
| 142 | inlined: `a && (a || "hello".contains("lo"))`, |
| 143 | folded: `a`, |
| 144 | }, |
| 145 | { |
| 146 | expr: `a && b`, |
| 147 | vars: []varExpr{ |
| 148 | { |
| 149 | name: "a", |
| 150 | alias: "alpha", |
| 151 | t: cel.BoolType, |
| 152 | expr: `!'hello'.contains('lo')`, |
| 153 | }, |
| 154 | { |
| 155 | name: "b", |
| 156 | alias: "bravo", |
| 157 | t: cel.BoolType, |
| 158 | }, |
| 159 | }, |
| 160 | inlined: `!"hello".contains("lo") && b`, |
| 161 | folded: `false`, |
| 162 | }, |
| 163 | { |
| 164 | expr: `operation.system.consumers + operation.destination_consumers`, |
| 165 | vars: []varExpr{ |
| 166 | { |
| 167 | name: "operation.system", |
| 168 | t: cel.DynType, |
| 169 | }, |
| 170 | { |
| 171 | name: "operation.destination_consumers", |
| 172 | t: cel.ListType(cel.IntType), |
| 173 | expr: `productsToConsumers(operation.destination_products)`, |
| 174 | }, |
| 175 | { |
| 176 | name: "operation.destination_products", |
| 177 | t: cel.ListType(cel.IntType), |
| 178 | expr: `operation.system.products`, |
| 179 | }, |
| 180 | }, |
| 181 | inlined: `operation.system.consumers + productsToConsumers(operation.system.products)`, |
| 182 | folded: `operation.system.consumers + productsToConsumers(operation.system.products)`, |
| 183 | }, |
| 184 | } |
| 185 | for _, tst := range tests { |
| 186 | tc := tst |
| 187 | t.Run(tc.expr, func(t *testing.T) { |
| 188 | opts := []cel.EnvOption{cel.OptionalTypes(), |
| 189 | cel.EnableMacroCallTracking(), |
| 190 | cel.Function("productsToConsumers", |
| 191 | cel.Overload("productsToConsumers_list", |
| 192 | []*cel.Type{cel.ListType(cel.IntType)}, |
| 193 | cel.ListType(cel.IntType)))} |
| 194 | |
| 195 | varDecls := make([]cel.EnvOption, len(tc.vars)) |
| 196 | for i, v := range tc.vars { |
| 197 | varDecls[i] = cel.Variable(v.name, v.t) |
| 198 | } |
| 199 | e, err := cel.NewEnv(append(varDecls, opts...)...) |
| 200 | if err != nil { |
| 201 | t.Fatalf("NewEnv() failed: %v", err) |
| 202 | } |
| 203 | inlinedVars := []*cel.InlineVariable{} |
| 204 | for _, v := range tc.vars { |
| 205 | if v.expr == "" { |
| 206 | continue |
| 207 | } |
| 208 | checked, iss := e.Compile(v.expr) |
| 209 | if iss.Err() != nil { |
| 210 | t.Fatalf("Compile(%q) failed: %v", v.expr, iss.Err()) |
| 211 | } |
| 212 | if v.alias == "" { |
| 213 | inlinedVars = append(inlinedVars, cel.NewInlineVariable(v.name, checked)) |
| 214 | } else { |
| 215 | inlinedVars = append(inlinedVars, cel.NewInlineVariableWithAlias(v.name, v.alias, checked)) |
| 216 | } |
| 217 | } |
| 218 | checked, iss := e.Compile(tc.expr) |
| 219 | if iss.Err() != nil { |
| 220 | t.Fatalf("Compile() failed: %v", iss.Err()) |
| 221 | } |
| 222 | |
| 223 | opt, err := cel.NewStaticOptimizer(cel.NewInliningOptimizer(inlinedVars...)) |
| 224 | if err != nil { |
| 225 | t.Fatalf("NewStaticOptimizer() failed: %v", err) |
| 226 | } |
| 227 | optimized, iss := opt.Optimize(e, checked) |
| 228 | if iss.Err() != nil { |
| 229 | t.Fatalf("Optimize() generated an invalid AST: %v", iss.Err()) |
| 230 | } |
| 231 | inlined, err := cel.AstToString(optimized) |
| 232 | if err != nil { |
| 233 | t.Fatalf("cel.AstToString() failed: %v", err) |
| 234 | } |
| 235 | if inlined != tc.inlined { |
| 236 | t.Errorf("inlined got %q, wanted %q", inlined, tc.inlined) |
| 237 | } |
| 238 | folder, err := cel.NewConstantFoldingOptimizer() |
| 239 | if err != nil { |
| 240 | t.Fatalf("NewConstantFoldingOptimizer() failed: %v", err) |
| 241 | } |
| 242 | opt, err = cel.NewStaticOptimizer(folder) |
| 243 | if err != nil { |
| 244 | t.Fatalf("NewStaticOptimizer() failed: %v", err) |
| 245 | } |
| 246 | optimized, iss = opt.Optimize(e, optimized) |
| 247 | if iss.Err() != nil { |
| 248 | t.Fatalf("Optimize() generated an invalid AST: %v", iss.Err()) |
| 249 | } |
| 250 | folded, err := cel.AstToString(optimized) |
| 251 | if err != nil { |
| 252 | t.Fatalf("cel.AstToString() failed: %v", err) |
| 253 | } |
| 254 | if folded != tc.folded { |
| 255 | t.Errorf("folded got %q, wanted %q", folded, tc.folded) |
| 256 | } |
| 257 | }) |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | func TestInliningOptimizerMultiStage(t *testing.T) { |
| 262 | type varDecl struct { |
| 263 | name string |
| 264 | t *cel.Type |
| 265 | } |
| 266 | type inlineVarExpr struct { |
| 267 | name string |
| 268 | alias string |
| 269 | t *cel.Type |
| 270 | expr string |
| 271 | } |
| 272 | tests := []struct { |
| 273 | expr string |
| 274 | vars []varDecl |
| 275 | inlineVars []inlineVarExpr |
| 276 | inlined string |
| 277 | folded string |
| 278 | }{ |
| 279 | { |
| 280 | expr: `has(a.b)`, |
| 281 | vars: []varDecl{ |
| 282 | { |
| 283 | name: "a", |
| 284 | t: cel.MapType(cel.StringType, cel.StringType), |
| 285 | }, |
| 286 | }, |
| 287 | inlineVars: []inlineVarExpr{ |
| 288 | { |
| 289 | name: "a.b", |
| 290 | alias: "alpha", |
| 291 | t: cel.StringType, |
| 292 | expr: `a.b_long`, |
| 293 | }, |
| 294 | }, |
| 295 | inlined: `has(a.b_long)`, |
| 296 | folded: `has(a.b_long)`, |
| 297 | }, |
| 298 | { |
| 299 | expr: `has(a.b) ? a.b : 'default'`, |
| 300 | vars: []varDecl{ |
| 301 | { |
| 302 | name: "a", |
| 303 | t: cel.MapType(cel.StringType, cel.StringType), |
| 304 | }, |
| 305 | }, |
| 306 | inlineVars: []inlineVarExpr{ |
| 307 | { |
| 308 | name: "a.b", |
| 309 | alias: "alpha", |
| 310 | t: cel.StringType, |
| 311 | expr: `'hello'`, |
| 312 | }, |
| 313 | }, |
| 314 | inlined: `cel.bind(alpha, "hello", (alpha.size() != 0) ? alpha : "default")`, |
| 315 | folded: `"hello"`, |
| 316 | }, |
| 317 | { |
| 318 | expr: `has(a.b) ? a.b : ['default']`, |
| 319 | vars: []varDecl{ |
| 320 | { |
| 321 | name: "a", |
| 322 | t: cel.MapType(cel.StringType, cel.ListType(cel.StringType)), |
| 323 | }, |
| 324 | }, |
| 325 | inlineVars: []inlineVarExpr{ |
| 326 | { |
| 327 | name: "a.b", |
| 328 | alias: "alpha", |
| 329 | t: cel.StringType, |
| 330 | expr: `['hello']`, |
| 331 | }, |
| 332 | }, |
| 333 | inlined: `cel.bind(alpha, ["hello"], (alpha.size() != 0) ? alpha : ["default"])`, |
| 334 | folded: `["hello"]`, |
| 335 | }, |
| 336 | { |
| 337 | expr: `0 in msg.map_int64_nested_type`, |
| 338 | vars: []varDecl{ |
| 339 | { |
| 340 | name: "msg", |
| 341 | t: cel.ObjectType("google.expr.proto3.test.TestAllTypes"), |
| 342 | }, |
| 343 | { |
| 344 | name: "nested_map", |
| 345 | t: cel.MapType(cel.IntType, cel.ObjectType("google.expr.proto3.test.NestedTestAllTypes")), |
| 346 | }, |
| 347 | }, |
| 348 | inlineVars: []inlineVarExpr{ |
| 349 | |
| 350 | { |
| 351 | name: "msg.map_int64_nested_type", |
| 352 | t: cel.MapType(cel.IntType, cel.ObjectType("google.expr.proto3.test.NestedTestAllTypes")), |
| 353 | expr: `nested_map`, |
| 354 | }, |
| 355 | }, |
| 356 | inlined: `0 in nested_map`, |
| 357 | folded: `0 in nested_map`, |
| 358 | }, |
| 359 | { |
| 360 | expr: `has(msg.single_any)`, |
| 361 | vars: []varDecl{ |
| 362 | { |
| 363 | name: "msg", |
| 364 | t: cel.ObjectType("google.expr.proto3.test.TestAllTypes"), |
| 365 | }, |
| 366 | { |
| 367 | name: "unpacked_wrapper", |
| 368 | t: cel.NullableType(cel.StringType), |
| 369 | }, |
| 370 | }, |
| 371 | inlineVars: []inlineVarExpr{ |
| 372 | { |
| 373 | name: "msg.single_any", |
| 374 | t: cel.NullableType(cel.StringType), |
| 375 | expr: `unpacked_wrapper`, |
| 376 | }, |
| 377 | }, |
| 378 | inlined: `unpacked_wrapper != null`, |
| 379 | folded: `unpacked_wrapper != null`, |
| 380 | }, |
| 381 | { |
| 382 | expr: `has(msg.single_any) ? msg.single_any : '10'`, |
| 383 | vars: []varDecl{ |
| 384 | { |
| 385 | name: "msg", |
| 386 | t: cel.ObjectType("google.expr.proto3.test.TestAllTypes"), |
| 387 | }, |
| 388 | { |
| 389 | name: "unpacked_wrapper", |
| 390 | t: cel.NullableType(cel.StringType), |
| 391 | }, |
| 392 | }, |
| 393 | inlineVars: []inlineVarExpr{ |
| 394 | { |
| 395 | name: "msg.single_any", |
| 396 | t: cel.NullableType(cel.StringType), |
| 397 | alias: "wrapped", |
| 398 | expr: `unpacked_wrapper`, |
| 399 | }, |
| 400 | }, |
| 401 | inlined: `cel.bind(wrapped, unpacked_wrapper, (wrapped != null) ? wrapped : "10")`, |
| 402 | folded: `cel.bind(wrapped, unpacked_wrapper, (wrapped != null) ? wrapped : "10")`, |
| 403 | }, |
| 404 | { |
| 405 | expr: `has(msg.child.payload.single_int32_wrapper)`, |
| 406 | vars: []varDecl{ |
| 407 | { |
| 408 | name: "msg", |
| 409 | t: cel.ObjectType("google.expr.proto3.test.NestedTestAllTypes"), |
| 410 | }, |
| 411 | { |
| 412 | name: "unpacked_child", |
| 413 | t: cel.ObjectType("google.expr.proto3.test.NestedTestAllTypes"), |
| 414 | }, |
| 415 | }, |
| 416 | inlineVars: []inlineVarExpr{ |
| 417 | { |
| 418 | name: "msg.child.payload", |
| 419 | t: cel.ObjectType("google.expr.proto3.test.NestedTestAllTypes"), |
| 420 | alias: "payload", |
| 421 | expr: `unpacked_child.payload`, |
| 422 | }, |
| 423 | }, |
| 424 | inlined: `has(unpacked_child.payload.single_int32_wrapper)`, |
| 425 | folded: `has(unpacked_child.payload.single_int32_wrapper)`, |
| 426 | }, |
| 427 | { |
| 428 | expr: `has(msg.child.payload.single_int32_wrapper)`, |
| 429 | vars: []varDecl{ |
| 430 | { |
| 431 | name: "msg", |
| 432 | t: cel.ObjectType("google.expr.proto3.test.NestedTestAllTypes"), |
| 433 | }, |
| 434 | { |
| 435 | name: "unpacked_payload", |
| 436 | t: cel.ObjectType("google.expr.proto3.test.TestAllTypes"), |
| 437 | }, |
| 438 | }, |
| 439 | inlineVars: []inlineVarExpr{ |
| 440 | { |
| 441 | name: "msg.child.payload.single_int32_wrapper", |
| 442 | t: cel.NullableType(cel.IntType), |
| 443 | alias: "payload", |
| 444 | expr: `unpacked_payload.single_int32_wrapper`, |
| 445 | }, |
| 446 | }, |
| 447 | inlined: `has(unpacked_payload.single_int32_wrapper)`, |
| 448 | folded: `has(unpacked_payload.single_int32_wrapper)`, |
| 449 | }, |
| 450 | { |
| 451 | expr: `has(msg.child.payload.single_int32_wrapper) ? msg.child.payload.single_int32_wrapper : 1`, |
| 452 | vars: []varDecl{ |
| 453 | { |
| 454 | name: "msg", |
| 455 | t: cel.ObjectType("google.expr.proto3.test.NestedTestAllTypes"), |
| 456 | }, |
| 457 | { |
| 458 | name: "unpacked_payload", |
| 459 | t: cel.ObjectType("google.expr.proto3.test.TestAllTypes"), |
| 460 | }, |
| 461 | }, |
| 462 | inlineVars: []inlineVarExpr{ |
| 463 | { |
| 464 | name: "msg.child.payload.single_int32_wrapper", |
| 465 | t: cel.NullableType(cel.IntType), |
| 466 | alias: "nullable_int", |
| 467 | expr: `unpacked_payload.single_int32_wrapper`, |
| 468 | }, |
| 469 | }, |
| 470 | inlined: `cel.bind(nullable_int, unpacked_payload.single_int32_wrapper, (nullable_int != null) ? nullable_int : 1)`, |
| 471 | folded: `cel.bind(nullable_int, unpacked_payload.single_int32_wrapper, (nullable_int != null) ? nullable_int : 1)`, |
| 472 | }, |
| 473 | { |
| 474 | expr: `has(msg.single_value) ? msg.single_value : null`, |
| 475 | vars: []varDecl{ |
| 476 | { |
| 477 | name: "msg", |
| 478 | t: cel.ObjectType("google.expr.proto3.test.TestAllTypes"), |
| 479 | }, |
| 480 | }, |
| 481 | inlineVars: []inlineVarExpr{ |
| 482 | { |
| 483 | name: "msg.single_value", |
| 484 | t: cel.NullableType(cel.DoubleType), |
| 485 | alias: "nullable_float", |
| 486 | expr: `dyn(1.5)`, |
| 487 | }, |
| 488 | }, |
| 489 | inlined: `cel.bind(nullable_float, dyn(1.5), (nullable_float != null) ? nullable_float : null)`, |
| 490 | folded: `1.5`, |
| 491 | }, |
| 492 | { |
| 493 | expr: `has(msg.single_any) ? msg.single_any : 42`, |
| 494 | vars: []varDecl{ |
| 495 | { |
| 496 | name: "msg", |
| 497 | t: cel.ObjectType("google.expr.proto3.test.TestAllTypes"), |
| 498 | }, |
| 499 | }, |
| 500 | inlineVars: []inlineVarExpr{ |
| 501 | { |
| 502 | name: "msg.single_any", |
| 503 | t: cel.IntType, |
| 504 | alias: "unpacked_nested", |
| 505 | expr: `proto3.test.NestedTestAllTypes{}.payload.single_int32`, |
| 506 | }, |
| 507 | }, |
| 508 | inlined: `has(google.expr.proto3.test.NestedTestAllTypes{}.payload.single_int32) ? google.expr.proto3.test.NestedTestAllTypes{}.payload.single_int32 : 42`, |
| 509 | folded: `42`, |
| 510 | }, |
| 511 | { |
| 512 | expr: `has(msg.single_any.processing_purpose)`, |
| 513 | vars: []varDecl{ |
| 514 | { |
| 515 | name: "msg", |
| 516 | t: cel.ObjectType("google.expr.proto3.test.TestAllTypes"), |
| 517 | }, |
| 518 | { |
| 519 | name: "unpacked_purpose", |
| 520 | t: cel.ListType(cel.IntType), |
| 521 | }, |
| 522 | }, |
| 523 | inlineVars: []inlineVarExpr{ |
| 524 | { |
| 525 | name: "msg.single_any.processing_purpose", |
| 526 | t: cel.ListType(cel.IntType), |
| 527 | alias: "unpacked_purpose", |
| 528 | expr: `[1, 2, 3].map(i, i * 2)`, |
| 529 | }, |
| 530 | }, |
| 531 | inlined: `[1, 2, 3].map(i, i * 2).size() != 0`, |
| 532 | folded: `true`, |
| 533 | }, |
| 534 | { |
| 535 | expr: `has(msg.single_any.processing_purpose) ? msg.single_any.processing_purpose[0] : 42`, |
| 536 | vars: []varDecl{ |
| 537 | { |
| 538 | name: "msg", |
| 539 | t: cel.ObjectType("google.expr.proto3.test.TestAllTypes"), |
| 540 | }, |
| 541 | { |
| 542 | name: "unpacked_purpose", |
| 543 | t: cel.ListType(cel.IntType), |
| 544 | }, |
| 545 | }, |
| 546 | inlineVars: []inlineVarExpr{ |
| 547 | { |
| 548 | name: "msg.single_any.processing_purpose", |
| 549 | t: cel.ListType(cel.IntType), |
| 550 | alias: "unpacked_purpose", |
| 551 | expr: `[1, 2, 3].map(i, i * 2)`, |
| 552 | }, |
| 553 | }, |
| 554 | inlined: `cel.bind(unpacked_purpose, [1, 2, 3].map(i, i * 2), (unpacked_purpose.size() != 0) ? (unpacked_purpose[0]) : 42)`, |
| 555 | folded: `2`, |
| 556 | }, |
| 557 | { |
| 558 | expr: `has(msg.single_any.processing_purpose) ? msg.single_any.processing_purpose.map(i, i * 2)[0] : 42`, |
| 559 | vars: []varDecl{ |
| 560 | { |
| 561 | name: "msg", |
| 562 | t: cel.ObjectType("google.expr.proto3.test.TestAllTypes"), |
| 563 | }, |
| 564 | { |
| 565 | name: "unpacked_purpose", |
| 566 | t: cel.ListType(cel.IntType), |
| 567 | }, |
| 568 | }, |
| 569 | inlineVars: []inlineVarExpr{ |
| 570 | { |
| 571 | name: "msg.single_any.processing_purpose", |
| 572 | t: cel.ListType(cel.IntType), |
| 573 | alias: "unpacked_purpose", |
| 574 | expr: `[1, 2, 3].map(i, i * 2)`, |
| 575 | }, |
| 576 | }, |
| 577 | inlined: `cel.bind(unpacked_purpose, [1, 2, 3].map(i, i * 2), (unpacked_purpose.size() != 0) ? (unpacked_purpose.map(i, i * 2)[0]) : 42)`, |
| 578 | folded: `4`, |
| 579 | }, |
| 580 | { |
| 581 | expr: `msg.single_any.processing_purpose.filter(j, |
| 582 | j < msg.single_any.processing_purpose.size()) == [2]`, |
| 583 | vars: []varDecl{ |
| 584 | { |
| 585 | name: "msg", |
| 586 | t: cel.ObjectType("google.expr.proto3.test.TestAllTypes"), |
| 587 | }, |
| 588 | { |
| 589 | name: "unpacked_purpose", |
| 590 | t: cel.ListType(cel.IntType), |
| 591 | }, |
| 592 | }, |
| 593 | inlineVars: []inlineVarExpr{ |
| 594 | { |
| 595 | name: "msg.single_any.processing_purpose", |
| 596 | t: cel.ListType(cel.IntType), |
| 597 | alias: "unpacked_purpose", |
| 598 | expr: `[1, 2, 3].map(i, i * 2)`, |
| 599 | }, |
| 600 | }, |
| 601 | inlined: `cel.bind(unpacked_purpose, [1, 2, 3].map(i, i * 2), unpacked_purpose.filter(j, j < unpacked_purpose.size())) == [2]`, |
| 602 | folded: `true`, |
| 603 | }, |
| 604 | { |
| 605 | expr: `has(msg.single_any.listA) && msg.single_any.listB.size() > 0 && |
| 606 | msg.single_any.listB.all(b, b == msg.single_any.listA[0]) && |
| 607 | msg.single_any.listA.all(a, a == msg.single_any.listB[0])`, |
| 608 | vars: []varDecl{ |
| 609 | { |
| 610 | name: "msg", |
| 611 | t: cel.ObjectType("google.expr.proto3.test.TestAllTypes"), |
| 612 | }, |
| 613 | { |
| 614 | name: "listA", |
| 615 | t: cel.ListType(cel.IntType), |
| 616 | }, |
| 617 | { |
| 618 | name: "listB", |
| 619 | t: cel.ListType(cel.IntType), |
| 620 | }, |
| 621 | }, |
| 622 | inlineVars: []inlineVarExpr{ |
| 623 | { |
| 624 | name: "msg.single_any.listA", |
| 625 | t: cel.ListType(cel.IntType), |
| 626 | alias: "listA", |
| 627 | expr: `[1, 1]`, |
| 628 | }, |
| 629 | { |
| 630 | name: "msg.single_any.listB", |
| 631 | t: cel.ListType(cel.IntType), |
| 632 | alias: "listB", |
| 633 | expr: `[1, 1, 1]`, |
| 634 | }, |
| 635 | }, |
| 636 | inlined: "cel.bind(listA, [1, 1], cel.bind(listB, [1, 1, 1], listA.size() != 0 && listB.size() > 0 &&\nlistB.all(b, b == listA[0]) && listA.all(a, a == listB[0])))", |
| 637 | folded: `true`, |
| 638 | }, |
| 639 | { |
| 640 | expr: `((msg.single_any.listB.all(b, b == msg.single_any.listA[0]) && |
| 641 | msg.single_any.listA.all(a, a == msg.single_any.listB[0])) || |
| 642 | msg.single_any.listA.size() == 0) || |
| 643 | false`, |
| 644 | vars: []varDecl{ |
| 645 | { |
| 646 | name: "msg", |
| 647 | t: cel.ObjectType("google.expr.proto3.test.TestAllTypes"), |
| 648 | }, |
| 649 | { |
| 650 | name: "listA", |
| 651 | t: cel.ListType(cel.IntType), |
| 652 | }, |
| 653 | { |
| 654 | name: "listB", |
| 655 | t: cel.ListType(cel.IntType), |
| 656 | }, |
| 657 | }, |
| 658 | inlineVars: []inlineVarExpr{ |
| 659 | { |
| 660 | name: "msg.single_any.listA", |
| 661 | t: cel.ListType(cel.IntType), |
| 662 | alias: "listA", |
| 663 | expr: `[1, 1]`, |
| 664 | }, |
| 665 | { |
| 666 | name: "msg.single_any.listB", |
| 667 | t: cel.ListType(cel.IntType), |
| 668 | alias: "listB", |
| 669 | expr: `[1, 1, 1]`, |
| 670 | }, |
| 671 | }, |
| 672 | inlined: "cel.bind(listA, [1, 1], cel.bind(listB, [1, 1, 1], listB.all(b, b == listA[0]) &&\nlistA.all(a, a == listB[0])) || listA.size() == 0) || false", |
| 673 | folded: `true`, |
| 674 | }, |
| 675 | { |
| 676 | expr: `has(m.child) && has(m.child.payload)`, |
| 677 | vars: []varDecl{ |
| 678 | { |
| 679 | name: "m", |
| 680 | t: cel.ObjectType("google.expr.proto3.test.NestedTestAllTypes"), |
| 681 | }, |
| 682 | { |
| 683 | name: "m_view", |
| 684 | t: cel.MapType(cel.StringType, cel.ObjectType("google.expr.proto3.test.NestedTestAllTypes")), |
| 685 | }, |
| 686 | }, |
| 687 | inlineVars: []inlineVarExpr{ |
| 688 | { |
| 689 | name: "m.child", |
| 690 | t: cel.ObjectType("google.expr.proto3.test.NestedTestAllTypes"), |
| 691 | alias: "child", |
| 692 | expr: "m_view.nested.child", |
| 693 | }, |
| 694 | }, |
| 695 | inlined: "has(m_view.nested.child) && has(m_view.nested.child.payload)", |
| 696 | folded: "has(m_view.nested.child) && has(m_view.nested.child.payload)", |
| 697 | }, |
| 698 | } |
| 699 | for _, tst := range tests { |
| 700 | tc := tst |
| 701 | t.Run(tc.expr, func(t *testing.T) { |
| 702 | opts := []cel.EnvOption{ |
| 703 | cel.Container("google.expr"), |
| 704 | cel.Types(&proto3pb.TestAllTypes{}), |
| 705 | cel.OptionalTypes(), |
| 706 | cel.EnableMacroCallTracking()} |
| 707 | |
| 708 | varDecls := make([]cel.EnvOption, len(tc.vars)) |
| 709 | for i, v := range tc.vars { |
| 710 | varDecls[i] = cel.Variable(v.name, v.t) |
| 711 | } |
| 712 | e, err := cel.NewEnv(append(varDecls, opts...)...) |
| 713 | if err != nil { |
| 714 | t.Fatalf("NewEnv() failed: %v", err) |
| 715 | } |
| 716 | inlinedVars := []*cel.InlineVariable{} |
| 717 | for _, v := range tc.inlineVars { |
| 718 | if v.expr == "" { |
| 719 | continue |
| 720 | } |
| 721 | checked, iss := e.Compile(v.expr) |
| 722 | if iss.Err() != nil { |
| 723 | t.Fatalf("Compile(%q) failed: %v", v.expr, iss.Err()) |
| 724 | } |
| 725 | if v.alias == "" { |
| 726 | inlinedVars = append(inlinedVars, cel.NewInlineVariable(v.name, checked)) |
| 727 | } else { |
| 728 | inlinedVars = append(inlinedVars, cel.NewInlineVariableWithAlias(v.name, v.alias, checked)) |
| 729 | } |
| 730 | } |
| 731 | checked, iss := e.Compile(tc.expr) |
| 732 | if iss.Err() != nil { |
| 733 | t.Fatalf("Compile() failed: %v", iss.Err()) |
| 734 | } |
| 735 | |
| 736 | opt, err := cel.NewStaticOptimizer(cel.NewInliningOptimizer(inlinedVars...)) |
| 737 | if err != nil { |
| 738 | t.Fatalf("NewStaticOptimizer() failed: %v", err) |
| 739 | } |
| 740 | optimized, iss := opt.Optimize(e, checked) |
| 741 | if iss.Err() != nil { |
| 742 | t.Fatalf("Optimize() generated an invalid AST: %v", iss.Err()) |
| 743 | } |
| 744 | inlined, err := cel.AstToString(optimized) |
| 745 | if err != nil { |
| 746 | t.Fatalf("cel.AstToString() failed: %v", err) |
| 747 | } |
| 748 | if inlined != tc.inlined { |
| 749 | t.Errorf("inlined got %q, wanted %q", inlined, tc.inlined) |
| 750 | } |
| 751 | folder, err := cel.NewConstantFoldingOptimizer() |
| 752 | if err != nil { |
| 753 | t.Fatalf("NewConstantFoldingOptimizer() failed: %v", err) |
| 754 | } |
| 755 | opt, err = cel.NewStaticOptimizer(folder) |
| 756 | if err != nil { |
| 757 | t.Fatalf("NewStaticOptimizer() failed: %v", err) |
| 758 | } |
| 759 | optimized, iss = opt.Optimize(e, optimized) |
| 760 | if iss.Err() != nil { |
| 761 | t.Fatalf("Optimize() generated an invalid AST: %v", iss.Err()) |
| 762 | } |
| 763 | folded, err := cel.AstToString(optimized) |
| 764 | if err != nil { |
| 765 | t.Fatalf("cel.AstToString() failed: %v", err) |
| 766 | } |
| 767 | if folded != tc.folded { |
| 768 | t.Errorf("folded got %q, wanted %q", folded, tc.folded) |
| 769 | } |
| 770 | }) |
| 771 | } |
| 772 | } |