1// Copyright 2017 The Prometheus Authors
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14package labels
15
16import (
17	"fmt"
18	"regexp"
19)
20
21// Selector holds constraints for matching against a label set.
22type Selector []Matcher
23
24// Matches returns whether the labels satisfy all matchers.
25func (s Selector) Matches(labels Labels) bool {
26	for _, m := range s {
27		if v := labels.Get(m.Name()); !m.Matches(v) {
28			return false
29		}
30	}
31	return true
32}
33
34// Matcher specifies a constraint for the value of a label.
35type Matcher interface {
36	// Name returns the label name the matcher should apply to.
37	Name() string
38	// Matches checks whether a value fulfills the constraints.
39	Matches(v string) bool
40	// String returns a human readable matcher.
41	String() string
42}
43
44// EqualMatcher matches on equality.
45type EqualMatcher struct {
46	name, value string
47}
48
49// Name implements Matcher interface.
50func (m EqualMatcher) Name() string { return m.name }
51
52// Matches implements Matcher interface.
53func (m EqualMatcher) Matches(v string) bool { return v == m.value }
54
55// String implements Matcher interface.
56func (m EqualMatcher) String() string { return fmt.Sprintf("%s=%q", m.name, m.value) }
57
58// Value returns the matched value.
59func (m EqualMatcher) Value() string { return m.value }
60
61// NewEqualMatcher returns a new matcher matching an exact label value.
62func NewEqualMatcher(name, value string) Matcher {
63	return &EqualMatcher{name: name, value: value}
64}
65
66type regexpMatcher struct {
67	name string
68	re   *regexp.Regexp
69}
70
71func (m regexpMatcher) Name() string          { return m.name }
72func (m regexpMatcher) Matches(v string) bool { return m.re.MatchString(v) }
73func (m regexpMatcher) String() string        { return fmt.Sprintf("%s=~%q", m.name, m.re.String()) }
74
75// NewRegexpMatcher returns a new matcher verifying that a value matches
76// the regular expression pattern.
77func NewRegexpMatcher(name, pattern string) (Matcher, error) {
78	re, err := regexp.Compile(pattern)
79	if err != nil {
80		return nil, err
81	}
82	return &regexpMatcher{name: name, re: re}, nil
83}
84
85// NewMustRegexpMatcher returns a new matcher verifying that a value matches
86// the regular expression pattern. Will panic if the pattern is not a valid
87// regular expression.
88func NewMustRegexpMatcher(name, pattern string) Matcher {
89	re, err := regexp.Compile(pattern)
90	if err != nil {
91		panic(err)
92	}
93	return &regexpMatcher{name: name, re: re}
94
95}
96
97// NotMatcher inverts the matching result for a matcher.
98type NotMatcher struct {
99	Matcher
100}
101
102func (m NotMatcher) Matches(v string) bool { return !m.Matcher.Matches(v) }
103func (m NotMatcher) String() string        { return fmt.Sprintf("not(%s)", m.Matcher.String()) }
104
105// Not inverts the matcher's matching result.
106func Not(m Matcher) Matcher {
107	return &NotMatcher{m}
108}
109