microsoft / microsoft/rego-cpp
`rego.metadata.*` and `rego.parse_module` builtins
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 47
- Forks
- 19
- PR merge metrics
- No merged PRs in 30d
Description
Summary
Implement the rego.metadata.chain, rego.metadata.rule, and rego.parse_module built-in functions. All three are currently registered as placeholders in src/builtins/rego.cc, returning "Rego metadata not supported" / "Rego module parsing not supported" at runtime.
OPA reference
rego.metadata.chain: returns the chain of metadata annotation objects from the current rule up through the package hierarchy. Each entry represents an ancestor node that has declared# METADATAannotations.rego.metadata.rule: returns the metadata annotation object for the active rule, or an empty object if no annotations exist.rego.parse_module:rego.parse_module(filename, rego)— parses a Rego module string and returns its AST as a JSON object.
OPA implementation reference
- Metadata annotation parsing:
ast/annotations.go - Metadata builtins:
topdown/rego.go parse_module:topdown/rego.go— look forbuiltinRegoParseModule- OPA annotations spec: Policy Reference — Annotations
Current state
src/builtins/rego.cc: Placeholder declarations for all three functions exist with full type metadata. All returnBuiltInDef::placeholder(...).- Dispatcher routing is fully wired. Only implementation functions and the required evaluation-context plumbing need to be added.
Work items
rego.metadata.chain and rego.metadata.rule
These two functions are the most architecturally invasive, as they require the evaluator to provide rule-level context to the built-in at call time.
- Annotation parsing — Extend the Rego parser to recognize
# METADATAcomment blocks (YAML) preceding rules and packages. Parse and store annotation objects (title, description, authors, organizations, related_resources, schemas, scope, custom, etc.) per rule/package node. - Annotation storage — Attach parsed annotation metadata to rule and package AST nodes so it survives through compilation.
- Evaluation context threading — The evaluator must make the "current rule" and its ancestry available to built-in functions. This likely requires extending the built-in call interface to pass evaluation context (or adding a context-aware variant of
BuiltInBehavior). rego.metadata.ruleimplementation — Return the annotation object for the currently evaluating rule, or an empty object{}if none exists.rego.metadata.chainimplementation — Walk up from the current rule through parent rules/packages, collecting all nodes that have annotations, and return as an array.
rego.parse_module
rego.parse_moduleimplementation — Invoke the existing Rego parser on the input string and convert the resulting AST into a JSON object matching OPA's AST format. The main challenge is ensuring the output JSON structure matches OPA's expected format (package, imports, rules, comments, annotations, etc.).
Finalization
- Swap
BuiltInDef::placeholder→BuiltInDef::createin all three factory functions. - Update
README.md— Removerego.metadata.chain/rego.metadata.rule/rego.parse_modulefrom the unsupported builtins list (line 157). - Tests — Add test cases covering:
- Rule with
# METADATAblock →rego.metadata.rulereturns annotation object - Rule without annotations → returns
{} - Nested package/rule chain →
rego.metadata.chainreturns correct ancestry - Multiple annotations at different scopes
rego.parse_modulewith a simple module → correct AST structurerego.parse_modulewith imports, multiple rules, and annotations- Error cases: invalid module string, empty input
- Validate behavior matches OPA for the relevant OPA compliance tests
- Rule with
Notes
- This is the highest-complexity built-in work item in the remaining backlog. The metadata functions require changes to the parser, AST representation, and evaluator — not just the built-in system. The
rego.parse_modulefunction is somewhat independent and could be delivered separately. - The annotation format is YAML embedded in comments. OPA uses a specific prefix (
# METADATA) and expects the YAML block to be contiguous. Consider whether to add a YAML parsing dependency or implement a minimal subset parser. - The OPA AST JSON format for
rego.parse_moduleis complex and version-dependent. Careful alignment with OPA's output will be needed for compliance. - Consider splitting this into sub-issues if the scope is too large for a single PR: (a) annotation parsing + metadata builtins, (b)
rego.parse_module.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the placeholder declarations in src/builtins/rego.cc, then trace the existing Rego parser, AST representation, and evaluator call interface. Compare those entry points with OPA's ast/annotations.go and topdown/rego.go references, and review how tests exercise builtins. Done means all three builtins are implemented, covered by the listed behavior and error tests, and removed from README.md's unsupported list.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- backend, compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100