Rego gives you two ways to package logic: functions and virtual documents. They overlap on paper; in practice the choice comes down to whether you need a relation you can iterate.
We’ll use a file list in input and ask: which paths look like config files?
1 | // input.json |
Functions
Rego allows multiple rules with the same name — overload by arity and by how arguments unify. Think lightweight pattern matching, not Java generics.
Properties:
- Default return is
true/false(undefined → false) - Or assign with
name(args) = value { ... } - Same name only if arity differs (or inputs unify differently)
- Same inputs must always yield the same outputs
Check file extensions — three heads, OR semantics:
1 | is_config_file(str) { |
Or chain with else:
1 | is_config_file2(str) { |
Value-returning overload — extract a filename per path style:
1 | getFileName(type, str) = x { |
Looks like duplicate signatures; the type = "posix" / type = "traditional-mac" lines discriminate inputs — similar to getFileName("posix", str) vs getFileName("traditional-mac", str).
Drop those guards and you’ll hit:
eval_conflict_error: functions must not produce multiple outputs for same inputs
Wire it together:
1 | import input.files |
Builtins: policy reference
Virtual documents
A virtual document is a finite relation you can query and enumerate — getFileNames[x] defines the set of names.
1 | import input.files |
getFileNames[x] { ... } is shorthand for getFileNames[x] = x { ... } — each successful x becomes a member.
Eval:
1 | cd syntax |
Iterate and filter:
1 | import data.example_func.is_config_file |
getFileNames[_] walks the set; each binding feeds is_config_file.
Pick one
Ask:
- Do you need to enumerate results?
- Is the output finite for a given input?
If either is yes, lean virtual document. Pure transforms on known arguments → function.
Next: comprehensions for the compact aggregation syntax.
Questions? Leave a comment below or email: newbvirgil@gmail.com![]()
