Skip to content

Commit a8e451d

Browse files
dmitriplotnikovcopybara-github
authored andcommitted
Update the "strings" extension to be compatible with CelEnvironmentExporter
PiperOrigin-RevId: 786914253
1 parent 09bd48f commit a8e451d

File tree

5 files changed

+60
-2
lines changed

5 files changed

+60
-2
lines changed

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

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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ java_library(
8585
"//common/internal",
8686
"//common/types",
8787
"//compiler:compiler_builder",
88+
"//extensions:extension_library",
8889
"//runtime",
8990
"//runtime:evaluation_exception_builder",
9091
"//runtime:function_binding",

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,8 @@ public static CelExtensionLibrary<? extends CelExtensionLibrary.FeatureSet> getE
317317
return CelRegexExtensions.library();
318318
case "sets":
319319
return CelSetsExtensions.library(options);
320+
case "strings":
321+
return CelStringExtensions.library();
320322
// TODO: add support for remaining standard extensions
321323
default:
322324
throw new IllegalArgumentException("Unknown standard extension '" + name + "'");

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

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

1515
package dev.cel.extensions;
1616

17+
import static com.google.common.collect.ImmutableSet.toImmutableSet;
1718
import static java.lang.Math.max;
1819
import static java.lang.Math.min;
1920

@@ -42,7 +43,8 @@
4243

4344
/** Internal implementation of CEL string extensions. */
4445
@Immutable
45-
public final class CelStringExtensions implements CelCompilerLibrary, CelRuntimeLibrary {
46+
public final class CelStringExtensions
47+
implements CelCompilerLibrary, CelRuntimeLibrary, CelExtensionLibrary.FeatureSet {
4648

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

255+
private static final CelExtensionLibrary<CelStringExtensions> LIBRARY =
256+
new CelExtensionLibrary<CelStringExtensions>() {
257+
private final CelStringExtensions version0 = new CelStringExtensions();
258+
259+
@Override
260+
public String name() {
261+
return "strings";
262+
}
263+
264+
@Override
265+
public ImmutableSet<CelStringExtensions> versions() {
266+
return ImmutableSet.of(version0);
267+
}
268+
};
269+
270+
static CelExtensionLibrary<CelStringExtensions> library() {
271+
return LIBRARY;
272+
}
273+
274+
@Override
275+
public int version() {
276+
return 0;
277+
}
278+
279+
@Override
280+
public ImmutableSet<CelFunctionDecl> functions() {
281+
return functions.stream().map(f -> f.functionDecl).collect(toImmutableSet());
282+
}
283+
253284
@Override
254285
public void setCheckerOptions(CelCheckerBuilder checkerBuilder) {
255286
functions.forEach(function -> checkerBuilder.addFunctionDeclarations(function.functionDecl));

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
2424
import com.google.testing.junit.testparameterinjector.TestParameters;
2525
import dev.cel.common.CelAbstractSyntaxTree;
26+
import dev.cel.common.CelFunctionDecl;
27+
import dev.cel.common.CelOptions;
2628
import dev.cel.common.CelValidationException;
2729
import dev.cel.common.types.SimpleType;
2830
import dev.cel.compiler.CelCompiler;
@@ -54,6 +56,27 @@ public final class CelStringExtensionsTest {
5456
private static final CelRuntime RUNTIME =
5557
CelRuntimeFactory.standardCelRuntimeBuilder().addLibraries(CelExtensions.strings()).build();
5658

59+
@Test
60+
public void library() {
61+
CelExtensionLibrary<?> library =
62+
CelExtensions.getExtensionLibrary("strings", CelOptions.DEFAULT);
63+
assertThat(library.name()).isEqualTo("strings");
64+
assertThat(library.latest().version()).isEqualTo(0);
65+
assertThat(library.version(0).functions().stream().map(CelFunctionDecl::name))
66+
.containsExactly(
67+
"charAt",
68+
"indexOf",
69+
"join",
70+
"lastIndexOf",
71+
"lowerAscii",
72+
"replace",
73+
"split",
74+
"substring",
75+
"trim",
76+
"upperAscii");
77+
assertThat(library.version(0).macros()).isEmpty();
78+
}
79+
5780
@Test
5881
@TestParameters("{string: 'abcd', beginIndex: 0, expectedResult: 'abcd'}")
5982
@TestParameters("{string: 'abcd', beginIndex: 1, expectedResult: 'bcd'}")

0 commit comments

Comments
 (0)