From eef6745bad7c327f94be2d755978e1cde5b6dc73 Mon Sep 17 00:00:00 2001 From: Lukasz Mierzwa Date: Fri, 24 Nov 2023 15:58:30 +0000 Subject: [PATCH] Move comments to a single slice per rule --- internal/parser/models.go | 89 +++++----------------------------- internal/parser/parser.go | 40 +++++++++------ internal/parser/parser_test.go | 16 +++--- 3 files changed, 47 insertions(+), 98 deletions(-) diff --git a/internal/parser/models.go b/internal/parser/models.go index e55dbde0..9440e197 100644 --- a/internal/parser/models.go +++ b/internal/parser/models.go @@ -83,20 +83,21 @@ func mergeComments(node *yaml.Node) (comments []string) { if node.FootComment != "" { comments = append(comments, node.FootComment) } + for _, child := range node.Content { + comments = append(comments, mergeComments(child)...) + } return comments } type YamlNode struct { Position FilePosition Value string - Comments []string } func newYamlNode(node *yaml.Node, offset int) *YamlNode { return &YamlNode{ Position: NewFilePosition(nodeLines(node, offset)), Value: node.Value, - Comments: mergeComments(node), } } @@ -242,36 +243,6 @@ func (ar AlertingRule) Lines() (lines []int) { return lines } -func (ar AlertingRule) Comments() (comments []string) { - comments = append(comments, ar.Alert.Key.Comments...) - comments = append(comments, ar.Alert.Value.Comments...) - comments = append(comments, ar.Expr.Key.Comments...) - comments = append(comments, ar.Expr.Value.Comments...) - if ar.For != nil { - comments = append(comments, ar.For.Key.Comments...) - comments = append(comments, ar.For.Value.Comments...) - } - if ar.KeepFiringFor != nil { - comments = append(comments, ar.KeepFiringFor.Key.Comments...) - comments = append(comments, ar.KeepFiringFor.Value.Comments...) - } - if ar.Labels != nil { - comments = append(comments, ar.Labels.Key.Comments...) - for _, label := range ar.Labels.Items { - comments = append(comments, label.Key.Comments...) - comments = append(comments, label.Value.Comments...) - } - } - if ar.Annotations != nil { - comments = append(comments, ar.Annotations.Key.Comments...) - for _, annotation := range ar.Annotations.Items { - comments = append(comments, annotation.Key.Comments...) - comments = append(comments, annotation.Value.Comments...) - } - } - return comments -} - type RecordingRule struct { Record YamlKeyValue Expr PromQLExpr @@ -288,21 +259,6 @@ func (rr RecordingRule) Lines() (lines []int) { return lines } -func (rr RecordingRule) Comments() (comments []string) { - comments = append(comments, rr.Record.Key.Comments...) - comments = append(comments, rr.Record.Value.Comments...) - comments = append(comments, rr.Expr.Key.Comments...) - comments = append(comments, rr.Expr.Value.Comments...) - if rr.Labels != nil { - comments = append(comments, rr.Labels.Key.Comments...) - for _, label := range rr.Labels.Items { - comments = append(comments, label.Key.Comments...) - comments = append(comments, label.Value.Comments...) - } - } - return comments -} - type ParseError struct { Fragment string Err error @@ -312,6 +268,7 @@ type ParseError struct { type Rule struct { AlertingRule *AlertingRule RecordingRule *RecordingRule + Comments []string Error ParseError } @@ -325,11 +282,13 @@ func (r Rule) ToYAML() string { } var b strings.Builder + + for _, c := range r.Comments { + b.WriteString(c) + b.WriteRune('\n') + } + if r.AlertingRule != nil { - for _, c := range r.AlertingRule.Comments() { - b.WriteString(c) - b.WriteRune('\n') - } b.WriteString("- ") b.WriteString(r.AlertingRule.Alert.Key.Value) b.WriteString(": ") @@ -382,10 +341,6 @@ func (r Rule) ToYAML() string { return b.String() } - for _, c := range r.RecordingRule.Comments() { - b.WriteString(c) - b.WriteRune('\n') - } b.WriteString("- ") b.WriteString(r.RecordingRule.Record.Key.Value) b.WriteString(": ") @@ -482,13 +437,7 @@ func (r Rule) LineRange() []int { } func (r Rule) HasComment(comment string) bool { - var comments []string - if r.RecordingRule != nil { - comments = r.RecordingRule.Comments() - } else if r.AlertingRule != nil { - comments = r.AlertingRule.Comments() - } - for _, c := range comments { + for _, c := range r.Comments { if hasComment(c, comment) { return true } @@ -497,13 +446,7 @@ func (r Rule) HasComment(comment string) bool { } func (r Rule) GetComment(comment ...string) (s Comment, ok bool) { - var comments []string - if r.RecordingRule != nil { - comments = r.RecordingRule.Comments() - } else if r.AlertingRule != nil { - comments = r.AlertingRule.Comments() - } - for _, c := range comments { + for _, c := range r.Comments { var val Comment if val, ok = GetLastComment(c, comment...); ok { return val, ok @@ -513,13 +456,7 @@ func (r Rule) GetComment(comment ...string) (s Comment, ok bool) { } func (r Rule) GetComments(key string) (cs []Comment) { - var comments []string - if r.RecordingRule != nil { - comments = r.RecordingRule.Comments() - } else if r.AlertingRule != nil { - comments = r.AlertingRule.Comments() - } - for _, c := range comments { + for _, c := range r.Comments { sc := bufio.NewScanner(strings.NewReader(c)) for sc.Scan() { if val, ok := GetLastComment(sc.Text(), key); ok { diff --git a/internal/parser/parser.go b/internal/parser/parser.go index d120cf80..e591a692 100644 --- a/internal/parser/parser.go +++ b/internal/parser/parser.go @@ -117,13 +117,17 @@ func parseRule(content []byte, node *yaml.Node, offset int) (rule Rule, _ bool, var key *yaml.Node unknownKeys := []*yaml.Node{} + var comments []string + for i, part := range unpackNodes(node) { - if i == 0 && node.HeadComment != "" { + if i == 0 && node.HeadComment != "" && part.HeadComment == "" { part.HeadComment = node.HeadComment } - if i == len(node.Content)-1 && node.FootComment != "" { + if i == len(node.Content)-1 && node.FootComment != "" && part.HeadComment == "" { part.FootComment = node.FootComment } + comments = append(comments, mergeComments(part)...) + if i%2 == 0 { key = part } else { @@ -229,23 +233,29 @@ func parseRule(content []byte, node *yaml.Node, offset int) (rule Rule, _ bool, } if recordPart != nil && exprPart != nil { - rule = Rule{RecordingRule: &RecordingRule{ - Record: *recordPart, - Expr: *exprPart, - Labels: labelsPart, - }} + rule = Rule{ + RecordingRule: &RecordingRule{ + Record: *recordPart, + Expr: *exprPart, + Labels: labelsPart, + }, + Comments: comments, + } return rule, false, err } if alertPart != nil && exprPart != nil { - rule = Rule{AlertingRule: &AlertingRule{ - Alert: *alertPart, - Expr: *exprPart, - For: forPart, - KeepFiringFor: keepFiringForPart, - Labels: labelsPart, - Annotations: annotationsPart, - }} + rule = Rule{ + AlertingRule: &AlertingRule{ + Alert: *alertPart, + Expr: *exprPart, + For: forPart, + KeepFiringFor: keepFiringForPart, + Labels: labelsPart, + Annotations: annotationsPart, + }, + Comments: comments, + } return rule, false, err } diff --git a/internal/parser/parser_test.go b/internal/parser/parser_test.go index c003c2f5..94cf0033 100644 --- a/internal/parser/parser_test.go +++ b/internal/parser/parser_test.go @@ -252,17 +252,23 @@ func TestParse(t *testing.T) { `), output: []parser.Rule{ { + Comments: []string{ + "# head comment", + "# record comment", + "# expr comment", + "# pre-labels comment", + "# pre-foo comment", + "# post-foo comment", + }, RecordingRule: &parser.RecordingRule{ Record: parser.YamlKeyValue{ Key: &parser.YamlNode{ Position: parser.FilePosition{Lines: []int{3}}, Value: "record", - Comments: []string{"# head comment"}, }, Value: &parser.YamlNode{ Position: parser.FilePosition{Lines: []int{3}}, Value: "foo", - Comments: []string{"# record comment"}, }, }, Expr: parser.PromQLExpr{ @@ -273,7 +279,6 @@ func TestParse(t *testing.T) { Value: &parser.YamlNode{ Position: parser.FilePosition{Lines: []int{4}}, Value: "foo offset 10m", - Comments: []string{"# expr comment"}, }, Query: &parser.PromQLNode{ Expr: "foo offset 10m", @@ -283,14 +288,12 @@ func TestParse(t *testing.T) { Key: &parser.YamlNode{ Position: parser.FilePosition{Lines: []int{6}}, Value: "labels", - Comments: []string{"# pre-labels comment"}, }, Items: []*parser.YamlKeyValue{ { Key: &parser.YamlNode{ Position: parser.FilePosition{Lines: []int{8}}, Value: "foo", - Comments: []string{"# pre-foo comment"}, }, Value: &parser.YamlNode{ Position: parser.FilePosition{Lines: []int{8}}, @@ -301,7 +304,6 @@ func TestParse(t *testing.T) { Key: &parser.YamlNode{ Position: parser.FilePosition{Lines: []int{10}}, Value: "bob", - Comments: []string{"# post-foo comment"}, }, Value: &parser.YamlNode{ Position: parser.FilePosition{Lines: []int{10}}, @@ -1164,6 +1166,7 @@ data: }, }, { + Comments: []string{"# foot comment"}, RecordingRule: &parser.RecordingRule{ Record: parser.YamlKeyValue{ Key: &parser.YamlNode{ @@ -1190,7 +1193,6 @@ data: Key: &parser.YamlNode{ Position: parser.FilePosition{Lines: []int{11}}, Value: "labels", - Comments: []string{"# foot comment"}, }, Items: []*parser.YamlKeyValue{ {