From 8707c54508a85de382359672d38e9000aecf589a Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Fri, 8 May 2015 16:43:02 +0200 Subject: [PATCH] Fix single quote parsing, add tests --- promql/lex_test.go | 18 ++++++++++++++++++ promql/parse.go | 26 ++++++++++++++------------ promql/parse_test.go | 8 ++++---- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/promql/lex_test.go b/promql/lex_test.go index bcdec6242..71dd9a988 100644 --- a/promql/lex_test.go +++ b/promql/lex_test.go @@ -247,6 +247,15 @@ var tests = []struct { { input: `台北`, fail: true, + }, { + input: `{foo='bar'}`, + expected: []item{ + {itemLeftBrace, 0, `{`}, + {itemIdentifier, 1, `foo`}, + {itemEQL, 4, `=`}, + {itemString, 5, `'bar'`}, + {itemRightBrace, 10, `}`}, + }, }, { input: `{foo="bar"}`, expected: []item{ @@ -256,6 +265,15 @@ var tests = []struct { {itemString, 5, `"bar"`}, {itemRightBrace, 10, `}`}, }, + }, { + input: `{foo="bar\"bar"}`, + expected: []item{ + {itemLeftBrace, 0, `{`}, + {itemIdentifier, 1, `foo`}, + {itemEQL, 4, `=`}, + {itemString, 5, `"bar\"bar"`}, + {itemRightBrace, 15, `}`}, + }, }, { input: `{NaN != "bar" }`, expected: []item{ diff --git a/promql/parse.go b/promql/parse.go index 8313cee2a..58e5d8f3d 100644 --- a/promql/parse.go +++ b/promql/parse.go @@ -257,16 +257,10 @@ func (p *parser) alertStmt() *AlertStmt { } p.expect(itemSummary, ctx) - sum, err := strconv.Unquote(p.expect(itemString, ctx).val) - if err != nil { - p.error(err) - } + sum := trimOne(p.expect(itemString, ctx).val) p.expect(itemDescription, ctx) - desc, err := strconv.Unquote(p.expect(itemString, ctx).val) - if err != nil { - p.error(err) - } + desc := trimOne(p.expect(itemString, ctx).val) return &AlertStmt{ Name: name.val, @@ -663,10 +657,7 @@ func (p *parser) labelMatchers(operators ...itemType) metric.LabelMatchers { p.errorf("operator must be one of %q, is %q", operators, op) } - val, err := strconv.Unquote(p.expect(itemString, ctx).val) - if err != nil { - p.error(err) - } + val := trimOne(p.expect(itemString, ctx).val) // Map the item to the respective match type. var matchType metric.MatchType @@ -887,3 +878,14 @@ func parseDuration(ds string) (time.Duration, error) { } return dur, nil } + +// trimOne removes the first and last character from a string. +func trimOne(s string) string { + if len(s) > 0 { + s = s[1:] + } + if len(s) > 0 { + s = s[:len(s)-1] + } + return s +} diff --git a/promql/parse_test.go b/promql/parse_test.go index 8cb1da1d8..ab2ca5c25 100644 --- a/promql/parse_test.go +++ b/promql/parse_test.go @@ -497,22 +497,22 @@ var testExpr = []struct { }, }, }, { - input: `foo:bar{a="b"}`, + input: `foo:bar{a="bc"}`, expected: &VectorSelector{ Name: "foo:bar", Offset: 0, LabelMatchers: metric.LabelMatchers{ - {Type: metric.Equal, Name: "a", Value: "b"}, + {Type: metric.Equal, Name: "a", Value: "bc"}, {Type: metric.Equal, Name: clientmodel.MetricNameLabel, Value: "foo:bar"}, }, }, }, { - input: `foo{NaN='b'}`, + input: `foo{NaN='bc'}`, expected: &VectorSelector{ Name: "foo", Offset: 0, LabelMatchers: metric.LabelMatchers{ - {Type: metric.Equal, Name: "NaN", Value: "b"}, + {Type: metric.Equal, Name: "NaN", Value: "bc"}, {Type: metric.Equal, Name: clientmodel.MetricNameLabel, Value: "foo"}, }, },