Skip to content

Commit 32608d6

Browse files
dmitriplotnikovcopybara-github
authored andcommitted
Update the "regex" extension to be compatible with CelEnvironmentExporter
PiperOrigin-RevId: 789026242
1 parent ec0f274 commit 32608d6

5 files changed

Lines changed: 51 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
@@ -117,7 +117,8 @@ public Builder addStandardExtensions(CelOptions options) {
117117
CelExtensions.getExtensionLibrary("encoders", options),
118118
CelExtensions.getExtensionLibrary("lists", options),
119119
CelExtensions.getExtensionLibrary("math", options),
120-
CelExtensions.getExtensionLibrary("protos", options));
120+
CelExtensions.getExtensionLibrary("protos", options),
121+
CelExtensions.getExtensionLibrary("regex", options));
121122
// TODO: add support for remaining standard extensions
122123
return this;
123124
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ java_library(
278278
"//common:compiler_common",
279279
"//common/types",
280280
"//compiler:compiler_builder",
281+
"//extensions:extension_library",
281282
"//runtime",
282283
"//runtime:function_binding",
283284
"@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
@@ -313,6 +313,8 @@ public static CelExtensionLibrary<? extends CelExtensionLibrary.FeatureSet> getE
313313
return CelMathExtensions.library(options);
314314
case "protos":
315315
return CelProtoExtensions.library();
316+
case "regex":
317+
return CelRegexExtensions.library();
316318
// TODO: add support for remaining standard extensions
317319
default:
318320
throw new IllegalArgumentException("Unknown standard extension '" + name + "'");

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package dev.cel.extensions;
1616

17+
import static com.google.common.collect.ImmutableSet.toImmutableSet;
18+
1719
import com.google.common.collect.ImmutableList;
1820
import com.google.common.collect.ImmutableSet;
1921
import com.google.errorprone.annotations.Immutable;
@@ -35,7 +37,8 @@
3537

3638
/** Internal implementation of CEL regex extensions. */
3739
@Immutable
38-
final class CelRegexExtensions implements CelCompilerLibrary, CelRuntimeLibrary {
40+
final class CelRegexExtensions
41+
implements CelCompilerLibrary, CelRuntimeLibrary, CelExtensionLibrary.FeatureSet {
3942

4043
private static final String REGEX_REPLACE_FUNCTION = "regex.replace";
4144
private static final String REGEX_EXTRACT_FUNCTION = "regex.extract";
@@ -124,6 +127,25 @@ String getFunction() {
124127
}
125128
}
126129

130+
private static final CelExtensionLibrary<CelRegexExtensions> LIBRARY =
131+
new CelExtensionLibrary<CelRegexExtensions>() {
132+
private final CelRegexExtensions version0 = new CelRegexExtensions();
133+
134+
@Override
135+
public String name() {
136+
return "regex";
137+
}
138+
139+
@Override
140+
public ImmutableSet<CelRegexExtensions> versions() {
141+
return ImmutableSet.of(version0);
142+
}
143+
};
144+
145+
static CelExtensionLibrary<CelRegexExtensions> library() {
146+
return LIBRARY;
147+
}
148+
127149
private final ImmutableSet<Function> functions;
128150

129151
CelRegexExtensions() {
@@ -134,6 +156,16 @@ String getFunction() {
134156
this.functions = ImmutableSet.copyOf(functions);
135157
}
136158

159+
@Override
160+
public int version() {
161+
return 0;
162+
}
163+
164+
@Override
165+
public ImmutableSet<CelFunctionDecl> functions() {
166+
return functions.stream().map(f -> f.functionDecl).collect(toImmutableSet());
167+
}
168+
137169
@Override
138170
public void setCheckerOptions(CelCheckerBuilder checkerBuilder) {
139171
functions.forEach(function -> checkerBuilder.addFunctionDeclarations(function.functionDecl));

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
2222
import com.google.testing.junit.testparameterinjector.TestParameters;
2323
import dev.cel.common.CelAbstractSyntaxTree;
24+
import dev.cel.common.CelFunctionDecl;
25+
import dev.cel.common.CelOptions;
2426
import dev.cel.compiler.CelCompiler;
2527
import dev.cel.compiler.CelCompilerFactory;
2628
import dev.cel.runtime.CelEvaluationException;
@@ -38,6 +40,17 @@ public final class CelRegexExtensionsTest {
3840
private static final CelRuntime RUNTIME =
3941
CelRuntimeFactory.standardCelRuntimeBuilder().addLibraries(CelExtensions.regex()).build();
4042

43+
@Test
44+
public void library() {
45+
CelExtensionLibrary<?> library =
46+
CelExtensions.getExtensionLibrary("regex", CelOptions.DEFAULT);
47+
assertThat(library.name()).isEqualTo("regex");
48+
assertThat(library.latest().version()).isEqualTo(0);
49+
assertThat(library.version(0).functions().stream().map(CelFunctionDecl::name))
50+
.containsExactly("regex.replace", "regex.extract", "regex.extractAll");
51+
assertThat(library.version(0).macros()).isEmpty();
52+
}
53+
4154
@Test
4255
@TestParameters("{target: 'abc', regex: '^', replaceStr: 'start_', res: 'start_abc'}")
4356
@TestParameters("{target: 'abc', regex: '$', replaceStr: '_end', res: 'abc_end'}")

0 commit comments

Comments
 (0)