Skip to content

Commit 423e8bb

Browse files
jckingcopybara-github
authored andcommitted
Enable conformance tests for optional_type
PiperOrigin-RevId: 620293266
1 parent e37e14b commit 423e8bb

File tree

7 files changed

+125
-11
lines changed

7 files changed

+125
-11
lines changed

BUILD.bazel

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
# Includes package-wide build definitions for maven imported java targets
1616
# that needs to be defined separately.
1717

18+
load(
19+
"@bazel_tools//tools/jdk:default_java_toolchain.bzl",
20+
"BASE_JDK9_JVM_OPTS",
21+
"DEFAULT_JAVACOPTS",
22+
"DEFAULT_TOOLCHAIN_CONFIGURATION",
23+
"default_java_toolchain",
24+
)
1825
load("@rules_license//rules:license.bzl", "license")
1926

2027
licenses(["notice"]) # Apache License 2.0
@@ -82,14 +89,6 @@ java_library(
8289
],
8390
)
8491

85-
load(
86-
"@bazel_tools//tools/jdk:default_java_toolchain.bzl",
87-
"BASE_JDK9_JVM_OPTS",
88-
"DEFAULT_JAVACOPTS",
89-
"DEFAULT_TOOLCHAIN_CONFIGURATION",
90-
"default_java_toolchain",
91-
)
92-
9392
default_java_toolchain(
9493
name = "repository_default_toolchain",
9594
configuration = DEFAULT_TOOLCHAIN_CONFIGURATION,

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,34 +297,42 @@ public CelComprehension comprehension() {
297297
return exprKind().comprehension();
298298
}
299299

300+
@CanIgnoreReturnValue
300301
public Builder setConstant(CelConstant constant) {
301302
return setExprKind(AutoOneOf_CelExpr_ExprKind.constant(constant));
302303
}
303304

305+
@CanIgnoreReturnValue
304306
public Builder setIdent(CelIdent ident) {
305307
return setExprKind(AutoOneOf_CelExpr_ExprKind.ident(ident));
306308
}
307309

310+
@CanIgnoreReturnValue
308311
public Builder setCall(CelCall call) {
309312
return setExprKind(AutoOneOf_CelExpr_ExprKind.call(call));
310313
}
311314

315+
@CanIgnoreReturnValue
312316
public Builder setSelect(CelSelect select) {
313317
return setExprKind(AutoOneOf_CelExpr_ExprKind.select(select));
314318
}
315319

320+
@CanIgnoreReturnValue
316321
public Builder setCreateList(CelCreateList createList) {
317322
return setExprKind(AutoOneOf_CelExpr_ExprKind.createList(createList));
318323
}
319324

325+
@CanIgnoreReturnValue
320326
public Builder setCreateStruct(CelCreateStruct createStruct) {
321327
return setExprKind(AutoOneOf_CelExpr_ExprKind.createStruct(createStruct));
322328
}
323329

330+
@CanIgnoreReturnValue
324331
public Builder setCreateMap(CelCreateMap createMap) {
325332
return setExprKind(AutoOneOf_CelExpr_ExprKind.createMap(createMap));
326333
}
327334

335+
@CanIgnoreReturnValue
328336
public Builder setComprehension(CelComprehension comprehension) {
329337
return setExprKind(AutoOneOf_CelExpr_ExprKind.comprehension(comprehension));
330338
}

extensions/src/main/java/dev/cel/extensions/CelOptionalLibrary.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ private static Optional<CelExpr> expandOptMap(
240240
UNUSED_ITER_VAR,
241241
exprFactory.newList(),
242242
varName,
243-
exprFactory.newReceiverCall(Function.VALUE.getFunction(), target),
243+
exprFactory.newReceiverCall(
244+
Function.VALUE.getFunction(), exprFactory.copy(target)),
244245
exprFactory.newBoolLiteral(true),
245246
exprFactory.newIdentifier(varName),
246247
mapExpr)),
@@ -272,7 +273,7 @@ private static Optional<CelExpr> expandOptFlatMap(
272273
UNUSED_ITER_VAR,
273274
exprFactory.newList(),
274275
varName,
275-
exprFactory.newReceiverCall(Function.VALUE.getFunction(), target),
276+
exprFactory.newReceiverCall(Function.VALUE.getFunction(), exprFactory.copy(target)),
276277
exprFactory.newBoolLiteral(true),
277278
exprFactory.newIdentifier(varName),
278279
mapExpr),

parser/src/main/java/dev/cel/parser/CelMacroExprFactory.java

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,102 @@ public final CelSourceLocation getSourceLocation(CelExpr expr) {
5656
return getSourceLocation(expr.id());
5757
}
5858

59+
/** Duplicates {@link CelExpr} with a brand new set of identifiers. */
60+
public final CelExpr copy(CelExpr expr) {
61+
CelExpr.Builder builder = CelExpr.newBuilder().setId(copyExprId(expr.id()));
62+
switch (expr.exprKind().getKind()) {
63+
case CONSTANT:
64+
builder.setConstant(expr.constant());
65+
break;
66+
case IDENT:
67+
builder.setIdent(expr.ident());
68+
break;
69+
case SELECT:
70+
builder.setSelect(
71+
CelExpr.CelSelect.newBuilder()
72+
.setOperand(copy(expr.select().operand()))
73+
.setField(expr.select().field())
74+
.setTestOnly(expr.select().testOnly())
75+
.build());
76+
break;
77+
case CALL:
78+
{
79+
CelExpr.CelCall.Builder callBuilder =
80+
CelExpr.CelCall.newBuilder().setFunction(expr.call().function());
81+
expr.call().target().ifPresent(target -> callBuilder.setTarget(copy(target)));
82+
for (CelExpr arg : expr.call().args()) {
83+
callBuilder.addArgs(copy(arg));
84+
}
85+
builder.setCall(callBuilder.build());
86+
}
87+
break;
88+
case CREATE_LIST:
89+
{
90+
CelExpr.CelCreateList.Builder listBuilder =
91+
CelExpr.CelCreateList.newBuilder()
92+
.addOptionalIndices(expr.createList().optionalIndices());
93+
for (CelExpr element : expr.createList().elements()) {
94+
listBuilder.addElements(copy(element));
95+
}
96+
builder.setCreateList(listBuilder.build());
97+
}
98+
break;
99+
case CREATE_STRUCT:
100+
{
101+
CelExpr.CelCreateStruct.Builder structBuilder =
102+
CelExpr.CelCreateStruct.newBuilder()
103+
.setMessageName(expr.createStruct().messageName());
104+
for (CelExpr.CelCreateStruct.Entry entry : expr.createStruct().entries()) {
105+
structBuilder.addEntries(
106+
CelExpr.CelCreateStruct.Entry.newBuilder()
107+
.setId(copyExprId(entry.id()))
108+
.setFieldKey(entry.fieldKey())
109+
.setValue(copy(entry.value()))
110+
.setOptionalEntry(entry.optionalEntry())
111+
.build());
112+
}
113+
builder.setCreateStruct(structBuilder.build());
114+
}
115+
break;
116+
case CREATE_MAP:
117+
{
118+
CelExpr.CelCreateMap.Builder mapBuilder = CelExpr.CelCreateMap.newBuilder();
119+
for (CelExpr.CelCreateMap.Entry entry : expr.createMap().entries()) {
120+
mapBuilder.addEntries(
121+
CelExpr.CelCreateMap.Entry.newBuilder()
122+
.setId(copyExprId(entry.id()))
123+
.setKey(copy(entry.key()))
124+
.setValue(copy(entry.value()))
125+
.setOptionalEntry(entry.optionalEntry())
126+
.build());
127+
}
128+
builder.setCreateMap(mapBuilder.build());
129+
}
130+
break;
131+
case COMPREHENSION:
132+
builder.setComprehension(
133+
CelExpr.CelComprehension.newBuilder()
134+
.setIterVar(expr.comprehension().iterVar())
135+
.setIterRange(copy(expr.comprehension().iterRange()))
136+
.setAccuVar(expr.comprehension().accuVar())
137+
.setAccuInit(copy(expr.comprehension().accuInit()))
138+
.setLoopCondition(copy(expr.comprehension().loopCondition()))
139+
.setLoopStep(copy(expr.comprehension().loopStep()))
140+
.setResult(copy(expr.comprehension().result()))
141+
.build());
142+
break;
143+
case NOT_SET:
144+
break;
145+
}
146+
return builder.build();
147+
}
148+
149+
/**
150+
* Returns the next unique expression ID which is associated with the same metadata (i.e. source
151+
* location, types, references, etc.) as `id`.
152+
*/
153+
protected abstract long copyExprId(long id);
154+
59155
/** Retrieves the source location for the given {@link CelExpr} ID. */
60156
protected abstract CelSourceLocation getSourceLocation(long exprId);
61157

parser/src/main/java/dev/cel/parser/Parser.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,11 @@ private long nextExprId(int position) {
11101110
return exprId;
11111111
}
11121112

1113+
@Override
1114+
public long copyExprId(long id) {
1115+
return nextExprId(getPosition(id));
1116+
}
1117+
11131118
@Override
11141119
public long nextExprId() {
11151120
checkState(!positions.isEmpty()); // Should only be called while expanding macros.

parser/src/test/java/dev/cel/parser/CelMacroExprFactoryTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ private TestCelExprFactory() {
4141
exprId = 0L;
4242
}
4343

44+
@Override
45+
public long copyExprId(long unused) {
46+
return nextExprId();
47+
}
48+
4449
@Override
4550
public long nextExprId() {
4651
return ++exprId;

publish/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
load("@rules_jvm_external//:defs.bzl", "java_export")
21
load("@bazel_common//tools/maven:pom_file.bzl", "pom_file")
2+
load("@rules_jvm_external//:defs.bzl", "java_export")
33
load("//publish:cel_version.bzl", "CEL_VERSION")
44

55
# Note: These targets must reference the build targets in `src` directly in

0 commit comments

Comments
 (0)