3232import dev .cel .common .navigation .CelNavigableAst ;
3333import dev .cel .common .navigation .CelNavigableExpr ;
3434import dev .cel .extensions .CelOptionalLibrary .Function ;
35+ import dev .cel .optimizer .AstMutator ;
3536import dev .cel .optimizer .CelAstOptimizer ;
3637import dev .cel .optimizer .CelOptimizationException ;
37- import dev .cel .optimizer .MutableAst ;
3838import dev .cel .parser .Operator ;
3939import dev .cel .runtime .CelEvaluationException ;
4040import java .util .Collection ;
@@ -73,7 +73,7 @@ public static ConstantFoldingOptimizer newInstance(
7373 0 , Optional .empty (), Function .OPTIONAL_NONE .getFunction (), ImmutableList .of ());
7474
7575 private final ConstantFoldingOptions constantFoldingOptions ;
76- private final MutableAst mutableAst ;
76+ private final AstMutator astMutator ;
7777
7878 @ Override
7979 public OptimizationResult optimize (CelNavigableAst navigableAst , Cel cel )
@@ -117,7 +117,7 @@ public OptimizationResult optimize(CelNavigableAst navigableAst, Cel cel)
117117 // If the output is a list, map, or struct which contains optional entries, then prune it
118118 // to make sure that the optionals, if resolved, do not surface in the output literal.
119119 CelAbstractSyntaxTree newAst = pruneOptionalElements (navigableAst );
120- return OptimizationResult .create (mutableAst .renumberIdsConsecutively (newAst ));
120+ return OptimizationResult .create (astMutator .renumberIdsConsecutively (newAst ));
121121 }
122122
123123 private static boolean canFold (CelNavigableExpr navigableExpr ) {
@@ -236,7 +236,7 @@ private Optional<CelAbstractSyntaxTree> maybeFold(
236236 }
237237
238238 return maybeAdaptEvaluatedResult (result )
239- .map (celExpr -> mutableAst .replaceSubtree (ast , celExpr , node .id ()));
239+ .map (celExpr -> astMutator .replaceSubtree (ast , celExpr , node .id ()));
240240 }
241241
242242 private Optional <CelExpr > maybeAdaptEvaluatedResult (Object result ) {
@@ -289,7 +289,7 @@ private Optional<CelAbstractSyntaxTree> maybeRewriteOptional(
289289 // An empty optional value was encountered. Rewrite the tree with optional.none call.
290290 // This is to account for other optional functions returning an empty optional value
291291 // e.g: optional.ofNonZeroValue(0)
292- return Optional .of (mutableAst .replaceSubtree (ast , OPTIONAL_NONE_EXPR , expr .id ()));
292+ return Optional .of (astMutator .replaceSubtree (ast , OPTIONAL_NONE_EXPR , expr .id ()));
293293 }
294294 } else if (!expr .callOrDefault ().function ().equals (Function .OPTIONAL_OF .getFunction ())) {
295295 Object unwrappedResult = optResult .get ();
@@ -309,7 +309,7 @@ private Optional<CelAbstractSyntaxTree> maybeRewriteOptional(
309309 .build ())
310310 .build ())
311311 .build ();
312- return Optional .of (mutableAst .replaceSubtree (ast , newOptionalOfCall , expr .id ()));
312+ return Optional .of (astMutator .replaceSubtree (ast , newOptionalOfCall , expr .id ()));
313313 }
314314
315315 return Optional .empty ();
@@ -338,7 +338,7 @@ private Optional<CelAbstractSyntaxTree> maybePruneBranches(
338338 }
339339 CelExpr result = cond .constant ().booleanValue () ? truthy : falsy ;
340340
341- return Optional .of (mutableAst .replaceSubtree (ast , result , expr .id ()));
341+ return Optional .of (astMutator .replaceSubtree (ast , result , expr .id ()));
342342 } else if (function .equals (Operator .IN .getFunction ())) {
343343 CelExpr callArg = call .args ().get (1 );
344344 if (!callArg .exprKind ().getKind ().equals (Kind .CREATE_LIST )) {
@@ -348,7 +348,7 @@ private Optional<CelAbstractSyntaxTree> maybePruneBranches(
348348 CelCreateList haystack = callArg .createList ();
349349 if (haystack .elements ().isEmpty ()) {
350350 return Optional .of (
351- mutableAst .replaceSubtree (
351+ astMutator .replaceSubtree (
352352 ast ,
353353 CelExpr .newBuilder ().setConstant (CelConstant .ofValue (false )).build (),
354354 expr .id ()));
@@ -363,7 +363,7 @@ private Optional<CelAbstractSyntaxTree> maybePruneBranches(
363363 if (elem .constantOrDefault ().equals (needleValue )
364364 || elem .identOrDefault ().equals (needleValue )) {
365365 return Optional .of (
366- mutableAst .replaceSubtree (
366+ astMutator .replaceSubtree (
367367 ast ,
368368 CelExpr .newBuilder ().setConstant (CelConstant .ofValue (true )).build (),
369369 expr .id ()));
@@ -396,16 +396,17 @@ private Optional<CelAbstractSyntaxTree> maybeShortCircuitCall(
396396 }
397397
398398 if (arg .constant ().booleanValue () == shortCircuit ) {
399- return Optional .of (mutableAst .replaceSubtree (ast , arg , expr .id ()));
399+ return Optional .of (astMutator .replaceSubtree (ast , arg , expr .id ()));
400400 }
401401 }
402402
403403 ImmutableList <CelExpr > newArgs = newArgsBuilder .build ();
404404 if (newArgs .isEmpty ()) {
405- return Optional .of (mutableAst .replaceSubtree (ast , call .args ().get (0 ), expr .id ()));
405+ CelExpr shortCircuitTarget = call .args ().get (0 ); // either args(0) or args(1) would work here
406+ return Optional .of (astMutator .replaceSubtree (ast , shortCircuitTarget , expr .id ()));
406407 }
407408 if (newArgs .size () == 1 ) {
408- return Optional .of (mutableAst .replaceSubtree (ast , newArgs .get (0 ), expr .id ()));
409+ return Optional .of (astMutator .replaceSubtree (ast , newArgs .get (0 ), expr .id ()));
409410 }
410411
411412 // TODO: Support folding variadic AND/ORs.
@@ -483,7 +484,7 @@ private CelAbstractSyntaxTree pruneOptionalListElements(CelAbstractSyntaxTree as
483484 updatedIndicesBuilder .add (newOptIndex );
484485 }
485486
486- return mutableAst .replaceSubtree (
487+ return astMutator .replaceSubtree (
487488 ast ,
488489 CelExpr .newBuilder ()
489490 .setCreateList (
@@ -530,7 +531,7 @@ private CelAbstractSyntaxTree pruneOptionalMapElements(CelAbstractSyntaxTree ast
530531 }
531532
532533 if (modified ) {
533- return mutableAst .replaceSubtree (
534+ return astMutator .replaceSubtree (
534535 ast ,
535536 CelExpr .newBuilder ()
536537 .setCreateMap (
@@ -576,7 +577,7 @@ private CelAbstractSyntaxTree pruneOptionalStructElements(
576577 }
577578
578579 if (modified ) {
579- return mutableAst .replaceSubtree (
580+ return astMutator .replaceSubtree (
580581 ast ,
581582 CelExpr .newBuilder ()
582583 .setCreateStruct (
@@ -622,6 +623,6 @@ public static Builder newBuilder() {
622623
623624 private ConstantFoldingOptimizer (ConstantFoldingOptions constantFoldingOptions ) {
624625 this .constantFoldingOptions = constantFoldingOptions ;
625- this .mutableAst = MutableAst .newInstance (constantFoldingOptions .maxIterationLimit ());
626+ this .astMutator = AstMutator .newInstance (constantFoldingOptions .maxIterationLimit ());
626627 }
627628}
0 commit comments