Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions bundle/src/main/java/dev/cel/bundle/CelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,15 @@ public interface CelBuilder {
@CanIgnoreReturnValue
CelBuilder addFileTypes(FileDescriptorSet fileDescriptorSet);

/** Enable or disable the standard CEL library functions and variables */
/**
* Enable or disable the standard CEL library functions and variables.
*
* @deprecated Use {@link #setStandardDeclarations(CelStandardDeclarations)} and/or {@link
* #setStandardFunctions(CelStandardFunctions)} to configure or subset the standard
* environment. Use {@link CelStandardDeclarations#EMPTY} and {@link
* CelStandardFunctions#EMPTY} to disable all standard declarations and functions.
*/
@Deprecated
@CanIgnoreReturnValue
CelBuilder setStandardEnvironmentEnabled(boolean value);

Expand All @@ -314,8 +322,7 @@ public interface CelBuilder {

/**
* 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.
* standard environment to only expose the desired declarations to the type-checker.
*/
@CanIgnoreReturnValue
CelBuilder setStandardDeclarations(CelStandardDeclarations standardDeclarations);
Expand Down
1 change: 1 addition & 0 deletions bundle/src/main/java/dev/cel/bundle/CelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ public CelBuilder addFileTypes(FileDescriptorSet fileDescriptorSet) {
}

@Override
@Deprecated
public CelBuilder setStandardEnvironmentEnabled(boolean value) {
compilerBuilder.setStandardEnvironmentEnabled(value);
runtimeBuilder.setStandardEnvironmentEnabled(value);
Expand Down
2 changes: 2 additions & 0 deletions bundle/src/test/java/dev/cel/bundle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ java_library(
"//checker",
"//checker:checker_legacy_environment",
"//checker:proto_type_mask",
"//checker:standard_decl",
"//common:cel_ast",
"//common:cel_descriptor_util",
"//common:cel_source",
Expand Down Expand Up @@ -56,6 +57,7 @@ java_library(
"//runtime:evaluation_exception_builder",
"//runtime:evaluation_listener",
"//runtime:function_binding",
"//runtime:standard_functions",
"//runtime:unknown_attributes",
"//testing:cel_runtime_flavor",
"//testing/protos:single_file_extension_java_proto",
Expand Down
25 changes: 25 additions & 0 deletions bundle/src/test/java/dev/cel/bundle/CelImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import com.google.testing.junit.testparameterinjector.TestParameter;
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
import dev.cel.checker.CelCheckerLegacyImpl;
import dev.cel.checker.CelStandardDeclarations;
import dev.cel.checker.DescriptorTypeProvider;
import dev.cel.checker.ProtoTypeMask;
import dev.cel.checker.TypeProvider;
Expand Down Expand Up @@ -110,6 +111,7 @@
import dev.cel.runtime.CelRuntime.Program;
import dev.cel.runtime.CelRuntimeFactory;
import dev.cel.runtime.CelRuntimeLegacyImpl;
import dev.cel.runtime.CelStandardFunctions;
import dev.cel.runtime.CelUnknownSet;
import dev.cel.runtime.CelVariableResolver;
import dev.cel.runtime.UnknownContext;
Expand Down Expand Up @@ -2294,4 +2296,27 @@ private static Cel setupEnv(CelBuilder celBuilder) {
.build())
.build();
}

@Test
public void plannerCelBuilder_setStandardDeclarationsAndFunctions_subsetsEnvironment()
throws Exception {
Cel cel =
CelFactory.plannerCelBuilder()
.setStandardDeclarations(
CelStandardDeclarations.newBuilder()
.includeFunctions(CelStandardDeclarations.StandardFunction.ADD)
.build())
.setStandardFunctions(
CelStandardFunctions.newBuilder()
.includeFunctions(CelStandardFunctions.StandardFunction.ADD)
.build())
.build();

CelAbstractSyntaxTree ast = cel.compile("1 + 1").getAst();
assertThat(cel.createProgram(ast).eval()).isEqualTo(2L);

CelValidationException validationException =
assertThrows(CelValidationException.class, () -> cel.compile("1 - 1").getAst());
assertThat(validationException).hasMessageThat().contains("undeclared reference to '_-_'");
}
}
12 changes: 9 additions & 3 deletions checker/src/main/java/dev/cel/checker/CelCheckerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,20 @@ public interface CelCheckerBuilder {
@CanIgnoreReturnValue
CelCheckerBuilder addFileTypes(FileDescriptorSet fileDescriptorSet);

/** Enable or disable the standard CEL library functions and variables */
/**
* Enable or disable the standard CEL library functions and variables.
*
* @deprecated Use {@link #setStandardDeclarations(CelStandardDeclarations)} to configure or
* subset the standard environment. Use {@link CelStandardDeclarations#EMPTY} to disable all
* standard declarations.
*/
@Deprecated
@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.
* standard environment to only expose the desired declarations to the type-checker.
*/
@CanIgnoreReturnValue
CelCheckerBuilder setStandardDeclarations(CelStandardDeclarations standardDeclarations);
Expand Down
13 changes: 4 additions & 9 deletions checker/src/main/java/dev/cel/checker/CelCheckerLegacyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ public void accept(EnvVisitor envVisitor) {

private Env getEnv(Errors errors) {
Env env;
if (standardEnvironmentEnabled) {
env = Env.standard(errors, typeProvider, celOptions);
} else if (overriddenStandardDeclarations != null) {
if (overriddenStandardDeclarations != null) {
env = Env.standard(overriddenStandardDeclarations, errors, typeProvider, celOptions);
} else if (standardEnvironmentEnabled) {
env = Env.standard(errors, typeProvider, celOptions);
} else {
env = Env.unconfigured(errors, typeProvider, celOptions);
}
Expand Down Expand Up @@ -359,6 +359,7 @@ public CelCheckerBuilder addFileTypes(FileDescriptorSet fileDescriptorSet) {
}

@Override
@Deprecated
public CelCheckerBuilder setStandardEnvironmentEnabled(boolean value) {
this.standardEnvironmentEnabled = value;
return this;
Expand Down Expand Up @@ -434,12 +435,6 @@ CelTypeProvider celTypeProvider() {
@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
ImmutableSet<CelCheckerLibrary> checkerLibraries = celCheckerLibraries.build();
checkerLibraries.forEach(celLibrary -> celLibrary.setCheckerOptions(this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public final class CelStandardDeclarations {
private static final TypeParamType TYPE_PARAM_B = TypeParamType.create("B");
private static final MapType MAP_OF_AB = MapType.create(TYPE_PARAM_A, TYPE_PARAM_B);

/** An empty instance of {@link CelStandardDeclarations} with no functions or identifiers. */
public static final CelStandardDeclarations EMPTY =
new CelStandardDeclarations(ImmutableSet.of(), ImmutableSet.of());

private final ImmutableSet<CelFunctionDecl> celFunctionDecls;
private final ImmutableSet<CelIdentDecl> celIdentDecls;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,63 @@ public void standardDeclaration_moreThanOneIdentifierFilterSet_throws(
}

@Test
public void compiler_standardEnvironmentEnabled_throwsWhenOverridingDeclarations() {
IllegalArgumentException e =
assertThrows(
IllegalArgumentException.class,
() ->
CelCompilerFactory.standardCelCompilerBuilder()
.setStandardEnvironmentEnabled(true)
.setStandardDeclarations(
CelStandardDeclarations.newBuilder()
.includeFunctions(StandardFunction.ADD, StandardFunction.SUBTRACT)
.build())
.build());
public void compiler_setStandardDeclarations_overridesDefaultStandardEnvironment()
throws Exception {
CelCompiler compiler =
CelCompilerFactory.standardCelCompilerBuilder()
.setStandardDeclarations(
CelStandardDeclarations.newBuilder()
.includeFunctions(StandardFunction.ADD)
.build())
.build();

assertThat(e)
.hasMessageThat()
.contains(
"setStandardEnvironmentEnabled must be set to false to override standard"
+ " declarations.");
assertThat(compiler.compile("1 + 1").hasError()).isFalse();
assertThat(compiler.compile("1 - 1").hasError()).isTrue();
}

@Test
public void compiler_setStandardDeclarations_withStandardEnvironmentExplicitlyEnabled()
throws Exception {
CelCompiler compiler =
CelCompilerFactory.standardCelCompilerBuilder()
.setStandardEnvironmentEnabled(true)
.setStandardDeclarations(
CelStandardDeclarations.newBuilder()
.includeFunctions(StandardFunction.ADD)
.build())
.build();

assertThat(compiler.compile("1 + 1").hasError()).isFalse();
assertThat(compiler.compile("1 - 1").hasError()).isTrue();
}

@Test
public void compiler_setStandardDeclarations_withStandardEnvironmentExplicitlyDisabled()
throws Exception {
CelCompiler compiler =
CelCompilerFactory.standardCelCompilerBuilder()
.setStandardEnvironmentEnabled(false)
.setStandardDeclarations(
CelStandardDeclarations.newBuilder()
.includeFunctions(StandardFunction.ADD)
.build())
.build();

assertThat(compiler.compile("1 + 1").hasError()).isFalse();
assertThat(compiler.compile("1 - 1").hasError()).isTrue();
}

@Test
public void compiler_setStandardDeclarations_emptyDisablesAllStandardDeclarations()
throws Exception {
CelCompiler compiler =
CelCompilerFactory.standardCelCompilerBuilder()
.setStandardDeclarations(CelStandardDeclarations.EMPTY)
.build();

assertThat(compiler.compile("1 + 1").hasError()).isTrue();
assertThat(compiler.compile("1 - 1").hasError()).isTrue();
assertThat(compiler.compile("size([1])").hasError()).isTrue();
}

@Test
Expand Down
12 changes: 9 additions & 3 deletions compiler/src/main/java/dev/cel/compiler/CelCompilerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,20 @@ public interface CelCompilerBuilder {
@CanIgnoreReturnValue
CelCompilerBuilder addFileTypes(FileDescriptorSet fileDescriptorSet);

/** Enable or disable the standard CEL library functions and variables */
/**
* Enable or disable the standard CEL library functions and variables.
*
* @deprecated Use {@link #setStandardDeclarations(CelStandardDeclarations)} to configure or
* subset the standard environment. Use {@link CelStandardDeclarations#EMPTY} to disable all
* standard declarations.
*/
@Deprecated
@CanIgnoreReturnValue
CelCompilerBuilder 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.
* standard environment to only expose the desired declarations to the type-checker.
*/
@CanIgnoreReturnValue
CelCompilerBuilder setStandardDeclarations(CelStandardDeclarations standardDeclarations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ public CelCompilerBuilder addFileTypes(FileDescriptorSet fileDescriptorSet) {
}

@Override
@Deprecated
public CelCompilerBuilder setStandardEnvironmentEnabled(boolean value) {
checkerBuilder.setStandardEnvironmentEnabled(value);
return this;
Expand Down
9 changes: 8 additions & 1 deletion runtime/src/main/java/dev/cel/runtime/CelRuntimeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,14 @@ public interface CelRuntimeBuilder {
/** Returns the configured {@link CelValueProvider}, or null if not set. */
CelValueProvider valueProvider();

/** Enable or disable the standard CEL library functions and variables. */
/**
* Enable or disable the standard CEL library functions and variables.
*
* @deprecated Use {@link #setStandardFunctions(CelStandardFunctions)} to configure or subset the
* standard environment. Use {@link CelStandardFunctions#EMPTY} to disable all standard
* functions.
*/
@Deprecated
@CanIgnoreReturnValue
CelRuntimeBuilder setStandardEnvironmentEnabled(boolean value);

Expand Down
1 change: 1 addition & 0 deletions runtime/src/main/java/dev/cel/runtime/CelRuntimeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ public Builder setTypeFactory(Function<String, Message.Builder> typeFactory) {
}

@Override
@Deprecated
public Builder setStandardEnvironmentEnabled(boolean value) {
throw new UnsupportedOperationException(
"Unsupported. Subset the environment using setStandardFunctions instead.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ public CelRuntimeBuilder setTypeFactory(Function<String, Message.Builder> typeFa
}

@Override
@Deprecated
public CelRuntimeBuilder setStandardEnvironmentEnabled(boolean value) {
standardEnvironmentEnabled = value;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ public final class CelStandardFunctions {
GreaterEqualsOverload.GREATER_EQUALS_UINT64_DOUBLE,
GreaterEqualsOverload.GREATER_EQUALS_DOUBLE_UINT64);

/** An empty instance of {@link CelStandardFunctions} with no functions. */
public static final CelStandardFunctions EMPTY =
new CelStandardFunctions(ImmutableMultimap.of());

private final ImmutableMultimap<String, CelStandardOverload> standardOverloads;

public static final ImmutableSet<CelStandardFunction> ALL_STANDARD_FUNCTIONS =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ public void standardFunctions_filterFunctions() {
.containsExactly(AddOverload.ADD_INT64, SubtractOverload.SUBTRACT_INT64);
}

@Test
public void standardFunctions_empty() {
assertThat(CelStandardFunctions.EMPTY.getOverloads()).isEmpty();
}

@Test
public void standardEnvironment_subsetEnvironment() throws Exception {
CelCompiler celCompiler = CelCompilerFactory.standardCelCompilerBuilder().build();
Expand Down
Loading