Traversing recursive documents #523
-
I am wondering if it is possible to express a Rego rule to collect an entity and all its children (recursively). Given the following input: {
"group": "parent"
} I'm trying to get the following result: {
"groups": ["parent", "childA", "childB", "childC"]
} Given the following data: {
"entities": [
{
"name": "parent",
"relations": [
{
"type": "parentOf",
"name": "childA"
},
{
"type": "parentOf",
"name": "childB"
}
]
},
{
"name": "childA",
"relations": [
{
"type": "parentOf",
"name": "childC"
}
]
},
{
"name": "childB",
"relations": []
},
{
"name": "childC",
"relations": []
}
]
}
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Heya. This might not be possible in rego, as recursion is forbidden. One workaround is to unroll the logic for a bounded depth, but it would need you to drop this constraint:
Say, if you only allow nesting of level <= 3, you could write out the rules explicitly. This is a start, to get the gist of it 👇 package play
import future.keywords
# direct decendants
result[entity.name] contains child if {
some entity in input.entities
some rel in entity.relations
rel.type == "parentOf"
child := rel.name
}
# one indirection
result[entity.name] contains child if {
some entity in input.entities
some rel in entity.relations
rel.type == "parentOf"
some other_entity in input.entities
other_entity.name == rel.name
some other_rel in other_entity.relations
other_rel.type == "parentOf"
child := other_rel.name
} |
Beta Was this translation helpful? Give feedback.
-
@srenatus is probably right, as he often is :) But I wonder if this could be modeled for input to graph.reachable_paths? I'm sick at home right now, so my brain isn't going to handle graphs today, but could be worth looking into, I guess. There are some known issues with that built-in function to be aware of though: open-policy-agent/opa#5871 |
Beta Was this translation helpful? Give feedback.
Heya. This might not be possible in rego, as recursion is forbidden.
One workaround is to unroll the logic for a bounded depth, but it would need you to drop this constraint:
Say, if you only allow nesting of level <= 3, you could write out the rules explicitly.
This is a start, to get the gist of it 👇