Skip to content

Commit 7ff74ab

Browse files
l46kokcopybara-github
authored andcommitted
Policy nested rule fix
PiperOrigin-RevId: 905187056
1 parent f566450 commit 7ff74ab

16 files changed

Lines changed: 542 additions & 86 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,19 +247,19 @@ java_library(
247247
visibility = ["//visibility:private"],
248248
deps = [
249249
":compiled_rule",
250-
"//:auto_value",
251250
"//bundle:cel",
252251
"//common:cel_ast",
253252
"//common:compiler_common",
254253
"//common:mutable_ast",
254+
"//common:mutable_source",
255255
"//common:operator",
256256
"//common/ast",
257+
"//common/ast:mutable_expr",
257258
"//common/formats:value_string",
258259
"//common/navigation:mutable_navigation",
259260
"//extensions:optional_library",
260261
"//optimizer:ast_optimizer",
261262
"//optimizer:mutable_ast",
262-
"@maven//:com_google_errorprone_error_prone_annotations",
263263
"@maven//:com_google_guava_guava",
264264
],
265265
)

policy/src/main/java/dev/cel/policy/RuleComposer.java

Lines changed: 205 additions & 79 deletions
Large diffs are not rendered by default.

policy/src/test/java/dev/cel/policy/CelPolicyCompilerImplTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,8 @@ public void evaluateYamlPolicy_nestedRuleProducesOptionalOutput() throws Excepti
266266
CelPolicyCompilerFactory.newPolicyCompiler(cel).build().compile(policy);
267267
Optional<Object> evalResult = (Optional<Object>) cel.createProgram(compiledPolicyAst).eval();
268268

269-
// Result is Optional<Optional<Object>>
270-
assertThat(evalResult).hasValue(Optional.of(true));
269+
// Result is Optional<Object> containing true
270+
assertThat(evalResult).hasValue(true);
271271
}
272272

273273
@Test

policy/src/test/java/dev/cel/policy/PolicyTestHelper.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ final class PolicyTestHelper {
4040
enum TestYamlPolicy {
4141
NESTED_RULE(
4242
"nested_rule",
43-
true,
43+
false,
4444
"cel.@block([resource.origin, @index0 in [\"us\", \"uk\", \"es\"], {\"banned\": true}],"
4545
+ " ((@index0 in {\"us\": false, \"ru\": false, \"ir\": false} && !@index1) ?"
46-
+ " optional.of(@index2) : optional.none()).or(optional.of(@index1 ? {\"banned\":"
47-
+ " false} : @index2)))"),
46+
+ " optional.of(@index2) : optional.none()).orValue(@index1 ? {\"banned\":"
47+
+ " false} : @index2))"),
4848
NESTED_RULE2(
4949
"nested_rule2",
5050
false,
@@ -61,6 +61,22 @@ enum TestYamlPolicy {
6161
+ " false, \"ru\": false, \"ir\": false} && @index1) ? {\"banned\":"
6262
+ " \"restricted_region\"} : {\"banned\": \"bad_actor\"}) : (@index1 ?"
6363
+ " optional.of({\"banned\": \"unconfigured_region\"}) : optional.none()))"),
64+
NESTED_RULE4("nested_rule4", false, "(x > 0) ? true : false"),
65+
NESTED_RULE5(
66+
"nested_rule5",
67+
true,
68+
"cel.@block([optional.of(true), optional.none()], (x > 0) ? ((x > 2) ? @index0 : @index1) :"
69+
+ " ((x > 1) ? ((x >= 2) ? @index0 : @index1) : optional.of(false)))"),
70+
NESTED_RULE6(
71+
"nested_rule6",
72+
false,
73+
"cel.@block([optional.of(true), optional.none()], ((x > 2) ? @index0 : @index1).orValue(((x"
74+
+ " > 3) ? @index0 : @index1).orValue(false)))"),
75+
NESTED_RULE7(
76+
"nested_rule7",
77+
true,
78+
"cel.@block([optional.of(true), optional.none()], ((x > 2) ? @index0 : @index1).or(((x > 3)"
79+
+ " ? @index0 : @index1).or((x > 1) ? optional.of(false) : @index1)))"),
6480
REQUIRED_LABELS(
6581
"required_labels",
6682
true,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
name: "nested_rule4"
16+
variables:
17+
- name: x
18+
type:
19+
type_name: int
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
name: nested_rule4
16+
rule:
17+
match:
18+
- condition: x > 0
19+
rule:
20+
match:
21+
- rule:
22+
match:
23+
- output: "true"
24+
- output: "false"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
description: "Nested rule tests which explore optional vs non-optional returns"
16+
section:
17+
- name: "valid"
18+
tests:
19+
- name: "x=0"
20+
input:
21+
x:
22+
value: 0
23+
output: "false"
24+
- name: "x=2"
25+
input:
26+
x:
27+
value: 2
28+
output: "true"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
name: "nested_rule5"
16+
variables:
17+
- name: x
18+
type:
19+
type_name: int
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
name: nested_rule5
16+
rule:
17+
match:
18+
- condition: x > 0
19+
rule:
20+
match:
21+
- rule:
22+
match:
23+
- condition: "x > 2"
24+
output: "true"
25+
- condition: x > 1
26+
rule:
27+
match:
28+
- condition: "x >= 2"
29+
output: "true"
30+
- output: "false"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
description: "Nested rule tests which explore optional vs non-optional returns"
16+
section:
17+
- name: "valid"
18+
tests:
19+
- name: "x=0"
20+
input:
21+
x:
22+
value: 0
23+
output: "false"
24+
- name: "x=1"
25+
input:
26+
x:
27+
value: 1
28+
output: "optional.none()"
29+
- name: "x=2"
30+
input:
31+
x:
32+
value: 2
33+
output: "optional.none()"
34+
- name: "x=3"
35+
input:
36+
x:
37+
value: 3
38+
output: "true"

0 commit comments

Comments
 (0)