Skip to content

Commit 4e5c36d

Browse files
Include annotations in rule AST (#6771)
Signed-off-by: Ashutosh Narkar <[email protected]> Co-authored-by: Johan Fylling <[email protected]>
1 parent 02c565a commit 4e5c36d

File tree

6 files changed

+548
-7
lines changed

6 files changed

+548
-7
lines changed

ast/annotations.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,34 @@ func (a *Annotations) toObject() (*Object, *Error) {
509509
return &obj, nil
510510
}
511511

512+
func attachRuleAnnotations(mod *Module) {
513+
// make a copy of the annotations
514+
cpy := make([]*Annotations, len(mod.Annotations))
515+
for i, a := range mod.Annotations {
516+
cpy[i] = a.Copy(a.node)
517+
}
518+
519+
for _, rule := range mod.Rules {
520+
var j int
521+
var found bool
522+
for i, a := range cpy {
523+
if rule.Ref().Equal(a.GetTargetPath()) {
524+
if a.Scope == annotationScopeDocument {
525+
rule.Annotations = append(rule.Annotations, a)
526+
} else if a.Scope == annotationScopeRule && rule.Loc().Row > a.Location.Row {
527+
j = i
528+
found = true
529+
rule.Annotations = append(rule.Annotations, a)
530+
}
531+
}
532+
}
533+
534+
if found && j < len(cpy) {
535+
cpy = append(cpy[:j], cpy[j+1:]...)
536+
}
537+
}
538+
}
539+
512540
func attachAnnotationsNodes(mod *Module) Errors {
513541
var errs Errors
514542

ast/compile.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,6 +2196,8 @@ func (c *Compiler) parseMetadataBlocks() {
21962196
for _, err := range errs {
21972197
c.err(err)
21982198
}
2199+
2200+
attachRuleAnnotations(mod)
21992201
}
22002202
}
22012203
}

ast/marshal_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,22 @@ func TestRule_MarshalJSON(t *testing.T) {
444444
}(),
445445
ExpectedJSON: `{"body":[{"index":0,"terms":{"type":"boolean","value":true}}],"head":{"name":"allow","value":{"type":"boolean","value":true},"ref":[{"type":"var","value":"allow"}]},"location":{"file":"example.rego","row":6,"col":2}}`,
446446
},
447+
"annotations included": {
448+
Rule: func() *Rule {
449+
r := rule.Copy()
450+
r.Annotations = []*Annotations{{
451+
Scope: "rule",
452+
Title: "My rule",
453+
Entrypoint: true,
454+
Organizations: []string{"org1"},
455+
Description: "My desc",
456+
Custom: map[string]interface{}{
457+
"foo": "bar",
458+
}}}
459+
return r
460+
}(),
461+
ExpectedJSON: `{"annotations":[{"custom":{"foo":"bar"},"description":"My desc","entrypoint":true,"organizations":["org1"],"scope":"rule","title":"My rule"}],"body":[{"index":0,"terms":{"type":"boolean","value":true}}],"head":{"name":"allow","value":{"type":"boolean","value":true},"ref":[{"type":"var","value":"allow"}]}}`,
462+
},
447463
}
448464

449465
for name, data := range testCases {

ast/parser_ext.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,8 @@ func parseModule(filename string, stmts []Statement, comments []*Comment, regoCo
713713
return nil, errs
714714
}
715715

716+
attachRuleAnnotations(mod)
717+
716718
return mod, nil
717719
}
718720

0 commit comments

Comments
 (0)