Skip to content

Commit 09bd48f

Browse files
dmitriplotnikovcopybara-github
authored andcommitted
Update the "sets" extension to be compatible with CelEnvironmentExporter
PiperOrigin-RevId: 789087644
1 parent 32608d6 commit 09bd48f

5 files changed

Lines changed: 50 additions & 2 deletions

File tree

bundle/src/main/java/dev/cel/bundle/CelEnvironmentExporter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ public Builder addStandardExtensions(CelOptions options) {
118118
CelExtensions.getExtensionLibrary("lists", options),
119119
CelExtensions.getExtensionLibrary("math", options),
120120
CelExtensions.getExtensionLibrary("protos", options),
121-
CelExtensions.getExtensionLibrary("regex", options));
121+
CelExtensions.getExtensionLibrary("regex", options),
122+
CelExtensions.getExtensionLibrary("sets", options));
122123
// TODO: add support for remaining standard extensions
123124
return this;
124125
}

extensions/src/main/java/dev/cel/extensions/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ java_library(
203203
"//common/internal:dynamic_proto",
204204
"//common/types",
205205
"//compiler:compiler_builder",
206+
"//extensions:extension_library",
206207
"//runtime",
207208
"//runtime:proto_message_runtime_equality",
208209
"@maven//:com_google_errorprone_error_prone_annotations",

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ public static CelExtensionLibrary<? extends CelExtensionLibrary.FeatureSet> getE
315315
return CelProtoExtensions.library();
316316
case "regex":
317317
return CelRegexExtensions.library();
318+
case "sets":
319+
return CelSetsExtensions.library(options);
318320
// TODO: add support for remaining standard extensions
319321
default:
320322
throw new IllegalArgumentException("Unknown standard extension '" + name + "'");

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
* rewrite the AST into a map to achieve a O(1) lookup.
4242
*/
4343
@Immutable
44-
final class CelSetsExtensions implements CelCompilerLibrary, CelRuntimeLibrary {
44+
final class CelSetsExtensions
45+
implements CelCompilerLibrary, CelRuntimeLibrary, CelExtensionLibrary.FeatureSet {
4546

4647
private static final String SET_CONTAINS_OVERLOAD_DOC =
4748
"Returns whether the first list argument contains all elements in the second list"
@@ -90,6 +91,28 @@ final class CelSetsExtensions implements CelCompilerLibrary, CelRuntimeLibrary {
9091
ListType.create(TypeParamType.create("T")),
9192
ListType.create(TypeParamType.create("T")))));
9293

94+
private static final class Library implements CelExtensionLibrary<CelSetsExtensions> {
95+
private final CelSetsExtensions version0;
96+
97+
Library(CelOptions celOptions) {
98+
version0 = new CelSetsExtensions(celOptions);
99+
}
100+
101+
@Override
102+
public String name() {
103+
return "sets";
104+
}
105+
106+
@Override
107+
public ImmutableSet<CelSetsExtensions> versions() {
108+
return ImmutableSet.of(version0);
109+
}
110+
}
111+
112+
static CelExtensionLibrary<CelSetsExtensions> library(CelOptions options) {
113+
return new Library(options);
114+
}
115+
93116
private final ImmutableSet<SetsFunction> functions;
94117
private final SetsExtensionsRuntimeImpl setsExtensionsRuntime;
95118

@@ -105,6 +128,16 @@ final class CelSetsExtensions implements CelCompilerLibrary, CelRuntimeLibrary {
105128
this.setsExtensionsRuntime = new SetsExtensionsRuntimeImpl(runtimeEquality, functions);
106129
}
107130

131+
@Override
132+
public int version() {
133+
return 0;
134+
}
135+
136+
@Override
137+
public ImmutableSet<CelFunctionDecl> functions() {
138+
return ImmutableSet.copyOf(FUNCTION_DECL_MAP.values());
139+
}
140+
108141
@Override
109142
public void setCheckerOptions(CelCheckerBuilder checkerBuilder) {
110143
functions.forEach(

extensions/src/test/java/dev/cel/extensions/CelSetsExtensionsTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ public final class CelSetsExtensionsTest {
7272
Math::toIntExact))
7373
.build();
7474

75+
@Test
76+
public void library() {
77+
CelExtensionLibrary<?> library =
78+
CelExtensions.getExtensionLibrary("sets", CelOptions.DEFAULT);
79+
assertThat(library.name()).isEqualTo("sets");
80+
assertThat(library.latest().version()).isEqualTo(0);
81+
assertThat(library.version(0).functions().stream().map(CelFunctionDecl::name))
82+
.containsExactly("sets.contains", "sets.equivalent", "sets.intersects");
83+
assertThat(library.version(0).macros()).isEmpty();
84+
}
85+
7586
@Test
7687
public void contains_integerListWithSameValue_succeeds() throws Exception {
7788
ImmutableList<Integer> list = ImmutableList.of(1, 2, 3, 4);

0 commit comments

Comments
 (0)