Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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
2 changes: 2 additions & 0 deletions 2 bundle/src/test/java/dev/cel/bundle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ java_library(
"//compiler:compiler_builder",
"//parser",
"//parser:macro",
"//parser:unparser",
"//runtime",
"//runtime:evaluation_exception_builder",
"//runtime:evaluation_listener",
"//runtime:unknown_attributes",
"@cel_spec//proto/cel/expr:checked_java_proto",
"@cel_spec//proto/cel/expr:syntax_java_proto",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.Assert.assertThrows;

import com.google.common.base.Ascii;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.Resources;
import com.google.rpc.context.AttributeContext;
Expand All @@ -29,10 +30,19 @@
import dev.cel.bundle.CelEnvironment.OverloadDecl;
import dev.cel.bundle.CelEnvironment.TypeDecl;
import dev.cel.bundle.CelEnvironment.VariableDecl;
import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.CelOptions;
import dev.cel.common.CelSource;
import dev.cel.common.ast.CelExpr;
import dev.cel.common.types.SimpleType;
import dev.cel.parser.CelUnparserFactory;
import dev.cel.runtime.CelEvaluationListener;
import dev.cel.runtime.CelLateFunctionBindings;
import dev.cel.runtime.CelRuntime.CelFunctionBinding;
import java.io.IOException;
import java.net.URL;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Test;
import org.junit.runner.RunWith;

Expand Down Expand Up @@ -723,6 +733,83 @@ public void environment_withYamlResource(@TestParameter EnvironmentYamlResourceT
assertThat(environment).isEqualTo(testCase.expectedEnvironment);
}

@Test
public void lateBoundFunction_evaluate_callExpr() throws Exception {
String configSource =
"name: late_bound_function_config\n"
+ "functions:\n"
+ " - name: 'test'\n"
+ " overloads:\n"
+ " - id: 'test_bool'\n"
+ " args:\n"
+ " - type_name: 'bool'\n"
+ " return:\n"
+ " type_name: 'bool'";
CelEnvironment celEnvironment = ENVIRONMENT_PARSER.parse(configSource);
Cel celDetails =
CelFactory.standardCelBuilder()
.addVar("a", SimpleType.INT)
.addVar("b", SimpleType.INT)
.addVar("c", SimpleType.INT)
.build();
Cel cel = celEnvironment.extend(celDetails, CelOptions.DEFAULT);
CelAbstractSyntaxTree ast = cel.compile("a < 0 && b < 0 && c < 0 && test(a<0)").getAst();
CelLateFunctionBindings bindings =
CelLateFunctionBindings.from(
CelFunctionBinding.from("test_bool", Boolean.class, result -> result));

boolean result =
(boolean) cel.createProgram(ast).eval(ImmutableMap.of("a", -1, "b", -1, "c", -4), bindings);

assertThat(result).isTrue();
}

@Test
public void lateBoundFunction_trace_callExpr_identifyFalseBranch() throws Exception {
AtomicReference<CelExpr> capturedExpr = new AtomicReference<>();
CelEvaluationListener listener =
(expr, res) -> {
if (res instanceof Boolean && !(boolean) res && capturedExpr.get() == null) {
capturedExpr.set(expr);
}
};

String configSource =
"name: late_bound_function_config\n"
+ "functions:\n"
+ " - name: 'test'\n"
+ " overloads:\n"
+ " - id: 'test_bool'\n"
+ " args:\n"
+ " - type_name: 'bool'\n"
+ " return:\n"
+ " type_name: 'bool'";

CelEnvironment celEnvironment = ENVIRONMENT_PARSER.parse(configSource);
Cel celDetails =
CelFactory.standardCelBuilder()
.addVar("a", SimpleType.INT)
.addVar("b", SimpleType.INT)
.addVar("c", SimpleType.INT)
.build();
Cel cel = celEnvironment.extend(celDetails, CelOptions.DEFAULT);
CelAbstractSyntaxTree ast = cel.compile("a < 0 && b < 0 && c < 0 && test(a<0)").getAst();
CelLateFunctionBindings bindings =
CelLateFunctionBindings.from(
CelFunctionBinding.from("test_bool", Boolean.class, result -> result));

boolean result =
(boolean)
cel.createProgram(ast)
.trace(ImmutableMap.of("a", -1, "b", 1, "c", -4), bindings, listener);

assertThat(result).isFalse();
// Demonstrate that "b < 0" is what caused the expression to be false
CelAbstractSyntaxTree subtree =
CelAbstractSyntaxTree.newParsedAst(capturedExpr.get(), CelSource.newBuilder().build());
assertThat(CelUnparserFactory.newUnparser().unparse(subtree)).isEqualTo("b < 0");
}

private static String readFile(String path) throws IOException {
URL url = Resources.getResource(Ascii.toLowerCase(path));
return Resources.toString(url, UTF_8);
Expand Down
10 changes: 0 additions & 10 deletions 10 policy/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@ java_library(
exports = ["//policy/src/main/java/dev/cel/policy:validation_exception"],
)

java_library(
name = "config",
exports = ["//policy/src/main/java/dev/cel/policy:config"],
)

java_library(
name = "config_parser",
exports = ["//policy/src/main/java/dev/cel/policy:config_parser"],
)

java_library(
name = "parser",
exports = ["//policy/src/main/java/dev/cel/policy:parser"],
Expand Down
60 changes: 0 additions & 60 deletions 60 policy/src/main/java/dev/cel/policy/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -50,52 +50,13 @@ java_library(
],
)

java_library(
name = "config",
srcs = [
"CelPolicyConfig.java",
],
tags = [
],
deps = [
":required_fields_checker",
":source",
":validation_exception",
"//:auto_value",
"//bundle:cel",
"//common:compiler_common",
"//common:options",
"//common/types",
"//common/types:type_providers",
"//extensions",
"//extensions:optional_library",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
)

java_library(
name = "config_parser",
srcs = [
"CelPolicyConfigParser.java",
],
tags = [
],
deps = [
":config",
":validation_exception",
],
)

java_library(
name = "parser_factory",
srcs = ["CelPolicyParserFactory.java"],
tags = [
],
deps = [
":config_parser",
":parser_builder",
":yaml_config_parser",
":yaml_parser",
"@maven//:org_yaml_snakeyaml",
],
Expand Down Expand Up @@ -270,27 +231,6 @@ java_library(
],
)

java_library(
name = "yaml_config_parser",
srcs = [
"CelPolicyYamlConfigParser.java",
],
visibility = ["//visibility:private"],
deps = [
":config",
":config_parser",
":source",
":validation_exception",
"//common:compiler_common",
"//common/formats:parser_context",
"//common/formats:yaml_helper",
"//common/formats:yaml_parser_context_impl",
"//common/internal",
"@maven//:com_google_guava_guava",
"@maven//:org_yaml_snakeyaml",
],
)

java_library(
name = "rule_composer",
srcs = ["RuleComposer.java"],
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.