Skip to content

Commit

Permalink
Merge pull request cloudflare#168 from cloudflare/prase-line
Browse files Browse the repository at this point in the history
Use correct line numbers in yaml/parse
  • Loading branch information
prymitive authored Feb 21, 2022
2 parents 95ce63e + 58ae7de commit 17ebe4a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
7 changes: 7 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 6 additions & 6 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 7 additions & 7 deletions internal/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}},
},
},
{
Expand All @@ -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}},
},
},
{
Expand All @@ -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}},
},
},
{
Expand All @@ -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}},
},
},
{
Expand All @@ -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}},
},
},
{
Expand All @@ -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}},
},
},
{
Expand All @@ -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}},
},
},
{
Expand Down

0 comments on commit 17ebe4a

Please sign in to comment.