Skip to content

Commit abb3d10

Browse files
jnthntatumcopybara-github
authored andcommitted
Enable conformance tests for two-var comprehensions.
Add existsOne (preferred alternative to older exists_one) PiperOrigin-RevId: 853942944
1 parent f7b9112 commit abb3d10

5 files changed

Lines changed: 23 additions & 1 deletion

File tree

common/src/main/java/dev/cel/common/Operator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ public enum Operator {
4545
HAS("has"),
4646
ALL("all"),
4747
EXISTS("exists"),
48+
// Prefer EXISTS_ONE_NEW in new usages.
4849
EXISTS_ONE("exists_one"),
50+
EXISTS_ONE_NEW("existsOne"),
4951
MAP("map"),
5052
FILTER("filter"),
5153
NOT_STRICTLY_FALSE("@not_strictly_false"),
@@ -109,6 +111,7 @@ public static Optional<Operator> find(String text) {
109111
.put(EQUALS.getFunction(), EQUALS)
110112
.put(EXISTS.getFunction(), EXISTS)
111113
.put(EXISTS_ONE.getFunction(), EXISTS_ONE)
114+
.put(EXISTS_ONE_NEW.getFunction(), EXISTS_ONE_NEW)
112115
.put(FILTER.getFunction(), FILTER)
113116
.put(GREATER.getFunction(), GREATER)
114117
.put(GREATER_EQUALS.getFunction(), GREATER_EQUALS)

conformance/src/test/java/dev/cel/conformance/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ _ALL_TESTS = [
7575
"@cel_spec//tests/simple:testdata/lists.textproto",
7676
"@cel_spec//tests/simple:testdata/logic.textproto",
7777
"@cel_spec//tests/simple:testdata/macros.textproto",
78+
"@cel_spec//tests/simple:testdata/macros2.textproto",
7879
"@cel_spec//tests/simple:testdata/math_ext.textproto",
7980
"@cel_spec//tests/simple:testdata/namespace.textproto",
8081
"@cel_spec//tests/simple:testdata/optionals.textproto",

conformance/src/test/java/dev/cel/conformance/ConformanceTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public final class ConformanceTest extends Statement {
6666
.setOptions(OPTIONS)
6767
.addLibraries(
6868
CelExtensions.bindings(),
69+
CelExtensions.comprehensions(),
6970
CelExtensions.encoders(OPTIONS),
7071
CelExtensions.math(OPTIONS),
7172
CelExtensions.protos(),
@@ -80,6 +81,7 @@ public final class ConformanceTest extends Statement {
8081
.setOptions(OPTIONS)
8182
.addLibraries(
8283
CelExtensions.bindings(),
84+
CelExtensions.comprehensions(),
8385
CelExtensions.encoders(OPTIONS),
8486
CelExtensions.math(OPTIONS),
8587
CelExtensions.protos(),
@@ -106,10 +108,12 @@ private static CelChecker getChecker(SimpleTest test) throws Exception {
106108
.addFileTypes(dev.cel.expr.conformance.proto2.TestAllTypesExtensions.getDescriptor())
107109
.addLibraries(
108110
CelExtensions.bindings(),
111+
CelExtensions.comprehensions(),
109112
CelExtensions.encoders(OPTIONS),
110113
CelExtensions.math(OPTIONS),
111114
CelExtensions.sets(OPTIONS),
112115
CelExtensions.strings(),
116+
CelExtensions.comprehensions(),
113117
CelOptionalLibrary.INSTANCE)
114118
.addMessageTypes(dev.cel.expr.conformance.proto2.TestAllTypes.getDescriptor())
115119
.addMessageTypes(dev.cel.expr.conformance.proto3.TestAllTypes.getDescriptor())
@@ -120,6 +124,7 @@ private static CelChecker getChecker(SimpleTest test) throws Exception {
120124
CelRuntimeFactory.standardCelRuntimeBuilder()
121125
.setOptions(OPTIONS)
122126
.addLibraries(
127+
CelExtensions.comprehensions(),
123128
CelExtensions.encoders(OPTIONS),
124129
CelExtensions.math(OPTIONS),
125130
CelExtensions.sets(OPTIONS),
@@ -182,6 +187,7 @@ public void evaluate() throws Throwable {
182187
CelValidationResult response = getParser(test).parse(test.getExpr(), test.getName());
183188
assertThat(response.hasError()).isFalse();
184189
response = getChecker(test).check(response.getAst());
190+
System.out.println("response: " + response.getIssueString());
185191
assertThat(response.hasError()).isFalse();
186192
Type resultType = CelProtoTypes.celTypeToType(response.getAst().getResultType());
187193

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ public ImmutableSet<CelMacro> macros() {
159159
Operator.EXISTS_ONE.getFunction(),
160160
3,
161161
CelComprehensionsExtensions::expandExistsOneMacro),
162+
CelMacro.newReceiverMacro(
163+
Operator.EXISTS_ONE_NEW.getFunction(),
164+
3,
165+
CelComprehensionsExtensions::expandExistsOneMacro),
162166
CelMacro.newReceiverMacro(
163167
"transformList", 3, CelComprehensionsExtensions::transformListMacro),
164168
CelMacro.newReceiverMacro(

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,20 @@ public enum CelStandardMacro {
4848

4949
/**
5050
* Boolean comprehension which asserts that a predicate holds true for exactly one element in the
51-
* input range.
51+
* input range. Prefer EXISTS_ONE_NEW.
5252
*/
5353
EXISTS_ONE(
5454
CelMacro.newReceiverMacro(
5555
Operator.EXISTS_ONE.getFunction(), 2, CelStandardMacro::expandExistsOneMacro)),
5656

57+
/**
58+
* Boolean comprehension which asserts that a predicate holds true for exactly one element in the
59+
* input range.
60+
*/
61+
EXISTS_ONE_NEW(
62+
CelMacro.newReceiverMacro(
63+
Operator.EXISTS_ONE_NEW.getFunction(), 2, CelStandardMacro::expandExistsOneMacro)),
64+
5765
/**
5866
* Comprehension which applies a transform to each element in the input range and produces a list
5967
* of equivalent size as output.

0 commit comments

Comments
 (0)