Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arbitrary vars in rule refs #5913

Merged
Merged
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
525c93d
topdown: Adding support for multiple variables in rule refs
johanfylling Apr 6, 2023
5f70376
Merge branch 'main' into jf/5685/multi-var-rule-refs
johanfylling Jul 3, 2023
d387cfd
Updating commented-out tests.
johanfylling Jul 6, 2023
d1dcb03
Fixing yaml test expecting multiple results.
johanfylling Jul 10, 2023
2073c3d
Not running yaml test for WASM
johanfylling Jul 12, 2023
29c2a2e
Merge branch 'main' into jf/5685/multi-var-rule-refs
johanfylling Aug 1, 2023
659ef2a
Moving type merge from `typeChecker.checkRule()` to `typeTreeNode.Ins…
johanfylling Jul 4, 2023
692770e
Updating type-checker to handle generic rule refs
johanfylling Jul 4, 2023
9707e66
Updating tests
johanfylling Jul 5, 2023
391d469
Generalizing `types.Object.Merge()``
johanfylling Jul 5, 2023
ea52a01
Merging sub-objects in `types.Object.Merge()`
johanfylling Jul 5, 2023
996cdec
Removing comment in test
johanfylling Jul 10, 2023
1065755
Updating tests
johanfylling Jul 5, 2023
67ba943
Aligning hint-key and entry for general refs on virtual partial eval
johanfylling Jul 11, 2023
8ffcf1d
Enforcing no-else parser rule on general ref heads
johanfylling Aug 10, 2023
f02441d
PE impl
johanfylling Aug 14, 2023
68f9436
Removing redundant legacy PE eval branch
johanfylling Aug 14, 2023
bec96d1
Fixing issue where PE-produced query contained non-namespaced local vars
johanfylling Aug 18, 2023
33ebc2f
Adding PE tests
johanfylling Aug 18, 2023
80620a0
Invoking evalOneRulePostUnify() for any ref with unknowns overlapping…
johanfylling Aug 21, 2023
98178fb
fmt: Not using rule ref ground prefix if ref has dynamic part
johanfylling Aug 21, 2023
372def8
Fixing linter warnings
johanfylling Aug 21, 2023
9e544c6
Ignoring additional yaml tests not supported by Wasm
johanfylling Aug 22, 2023
0fa5652
Merge branch 'main' into jf/5685/multi-var-rule-refs
johanfylling Aug 22, 2023
52bb04d
Cleanup
johanfylling Aug 22, 2023
18702a3
Producing cache hint keys containing entire applicable ref for genera…
johanfylling Aug 22, 2023
d4ac3b0
Merge branch 'main' into jf/5685/multi-var-rule-refs
johanfylling Aug 29, 2023
9e46555
Updating type-tree construction
johanfylling Aug 29, 2023
e1c0a23
Merge branch 'main' into jf/5685/multi-var-rule-refs
johanfylling Aug 29, 2023
2e7121b
Removing debug print
johanfylling Aug 29, 2023
a98a6ee
Merge branch 'main' into jf/5685/multi-var-rule-refs
johanfylling Aug 30, 2023
9dcaac8
Adding docs
johanfylling Aug 30, 2023
c0d38ea
Merge branch 'main' into jf/5685/multi-var-rule-refs
johanfylling Aug 31, 2023
ebf1503
Making changes suggested by @srenatus
johanfylling Aug 31, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
114 changes: 114 additions & 0 deletions docs/content/policy-language.md
Expand Up @@ -992,6 +992,120 @@ data.example
```live:eg/ref_heads:output
```

#### General References

Any term, except the very first, in a rule head's reference can be a variable. These variables can be assigned within the rule, just as for any other partial rule, to dynamically construct a nested collection of objects.

{{< danger >}}
General refs in rule heads is an experimental feature, and can be enabled by setting the `OPA_ENABLE_GENERAL_RULE_REFS` environment variable.
Copy link
Contributor

Choose a reason for hiding this comment

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

I was wondering about the name for a moment, but I think it's OK. We've got another example here, FWIW.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I'll change this to EXPERIMENTAL_GENERAL_RULE_REFS, to align 👍 .


This feature is currently not supported for Wasm and IR.
{{< /danger >}}

Data:

```json
{
"users": [
{
"id": "alice",
"role": "employee",
"country": "USA"
},
{
"id": "bob",
"role": "customer",
"country": "USA"
},
{
"id": "dora",
"role": "admin",
"country": "Sweden"
}
],
"admins": [
{
"id": "charlie"
}
]
}
```

Module:

```rego
package example

import future.keywords

# A partial object rule that converts a list of users to a mapping by "role" and then "id".
users_by_role[role][id] := user {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's throw in an if here and below

some user in data.users
id := user.id
role := user.role
}

# Partial rule with an explicit "admin" key override
users_by_role.admin[id] := user {
some user in data.admins
id := user.id
}

# Leaf entries can be partial sets
users_by_country[country] contains user.id {
some user in data.users
country := user.country
}
```

Query:

```
data.example
```

Output:

```json
{
"users_by_country": {
"Sweden": [
"dora"
],
"USA": [
"alice",
"bob"
]
},
"users_by_role": {
"admin": {
"charlie": {
"id": "charlie"
},
"dora": {
"country": "Sweden",
"id": "dora",
"role": "admin"
}
},
"customer": {
"bob": {
"country": "USA",
"id": "bob",
"role": "customer"
}
},
"employee": {
"alice": {
"country": "USA",
"id": "alice",
"role": "employee"
}
}
}
}
```

### Functions

Rego supports user-defined functions that can be called with the same semantics as [Built-in Functions](#built-in-functions). They have access to both the [the data Document](../philosophy/#the-opa-document-model) and [the input Document](../philosophy/#the-opa-document-model).
Expand Down