Skip to content

Commit 02ac761

Browse files
l46kokcopybara-github
authored andcommitted
Plan CreateList
PiperOrigin-RevId: 833505696
1 parent 1c1dcc4 commit 02ac761

File tree

4 files changed

+112
-2
lines changed

4 files changed

+112
-2
lines changed

runtime/src/main/java/dev/cel/runtime/planner/BUILD.bazel

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ java_library(
1616
":attribute",
1717
":eval_attribute",
1818
":eval_const",
19+
":eval_create_list",
1920
":planned_program",
2021
"//:auto_value",
2122
"//common:cel_ast",
@@ -90,3 +91,16 @@ java_library(
9091
"@maven//:com_google_guava_guava",
9192
],
9293
)
94+
95+
java_library(
96+
name = "eval_create_list",
97+
srcs = ["EvalCreateList.java"],
98+
deps = [
99+
"//runtime",
100+
"//runtime:evaluation_listener",
101+
"//runtime:function_resolver",
102+
"//runtime:interpretable",
103+
"@maven//:com_google_errorprone_error_prone_annotations",
104+
"@maven//:com_google_guava_guava",
105+
],
106+
)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2025 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+
// https://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 dev.cel.runtime.planner;
16+
17+
import com.google.common.collect.ImmutableList;
18+
import com.google.errorprone.annotations.Immutable;
19+
import dev.cel.runtime.CelEvaluationException;
20+
import dev.cel.runtime.CelEvaluationListener;
21+
import dev.cel.runtime.CelFunctionResolver;
22+
import dev.cel.runtime.GlobalResolver;
23+
import dev.cel.runtime.Interpretable;
24+
25+
@Immutable
26+
final class EvalCreateList implements Interpretable {
27+
28+
// Array contents are not mutated
29+
@SuppressWarnings("Immutable")
30+
private final Interpretable[] values;
31+
32+
@Override
33+
public Object eval(GlobalResolver resolver) throws CelEvaluationException {
34+
ImmutableList.Builder<Object> builder = ImmutableList.builderWithExpectedSize(values.length);
35+
for (Interpretable value : values) {
36+
builder.add(value.eval(resolver));
37+
}
38+
return builder.build();
39+
}
40+
41+
@Override
42+
public Object eval(GlobalResolver resolver, CelEvaluationListener listener) {
43+
// TODO: Implement support
44+
throw new UnsupportedOperationException("Not yet supported");
45+
}
46+
47+
@Override
48+
public Object eval(GlobalResolver resolver, CelFunctionResolver lateBoundFunctionResolver) {
49+
// TODO: Implement support
50+
throw new UnsupportedOperationException("Not yet supported");
51+
}
52+
53+
@Override
54+
public Object eval(GlobalResolver resolver, CelFunctionResolver lateBoundFunctionResolver,
55+
CelEvaluationListener listener) {
56+
// TODO: Implement support
57+
throw new UnsupportedOperationException("Not yet supported");
58+
}
59+
60+
static EvalCreateList create(Interpretable[] values) {
61+
return new EvalCreateList(values);
62+
}
63+
64+
private EvalCreateList(Interpretable[] values) {
65+
this.values = values;
66+
}
67+
}

runtime/src/main/java/dev/cel/runtime/planner/ProgramPlanner.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
package dev.cel.runtime.planner;
1616

1717
import com.google.auto.value.AutoValue;
18+
import com.google.common.collect.ImmutableList;
1819
import com.google.common.collect.ImmutableMap;
1920
import javax.annotation.concurrent.ThreadSafe;
2021
import dev.cel.common.CelAbstractSyntaxTree;
2122
import dev.cel.common.CelContainer;
2223
import dev.cel.common.annotations.Internal;
2324
import dev.cel.common.ast.CelConstant;
2425
import dev.cel.common.ast.CelExpr;
26+
import dev.cel.common.ast.CelExpr.CelList;
2527
import dev.cel.common.ast.CelReference;
2628
import dev.cel.common.types.CelKind;
2729
import dev.cel.common.types.CelType;
@@ -65,6 +67,8 @@ private Interpretable plan(CelExpr celExpr, PlannerContext ctx) {
6567
return planConstant(celExpr.constant());
6668
case IDENT:
6769
return planIdent(celExpr, ctx);
70+
case LIST:
71+
return planCreateList(celExpr, ctx);
6872
case NOT_SET:
6973
throw new UnsupportedOperationException("Unsupported kind: " + celExpr.getKind());
7074
default:
@@ -124,6 +128,19 @@ private Interpretable planCheckedIdent(
124128
return EvalAttribute.create(attributeFactory.newAbsoluteAttribute(identRef.name()));
125129
}
126130

131+
private Interpretable planCreateList(CelExpr celExpr, PlannerContext ctx) {
132+
CelList list = celExpr.list();
133+
134+
ImmutableList<CelExpr> elements = list.elements();
135+
Interpretable[] values = new Interpretable[elements.size()];
136+
137+
for (int i = 0; i < elements.size(); i++) {
138+
values[i] = plan(elements.get(i), ctx);
139+
}
140+
141+
return EvalCreateList.create(values);
142+
}
143+
127144
@AutoValue
128145
abstract static class PlannerContext {
129146

runtime/src/test/java/dev/cel/runtime/planner/ProgramPlannerTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static java.nio.charset.StandardCharsets.UTF_8;
1919
import static org.junit.Assert.assertThrows;
2020

21+
import com.google.common.collect.ImmutableList;
2122
import com.google.common.collect.ImmutableMap;
2223
import com.google.common.collect.ImmutableSet;
2324
import com.google.common.primitives.UnsignedLong;
@@ -88,7 +89,7 @@ public void plan_constant(@TestParameter ConstantTestCase testCase) throws Excep
8889
}
8990

9091
@Test
91-
public void planIdent_enum() throws Exception {
92+
public void plan_ident_enum() throws Exception {
9293
if (isParseOnly) {
9394
// TODO Skip for now, requires attribute qualification
9495
return;
@@ -103,7 +104,7 @@ public void planIdent_enum() throws Exception {
103104
}
104105

105106
@Test
106-
public void planIdent_variable() throws Exception {
107+
public void plan_ident_variable() throws Exception {
107108
CelAbstractSyntaxTree ast = compile("int_var");
108109
Program program = PLANNER.plan(ast);
109110

@@ -112,6 +113,17 @@ public void planIdent_variable() throws Exception {
112113
assertThat(result).isEqualTo(1);
113114
}
114115

116+
@Test
117+
@SuppressWarnings("unchecked") // test only
118+
public void plan_createList() throws Exception {
119+
CelAbstractSyntaxTree ast = compile("[1, 'foo', true, [2, false]]");
120+
Program program = PLANNER.plan(ast);
121+
122+
ImmutableList<Object> result = (ImmutableList<Object>) program.eval();
123+
124+
assertThat(result).containsExactly(1L, "foo", true, ImmutableList.of(2L, false)).inOrder();
125+
}
126+
115127
@Test
116128
public void planIdent_typeLiteral(@TestParameter TypeLiteralTestCase testCase) throws Exception {
117129
if (isParseOnly) {

0 commit comments

Comments
 (0)