Fix single quote parsing, add tests

This commit is contained in:
Fabian Reinartz 2015-05-08 16:43:02 +02:00
parent b404ad5c91
commit 8707c54508
3 changed files with 36 additions and 16 deletions

View File

@ -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{

View File

@ -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
}

View File

@ -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"},
},
},