Skip to content

Commit 505d2cb

Browse files
authored
OPA v1.13.1 (#1850)
And use the new `array.flatten` to our benefit where we can! Some of those locations turned out to not need flattening at all, and could be rewritten to something even somple. Which is a win too, of course :) A minor but clearly measurable performance win, but more from the changes here than from bumping OPA this time. Signed-off-by: Anders Eknert <anders.eknert@apple.com>
1 parent ca50d63 commit 505d2cb

19 files changed

Lines changed: 169 additions & 159 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<!-- markdownlint-disable MD041 -->
99

1010
[![Build Status](https://github.com/open-policy-agent/regal/workflows/Build/badge.svg)](https://github.com/open-policy-agent/regal/actions)
11-
![OPA v1.12.2](https://www.openpolicyagent.org/badge/v1.12.2)
11+
![OPA v1.13.1](https://www.openpolicyagent.org/badge/v1.13.1)
1212
[![codecov](https://codecov.io/github/open-policy-agent/regal/graph/badge.svg?token=EQK01YF3X3)](https://codecov.io/github/StyraInc/regal)
1313
[![Downloads](https://img.shields.io/github/downloads/open-policy-agent/regal/total.svg)](https://github.com/open-policy-agent/regal/releases)
1414

build/capabilities.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,31 @@
150150
"type": "function"
151151
}
152152
},
153+
{
154+
"name": "array.flatten",
155+
"description": "Non-recursively unpacks array items in arr into the flattened array. Other types are appended as-is.",
156+
"decl": {
157+
"args": [
158+
{
159+
"description": "the array to be flattened",
160+
"dynamic": {
161+
"type": "any"
162+
},
163+
"name": "arr",
164+
"type": "array"
165+
}
166+
],
167+
"result": {
168+
"description": "array flattened one level",
169+
"dynamic": {
170+
"type": "any"
171+
},
172+
"name": "flattened",
173+
"type": "array"
174+
},
175+
"type": "function"
176+
}
177+
},
153178
{
154179
"name": "array.reverse",
155180
"description": "Returns the reverse of a given array.",

bundle/regal/ast/ast.rego

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,11 @@ ref_value_equal(v1, v2) if {
237237
# description: |
238238
# returns a new ref value made by extending terms1 with terms2,
239239
# where the first term of terms2 transformed to string
240-
extend_ref_terms(terms1, terms2) := array.concat(
240+
extend_ref_terms(terms1, terms2) := array.flatten([
241241
terms1,
242-
array.concat(
243-
[object.union(terms2[0], {"type": "string"})],
244-
array.slice(terms2, 1, 100),
245-
),
246-
)
242+
object.union(terms2[0], {"type": "string"}),
243+
array.slice(terms2, 1, 100),
244+
])
247245

248246
# METADATA
249247
# description: |

bundle/regal/ast/comments.rego

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,6 @@ comments["annotation_match"](str) if regex.match(
3535
str,
3636
)
3737

38-
# METADATA
39-
# description: array containing all annotations from the module
40-
annotations := array.concat(
41-
[annotation | some annotation in input.package.annotations],
42-
[annotation | annotation := input.rules[_].annotations[_]],
43-
)
44-
4538
# METADATA
4639
# description: |
4740
# map of all ignore directive comments, like ("# regal ignore:line-length")
@@ -92,15 +85,15 @@ comment_blocks(comments) := blocks if {
9285
]
9386
}
9487

95-
_splits(xs) := array.concat(
96-
array.concat(
97-
# [-1] ++ [ all indices where there's a step larger than one ] ++ [length of xs]
98-
# the -1 is because we're adding +1 in array.slice
99-
[-1],
100-
[i |
101-
some i in numbers.range(0, count(xs) - 1)
102-
xs[i + 1] != xs[i] + 1
103-
],
104-
),
105-
[count(xs)],
106-
)
88+
_splits(xs) := array.flatten([
89+
# -1 ++ [ all indices where there's a step larger than one ] ++ length of xs
90+
# the -1 is because we're adding +1 in array.slice
91+
-1,
92+
[i |
93+
some i in numbers.range(0, n - 1)
94+
xs[i + 1] != xs[i] + 1
95+
],
96+
n,
97+
]) if {
98+
n := count(xs)
99+
}

bundle/regal/ast/search.rego

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,12 @@ find_ref_vars(value) := [var | # regal ignore:narrow-argument
5151

5252
# one or two vars declared via `every`, i.e. `every x in y {}`
5353
# or `every`, i.e. `every x, y in z {}`
54-
_find_every_vars(value) := array.concat(key_var, val_var) if {
55-
key_var := [value.key |
56-
value.key.type == "var"
57-
indexof(value.key.value, "$") == -1
58-
]
59-
val_var := [value.value |
60-
value.value.type == "var"
61-
indexof(value.value.value, "$") == -1
62-
]
63-
}
54+
_find_every_vars(value) := [var |
55+
some kind in ["key", "value"]
56+
var := value[kind]
57+
var.type == "var"
58+
indexof(var.value, "$") == -1
59+
]
6460

6561
# METADATA
6662
# description: |

bundle/regal/lsp/selectionrange/selectionrange.rego

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ default result["response"] := null
1919
# METADATA
2020
# entrypoint: true
2121
result["response"] := ranges if {
22-
# we don't have a array.concat_n function, and of course, no reduce
23-
ranges := array.concat(array.concat(package_ranges, import_ranges), rule_ranges)
22+
ranges := array.flatten([package_ranges, import_ranges, rule_ranges])
2423

2524
count(ranges) > 0
2625
}
@@ -61,7 +60,7 @@ package_ranges := [item |
6160
# package locations are even worse than imports, as there isn't even a location of
6261
# the path itself, but only the `package` keyword and the indivdual path terms.. but OTOH,
6362
# copy/pasting that line is probably not too common
64-
item := _to_selection_range(array.concat(ranges, [path_range, line_range]))
63+
item := _to_selection_range(array.flatten([ranges, path_range, line_range]))
6564
]
6665

6766
# METADATA
@@ -81,7 +80,7 @@ import_ranges := [item |
8180

8281
# import locations don't include the full text of the line, so we'll need to
8382
# improvise here some, assuming properly formatted Rego
84-
item := _to_selection_range(array.concat(ranges, [{"range": _estimated_import_range(imp)}]))
83+
item := _to_selection_range(array.flatten([ranges, {"range": _estimated_import_range(imp)}]))
8584
]
8685

8786
# METADATA

bundle/regal/lsp/signaturehelp/signaturehelp.rego

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ _text_up_to_position(lines, _, position) := concat("\n", all_lines) if {
6060
current_line := lines[position.line]
6161
position.character < count(current_line)
6262

63-
complete_lines := [line | some i, line in lines; i < position.line]
64-
partial_line := substring(current_line, 0, position.character)
65-
66-
all_lines := array.concat(complete_lines, [partial_line])
63+
all_lines := array.flatten([
64+
[line | some i, line in lines; i < position.line],
65+
substring(current_line, 0, position.character),
66+
])
6767
}
6868

6969
_build_function_label(decl, func_name) := label if {

bundle/regal/result/result.rego

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ _fail_annotated_custom(metadata, details) := violation if {
150150

151151
_resource_urls(related_resources, category) := [r |
152152
some item in related_resources
153-
r := object.union(object.remove(item, ["ref"]), {"ref": config.docs.resolve_url(item.ref, category)})
153+
r := object.union(item, {"ref": config.docs.resolve_url(item.ref, category)})
154154
]
155155

156156
# Note that the `text` attribute always returns the entire line and *not*
@@ -189,7 +189,8 @@ ranged_from_ref(ref) := ranged_location_between(ref[0], regal.last(ref))
189189
# description: |
190190
# creates a ranged location where the start location is the left hand side of an infix
191191
# expression, like `"foo" == "bar"`, and the end location is the end of the infix operator
192-
infix_expr_location(terms) := location(sprintf("%s:%s:%s:%s", array.concat(
193-
array.slice(split(terms[1].location, ":"), 0, 2),
194-
array.slice(split(terms[0].location, ":"), 2, 4),
195-
)))
192+
infix_expr_location(terms) := location(regex.replace(
193+
$"{terms[1].location}{terms[0].location}",
194+
`^(\d+:\d+:).+:(\d+:\d+)$`,
195+
`$1$2`,
196+
))

bundle/regal/rules/bugs/impossible-not/impossible_not.rego

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ report contains violation if {
7373
# note that the "not" isn't present in the AST, so we'll add it manually to the text
7474
# in the location to try and make it clear where the issue is (as opposed to just
7575
# printing the ref)
76-
"text": concat(" ", ["not", _to_string(negated.ref)]),
76+
"text": $"not {_to_string(negated.ref)}",
7777
}})
7878

7979
violation := result.fail(rego.metadata.chain(), loc)
@@ -102,7 +102,7 @@ aggregate_report contains violation if {
102102
# note that the "not" isn't present in the AST, so we'll add it manually to the text
103103
# in the location to try and make it clear where the issue is (as opposed to just
104104
# printing the ref)
105-
"text": concat(" ", ["not", _to_string(negated.ref)]),
105+
"text": $"not {_to_string(negated.ref)}",
106106
}})
107107

108108
violation := result.fail(rego.metadata.chain(), loc)

bundle/regal/rules/idiomatic/no-defined-entrypoint/no_defined_entrypoint.rego

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,25 @@
22
# description: Missing entrypoint annotation
33
package regal.rules.idiomatic["no-defined-entrypoint"]
44

5-
import data.regal.ast
65
import data.regal.result
76
import data.regal.util
87

98
# METADATA
109
# description: |
1110
# collects `entrypoint: true` annotations from any given module
11+
# scope: document
1212
aggregate contains entry if {
13-
some annotation in ast.annotations
14-
annotation.entrypoint == true
13+
some i
14+
input.package.annotations[i].entrypoint == true
1515

16-
entry := {"entrypoint": util.to_location_object(annotation.location)}
16+
entry := {"entrypoint": util.to_location_object(input.package.annotations[i].location)}
17+
}
18+
19+
aggregate contains entry if {
20+
some i, j
21+
input.rules[i].annotations[j].entrypoint == true
22+
23+
entry := {"entrypoint": util.to_location_object(input.rules[i].annotations[j].location)}
1724
}
1825

1926
# METADATA

0 commit comments

Comments
 (0)