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: `台北`, input: `台北`,
fail: true, fail: true,
}, {
input: `{foo='bar'}`,
expected: []item{
{itemLeftBrace, 0, `{`},
{itemIdentifier, 1, `foo`},
{itemEQL, 4, `=`},
{itemString, 5, `'bar'`},
{itemRightBrace, 10, `}`},
},
}, { }, {
input: `{foo="bar"}`, input: `{foo="bar"}`,
expected: []item{ expected: []item{
@ -256,6 +265,15 @@ var tests = []struct {
{itemString, 5, `"bar"`}, {itemString, 5, `"bar"`},
{itemRightBrace, 10, `}`}, {itemRightBrace, 10, `}`},
}, },
}, {
input: `{foo="bar\"bar"}`,
expected: []item{
{itemLeftBrace, 0, `{`},
{itemIdentifier, 1, `foo`},
{itemEQL, 4, `=`},
{itemString, 5, `"bar\"bar"`},
{itemRightBrace, 15, `}`},
},
}, { }, {
input: `{NaN != "bar" }`, input: `{NaN != "bar" }`,
expected: []item{ expected: []item{

View file

@ -257,16 +257,10 @@ func (p *parser) alertStmt() *AlertStmt {
} }
p.expect(itemSummary, ctx) p.expect(itemSummary, ctx)
sum, err := strconv.Unquote(p.expect(itemString, ctx).val) sum := trimOne(p.expect(itemString, ctx).val)
if err != nil {
p.error(err)
}
p.expect(itemDescription, ctx) p.expect(itemDescription, ctx)
desc, err := strconv.Unquote(p.expect(itemString, ctx).val) desc := trimOne(p.expect(itemString, ctx).val)
if err != nil {
p.error(err)
}
return &AlertStmt{ return &AlertStmt{
Name: name.val, 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) p.errorf("operator must be one of %q, is %q", operators, op)
} }
val, err := strconv.Unquote(p.expect(itemString, ctx).val) val := trimOne(p.expect(itemString, ctx).val)
if err != nil {
p.error(err)
}
// Map the item to the respective match type. // Map the item to the respective match type.
var matchType metric.MatchType var matchType metric.MatchType
@ -887,3 +878,14 @@ func parseDuration(ds string) (time.Duration, error) {
} }
return dur, nil 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{ expected: &VectorSelector{
Name: "foo:bar", Name: "foo:bar",
Offset: 0, Offset: 0,
LabelMatchers: metric.LabelMatchers{ 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"}, {Type: metric.Equal, Name: clientmodel.MetricNameLabel, Value: "foo:bar"},
}, },
}, },
}, { }, {
input: `foo{NaN='b'}`, input: `foo{NaN='bc'}`,
expected: &VectorSelector{ expected: &VectorSelector{
Name: "foo", Name: "foo",
Offset: 0, Offset: 0,
LabelMatchers: metric.LabelMatchers{ 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"}, {Type: metric.Equal, Name: clientmodel.MetricNameLabel, Value: "foo"},
}, },
}, },