Skip to content

Commit 03e20b9

Browse files
authored
Merge pull request #1407 from cloudflare/issue-1405
Fix relaxed parser
2 parents 0bf8232 + 5d6f2e9 commit 03e20b9

File tree

3 files changed

+111
-7
lines changed

3 files changed

+111
-7
lines changed

docs/changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## v0.73.1
4+
5+
### Fixed
6+
7+
- Fixed `relaxed` parsing mode not finding all rules in some files - [#1405](https://github.com/cloudflare/pint/issues/1405).
8+
39
## v0.73.0
410

511
### Changed

internal/parser/parser.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,20 @@ To allow for multi-document YAML files set parser->relaxed option in pint config
101101
func (p *Parser) parseNode(node, parent *yaml.Node, group *Group, offsetLine, offsetColumn int, contentLines []string) (groups []Group) {
102102
switch node.Kind { // nolint: exhaustive
103103
case yaml.SequenceNode:
104-
if parent != nil && nodeValue(parent) == "groups" {
105-
for _, n := range unpackNodes(node) {
106-
if g, rulesMap, ok := tryParseGroup(n, offsetLine, offsetColumn, contentLines); ok {
107-
groups = append(groups, p.parseNode(rulesMap.val, rulesMap.key, &g, offsetLine, offsetColumn, contentLines)...)
108-
}
104+
// First check for group list
105+
for _, n := range unpackNodes(node) {
106+
if g, rulesMap, ok := tryParseGroup(n, offsetLine, offsetColumn, contentLines); ok {
107+
groups = append(groups, p.parseNode(rulesMap.val, rulesMap.key, &g, offsetLine, offsetColumn, contentLines)...)
109108
}
109+
}
110+
if len(groups) > 0 {
110111
return groups
111112
}
112-
// Try parsing rules.
113+
113114
if group == nil {
114115
group = &Group{} // nolint: exhaustruct
115116
}
117+
// Try parsing rules.
116118
for _, n := range unpackNodes(node) {
117119
if ret, isEmpty := parseRule(n, offsetLine, offsetColumn, contentLines); !isEmpty {
118120
group.Rules = append(group.Rules, ret)
@@ -122,7 +124,9 @@ func (p *Parser) parseNode(node, parent *yaml.Node, group *Group, offsetLine, of
122124
if len(group.Rules) > 0 || (parent != nil && nodeValue(parent) == "rules" && len(groups) == 0 && group != nil) {
123125
groups = append(groups, *group)
124126
}
125-
return groups
127+
if len(groups) > 0 {
128+
return groups
129+
}
126130
case yaml.MappingNode:
127131
for _, field := range mappingNodes(node) {
128132
groups = append(groups, p.parseNode(field.val, field.key, group, offsetLine, offsetColumn, contentLines)...)

internal/parser/parser_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5037,6 +5037,100 @@ groups:
50375037
},
50385038
},
50395039
},
5040+
{
5041+
input: []byte(`
5042+
- name: xxx
5043+
interval: 3m
5044+
query_offset: 1s
5045+
limit: 5
5046+
labels:
5047+
foo: bar
5048+
rules:
5049+
- record: up:count
5050+
expr: count(up)
5051+
`),
5052+
output: parser.File{
5053+
IsRelaxed: true,
5054+
Groups: []parser.Group{
5055+
{
5056+
Name: "xxx",
5057+
Interval: time.Minute * 3,
5058+
QueryOffset: time.Second,
5059+
Limit: 5,
5060+
Labels: &parser.YamlMap{
5061+
Key: &parser.YamlNode{
5062+
Value: "labels",
5063+
},
5064+
Items: []*parser.YamlKeyValue{
5065+
{
5066+
Key: &parser.YamlNode{
5067+
Value: "foo",
5068+
},
5069+
Value: &parser.YamlNode{
5070+
Value: "bar",
5071+
},
5072+
},
5073+
},
5074+
},
5075+
Rules: []parser.Rule{
5076+
{
5077+
Lines: diags.LineRange{First: 9, Last: 10},
5078+
RecordingRule: &parser.RecordingRule{
5079+
Record: parser.YamlNode{
5080+
Value: "up:count",
5081+
Pos: diags.PositionRanges{{Line: 9, FirstColumn: 13, LastColumn: 20}},
5082+
},
5083+
Expr: parser.PromQLExpr{
5084+
Value: &parser.YamlNode{
5085+
Value: "count(up)",
5086+
Pos: diags.PositionRanges{{Line: 10, FirstColumn: 11, LastColumn: 19}},
5087+
},
5088+
},
5089+
},
5090+
},
5091+
},
5092+
},
5093+
},
5094+
},
5095+
},
5096+
{
5097+
input: []byte(`
5098+
5099+
- interval: 3m
5100+
query_offset: 1s
5101+
limit: 5
5102+
labels:
5103+
foo: bar
5104+
rules:
5105+
- record: up:count
5106+
expr: count(up)
5107+
`),
5108+
output: parser.File{
5109+
IsRelaxed: true,
5110+
Groups: []parser.Group{
5111+
{
5112+
Name: "",
5113+
Rules: []parser.Rule{
5114+
{
5115+
Lines: diags.LineRange{First: 9, Last: 10},
5116+
RecordingRule: &parser.RecordingRule{
5117+
Record: parser.YamlNode{
5118+
Value: "up:count",
5119+
Pos: diags.PositionRanges{{Line: 9, FirstColumn: 13, LastColumn: 20}},
5120+
},
5121+
Expr: parser.PromQLExpr{
5122+
Value: &parser.YamlNode{
5123+
Value: "count(up)",
5124+
Pos: diags.PositionRanges{{Line: 10, FirstColumn: 11, LastColumn: 19}},
5125+
},
5126+
},
5127+
},
5128+
},
5129+
},
5130+
},
5131+
},
5132+
},
5133+
},
50405134
}
50415135

50425136
alwaysEqual := cmp.Comparer(func(_, _ any) bool { return true })

0 commit comments

Comments
 (0)