From 0d0c5cfaf1285b72356e49645d37d9e499ada344 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Sat, 24 Dec 2016 10:19:46 +0100 Subject: [PATCH] labels: add string constructor, expose matcher --- labels/labels.go | 14 ++++++++++++++ labels/selector.go | 10 +++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/labels/labels.go b/labels/labels.go index ef2bd2fd3..001f11247 100644 --- a/labels/labels.go +++ b/labels/labels.go @@ -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 +} diff --git a/labels/selector.go b/labels/selector.go index c7c1182f7..19c99a24f 100644 --- a/labels/selector.go +++ b/labels/selector.go @@ -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 { -- GitLab