Skip to content

Commit 3aed2ef

Browse files
l46kokcopybara-github
authored andcommitted
Add CelStandardFunctions to allow environment subsetting for the runtime
PiperOrigin-RevId: 677902385
1 parent 1021e70 commit 3aed2ef

File tree

22 files changed

+4665
-947
lines changed

22 files changed

+4665
-947
lines changed

checker/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,8 @@ java_library(
5353
name = "proto_expr_visitor",
5454
exports = ["//checker/src/main/java/dev/cel/checker:proto_expr_visitor"],
5555
)
56+
57+
java_library(
58+
name = "standard_decl",
59+
exports = ["//checker/src/main/java/dev/cel/checker:standard_decl"],
60+
)

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ CHECKER_LEGACY_ENV_SOURCES = [
3030
"ExprChecker.java",
3131
"ExprVisitor.java",
3232
"InferenceContext.java",
33-
"Standard.java",
3433
"TypeFormatter.java",
3534
"TypeProvider.java",
3635
"Types.java",
@@ -70,6 +69,7 @@ java_library(
7069
":checker_builder",
7170
":checker_legacy_environment",
7271
":proto_type_mask",
72+
":standard_decl",
7373
":type_provider_legacy_impl",
7474
"//:auto_value",
7575
"//common",
@@ -99,6 +99,7 @@ java_library(
9999
deps = [
100100
":checker_legacy_environment",
101101
":proto_type_mask",
102+
":standard_decl",
102103
"//common",
103104
"//common:compiler_common",
104105
"//common:options",
@@ -168,6 +169,7 @@ java_library(
168169
],
169170
deps = [
170171
":cel_ident_decl",
172+
":standard_decl",
171173
"//:auto_value",
172174
"//common",
173175
"//common:compiler_common",
@@ -219,3 +221,22 @@ java_library(
219221
"//common:proto_ast",
220222
],
221223
)
224+
225+
java_library(
226+
name = "standard_decl",
227+
srcs = [
228+
"CelStandardDeclarations.java",
229+
],
230+
tags = [
231+
],
232+
deps = [
233+
":cel_ident_decl",
234+
"//common:compiler_common",
235+
"//common/types",
236+
"//common/types:cel_types",
237+
"//common/types:type_providers",
238+
"//parser:operator",
239+
"@maven//:com_google_errorprone_error_prone_annotations",
240+
"@maven//:com_google_guava_guava",
241+
],
242+
)

checker/src/main/java/dev/cel/checker/CelCheckerBuilder.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ public interface CelCheckerBuilder {
152152
@CanIgnoreReturnValue
153153
CelCheckerBuilder setStandardEnvironmentEnabled(boolean value);
154154

155+
/**
156+
* Override the standard declarations for the type-checker. This can be used to subset the
157+
* standard environment to only expose the desired declarations to the type-checker. {@link
158+
* #setStandardEnvironmentEnabled(boolean)} must be set to false for this to take effect.
159+
*/
160+
@CanIgnoreReturnValue
161+
CelCheckerBuilder setStandardDeclarations(CelStandardDeclarations standardDeclarations);
162+
155163
/** Adds one or more libraries for parsing and type-checking. */
156164
@CanIgnoreReturnValue
157165
CelCheckerBuilder addLibraries(CelCheckerLibrary... libraries);

checker/src/main/java/dev/cel/checker/CelCheckerLegacyImpl.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public final class CelCheckerLegacyImpl implements CelChecker, EnvVisitable {
7878
private final CelTypeProvider celTypeProvider;
7979
private final boolean standardEnvironmentEnabled;
8080

81+
private final CelStandardDeclarations overriddenStandardDeclarations;
82+
8183
// Builder is mutable by design. APIs must make defensive copies in and out of this class.
8284
@SuppressWarnings("Immutable")
8385
private final Builder checkerBuilder;
@@ -137,6 +139,8 @@ private Env getEnv(Errors errors) {
137139
Env env;
138140
if (standardEnvironmentEnabled) {
139141
env = Env.standard(errors, typeProvider, celOptions);
142+
} else if (overriddenStandardDeclarations != null) {
143+
env = Env.standard(overriddenStandardDeclarations, errors, typeProvider, celOptions);
140144
} else {
141145
env = Env.unconfigured(errors, typeProvider, celOptions);
142146
}
@@ -165,6 +169,7 @@ public static final class Builder implements CelCheckerBuilder {
165169
private TypeProvider customTypeProvider;
166170
private CelTypeProvider celTypeProvider;
167171
private boolean standardEnvironmentEnabled;
172+
private CelStandardDeclarations standardDeclarations;
168173

169174
@Override
170175
public CelCheckerBuilder setOptions(CelOptions celOptions) {
@@ -320,7 +325,13 @@ public CelCheckerBuilder addFileTypes(FileDescriptorSet fileDescriptorSet) {
320325

321326
@Override
322327
public CelCheckerBuilder setStandardEnvironmentEnabled(boolean value) {
323-
standardEnvironmentEnabled = value;
328+
this.standardEnvironmentEnabled = value;
329+
return this;
330+
}
331+
332+
@Override
333+
public CelCheckerBuilder setStandardDeclarations(CelStandardDeclarations standardDeclarations) {
334+
this.standardDeclarations = checkNotNull(standardDeclarations);
324335
return this;
325336
}
326337

@@ -372,6 +383,11 @@ ImmutableSet.Builder<CelCheckerLibrary> getCheckerLibraries() {
372383
@Override
373384
@CheckReturnValue
374385
public CelCheckerLegacyImpl build() {
386+
if (standardEnvironmentEnabled && standardDeclarations != null) {
387+
throw new IllegalArgumentException(
388+
"setStandardEnvironmentEnabled must be set to false to override standard"
389+
+ " declarations.");
390+
}
375391
// Add libraries, such as extensions
376392
celCheckerLibraries.build().forEach(celLibrary -> celLibrary.setCheckerOptions(this));
377393

@@ -430,6 +446,7 @@ public CelCheckerLegacyImpl build() {
430446
legacyProvider,
431447
messageTypeProvider,
432448
standardEnvironmentEnabled,
449+
standardDeclarations,
433450
this);
434451
}
435452

@@ -478,6 +495,7 @@ private CelCheckerLegacyImpl(
478495
TypeProvider typeProvider,
479496
CelTypeProvider celTypeProvider,
480497
boolean standardEnvironmentEnabled,
498+
CelStandardDeclarations overriddenStandardDeclarations,
481499
Builder checkerBuilder) {
482500
this.celOptions = celOptions;
483501
this.container = container;
@@ -487,6 +505,7 @@ private CelCheckerLegacyImpl(
487505
this.typeProvider = typeProvider;
488506
this.celTypeProvider = celTypeProvider;
489507
this.standardEnvironmentEnabled = standardEnvironmentEnabled;
508+
this.overriddenStandardDeclarations = overriddenStandardDeclarations;
490509
this.checkerBuilder = new Builder(checkerBuilder);
491510
}
492511

0 commit comments

Comments
 (0)