master
go 54 lines 1.49 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package selectorcore
4
5 type (
6 trueSelector struct{}
7 falseSelector struct{}
8 negSelector struct{ s Selector }
9 andSelector struct{ lhs, rhs Selector }
10 orSelector struct{ lhs, rhs Selector }
11 )
12
13 func (trueSelector) Matches(_ string, _ Labels) bool { return true }
14 func (falseSelector) Matches(_ string, _ Labels) bool { return false }
15
16 func (s negSelector) Matches(metricName string, labels Labels) bool {
17 return !s.s.Matches(metricName, labels)
18 }
19
20 func (s andSelector) Matches(metricName string, labels Labels) bool {
21 return s.lhs.Matches(metricName, labels) && s.rhs.Matches(metricName, labels)
22 }
23
24 func (s orSelector) Matches(metricName string, labels Labels) bool {
25 return s.lhs.Matches(metricName, labels) || s.rhs.Matches(metricName, labels)
26 }
27
28 // True returns a selector which always matches.
29 func True() Selector {
30 return trueSelector{}
31 }
32
33 // And returns a selector that matches only when all sub-selectors match.
34 func And(lhs, rhs Selector, others ...Selector) Selector {
35 s := andSelector{lhs: lhs, rhs: rhs}
36 if len(others) == 0 {
37 return s
38 }
39 return And(s, others[0], others[1:]...)
40 }
41
42 // Or returns a selector that matches when any sub-selector matches.
43 func Or(lhs, rhs Selector, others ...Selector) Selector {
44 s := orSelector{lhs: lhs, rhs: rhs}
45 if len(others) == 0 {
46 return s
47 }
48 return Or(s, others[0], others[1:]...)
49 }
50
51 // Not returns a selector that negates the wrapped selector.
52 func Not(s Selector) Selector {
53 return negSelector{s: s}
54 }