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