parse: fix parsing for label values with commas (#1395)
Signed-off-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
parent
a9e584be75
commit
49717d91b0
|
@ -40,13 +40,31 @@ func Matchers(s string) ([]*labels.Matcher, error) {
|
|||
s = s[:len(s)-1]
|
||||
}
|
||||
|
||||
for _, toParse := range strings.Split(s, ",") {
|
||||
m, err := Matcher(toParse)
|
||||
var insideQuotes bool
|
||||
var token string
|
||||
var tokens []string
|
||||
for _, r := range s {
|
||||
if !insideQuotes && r == ',' {
|
||||
tokens = append(tokens, token)
|
||||
token = ""
|
||||
continue
|
||||
}
|
||||
token += string(r)
|
||||
if r == '"' {
|
||||
insideQuotes = !insideQuotes
|
||||
}
|
||||
}
|
||||
if token != "" {
|
||||
tokens = append(tokens, token)
|
||||
}
|
||||
for _, token := range tokens {
|
||||
m, err := Matcher(token)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
matchers = append(matchers, m)
|
||||
}
|
||||
|
||||
return matchers, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -24,6 +24,7 @@ func TestMatchers(t *testing.T) {
|
|||
testCases := []struct {
|
||||
input string
|
||||
want []*labels.Matcher
|
||||
err error
|
||||
}{
|
||||
{
|
||||
input: `{foo="bar"}`,
|
||||
|
@ -122,15 +123,24 @@ func TestMatchers(t *testing.T) {
|
|||
return append(ms, m)
|
||||
}(),
|
||||
},
|
||||
{
|
||||
input: `{foo="bar,quux", job="job1"}`,
|
||||
want: func() []*labels.Matcher {
|
||||
ms := []*labels.Matcher{}
|
||||
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "bar,quux")
|
||||
m2, _ := labels.NewMatcher(labels.MatchEqual, "job", "job1")
|
||||
return append(ms, m, m2)
|
||||
}(),
|
||||
},
|
||||
}
|
||||
|
||||
for i, tc := range testCases {
|
||||
got, err := Matchers(tc.input)
|
||||
if err != nil {
|
||||
t.Fatalf("error (i=%d): %v", i, err)
|
||||
if tc.err != err {
|
||||
t.Fatalf("error not equal (i=%d):\ngot %v\nwant %v", i, err, tc.err)
|
||||
}
|
||||
if !reflect.DeepEqual(got, tc.want) {
|
||||
t.Fatalf("error not equal (i=%d):\ngot %v\nwant %v", i, got, tc.want)
|
||||
t.Fatalf("labels not equal (i=%d):\ngot %v\nwant %v", i, got, tc.want)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue