Skip to content

Commit 00de410

Browse files
CEL Dev Teamcopybara-github
authored andcommitted
Add an overload for CelMutableExpr.ofComprehension.
The new overload allows creating a `CelMutableExpr` from a `CelMutableComprehension` without specifying an expression ID, defaulting the ID to 0. PiperOrigin-RevId: 807117259
1 parent 0bb2f72 commit 00de410

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

common/src/main/java/dev/cel/common/ast/CelMutableExpr.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,10 @@ public static CelMutableExpr ofMap(long id, CelMutableMap mutableMap) {
10341034
return new CelMutableExpr(id, mutableMap);
10351035
}
10361036

1037+
public static CelMutableExpr ofComprehension(CelMutableComprehension mutableComprehension) {
1038+
return ofComprehension(0, mutableComprehension);
1039+
}
1040+
10371041
public static CelMutableExpr ofComprehension(
10381042
long id, CelMutableComprehension mutableComprehension) {
10391043
return new CelMutableExpr(id, mutableComprehension);

common/src/test/java/dev/cel/common/ast/CelMutableExprTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,35 @@ public void mutableMap_deepCopy() {
600600
.containsExactlyElementsIn(deepCopiedExpr.map().entries());
601601
}
602602

603+
@Test
604+
public void ofComprehension() {
605+
CelMutableExpr mutableExpr =
606+
CelMutableExpr.ofComprehension(
607+
CelMutableComprehension.create(
608+
"iterVar",
609+
CelMutableExpr.ofList(
610+
CelMutableList.create(CelMutableExpr.ofConstant(CelConstant.ofValue(true)))),
611+
"accuVar",
612+
CelMutableExpr.ofConstant(CelConstant.ofValue(true)),
613+
CelMutableExpr.ofConstant(CelConstant.ofValue(true)),
614+
CelMutableExpr.ofConstant(CelConstant.ofValue(true)),
615+
CelMutableExpr.ofIdent("__result__")));
616+
617+
assertThat(mutableExpr.id()).isEqualTo(0L);
618+
assertThat(mutableExpr.comprehension())
619+
.isEqualTo(
620+
CelMutableComprehension.create(
621+
"iterVar",
622+
"",
623+
CelMutableExpr.ofList(
624+
CelMutableList.create(CelMutableExpr.ofConstant(CelConstant.ofValue(true)))),
625+
"accuVar",
626+
CelMutableExpr.ofConstant(CelConstant.ofValue(true)),
627+
CelMutableExpr.ofConstant(CelConstant.ofValue(true)),
628+
CelMutableExpr.ofConstant(CelConstant.ofValue(true)),
629+
CelMutableExpr.ofIdent("__result__")));
630+
}
631+
603632
@Test
604633
public void ofComprehension_withId() {
605634
CelMutableExpr mutableExpr =

0 commit comments

Comments
 (0)