diff --git a/docs/changelog.md b/docs/changelog.md index 52394e50..9d10be5b 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,5 +1,12 @@ # Changelog +## v0.13.2 + +### Fixed + +- `yaml/parse` was using incorrect line numbers for errors caused by duplicated + YAML keys. + ## v0.13.1 ### Fixed diff --git a/internal/parser/parser.go b/internal/parser/parser.go index 1b2d03de..1777609e 100644 --- a/internal/parser/parser.go +++ b/internal/parser/parser.go @@ -107,32 +107,32 @@ func parseRule(content []byte, node *yaml.Node) (rule Rule, isEmpty bool, err er switch key.Value { case recordKey: if recordPart != nil { - return duplicatedKeyError(node.Line, recordKey, nil) + return duplicatedKeyError(part.Line, recordKey, nil) } recordPart = newYamlKeyValue(key, part) case alertKey: if alertPart != nil { - return duplicatedKeyError(node.Line, alertKey, nil) + return duplicatedKeyError(part.Line, alertKey, nil) } alertPart = newYamlKeyValue(key, part) case exprKey: if exprPart != nil { - return duplicatedKeyError(node.Line, exprKey, nil) + return duplicatedKeyError(part.Line, exprKey, nil) } exprPart = newPromQLExpr(key, part) case forKey: if forPart != nil { - return duplicatedKeyError(node.Line, forKey, nil) + return duplicatedKeyError(part.Line, forKey, nil) } forPart = newYamlKeyValue(key, part) case labelsKey: if labelsPart != nil { - return duplicatedKeyError(node.Line, labelsKey, nil) + return duplicatedKeyError(part.Line, labelsKey, nil) } labelsPart = newYamlMap(key, part) case annotationsKey: if annotationsPart != nil { - return duplicatedKeyError(node.Line, annotationsKey, nil) + return duplicatedKeyError(part.Line, annotationsKey, nil) } annotationsPart = newYamlMap(key, part) default: diff --git a/internal/parser/parser_test.go b/internal/parser/parser_test.go index bb1fba13..f0212bd6 100644 --- a/internal/parser/parser_test.go +++ b/internal/parser/parser_test.go @@ -78,7 +78,7 @@ func TestParse(t *testing.T) { expr: bar `), output: []parser.Rule{ - {Error: parser.ParseError{Err: fmt.Errorf("duplicated expr key"), Line: 2}}, + {Error: parser.ParseError{Err: fmt.Errorf("duplicated expr key"), Line: 4}}, }, }, { @@ -88,7 +88,7 @@ func TestParse(t *testing.T) { record: bar `), output: []parser.Rule{ - {Error: parser.ParseError{Err: fmt.Errorf("duplicated record key"), Line: 2}}, + {Error: parser.ParseError{Err: fmt.Errorf("duplicated record key"), Line: 4}}, }, }, { @@ -98,7 +98,7 @@ func TestParse(t *testing.T) { expr: bar `), output: []parser.Rule{ - {Error: parser.ParseError{Err: fmt.Errorf("duplicated alert key"), Line: 2}}, + {Error: parser.ParseError{Err: fmt.Errorf("duplicated alert key"), Line: 3}}, }, }, { @@ -109,7 +109,7 @@ func TestParse(t *testing.T) { for: 1m `), output: []parser.Rule{ - {Error: parser.ParseError{Err: fmt.Errorf("duplicated for key"), Line: 2}}, + {Error: parser.ParseError{Err: fmt.Errorf("duplicated for key"), Line: 5}}, }, }, { @@ -120,7 +120,7 @@ func TestParse(t *testing.T) { labels: {} `), output: []parser.Rule{ - {Error: parser.ParseError{Err: fmt.Errorf("duplicated labels key"), Line: 2}}, + {Error: parser.ParseError{Err: fmt.Errorf("duplicated labels key"), Line: 5}}, }, }, { @@ -131,7 +131,7 @@ func TestParse(t *testing.T) { labels: {} `), output: []parser.Rule{ - {Error: parser.ParseError{Err: fmt.Errorf("duplicated labels key"), Line: 2}}, + {Error: parser.ParseError{Err: fmt.Errorf("duplicated labels key"), Line: 5}}, }, }, { @@ -142,7 +142,7 @@ func TestParse(t *testing.T) { annotations: {} `), output: []parser.Rule{ - {Error: parser.ParseError{Err: fmt.Errorf("duplicated annotations key"), Line: 2}}, + {Error: parser.ParseError{Err: fmt.Errorf("duplicated annotations key"), Line: 5}}, }, }, {