Skip to content

Update the "strings" extension to be compatible with CelEnvironmentExporter #767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ public Builder addStandardExtensions(CelOptions options) {
CelExtensions.getExtensionLibrary("math", options),
CelExtensions.getExtensionLibrary("protos", options),
CelExtensions.getExtensionLibrary("regex", options),
CelExtensions.getExtensionLibrary("sets", options));
CelExtensions.getExtensionLibrary("sets", options),
CelExtensions.getExtensionLibrary("strings", options));
// TODO: add support for remaining standard extensions
return this;
}
Expand Down
1 change: 1 addition & 0 deletions extensions/src/main/java/dev/cel/extensions/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ java_library(
"//common/internal",
"//common/types",
"//compiler:compiler_builder",
"//extensions:extension_library",
"//runtime",
"//runtime:evaluation_exception_builder",
"//runtime:function_binding",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ public static CelExtensionLibrary<? extends CelExtensionLibrary.FeatureSet> getE
return CelRegexExtensions.library();
case "sets":
return CelSetsExtensions.library(options);
case "strings":
return CelStringExtensions.library();
// TODO: add support for remaining standard extensions
default:
throw new IllegalArgumentException("Unknown standard extension '" + name + "'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package dev.cel.extensions;

import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static java.lang.Math.max;
import static java.lang.Math.min;

Expand Down Expand Up @@ -42,7 +43,8 @@

/** Internal implementation of CEL string extensions. */
@Immutable
public final class CelStringExtensions implements CelCompilerLibrary, CelRuntimeLibrary {
public final class CelStringExtensions
implements CelCompilerLibrary, CelRuntimeLibrary, CelExtensionLibrary.FeatureSet {

/** Denotes the string extension function */
@SuppressWarnings({"unchecked"}) // Unchecked: Type-checker guarantees casting safety.
Expand Down Expand Up @@ -250,6 +252,35 @@ String getFunction() {
this.functions = ImmutableSet.copyOf(functions);
}

private static final CelExtensionLibrary<CelStringExtensions> LIBRARY =
new CelExtensionLibrary<CelStringExtensions>() {
private final CelStringExtensions version0 = new CelStringExtensions();

@Override
public String name() {
return "strings";
}

@Override
public ImmutableSet<CelStringExtensions> versions() {
return ImmutableSet.of(version0);
}
};

static CelExtensionLibrary<CelStringExtensions> library() {
return LIBRARY;
}

@Override
public int version() {
return 0;
}

@Override
public ImmutableSet<CelFunctionDecl> functions() {
return functions.stream().map(f -> f.functionDecl).collect(toImmutableSet());
}

@Override
public void setCheckerOptions(CelCheckerBuilder checkerBuilder) {
functions.forEach(function -> checkerBuilder.addFunctionDeclarations(function.functionDecl));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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.common.CelValidationException;
import dev.cel.common.types.SimpleType;
import dev.cel.compiler.CelCompiler;
Expand Down Expand Up @@ -54,6 +56,27 @@ public final class CelStringExtensionsTest {
private static final CelRuntime RUNTIME =
CelRuntimeFactory.standardCelRuntimeBuilder().addLibraries(CelExtensions.strings()).build();

@Test
public void library() {
CelExtensionLibrary<?> library =
CelExtensions.getExtensionLibrary("strings", CelOptions.DEFAULT);
assertThat(library.name()).isEqualTo("strings");
assertThat(library.latest().version()).isEqualTo(0);
assertThat(library.version(0).functions().stream().map(CelFunctionDecl::name))
.containsExactly(
"charAt",
"indexOf",
"join",
"lastIndexOf",
"lowerAscii",
"replace",
"split",
"substring",
"trim",
"upperAscii");
assertThat(library.version(0).macros()).isEmpty();
}

@Test
@TestParameters("{string: 'abcd', beginIndex: 0, expectedResult: 'abcd'}")
@TestParameters("{string: 'abcd', beginIndex: 1, expectedResult: 'bcd'}")
Expand Down
Loading