diff --git a/bundle/src/main/java/dev/cel/bundle/BUILD.bazel b/bundle/src/main/java/dev/cel/bundle/BUILD.bazel index 35b4679f7..50b78c63b 100644 --- a/bundle/src/main/java/dev/cel/bundle/BUILD.bazel +++ b/bundle/src/main/java/dev/cel/bundle/BUILD.bazel @@ -25,6 +25,7 @@ java_library( "//checker:checker_builder", "//checker:checker_legacy_environment", "//checker:proto_type_mask", + "//checker:standard_decl", "//common:cel_ast", "//common:cel_source", "//common:compiler_common", @@ -42,6 +43,7 @@ java_library( "//parser:parser_builder", "//runtime", "//runtime:function_binding", + "//runtime:standard_functions", "@cel_spec//proto/cel/expr:checked_java_proto", "@maven//:com_google_code_findbugs_annotations", "@maven//:com_google_errorprone_error_prone_annotations", diff --git a/bundle/src/main/java/dev/cel/bundle/CelBuilder.java b/bundle/src/main/java/dev/cel/bundle/CelBuilder.java index 9580dc555..1dadaeb39 100644 --- a/bundle/src/main/java/dev/cel/bundle/CelBuilder.java +++ b/bundle/src/main/java/dev/cel/bundle/CelBuilder.java @@ -22,6 +22,7 @@ import com.google.protobuf.Descriptors.FileDescriptor; import com.google.protobuf.ExtensionRegistry; import com.google.protobuf.Message; +import dev.cel.checker.CelStandardDeclarations; import dev.cel.checker.ProtoTypeMask; import dev.cel.checker.TypeProvider; import dev.cel.common.CelContainer; @@ -36,6 +37,7 @@ import dev.cel.parser.CelStandardMacro; import dev.cel.runtime.CelFunctionBinding; import dev.cel.runtime.CelRuntimeLibrary; +import dev.cel.runtime.CelStandardFunctions; import java.util.function.Function; /** Interface for building an instance of Cel. */ @@ -84,7 +86,7 @@ public interface CelBuilder { /** Retrieves the currently configured {@link CelContainer} in the builder. */ CelContainer container(); - /* + /** * Set the {@link CelContainer} to use as the namespace for resolving CEL expression variables and * functions. */ @@ -299,6 +301,24 @@ public interface CelBuilder { @CanIgnoreReturnValue CelBuilder addRuntimeLibraries(Iterable libraries); + /** + * 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 + CelBuilder setStandardDeclarations(CelStandardDeclarations standardDeclarations); + + /** + * Override the standard functions for the runtime. This can be used to subset the standard + * environment to only expose the desired function overloads to the runtime. + * + *

{@link #setStandardEnvironmentEnabled(boolean)} must be set to false for this to take + * effect. + */ + @CanIgnoreReturnValue + CelBuilder setStandardFunctions(CelStandardFunctions standardFunctions); + /** * Sets a proto ExtensionRegistry to assist with unpacking Any messages containing a proto2 extension field. diff --git a/bundle/src/main/java/dev/cel/bundle/CelImpl.java b/bundle/src/main/java/dev/cel/bundle/CelImpl.java index 795e202e3..bc92cca7a 100644 --- a/bundle/src/main/java/dev/cel/bundle/CelImpl.java +++ b/bundle/src/main/java/dev/cel/bundle/CelImpl.java @@ -28,6 +28,7 @@ import com.google.protobuf.ExtensionRegistry; import com.google.protobuf.Message; import dev.cel.checker.CelCheckerBuilder; +import dev.cel.checker.CelStandardDeclarations; import dev.cel.checker.ProtoTypeMask; import dev.cel.checker.TypeProvider; import dev.cel.common.CelAbstractSyntaxTree; @@ -54,6 +55,7 @@ import dev.cel.runtime.CelRuntime; import dev.cel.runtime.CelRuntimeBuilder; import dev.cel.runtime.CelRuntimeLibrary; +import dev.cel.runtime.CelStandardFunctions; import java.util.Arrays; import java.util.function.Function; @@ -382,6 +384,20 @@ public CelBuilder addRuntimeLibraries(Iterable libraries) { return this; } + @Override + public CelBuilder setStandardDeclarations(CelStandardDeclarations standardDeclarations) { + checkNotNull(standardDeclarations); + compilerBuilder.setStandardDeclarations(standardDeclarations); + return this; + } + + @Override + public CelBuilder setStandardFunctions(CelStandardFunctions standardFunctions) { + checkNotNull(standardFunctions); + runtimeBuilder.setStandardFunctions(standardFunctions); + return this; + } + @Override public CelBuilder setExtensionRegistry(ExtensionRegistry extensionRegistry) { checkNotNull(extensionRegistry);