From 32608d69ebf2cca215f2d8c039d52faa61187a0e Mon Sep 17 00:00:00 2001 From: Dmitri Plotnikov Date: Wed, 30 Jul 2025 13:43:53 -0700 Subject: [PATCH] Update the "regex" extension to be compatible with CelEnvironmentExporter PiperOrigin-RevId: 789026242 --- .../cel/bundle/CelEnvironmentExporter.java | 3 +- .../main/java/dev/cel/extensions/BUILD.bazel | 1 + .../dev/cel/extensions/CelExtensions.java | 2 ++ .../cel/extensions/CelRegexExtensions.java | 34 ++++++++++++++++++- .../extensions/CelRegexExtensionsTest.java | 13 +++++++ 5 files changed, 51 insertions(+), 2 deletions(-) diff --git a/bundle/src/main/java/dev/cel/bundle/CelEnvironmentExporter.java b/bundle/src/main/java/dev/cel/bundle/CelEnvironmentExporter.java index 92216fb50..61b87a7b5 100644 --- a/bundle/src/main/java/dev/cel/bundle/CelEnvironmentExporter.java +++ b/bundle/src/main/java/dev/cel/bundle/CelEnvironmentExporter.java @@ -117,7 +117,8 @@ public Builder addStandardExtensions(CelOptions options) { CelExtensions.getExtensionLibrary("encoders", options), CelExtensions.getExtensionLibrary("lists", options), CelExtensions.getExtensionLibrary("math", options), - CelExtensions.getExtensionLibrary("protos", options)); + CelExtensions.getExtensionLibrary("protos", options), + CelExtensions.getExtensionLibrary("regex", options)); // TODO: add support for remaining standard extensions return this; } diff --git a/extensions/src/main/java/dev/cel/extensions/BUILD.bazel b/extensions/src/main/java/dev/cel/extensions/BUILD.bazel index e84924434..5dbea5ca5 100644 --- a/extensions/src/main/java/dev/cel/extensions/BUILD.bazel +++ b/extensions/src/main/java/dev/cel/extensions/BUILD.bazel @@ -278,6 +278,7 @@ java_library( "//common:compiler_common", "//common/types", "//compiler:compiler_builder", + "//extensions:extension_library", "//runtime", "//runtime:function_binding", "@maven//:com_google_errorprone_error_prone_annotations", diff --git a/extensions/src/main/java/dev/cel/extensions/CelExtensions.java b/extensions/src/main/java/dev/cel/extensions/CelExtensions.java index d82256a46..5dd934bd9 100644 --- a/extensions/src/main/java/dev/cel/extensions/CelExtensions.java +++ b/extensions/src/main/java/dev/cel/extensions/CelExtensions.java @@ -313,6 +313,8 @@ public static CelExtensionLibrary getE return CelMathExtensions.library(options); case "protos": return CelProtoExtensions.library(); + case "regex": + return CelRegexExtensions.library(); // TODO: add support for remaining standard extensions default: throw new IllegalArgumentException("Unknown standard extension '" + name + "'"); diff --git a/extensions/src/main/java/dev/cel/extensions/CelRegexExtensions.java b/extensions/src/main/java/dev/cel/extensions/CelRegexExtensions.java index 545512fbd..368301ee8 100644 --- a/extensions/src/main/java/dev/cel/extensions/CelRegexExtensions.java +++ b/extensions/src/main/java/dev/cel/extensions/CelRegexExtensions.java @@ -14,6 +14,8 @@ package dev.cel.extensions; +import static com.google.common.collect.ImmutableSet.toImmutableSet; + import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.errorprone.annotations.Immutable; @@ -35,7 +37,8 @@ /** Internal implementation of CEL regex extensions. */ @Immutable -final class CelRegexExtensions implements CelCompilerLibrary, CelRuntimeLibrary { +final class CelRegexExtensions + implements CelCompilerLibrary, CelRuntimeLibrary, CelExtensionLibrary.FeatureSet { private static final String REGEX_REPLACE_FUNCTION = "regex.replace"; private static final String REGEX_EXTRACT_FUNCTION = "regex.extract"; @@ -124,6 +127,25 @@ String getFunction() { } } + private static final CelExtensionLibrary LIBRARY = + new CelExtensionLibrary() { + private final CelRegexExtensions version0 = new CelRegexExtensions(); + + @Override + public String name() { + return "regex"; + } + + @Override + public ImmutableSet versions() { + return ImmutableSet.of(version0); + } + }; + + static CelExtensionLibrary library() { + return LIBRARY; + } + private final ImmutableSet functions; CelRegexExtensions() { @@ -134,6 +156,16 @@ String getFunction() { this.functions = ImmutableSet.copyOf(functions); } + @Override + public int version() { + return 0; + } + + @Override + public ImmutableSet functions() { + return functions.stream().map(f -> f.functionDecl).collect(toImmutableSet()); + } + @Override public void setCheckerOptions(CelCheckerBuilder checkerBuilder) { functions.forEach(function -> checkerBuilder.addFunctionDeclarations(function.functionDecl)); diff --git a/extensions/src/test/java/dev/cel/extensions/CelRegexExtensionsTest.java b/extensions/src/test/java/dev/cel/extensions/CelRegexExtensionsTest.java index 7130b1c23..8a1bef014 100644 --- a/extensions/src/test/java/dev/cel/extensions/CelRegexExtensionsTest.java +++ b/extensions/src/test/java/dev/cel/extensions/CelRegexExtensionsTest.java @@ -21,6 +21,8 @@ import com.google.testing.junit.testparameterinjector.TestParameterInjector; import com.google.testing.junit.testparameterinjector.TestParameters; import dev.cel.common.CelAbstractSyntaxTree; +import dev.cel.common.CelFunctionDecl; +import dev.cel.common.CelOptions; import dev.cel.compiler.CelCompiler; import dev.cel.compiler.CelCompilerFactory; import dev.cel.runtime.CelEvaluationException; @@ -38,6 +40,17 @@ public final class CelRegexExtensionsTest { private static final CelRuntime RUNTIME = CelRuntimeFactory.standardCelRuntimeBuilder().addLibraries(CelExtensions.regex()).build(); + @Test + public void library() { + CelExtensionLibrary library = + CelExtensions.getExtensionLibrary("regex", CelOptions.DEFAULT); + assertThat(library.name()).isEqualTo("regex"); + assertThat(library.latest().version()).isEqualTo(0); + assertThat(library.version(0).functions().stream().map(CelFunctionDecl::name)) + .containsExactly("regex.replace", "regex.extract", "regex.extractAll"); + assertThat(library.version(0).macros()).isEmpty(); + } + @Test @TestParameters("{target: 'abc', regex: '^', replaceStr: 'start_', res: 'start_abc'}") @TestParameters("{target: 'abc', regex: '$', replaceStr: '_end', res: 'abc_end'}")