fix(pkg): range parser supports single numbers

These are `[0:X]` ranges, according to the Nagios doc.
This commit is contained in:
Emmanuel BENOîT 2024-07-20 18:17:53 +02:00
parent 7c8c624b17
commit 6d44df6216
Signed by: Emmanuel BENOîT
SSH key fingerprint: SHA256:l7PFUUF5TCDsvYeQC9OnTNz08dFY7Fvf4Hv3neIqYpg
2 changed files with 12 additions and 7 deletions

View file

@ -116,7 +116,7 @@ func ParseRange(input string) (*Range, error) {
case rpsInit: case rpsInit:
if curRune == '@' { if curRune == '@' {
parsed.inside = true parsed.inside = true
index += 1 index++
} }
state = rpsExpectStart state = rpsExpectStart
@ -132,7 +132,7 @@ func ParseRange(input string) (*Range, error) {
startOfStart = index startOfStart = index
state = rpsInStart state = rpsInStart
} }
index += 1 index++
case rpsInStart: case rpsInStart:
switch curRune { switch curRune {
@ -146,13 +146,13 @@ func ParseRange(input string) (*Range, error) {
default: default:
strBuilder.WriteRune(curRune) strBuilder.WriteRune(curRune)
} }
index += 1 index++
case rpsExpectColon: case rpsExpectColon:
switch curRune { switch curRune {
case ':': case ':':
state = rpsExpectEnd state = rpsExpectEnd
index += 1 index++
default: default:
return nil, parseError(input, index, "expected ':'") return nil, parseError(input, index, "expected ':'")
} }
@ -163,10 +163,15 @@ func ParseRange(input string) (*Range, error) {
case rpsInEnd: case rpsInEnd:
strBuilder.WriteRune(curRune) strBuilder.WriteRune(curRune)
index += 1 index++
} }
} }
if state == rpsInStart {
// The range was a single value, so that's the upper bound.
parsed.start = "0"
state = rpsInEnd
}
if state != rpsInEnd { if state != rpsInEnd {
return nil, parseError(input, index, "unexpected end of input") return nil, parseError(input, index, "unexpected end of input")
} }

View file

@ -81,12 +81,14 @@ func TestRangeParserOk(t *testing.T) {
{in: ":123", out: Range{start: "0", end: "123"}}, {in: ":123", out: Range{start: "0", end: "123"}},
{in: "0:123", out: Range{start: "0", end: "123"}}, {in: "0:123", out: Range{start: "0", end: "123"}},
{in: "~:123", out: Range{start: "", end: "123"}}, {in: "~:123", out: Range{start: "", end: "123"}},
{in: "1", out: Range{start: "0", end: "1"}},
{in: "@:0", out: Range{start: "0", end: "0", inside: true}}, {in: "@:0", out: Range{start: "0", end: "0", inside: true}},
{in: "@0:0", out: Range{start: "0", end: "0", inside: true}}, {in: "@0:0", out: Range{start: "0", end: "0", inside: true}},
{in: "@~:0", out: Range{start: "", end: "0", inside: true}}, {in: "@~:0", out: Range{start: "", end: "0", inside: true}},
{in: "@:123", out: Range{start: "0", end: "123", inside: true}}, {in: "@:123", out: Range{start: "0", end: "123", inside: true}},
{in: "@0:123", out: Range{start: "0", end: "123", inside: true}}, {in: "@0:123", out: Range{start: "0", end: "123", inside: true}},
{in: "@~:123", out: Range{start: "", end: "123", inside: true}}, {in: "@~:123", out: Range{start: "", end: "123", inside: true}},
{in: "@1", out: Range{start: "0", end: "1", inside: true}},
} }
for _, test := range tests { for _, test := range tests {
@ -106,12 +108,10 @@ func TestRangeParserError(t *testing.T) {
{in: ":", errPos: 1}, {in: ":", errPos: 1},
{in: "x:1", errPos: 0}, {in: "x:1", errPos: 0},
{in: ":~", errPos: 1}, {in: ":~", errPos: 1},
{in: "1", errPos: 1},
{in: "@", errPos: 1}, {in: "@", errPos: 1},
{in: "@:", errPos: 2}, {in: "@:", errPos: 2},
{in: "@x:1", errPos: 1}, {in: "@x:1", errPos: 1},
{in: "@:~", errPos: 2}, {in: "@:~", errPos: 2},
{in: "@1", errPos: 2},
} }
for _, test := range tests { for _, test := range tests {