Skip to content

Commit

Permalink
Add CelStandardDeclaration to allow environment subsetting for type-c…
Browse files Browse the repository at this point in the history
…hecker

PiperOrigin-RevId: 672004289
  • Loading branch information
l46kok authored and copybara-github committed Sep 10, 2024
1 parent 10bb524 commit 2a39ca1
Show file tree
Hide file tree
Showing 15 changed files with 2,173 additions and 853 deletions.
5 changes: 5 additions & 0 deletions checker/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,8 @@ java_library(
name = "proto_expr_visitor",
exports = ["//checker/src/main/java/dev/cel/checker:proto_expr_visitor"],
)

java_library(
name = "standard_decl",
exports = ["//checker/src/main/java/dev/cel/checker:standard_decl"],
)
23 changes: 22 additions & 1 deletion checker/src/main/java/dev/cel/checker/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ CHECKER_LEGACY_ENV_SOURCES = [
"ExprChecker.java",
"ExprVisitor.java",
"InferenceContext.java",
"Standard.java",
"TypeFormatter.java",
"TypeProvider.java",
"Types.java",
Expand Down Expand Up @@ -68,6 +67,7 @@ java_library(
":checker_builder",
":checker_legacy_environment",
":proto_type_mask",
":standard_decl",
":type_provider_legacy_impl",
"//:auto_value",
"//common",
Expand Down Expand Up @@ -97,6 +97,7 @@ java_library(
deps = [
":checker_legacy_environment",
":proto_type_mask",
":standard_decl",
"//common",
"//common:compiler_common",
"//common:options",
Expand Down Expand Up @@ -166,6 +167,7 @@ java_library(
],
deps = [
":cel_ident_decl",
":standard_decl",
"//:auto_value",
"//common",
"//common:compiler_common",
Expand Down Expand Up @@ -217,3 +219,22 @@ java_library(
"//common:proto_ast",
],
)

java_library(
name = "standard_decl",
srcs = [
"CelStandardDeclaration.java",
],
tags = [
],
deps = [
":cel_ident_decl",
"//common:compiler_common",
"//common/types",
"//common/types:cel_types",
"//common/types:type_providers",
"//parser:operator",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
)
8 changes: 8 additions & 0 deletions checker/src/main/java/dev/cel/checker/CelCheckerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ public interface CelCheckerBuilder {
@CanIgnoreReturnValue
CelCheckerBuilder setStandardEnvironmentEnabled(boolean value);

/**
* Override the standard declarations for the type-checker. This can be used to subset the
* standard environment to only expose the desired declarations to the type-checker. {@link
* #setStandardEnvironmentEnabled(boolean)} must be set to false for this to take effect.
*/
@CanIgnoreReturnValue
CelCheckerBuilder setStandardDeclarations(CelStandardDeclaration standardDeclarations);

/** Adds one or more libraries for parsing and type-checking. */
@CanIgnoreReturnValue
CelCheckerBuilder addLibraries(CelCheckerLibrary... libraries);
Expand Down
21 changes: 20 additions & 1 deletion checker/src/main/java/dev/cel/checker/CelCheckerLegacyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public final class CelCheckerLegacyImpl implements CelChecker, EnvVisitable {
private final CelTypeProvider celTypeProvider;
private final boolean standardEnvironmentEnabled;

private final CelStandardDeclaration overriddenStandardDeclarations;

// Builder is mutable by design. APIs must make defensive copies in and out of this class.
@SuppressWarnings("Immutable")
private final Builder checkerBuilder;
Expand Down Expand Up @@ -137,6 +139,8 @@ private Env getEnv(Errors errors) {
Env env;
if (standardEnvironmentEnabled) {
env = Env.standard(errors, typeProvider, celOptions);
} else if (overriddenStandardDeclarations != null) {
env = Env.standard(overriddenStandardDeclarations, errors, typeProvider, celOptions);
} else {
env = Env.unconfigured(errors, typeProvider, celOptions);
}
Expand Down Expand Up @@ -165,6 +169,7 @@ public static final class Builder implements CelCheckerBuilder {
private TypeProvider customTypeProvider;
private CelTypeProvider celTypeProvider;
private boolean standardEnvironmentEnabled;
private CelStandardDeclaration standardDeclarations;

@Override
public CelCheckerBuilder setOptions(CelOptions celOptions) {
Expand Down Expand Up @@ -320,7 +325,13 @@ public CelCheckerBuilder addFileTypes(FileDescriptorSet fileDescriptorSet) {

@Override
public CelCheckerBuilder setStandardEnvironmentEnabled(boolean value) {
standardEnvironmentEnabled = value;
this.standardEnvironmentEnabled = value;
return this;
}

@Override
public CelCheckerBuilder setStandardDeclarations(CelStandardDeclaration standardDeclarations) {
this.standardDeclarations = checkNotNull(standardDeclarations);
return this;
}

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

Expand Down Expand Up @@ -430,6 +446,7 @@ public CelCheckerLegacyImpl build() {
legacyProvider,
messageTypeProvider,
standardEnvironmentEnabled,
standardDeclarations,
this);
}

Expand Down Expand Up @@ -478,6 +495,7 @@ private CelCheckerLegacyImpl(
TypeProvider typeProvider,
CelTypeProvider celTypeProvider,
boolean standardEnvironmentEnabled,
CelStandardDeclaration overriddenStandardDeclarations,
Builder checkerBuilder) {
this.celOptions = celOptions;
this.container = container;
Expand All @@ -487,6 +505,7 @@ private CelCheckerLegacyImpl(
this.typeProvider = typeProvider;
this.celTypeProvider = celTypeProvider;
this.standardEnvironmentEnabled = standardEnvironmentEnabled;
this.overriddenStandardDeclarations = overriddenStandardDeclarations;
this.checkerBuilder = new Builder(checkerBuilder);
}

Expand Down
Loading

0 comments on commit 2a39ca1

Please sign in to comment.