Skip to content

Commit 5f9c853

Browse files
l46kokcopybara-github
authored andcommitted
Remove deprecated CelPolicyConfig and its parser
PiperOrigin-RevId: 731784974
1 parent c44efa8 commit 5f9c853

File tree

13 files changed

+103
-1737
lines changed

13 files changed

+103
-1737
lines changed

bundle/src/test/java/dev/cel/bundle/BUILD.bazel

+2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ java_library(
3939
"//compiler:compiler_builder",
4040
"//parser",
4141
"//parser:macro",
42+
"//parser:unparser",
4243
"//runtime",
4344
"//runtime:evaluation_exception_builder",
45+
"//runtime:evaluation_listener",
4446
"//runtime:unknown_attributes",
4547
"@cel_spec//proto/cel/expr:checked_java_proto",
4648
"@cel_spec//proto/cel/expr:syntax_java_proto",

bundle/src/test/java/dev/cel/bundle/CelEnvironmentYamlParserTest.java

+87
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.junit.Assert.assertThrows;
2020

2121
import com.google.common.base.Ascii;
22+
import com.google.common.collect.ImmutableMap;
2223
import com.google.common.collect.ImmutableSet;
2324
import com.google.common.io.Resources;
2425
import com.google.rpc.context.AttributeContext;
@@ -29,10 +30,19 @@
2930
import dev.cel.bundle.CelEnvironment.OverloadDecl;
3031
import dev.cel.bundle.CelEnvironment.TypeDecl;
3132
import dev.cel.bundle.CelEnvironment.VariableDecl;
33+
import dev.cel.common.CelAbstractSyntaxTree;
3234
import dev.cel.common.CelOptions;
35+
import dev.cel.common.CelSource;
36+
import dev.cel.common.ast.CelExpr;
37+
import dev.cel.common.types.SimpleType;
38+
import dev.cel.parser.CelUnparserFactory;
39+
import dev.cel.runtime.CelEvaluationListener;
40+
import dev.cel.runtime.CelLateFunctionBindings;
41+
import dev.cel.runtime.CelRuntime.CelFunctionBinding;
3342
import java.io.IOException;
3443
import java.net.URL;
3544
import java.util.Optional;
45+
import java.util.concurrent.atomic.AtomicReference;
3646
import org.junit.Test;
3747
import org.junit.runner.RunWith;
3848

@@ -723,6 +733,83 @@ public void environment_withYamlResource(@TestParameter EnvironmentYamlResourceT
723733
assertThat(environment).isEqualTo(testCase.expectedEnvironment);
724734
}
725735

736+
@Test
737+
public void lateBoundFunction_evaluate_callExpr() throws Exception {
738+
String configSource =
739+
"name: late_bound_function_config\n"
740+
+ "functions:\n"
741+
+ " - name: 'test'\n"
742+
+ " overloads:\n"
743+
+ " - id: 'test_bool'\n"
744+
+ " args:\n"
745+
+ " - type_name: 'bool'\n"
746+
+ " return:\n"
747+
+ " type_name: 'bool'";
748+
CelEnvironment celEnvironment = ENVIRONMENT_PARSER.parse(configSource);
749+
Cel celDetails =
750+
CelFactory.standardCelBuilder()
751+
.addVar("a", SimpleType.INT)
752+
.addVar("b", SimpleType.INT)
753+
.addVar("c", SimpleType.INT)
754+
.build();
755+
Cel cel = celEnvironment.extend(celDetails, CelOptions.DEFAULT);
756+
CelAbstractSyntaxTree ast = cel.compile("a < 0 && b < 0 && c < 0 && test(a<0)").getAst();
757+
CelLateFunctionBindings bindings =
758+
CelLateFunctionBindings.from(
759+
CelFunctionBinding.from("test_bool", Boolean.class, result -> result));
760+
761+
boolean result =
762+
(boolean) cel.createProgram(ast).eval(ImmutableMap.of("a", -1, "b", -1, "c", -4), bindings);
763+
764+
assertThat(result).isTrue();
765+
}
766+
767+
@Test
768+
public void lateBoundFunction_trace_callExpr_identifyFalseBranch() throws Exception {
769+
AtomicReference<CelExpr> capturedExpr = new AtomicReference<>();
770+
CelEvaluationListener listener =
771+
(expr, res) -> {
772+
if (res instanceof Boolean && !(boolean) res && capturedExpr.get() == null) {
773+
capturedExpr.set(expr);
774+
}
775+
};
776+
777+
String configSource =
778+
"name: late_bound_function_config\n"
779+
+ "functions:\n"
780+
+ " - name: 'test'\n"
781+
+ " overloads:\n"
782+
+ " - id: 'test_bool'\n"
783+
+ " args:\n"
784+
+ " - type_name: 'bool'\n"
785+
+ " return:\n"
786+
+ " type_name: 'bool'";
787+
788+
CelEnvironment celEnvironment = ENVIRONMENT_PARSER.parse(configSource);
789+
Cel celDetails =
790+
CelFactory.standardCelBuilder()
791+
.addVar("a", SimpleType.INT)
792+
.addVar("b", SimpleType.INT)
793+
.addVar("c", SimpleType.INT)
794+
.build();
795+
Cel cel = celEnvironment.extend(celDetails, CelOptions.DEFAULT);
796+
CelAbstractSyntaxTree ast = cel.compile("a < 0 && b < 0 && c < 0 && test(a<0)").getAst();
797+
CelLateFunctionBindings bindings =
798+
CelLateFunctionBindings.from(
799+
CelFunctionBinding.from("test_bool", Boolean.class, result -> result));
800+
801+
boolean result =
802+
(boolean)
803+
cel.createProgram(ast)
804+
.trace(ImmutableMap.of("a", -1, "b", 1, "c", -4), bindings, listener);
805+
806+
assertThat(result).isFalse();
807+
// Demonstrate that "b < 0" is what caused the expression to be false
808+
CelAbstractSyntaxTree subtree =
809+
CelAbstractSyntaxTree.newParsedAst(capturedExpr.get(), CelSource.newBuilder().build());
810+
assertThat(CelUnparserFactory.newUnparser().unparse(subtree)).isEqualTo("b < 0");
811+
}
812+
726813
private static String readFile(String path) throws IOException {
727814
URL url = Resources.getResource(Ascii.toLowerCase(path));
728815
return Resources.toString(url, UTF_8);

policy/BUILD.bazel

-10
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,6 @@ java_library(
2525
exports = ["//policy/src/main/java/dev/cel/policy:validation_exception"],
2626
)
2727

28-
java_library(
29-
name = "config",
30-
exports = ["//policy/src/main/java/dev/cel/policy:config"],
31-
)
32-
33-
java_library(
34-
name = "config_parser",
35-
exports = ["//policy/src/main/java/dev/cel/policy:config_parser"],
36-
)
37-
3828
java_library(
3929
name = "parser",
4030
exports = ["//policy/src/main/java/dev/cel/policy:parser"],

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

-60
Original file line numberDiff line numberDiff line change
@@ -50,52 +50,13 @@ java_library(
5050
],
5151
)
5252

53-
java_library(
54-
name = "config",
55-
srcs = [
56-
"CelPolicyConfig.java",
57-
],
58-
tags = [
59-
],
60-
deps = [
61-
":required_fields_checker",
62-
":source",
63-
":validation_exception",
64-
"//:auto_value",
65-
"//bundle:cel",
66-
"//common:compiler_common",
67-
"//common:options",
68-
"//common/types",
69-
"//common/types:type_providers",
70-
"//extensions",
71-
"//extensions:optional_library",
72-
"@maven//:com_google_errorprone_error_prone_annotations",
73-
"@maven//:com_google_guava_guava",
74-
],
75-
)
76-
77-
java_library(
78-
name = "config_parser",
79-
srcs = [
80-
"CelPolicyConfigParser.java",
81-
],
82-
tags = [
83-
],
84-
deps = [
85-
":config",
86-
":validation_exception",
87-
],
88-
)
89-
9053
java_library(
9154
name = "parser_factory",
9255
srcs = ["CelPolicyParserFactory.java"],
9356
tags = [
9457
],
9558
deps = [
96-
":config_parser",
9759
":parser_builder",
98-
":yaml_config_parser",
9960
":yaml_parser",
10061
"@maven//:org_yaml_snakeyaml",
10162
],
@@ -270,27 +231,6 @@ java_library(
270231
],
271232
)
272233

273-
java_library(
274-
name = "yaml_config_parser",
275-
srcs = [
276-
"CelPolicyYamlConfigParser.java",
277-
],
278-
visibility = ["//visibility:private"],
279-
deps = [
280-
":config",
281-
":config_parser",
282-
":source",
283-
":validation_exception",
284-
"//common:compiler_common",
285-
"//common/formats:parser_context",
286-
"//common/formats:yaml_helper",
287-
"//common/formats:yaml_parser_context_impl",
288-
"//common/internal",
289-
"@maven//:com_google_guava_guava",
290-
"@maven//:org_yaml_snakeyaml",
291-
],
292-
)
293-
294234
java_library(
295235
name = "rule_composer",
296236
srcs = ["RuleComposer.java"],

0 commit comments

Comments
 (0)