Skip to content

Commit e66f804

Browse files
dmitriplotnikovcopybara-github
authored andcommitted
Update the "regex" extension to be compatible with CelEnvironmentExporter
PiperOrigin-RevId: 786907096
1 parent 55662ab commit e66f804

7 files changed

Lines changed: 177 additions & 12 deletions

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,12 @@ public abstract static class Builder {
113113
@CanIgnoreReturnValue
114114
public Builder addStandardExtensions(CelOptions options) {
115115
addExtensionLibraries(
116+
CelExtensions.getExtensionLibrary("bindings", options),
117+
CelExtensions.getExtensionLibrary("encoders", options),
118+
CelExtensions.getExtensionLibrary("lists", options),
116119
CelExtensions.getExtensionLibrary("math", options),
117-
CelExtensions.getExtensionLibrary("lists", options));
120+
CelExtensions.getExtensionLibrary("protos", options),
121+
CelExtensions.getExtensionLibrary("regex", options));
118122
// TODO: add support for remaining standard extensions
119123
return this;
120124
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ java_library(
101101
"//common/ast",
102102
"//common/internal",
103103
"//compiler:compiler_builder",
104+
"//extensions:extension_library",
104105
"//parser:macro",
105106
"//parser:parser_builder",
106107
"@maven//:com_google_errorprone_error_prone_annotations",
@@ -138,6 +139,7 @@ java_library(
138139
"//common:compiler_common",
139140
"//common/ast",
140141
"//compiler:compiler_builder",
142+
"//extensions:extension_library",
141143
"//parser:macro",
142144
"//parser:parser_builder",
143145
"@maven//:com_google_errorprone_error_prone_annotations",
@@ -153,6 +155,8 @@ java_library(
153155
"//common:compiler_common",
154156
"//common/types",
155157
"//compiler:compiler_builder",
158+
"//extensions:extension_library",
159+
"//parser:macro",
156160
"//runtime",
157161
"//runtime:function_binding",
158162
"@maven//:com_google_errorprone_error_prone_annotations",
@@ -275,6 +279,8 @@ java_library(
275279
"//common:compiler_common",
276280
"//common/types",
277281
"//compiler:compiler_builder",
282+
"//extensions:extension_library",
283+
"//parser:macro",
278284
"//runtime",
279285
"//runtime:function_binding",
280286
"@maven//:com_google_errorprone_error_prone_annotations",

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

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
import static com.google.common.base.Preconditions.checkNotNull;
1919

2020
import com.google.common.collect.ImmutableList;
21+
import com.google.common.collect.ImmutableSet;
2122
import com.google.errorprone.annotations.Immutable;
23+
import dev.cel.common.CelFunctionDecl;
2224
import dev.cel.common.CelIssue;
2325
import dev.cel.common.ast.CelExpr;
2426
import dev.cel.compiler.CelCompilerLibrary;
@@ -29,15 +31,47 @@
2931

3032
/** Internal implementation of the CEL local binding extensions. */
3133
@Immutable
32-
final class CelBindingsExtensions implements CelCompilerLibrary {
33-
34+
final class CelBindingsExtensions implements CelCompilerLibrary, CelExtensionLibrary.FeatureSet {
3435
private static final String CEL_NAMESPACE = "cel";
3536
private static final String UNUSED_ITER_VAR = "#unused";
3637

38+
private static final CelExtensionLibrary<CelBindingsExtensions> LIBRARY =
39+
new CelExtensionLibrary<CelBindingsExtensions>() {
40+
private final CelBindingsExtensions version0 = new CelBindingsExtensions();
41+
42+
@Override
43+
public String name() {
44+
return "bindings";
45+
}
46+
47+
@Override
48+
public ImmutableSet<CelBindingsExtensions> versions() {
49+
return ImmutableSet.of(version0);
50+
}
51+
};
52+
53+
static CelExtensionLibrary<CelBindingsExtensions> library() {
54+
return LIBRARY;
55+
}
56+
57+
@Override
58+
public int version() {
59+
return 0;
60+
}
61+
62+
@Override
63+
public ImmutableSet<CelFunctionDecl> functions() {
64+
return ImmutableSet.of();
65+
}
66+
67+
@Override
68+
public ImmutableSet<CelMacro> macros() {
69+
return ImmutableSet.of(CelMacro.newReceiverMacro("bind", 3, CelBindingsExtensions::expandBind));
70+
}
71+
3772
@Override
3873
public void setParserOptions(CelParserBuilder parserBuilder) {
39-
parserBuilder.addMacros(
40-
CelMacro.newReceiverMacro("bind", 3, CelBindingsExtensions::expandBind));
74+
parserBuilder.addMacros(macros());
4175
}
4276

4377
/**

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

Lines changed: 40 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.ImmutableSet;
1820
import com.google.errorprone.annotations.Immutable;
1921
import com.google.protobuf.ByteString;
@@ -22,6 +24,7 @@
2224
import dev.cel.common.CelOverloadDecl;
2325
import dev.cel.common.types.SimpleType;
2426
import dev.cel.compiler.CelCompilerLibrary;
27+
import dev.cel.parser.CelMacro;
2528
import dev.cel.runtime.CelFunctionBinding;
2629
import dev.cel.runtime.CelRuntimeBuilder;
2730
import dev.cel.runtime.CelRuntimeLibrary;
@@ -31,7 +34,9 @@
3134

3235
/** Internal implementation of Encoder Extensions. */
3336
@Immutable
34-
public class CelEncoderExtensions implements CelCompilerLibrary, CelRuntimeLibrary {
37+
public class CelEncoderExtensions
38+
implements CelCompilerLibrary, CelRuntimeLibrary, CelExtensionLibrary.FeatureSet {
39+
3540
private static final Encoder BASE64_ENCODER = Base64.getEncoder();
3641

3742
private static final Decoder BASE64_DECODER = Base64.getDecoder();
@@ -74,6 +79,40 @@ String getFunction() {
7479
}
7580
}
7681

82+
private static final CelExtensionLibrary<CelEncoderExtensions> LIBRARY =
83+
new CelExtensionLibrary<CelEncoderExtensions>() {
84+
private final CelEncoderExtensions version0 = new CelEncoderExtensions();
85+
86+
@Override
87+
public String name() {
88+
return "encoders";
89+
}
90+
91+
@Override
92+
public ImmutableSet<CelEncoderExtensions> versions() {
93+
return ImmutableSet.of(version0);
94+
}
95+
};
96+
97+
static CelExtensionLibrary<CelEncoderExtensions> library() {
98+
return LIBRARY;
99+
}
100+
101+
@Override
102+
public int version() {
103+
return 0;
104+
}
105+
106+
@Override
107+
public ImmutableSet<CelFunctionDecl> functions() {
108+
return functions.stream().map(f -> f.functionDecl).collect(toImmutableSet());
109+
}
110+
111+
@Override
112+
public ImmutableSet<CelMacro> macros() {
113+
return ImmutableSet.of();
114+
}
115+
77116
@Override
78117
public void setCheckerOptions(CelCheckerBuilder checkerBuilder) {
79118
functions.forEach(function -> checkerBuilder.addFunctionDeclarations(function.functionDecl));

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,18 @@ public static ImmutableSet<String> getAllFunctionNames() {
303303
public static CelExtensionLibrary<? extends CelExtensionLibrary.FeatureSet> getExtensionLibrary(
304304
String name, CelOptions options) {
305305
switch (name) {
306-
case "math":
307-
return CelMathExtensions.library(options);
306+
case "bindings":
307+
return CelBindingsExtensions.library();
308+
case "encoders":
309+
return CelEncoderExtensions.library();
308310
case "lists":
309311
return CelListsExtensions.library();
312+
case "math":
313+
return CelMathExtensions.library(options);
314+
case "protos":
315+
return CelProtoExtensions.library();
316+
case "regex":
317+
return CelRegexExtensions.library();
310318
// TODO: add support for remaining standard extensions
311319
default:
312320
throw new IllegalArgumentException("Unknown standard extension '" + name + "'");

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

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
import static com.google.common.base.Preconditions.checkNotNull;
1919

2020
import com.google.common.collect.ImmutableList;
21+
import com.google.common.collect.ImmutableSet;
2122
import com.google.errorprone.annotations.Immutable;
23+
import dev.cel.common.CelFunctionDecl;
2224
import dev.cel.common.CelIssue;
2325
import dev.cel.common.ast.CelExpr;
2426
import dev.cel.common.internal.Constants;
@@ -30,18 +32,52 @@
3032

3133
/** Internal implementation of CEL proto extensions. */
3234
@Immutable
33-
final class CelProtoExtensions implements CelCompilerLibrary {
35+
final class CelProtoExtensions implements CelCompilerLibrary, CelExtensionLibrary.FeatureSet {
3436

3537
private static final String PROTO_NAMESPACE = "proto";
3638
private static final CelExpr ERROR = CelExpr.newBuilder().setConstant(Constants.ERROR).build();
3739

40+
private static final CelExtensionLibrary<CelProtoExtensions> LIBRARY =
41+
new CelExtensionLibrary<CelProtoExtensions>() {
42+
private final CelProtoExtensions version0 = new CelProtoExtensions();
43+
44+
@Override
45+
public String name() {
46+
return "protos";
47+
}
48+
49+
@Override
50+
public ImmutableSet<CelProtoExtensions> versions() {
51+
return ImmutableSet.of(version0);
52+
}
53+
};
54+
55+
static CelExtensionLibrary<CelProtoExtensions> library() {
56+
return LIBRARY;
57+
}
58+
3859
@Override
39-
public void setParserOptions(CelParserBuilder parserBuilder) {
40-
parserBuilder.addMacros(
60+
public int version() {
61+
return 0;
62+
}
63+
64+
@Override
65+
public ImmutableSet<CelFunctionDecl> functions() {
66+
return ImmutableSet.of();
67+
}
68+
69+
@Override
70+
public ImmutableSet<CelMacro> macros() {
71+
return ImmutableSet.of(
4172
CelMacro.newReceiverMacro("hasExt", 2, CelProtoExtensions::expandHasProtoExt),
4273
CelMacro.newReceiverMacro("getExt", 2, CelProtoExtensions::expandGetProtoExt));
4374
}
4475

76+
@Override
77+
public void setParserOptions(CelParserBuilder parserBuilder) {
78+
parserBuilder.addMacros(macros());
79+
}
80+
4581
private static Optional<CelExpr> expandHasProtoExt(
4682
CelMacroExprFactory exprFactory, CelExpr target, ImmutableList<CelExpr> arguments) {
4783
return expandProtoExt(exprFactory, target, arguments, true);

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

Lines changed: 39 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;
@@ -27,6 +29,7 @@
2729
import dev.cel.common.types.OptionalType;
2830
import dev.cel.common.types.SimpleType;
2931
import dev.cel.compiler.CelCompilerLibrary;
32+
import dev.cel.parser.CelMacro;
3033
import dev.cel.runtime.CelFunctionBinding;
3134
import dev.cel.runtime.CelRuntimeBuilder;
3235
import dev.cel.runtime.CelRuntimeLibrary;
@@ -35,7 +38,8 @@
3538

3639
/** Internal implementation of CEL regex extensions. */
3740
@Immutable
38-
final class CelRegexExtensions implements CelCompilerLibrary, CelRuntimeLibrary {
41+
final class CelRegexExtensions
42+
implements CelCompilerLibrary, CelRuntimeLibrary, CelExtensionLibrary.FeatureSet {
3943

4044
private static final String REGEX_REPLACE_FUNCTION = "regex.replace";
4145
private static final String REGEX_EXTRACT_FUNCTION = "regex.extract";
@@ -124,6 +128,25 @@ String getFunction() {
124128
}
125129
}
126130

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

129152
CelRegexExtensions() {
@@ -134,6 +157,21 @@ String getFunction() {
134157
this.functions = ImmutableSet.copyOf(functions);
135158
}
136159

160+
@Override
161+
public int version() {
162+
return 0;
163+
}
164+
165+
@Override
166+
public ImmutableSet<CelFunctionDecl> functions() {
167+
return functions.stream().map(f -> f.functionDecl).collect(toImmutableSet());
168+
}
169+
170+
@Override
171+
public ImmutableSet<CelMacro> macros() {
172+
return ImmutableSet.of();
173+
}
174+
137175
@Override
138176
public void setCheckerOptions(CelCheckerBuilder checkerBuilder) {
139177
functions.forEach(function -> checkerBuilder.addFunctionDeclarations(function.functionDecl));

0 commit comments

Comments
 (0)