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
19 changes: 13 additions & 6 deletions bundle/regal/ast/imports.rego
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package regal.ast

import data.regal.capabilities

default imports := []

# METADATA
Expand Down Expand Up @@ -46,26 +48,31 @@ resolved_imports[identifier] := path if {
# returns true if provided path (like ["data", "foo", "bar"]) is in the
# list of imports (which is commonly ast.imports)
imports_has_path(imports, path) if {
some imp in imports
pv := imports[_].path.value

count(pv) == count(path)

_arr(imp) == path
every i, part in path {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this run any faster/slower than building an array and using ==?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially, as every can benefit from early exit. But it's mostly custom functions that is a win to avoid when they're not strictly needed.

See open-policy-agent/opa#7266

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(although this is not a hot path.. I just thought the change made the code nicer :)

part == pv[i].value
}
}

# METADATA
# description: |
# returns whether a keyword is imported in the policy, either explicitly
# like "future.keywords.if" or implicitly like "future.keywords" or "rego.v1"
imports_keyword(imports, keyword) if {
some imp in imports
capabilities.is_opa_v1 # regal ignore:external-reference
input.regal.file.rego_version != "v0"
} else if {
pv := imports[_].path.value

_has_keyword(_arr(imp), keyword)
_has_keyword([p.value | some p in pv], keyword)
}

_imported_identifier(imp) := imp.alias
_imported_identifier(imp) := regal.last(imp.path.value).value if not imp.alias

_arr(xs) := [y.value | some y in xs.path.value]

_has_keyword(["future", "keywords"], _)
_has_keyword(["future", "keywords", "every"], "in")
_has_keyword(["future", "keywords", keyword], keyword)
Expand Down
7 changes: 7 additions & 0 deletions bundle/regal/ast/imports_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,10 @@ test_imports_keyword_future_keywords_every if {
not ast.imports_keyword(module.imports, "if")
not ast.imports_keyword(module.imports, "contains")
}

test_imports_all_kewords_opa_1dot0[keyword] if {
some keyword in ["in", "if", "every", "contains"]

ast.imports_keyword([], keyword) with input.regal.file.rego_version as "v1"
with data.regal.capabilities.is_opa_v1 as true
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import data.regal.result

report contains violation if {
# No need to traverse rules here if we're not importing `in`
ast.imports_keyword(input.imports, "in")
ast.imports_keyword(ast.imports, "in")

symbols := ast.found.symbols[_][_]

Expand Down
Loading