Skip to content

Commit 1707b45

Browse files
l46kokcopybara-github
authored andcommitted
Add CelMutableCreateList
PiperOrigin-RevId: 623856014
1 parent d7b7a30 commit 1707b45

File tree

2 files changed

+166
-1
lines changed

2 files changed

+166
-1
lines changed

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

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public final class CelMutableExpr {
4343
private CelMutableIdent ident;
4444
private CelMutableSelect select;
4545
private CelMutableCall call;
46+
private CelMutableCreateList createList;
4647
private int hash = 0;
4748

4849
public long id() {
@@ -82,6 +83,11 @@ public CelMutableCall call() {
8283
return call;
8384
}
8485

86+
public CelMutableCreateList createList() {
87+
checkExprKind(Kind.CREATE_LIST);
88+
return createList;
89+
}
90+
8591
public void setConstant(CelConstant constant) {
8692
this.exprKind = ExprKind.Kind.CONSTANT;
8793
this.constant = checkNotNull(constant);
@@ -102,6 +108,11 @@ public void setCall(CelMutableCall call) {
102108
this.call = checkNotNull(call);
103109
}
104110

111+
public void setCreateList(CelMutableCreateList createList) {
112+
this.exprKind = ExprKind.Kind.CREATE_LIST;
113+
this.createList = checkNotNull(createList);
114+
}
115+
105116
/** A mutable identifier expression. */
106117
public static final class CelMutableIdent {
107118
private String name = "";
@@ -316,6 +327,74 @@ private CelMutableCall(CelMutableExpr target, String function, List<CelMutableEx
316327
}
317328
}
318329

330+
/**
331+
* A mutable list creation expression.
332+
*
333+
* <p>Lists may either be homogenous, e.g. `[1, 2, 3]`, or heterogeneous, e.g. `dyn([1, 'hello',
334+
* 2.0])`
335+
*/
336+
public static final class CelMutableCreateList {
337+
private final List<CelMutableExpr> elements;
338+
private final List<Integer> optionalIndices;
339+
340+
public List<CelMutableExpr> elements() {
341+
return elements;
342+
}
343+
344+
public void setElement(int index, CelMutableExpr element) {
345+
checkArgument(index >= 0 && index < elements().size());
346+
elements.set(index, checkNotNull(element));
347+
}
348+
349+
public List<Integer> optionalIndices() {
350+
return optionalIndices;
351+
}
352+
353+
@Override
354+
public boolean equals(Object obj) {
355+
if (obj == this) {
356+
return true;
357+
}
358+
if (obj instanceof CelMutableCreateList) {
359+
CelMutableCreateList that = (CelMutableCreateList) obj;
360+
return this.elements.equals(that.elements())
361+
&& this.optionalIndices.equals(that.optionalIndices());
362+
}
363+
364+
return false;
365+
}
366+
367+
@Override
368+
public int hashCode() {
369+
int h = 1;
370+
h *= 1000003;
371+
h ^= elements.hashCode();
372+
h *= 1000003;
373+
h ^= optionalIndices.hashCode();
374+
375+
return h;
376+
}
377+
378+
public static CelMutableCreateList create(CelMutableExpr... elements) {
379+
return create(Arrays.asList(checkNotNull(elements)));
380+
}
381+
382+
public static CelMutableCreateList create(List<CelMutableExpr> elements) {
383+
return create(elements, new ArrayList<>());
384+
}
385+
386+
public static CelMutableCreateList create(
387+
List<CelMutableExpr> mutableExprList, List<Integer> optionalIndices) {
388+
return new CelMutableCreateList(mutableExprList, optionalIndices);
389+
}
390+
391+
private CelMutableCreateList(
392+
List<CelMutableExpr> mutableExprList, List<Integer> optionalIndices) {
393+
this.elements = new ArrayList<>(checkNotNull(mutableExprList));
394+
this.optionalIndices = new ArrayList<>(checkNotNull(optionalIndices));
395+
}
396+
}
397+
319398
public static CelMutableExpr ofNotSet() {
320399
return ofNotSet(0L);
321400
}
@@ -356,6 +435,14 @@ public static CelMutableExpr ofCall(long id, CelMutableCall mutableCall) {
356435
return new CelMutableExpr(id, mutableCall);
357436
}
358437

438+
public static CelMutableExpr ofCreateList(CelMutableCreateList mutableCreateList) {
439+
return ofCreateList(0, mutableCreateList);
440+
}
441+
442+
public static CelMutableExpr ofCreateList(long id, CelMutableCreateList mutableCreateList) {
443+
return new CelMutableExpr(id, mutableCreateList);
444+
}
445+
359446
private CelMutableExpr(long id, CelConstant mutableConstant) {
360447
this.id = id;
361448
setConstant(mutableConstant);
@@ -376,6 +463,11 @@ private CelMutableExpr(long id, CelMutableCall mutableCall) {
376463
setCall(mutableCall);
377464
}
378465

466+
private CelMutableExpr(long id, CelMutableCreateList mutableCreateList) {
467+
this.id = id;
468+
setCreateList(mutableCreateList);
469+
}
470+
379471
private CelMutableExpr(long id) {
380472
this();
381473
this.id = id;
@@ -399,6 +491,7 @@ private Object exprValue() {
399491
case CALL:
400492
return call();
401493
case CREATE_LIST:
494+
return createList();
402495
case CREATE_STRUCT:
403496
case CREATE_MAP:
404497
case COMPREHENSION:

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

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
2424
import dev.cel.common.ast.CelExpr.ExprKind.Kind;
2525
import dev.cel.common.ast.CelMutableExpr.CelMutableCall;
26+
import dev.cel.common.ast.CelMutableExpr.CelMutableCreateList;
2627
import dev.cel.common.ast.CelMutableExpr.CelMutableIdent;
2728
import dev.cel.common.ast.CelMutableExpr.CelMutableSelect;
2829
import java.util.ArrayList;
@@ -250,6 +251,55 @@ public void mutableCall_setFunction() {
250251
assertThat(call.function()).isEqualTo("function2");
251252
}
252253

254+
@Test
255+
public void ofCreateList() {
256+
CelMutableExpr mutableExpr =
257+
CelMutableExpr.ofCreateList(
258+
CelMutableCreateList.create(
259+
CelMutableExpr.ofConstant(CelConstant.ofValue("element1")),
260+
CelMutableExpr.ofConstant(CelConstant.ofValue("element2"))));
261+
262+
assertThat(mutableExpr.id()).isEqualTo(0L);
263+
assertThat(mutableExpr.createList().elements())
264+
.containsExactly(
265+
CelMutableExpr.ofConstant(CelConstant.ofValue("element1")),
266+
CelMutableExpr.ofConstant(CelConstant.ofValue("element2")))
267+
.inOrder();
268+
assertThat(mutableExpr.createList().optionalIndices()).isEmpty();
269+
}
270+
271+
@Test
272+
public void ofCreateList_withId() {
273+
CelMutableExpr mutableExpr =
274+
CelMutableExpr.ofCreateList(
275+
1L,
276+
CelMutableCreateList.create(
277+
ImmutableList.of(
278+
CelMutableExpr.ofConstant(CelConstant.ofValue("element1")),
279+
CelMutableExpr.ofConstant(CelConstant.ofValue("element2"))),
280+
ImmutableList.of(0, 1)));
281+
282+
assertThat(mutableExpr.id()).isEqualTo(1L);
283+
assertThat(mutableExpr.createList().elements())
284+
.containsExactly(
285+
CelMutableExpr.ofConstant(CelConstant.ofValue("element1")),
286+
CelMutableExpr.ofConstant(CelConstant.ofValue("element2")))
287+
.inOrder();
288+
assertThat(mutableExpr.createList().optionalIndices()).containsExactly(0, 1).inOrder();
289+
}
290+
291+
@Test
292+
public void mutableCreateList_setElementAtIndex() {
293+
CelMutableCreateList createList =
294+
CelMutableCreateList.create(CelMutableExpr.ofConstant(CelConstant.ofValue("element1")));
295+
296+
createList.setElement(0, CelMutableExpr.ofConstant(CelConstant.ofValue("hello")));
297+
298+
assertThat(createList.elements())
299+
.containsExactly(CelMutableExpr.ofConstant(CelConstant.ofValue("hello")));
300+
assertThat(createList.elements()).isInstanceOf(ArrayList.class);
301+
}
302+
253303
@Test
254304
public void equalityTest() {
255305
new EqualsTester()
@@ -282,6 +332,18 @@ public void equalityTest() {
282332
CelMutableExpr.ofConstant(CelConstant.ofValue("target")),
283333
"function",
284334
CelMutableExpr.ofConstant(CelConstant.ofValue("arg")))))
335+
.addEqualityGroup(CelMutableExpr.ofCreateList(CelMutableCreateList.create()))
336+
.addEqualityGroup(
337+
CelMutableExpr.ofCreateList(
338+
6L,
339+
CelMutableCreateList.create(
340+
CelMutableExpr.ofConstant(CelConstant.ofValue("element1")),
341+
CelMutableExpr.ofConstant(CelConstant.ofValue("element2")))),
342+
CelMutableExpr.ofCreateList(
343+
6L,
344+
CelMutableCreateList.create(
345+
CelMutableExpr.ofConstant(CelConstant.ofValue("element1")),
346+
CelMutableExpr.ofConstant(CelConstant.ofValue("element2")))))
285347
.testEquals();
286348
}
287349

@@ -292,7 +354,7 @@ private enum MutableExprKindTestCase {
292354
IDENT(CelMutableExpr.ofIdent("test")),
293355
SELECT(CelMutableExpr.ofSelect(CelMutableSelect.create(CelMutableExpr.ofNotSet(), "field"))),
294356
CALL(CelMutableExpr.ofCall(CelMutableCall.create("call"))),
295-
;
357+
CREATE_LIST(CelMutableExpr.ofCreateList(CelMutableCreateList.create()));
296358

297359
private final CelMutableExpr mutableExpr;
298360

@@ -319,6 +381,9 @@ public void getExprValue_invalidKind_throws(@TestParameter MutableExprKindTestCa
319381
if (!testCaseKind.equals(Kind.CALL)) {
320382
assertThrows(IllegalArgumentException.class, testCase.mutableExpr::call);
321383
}
384+
if (!testCaseKind.equals(Kind.CREATE_LIST)) {
385+
assertThrows(IllegalArgumentException.class, testCase.mutableExpr::createList);
386+
}
322387
}
323388

324389
@SuppressWarnings("Immutable") // Mutable by design
@@ -339,6 +404,13 @@ private enum HashCodeTestCase {
339404
"function",
340405
CelMutableExpr.ofConstant(CelConstant.ofValue("arg")))),
341406
-1735261193),
407+
CREATE_LIST(
408+
CelMutableExpr.ofCreateList(
409+
6L,
410+
CelMutableCreateList.create(
411+
CelMutableExpr.ofConstant(CelConstant.ofValue("element1")),
412+
CelMutableExpr.ofConstant(CelConstant.ofValue("element2")))),
413+
165341403),
342414
;
343415

344416
private final CelMutableExpr mutableExpr;

0 commit comments

Comments
 (0)