Skip to content

Commit e613b3e

Browse files
dmitriplotnikovcopybara-github
authored andcommitted
Split CelRegexExtensions into CelRegexCompilerLibrary and CelRegexRuntimeLibrary to allow its usage in Lite runtime.
PiperOrigin-RevId: 993280770
1 parent 14b79d6 commit e613b3e

12 files changed

Lines changed: 762 additions & 242 deletions

File tree

‎extensions/BUILD.bazel‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,25 @@ cel_android_library(
141141
name = "lists_runtime_library_android",
142142
exports = ["//extensions/src/main/java/dev/cel/extensions:lists_runtime_library_android"],
143143
)
144+
145+
java_library(
146+
name = "regex",
147+
exports = ["//extensions/src/main/java/dev/cel/extensions:regex"],
148+
)
149+
150+
java_library(
151+
name = "regex_compiler_library",
152+
visibility = ["//:internal"],
153+
exports = ["//extensions/src/main/java/dev/cel/extensions:regex_compiler_library"],
154+
)
155+
156+
java_library(
157+
name = "regex_runtime_library",
158+
visibility = ["//:internal"],
159+
exports = ["//extensions/src/main/java/dev/cel/extensions:regex_runtime_library"],
160+
)
161+
162+
cel_android_library(
163+
name = "regex_runtime_library_android",
164+
exports = ["//extensions/src/main/java/dev/cel/extensions:regex_runtime_library_android"],
165+
)

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

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,20 +483,66 @@ cel_android_library(
483483
java_library(
484484
name = "regex",
485485
srcs = ["CelRegexExtensions.java"],
486+
tags = [
487+
],
486488
deps = [
489+
":extension_library",
490+
":regex_compiler_library",
491+
":regex_runtime_library",
487492
"//checker:checker_builder",
488-
"//common:compiler_common",
489-
"//common/types",
493+
"//common:cel_function_decl",
490494
"//compiler:compiler_builder",
491-
"//extensions:extension_library",
492495
"//runtime",
496+
"@maven//:com_google_errorprone_error_prone_annotations",
497+
"@maven//:com_google_guava_guava",
498+
],
499+
)
500+
501+
java_library(
502+
name = "regex_compiler_library",
503+
srcs = ["CelRegexCompilerLibrary.java"],
504+
tags = [
505+
],
506+
deps = [
507+
":extension_library",
508+
"//checker:checker_builder",
509+
"//common:cel_function_decl",
510+
"//common:cel_overload_decl",
511+
"//common/types",
512+
"//compiler:compiler_builder",
513+
"@maven//:com_google_errorprone_error_prone_annotations",
514+
"@maven//:com_google_guava_guava",
515+
],
516+
)
517+
518+
java_library(
519+
name = "regex_runtime_library",
520+
srcs = ["CelRegexRuntimeLibrary.java"],
521+
tags = [
522+
],
523+
deps = [
493524
"//runtime:function_binding",
525+
"//runtime:lite_runtime",
494526
"@maven//:com_google_errorprone_error_prone_annotations",
495527
"@maven//:com_google_guava_guava",
496528
"@maven//:com_google_re2j_re2j",
497529
],
498530
)
499531

532+
cel_android_library(
533+
name = "regex_runtime_library_android",
534+
srcs = ["CelRegexRuntimeLibrary.java"],
535+
tags = [
536+
],
537+
deps = [
538+
"//runtime:function_binding_android",
539+
"//runtime:lite_runtime_android",
540+
"@maven//:com_google_errorprone_error_prone_annotations",
541+
"@maven//:com_google_re2j_re2j",
542+
"@maven_android//:com_google_guava_guava",
543+
],
544+
)
545+
500546
java_library(
501547
name = "comprehensions",
502548
srcs = ["CelComprehensionsExtensions.java"],

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,39 @@ public static CelRegexExtensions regex() {
341341
return REGEX_EXTENSIONS;
342342
}
343343

344+
/**
345+
* Extended functions for Regular Expressions.
346+
*
347+
* <p>Refer to README.md for available functions.
348+
*/
349+
public static CelRegexExtensions regex(int version) {
350+
return CelRegexExtensions.library().version(version);
351+
}
352+
353+
/**
354+
* Extended functions for Regular Expressions.
355+
*
356+
* <p>Refer to README.md for available functions.
357+
*
358+
* <p>This will include only the specific functions denoted by {@link
359+
* CelRegexExtensions.Function}.
360+
*/
361+
public static CelRegexExtensions regex(CelRegexExtensions.Function... functions) {
362+
return regex(ImmutableSet.copyOf(functions));
363+
}
364+
365+
/**
366+
* Extended functions for Regular Expressions.
367+
*
368+
* <p>Refer to README.md for available functions.
369+
*
370+
* <p>This will include only the specific functions denoted by {@link
371+
* CelRegexExtensions.Function}.
372+
*/
373+
public static CelRegexExtensions regex(Set<CelRegexExtensions.Function> functions) {
374+
return new CelRegexExtensions(functions);
375+
}
376+
344377
/**
345378
* Extended functions for Two Variable Comprehensions Expressions.
346379
*
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package dev.cel.extensions;
16+
17+
import static com.google.common.collect.ImmutableSet.toImmutableSet;
18+
19+
import com.google.common.collect.ImmutableSet;
20+
import com.google.errorprone.annotations.Immutable;
21+
import dev.cel.checker.CelCheckerBuilder;
22+
import dev.cel.common.CelFunctionDecl;
23+
import dev.cel.common.CelOverloadDecl;
24+
import dev.cel.common.types.ListType;
25+
import dev.cel.common.types.OptionalType;
26+
import dev.cel.common.types.SimpleType;
27+
import dev.cel.compiler.CelCompilerLibrary;
28+
import java.util.Set;
29+
30+
/** Internal implementation of CEL regex compile-time extensions. */
31+
@Immutable
32+
public final class CelRegexCompilerLibrary
33+
implements CelCompilerLibrary, CelExtensionLibrary.FeatureSet {
34+
35+
private static final String REGEX_REPLACE_FUNCTION = "regex.replace";
36+
private static final String REGEX_EXTRACT_FUNCTION = "regex.extract";
37+
private static final String REGEX_EXTRACT_ALL_FUNCTION = "regex.extractAll";
38+
39+
/** Supported functions for the Regex compiler extension. */
40+
public enum Function {
41+
REPLACE(
42+
CelFunctionDecl.newFunctionDeclaration(
43+
REGEX_REPLACE_FUNCTION,
44+
CelOverloadDecl.newGlobalOverload(
45+
"regex_replaceAll_string_string_string",
46+
"Replaces all the matched values using the given replace string.",
47+
SimpleType.STRING,
48+
SimpleType.STRING,
49+
SimpleType.STRING,
50+
SimpleType.STRING),
51+
CelOverloadDecl.newGlobalOverload(
52+
"regex_replaceCount_string_string_string_int",
53+
"Replaces the given number of matched values using the given replace string.",
54+
SimpleType.STRING,
55+
SimpleType.STRING,
56+
SimpleType.STRING,
57+
SimpleType.STRING,
58+
SimpleType.INT))),
59+
EXTRACT(
60+
CelFunctionDecl.newFunctionDeclaration(
61+
REGEX_EXTRACT_FUNCTION,
62+
CelOverloadDecl.newGlobalOverload(
63+
"regex_extract_string_string",
64+
"Returns the first substring that matches the regex.",
65+
OptionalType.create(SimpleType.STRING),
66+
SimpleType.STRING,
67+
SimpleType.STRING))),
68+
EXTRACTALL(
69+
CelFunctionDecl.newFunctionDeclaration(
70+
REGEX_EXTRACT_ALL_FUNCTION,
71+
CelOverloadDecl.newGlobalOverload(
72+
"regex_extractAll_string_string",
73+
"Returns an array of all substrings that match the regex.",
74+
ListType.create(SimpleType.STRING),
75+
SimpleType.STRING,
76+
SimpleType.STRING)));
77+
78+
private final CelFunctionDecl functionDecl;
79+
80+
String getFunction() {
81+
return functionDecl.name();
82+
}
83+
84+
public CelFunctionDecl getFunctionDecl() {
85+
return functionDecl;
86+
}
87+
88+
Function(CelFunctionDecl functionDecl) {
89+
this.functionDecl = functionDecl;
90+
}
91+
}
92+
93+
private static final class Library implements CelExtensionLibrary<CelRegexCompilerLibrary> {
94+
private final CelRegexCompilerLibrary version0 =
95+
new CelRegexCompilerLibrary(0, ImmutableSet.copyOf(Function.values()));
96+
97+
@Override
98+
public String name() {
99+
return "regex";
100+
}
101+
102+
@Override
103+
public ImmutableSet<CelRegexCompilerLibrary> versions() {
104+
return ImmutableSet.of(version0);
105+
}
106+
}
107+
108+
private static final Library LIBRARY = new Library();
109+
110+
public static CelExtensionLibrary<CelRegexCompilerLibrary> library() {
111+
return LIBRARY;
112+
}
113+
114+
/** Returns the latest version of the 'regex' compiler extension. */
115+
public static CelRegexCompilerLibrary regex() {
116+
return library().latest();
117+
}
118+
119+
/** Returns the specified version of the 'regex' compiler extension. */
120+
public static CelRegexCompilerLibrary regex(int version) {
121+
return library().version(version);
122+
}
123+
124+
/** Returns the 'regex' compiler extension with only the specified functions. */
125+
public static CelRegexCompilerLibrary regex(Function... functions) {
126+
return regex(ImmutableSet.copyOf(functions));
127+
}
128+
129+
/** Returns the 'regex' compiler extension with only the specified functions. */
130+
public static CelRegexCompilerLibrary regex(Set<Function> functions) {
131+
return new CelRegexCompilerLibrary(functions);
132+
}
133+
134+
private final ImmutableSet<Function> functions;
135+
private final int version;
136+
137+
CelRegexCompilerLibrary(Set<Function> functions) {
138+
this(-1, functions);
139+
}
140+
141+
private CelRegexCompilerLibrary(int version, Set<Function> functions) {
142+
this.version = version;
143+
this.functions = ImmutableSet.copyOf(functions);
144+
}
145+
146+
@Override
147+
public int version() {
148+
return version;
149+
}
150+
151+
@Override
152+
public ImmutableSet<CelFunctionDecl> functions() {
153+
return functions.stream().map(Function::getFunctionDecl).collect(toImmutableSet());
154+
}
155+
156+
@Override
157+
public void setCheckerOptions(CelCheckerBuilder checkerBuilder) {
158+
functions.forEach(function -> checkerBuilder.addFunctionDeclarations(function.functionDecl));
159+
}
160+
}

0 commit comments

Comments
 (0)