Skip to content

Commit afdb56e

Browse files
dmitriplotnikovcopybara-github
authored andcommitted
Update "bindings" extension to be compatible with CelEnvironmentExporter
PiperOrigin-RevId: 786892945
1 parent 712b265 commit afdb56e

File tree

6 files changed

+57
-4
lines changed

6 files changed

+57
-4
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public abstract static class Builder {
113113
@CanIgnoreReturnValue
114114
public Builder addStandardExtensions(CelOptions options) {
115115
addExtensionLibraries(
116+
CelExtensions.getExtensionLibrary("bindings", options),
116117
CelExtensions.getExtensionLibrary("math", options),
117118
CelExtensions.getExtensionLibrary("lists", options));
118119
// TODO: add support for remaining standard extensions

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ java_library(
138138
"//common:compiler_common",
139139
"//common/ast",
140140
"//compiler:compiler_builder",
141+
"//extensions:extension_library",
141142
"//parser:macro",
142143
"//parser:parser_builder",
143144
"@maven//:com_google_errorprone_error_prone_annotations",

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

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
import static com.google.common.base.Preconditions.checkNotNull;
1919

2020
import com.google.common.collect.ImmutableList;
21+
import com.google.common.collect.ImmutableSet;
2122
import com.google.errorprone.annotations.Immutable;
23+
import dev.cel.common.CelFunctionDecl;
2224
import dev.cel.common.CelIssue;
2325
import dev.cel.common.ast.CelExpr;
2426
import dev.cel.compiler.CelCompilerLibrary;
@@ -29,15 +31,47 @@
2931

3032
/** Internal implementation of the CEL local binding extensions. */
3133
@Immutable
32-
final class CelBindingsExtensions implements CelCompilerLibrary {
33-
34+
final class CelBindingsExtensions implements CelCompilerLibrary, CelExtensionLibrary.FeatureSet {
3435
private static final String CEL_NAMESPACE = "cel";
3536
private static final String UNUSED_ITER_VAR = "#unused";
3637

38+
private static final CelExtensionLibrary<CelBindingsExtensions> LIBRARY =
39+
new CelExtensionLibrary<CelBindingsExtensions>() {
40+
private final CelBindingsExtensions version0 = new CelBindingsExtensions();
41+
42+
@Override
43+
public String name() {
44+
return "bindings";
45+
}
46+
47+
@Override
48+
public ImmutableSet<CelBindingsExtensions> versions() {
49+
return ImmutableSet.of(version0);
50+
}
51+
};
52+
53+
static CelExtensionLibrary<CelBindingsExtensions> library() {
54+
return LIBRARY;
55+
}
56+
57+
@Override
58+
public int version() {
59+
return 0;
60+
}
61+
62+
@Override
63+
public ImmutableSet<CelFunctionDecl> functions() {
64+
return ImmutableSet.of();
65+
}
66+
67+
@Override
68+
public ImmutableSet<CelMacro> macros() {
69+
return ImmutableSet.of(CelMacro.newReceiverMacro("bind", 3, CelBindingsExtensions::expandBind));
70+
}
71+
3772
@Override
3873
public void setParserOptions(CelParserBuilder parserBuilder) {
39-
parserBuilder.addMacros(
40-
CelMacro.newReceiverMacro("bind", 3, CelBindingsExtensions::expandBind));
74+
parserBuilder.addMacros(macros());
4175
}
4276

4377
/**

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ public static ImmutableSet<String> getAllFunctionNames() {
303303
public static CelExtensionLibrary<? extends CelExtensionLibrary.FeatureSet> getExtensionLibrary(
304304
String name, CelOptions options) {
305305
switch (name) {
306+
case "bindings":
307+
return CelBindingsExtensions.library();
306308
case "math":
307309
return CelMathExtensions.library(options);
308310
case "lists":

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ java_library(
2020
"//compiler",
2121
"//compiler:compiler_builder",
2222
"//extensions",
23+
"//extensions:extension_library",
2324
"//extensions:lite_extensions",
2425
"//extensions:math",
2526
"//extensions:optional_library",

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@
2424
import com.google.testing.junit.testparameterinjector.TestParameters;
2525
import dev.cel.common.CelAbstractSyntaxTree;
2626
import dev.cel.common.CelFunctionDecl;
27+
import dev.cel.common.CelOptions;
2728
import dev.cel.common.CelOverloadDecl;
2829
import dev.cel.common.CelValidationException;
2930
import dev.cel.common.types.SimpleType;
3031
import dev.cel.common.types.StructTypeReference;
3132
import dev.cel.compiler.CelCompiler;
3233
import dev.cel.compiler.CelCompilerFactory;
3334
import dev.cel.expr.conformance.proto3.TestAllTypes;
35+
import dev.cel.extensions.CelExtensionLibrary.FeatureSet;
36+
import dev.cel.parser.CelMacro;
3437
import dev.cel.parser.CelStandardMacro;
3538
import dev.cel.runtime.CelFunctionBinding;
3639
import dev.cel.runtime.CelRuntime;
@@ -54,6 +57,17 @@ public final class CelBindingsExtensionsTest {
5457
.addLibraries(CelOptionalLibrary.INSTANCE)
5558
.build();
5659

60+
@Test
61+
public void library() {
62+
CelExtensionLibrary<? extends FeatureSet> library = CelExtensions.getExtensionLibrary(
63+
"bindings", CelOptions.DEFAULT);
64+
assertThat(library.name()).isEqualTo("bindings");
65+
assertThat(library.latest().version()).isEqualTo(0);
66+
assertThat(library.version(0).functions()).isEmpty();
67+
assertThat(library.version(0).macros().stream().map(CelMacro::getFunction))
68+
.containsExactly("bind");
69+
}
70+
5771
private enum BindingTestCase {
5872
BOOL_LITERAL("cel.bind(t, true, t)"),
5973
STRING_CONCAT("cel.bind(msg, \"hello\", msg + msg + msg) == \"hellohellohello\""),

0 commit comments

Comments
 (0)