labels: add string constructor, expose matcher

This commit is contained in:
Fabian Reinartz 2016-12-24 10:19:46 +01:00
parent 787199a88e
commit 0d0c5cfaf1
2 changed files with 19 additions and 5 deletions

View File

@ -87,3 +87,17 @@ func FromMap(m map[string]string) Labels {
}
return New(l...)
}
// FromStrings creates new labels from pairs of strings.
func FromStrings(ss ...string) Labels {
if len(ss)%2 != 0 {
panic("invalid number of strings")
}
var res Labels
for i := 0; i < len(ss); i += 2 {
res = append(res, Label{Name: ss[i], Value: ss[i+1]})
}
sort.Sort(res)
return res
}

View File

@ -23,16 +23,16 @@ type Matcher interface {
Matches(v string) bool
}
type equalMatcher struct {
name, value string
type EqualMatcher struct {
LabelName, Value string
}
func (m *equalMatcher) Name() string { return m.name }
func (m *equalMatcher) Matches(v string) bool { return v == m.value }
func (m *EqualMatcher) Name() string { return m.LabelName }
func (m *EqualMatcher) Matches(v string) bool { return v == m.Value }
// NewEqualMatcher returns a new matcher matching an exact label value.
func NewEqualMatcher(name, value string) Matcher {
return &equalMatcher{name: name, value: value}
return &EqualMatcher{LabelName: name, Value: value}
}
type regexpMatcher struct {