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]
|
s = s[:len(s)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, toParse := range strings.Split(s, ",") {
|
var insideQuotes bool
|
||||||
m, err := Matcher(toParse)
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
matchers = append(matchers, m)
|
matchers = append(matchers, m)
|
||||||
}
|
}
|
||||||
|
|
||||||
return matchers, nil
|
return matchers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,7 @@ func TestMatchers(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
input string
|
input string
|
||||||
want []*labels.Matcher
|
want []*labels.Matcher
|
||||||
|
err error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
input: `{foo="bar"}`,
|
input: `{foo="bar"}`,
|
||||||
|
@ -122,15 +123,24 @@ func TestMatchers(t *testing.T) {
|
||||||
return append(ms, m)
|
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 {
|
for i, tc := range testCases {
|
||||||
got, err := Matchers(tc.input)
|
got, err := Matchers(tc.input)
|
||||||
if err != nil {
|
if tc.err != err {
|
||||||
t.Fatalf("error (i=%d): %v", i, err)
|
t.Fatalf("error not equal (i=%d):\ngot %v\nwant %v", i, err, tc.err)
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(got, tc.want) {
|
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