summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/pkg/prometheus/selector/logical.go
blob: 1556d17156c169d1e8fbadb0a249876843dc46f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// SPDX-License-Identifier: GPL-3.0-or-later

package selector

import (
	"github.com/prometheus/prometheus/model/labels"
)

type (
	trueSelector  struct{}
	falseSelector struct{}
	negSelector   struct{ s Selector }
	andSelector   struct{ lhs, rhs Selector }
	orSelector    struct{ lhs, rhs Selector }
)

func (trueSelector) Matches(_ labels.Labels) bool    { return true }
func (falseSelector) Matches(_ labels.Labels) bool   { return false }
func (s negSelector) Matches(lbs labels.Labels) bool { return !s.s.Matches(lbs) }
func (s andSelector) Matches(lbs labels.Labels) bool { return s.lhs.Matches(lbs) && s.rhs.Matches(lbs) }
func (s orSelector) Matches(lbs labels.Labels) bool  { return s.lhs.Matches(lbs) || s.rhs.Matches(lbs) }

// True returns a selector which always returns true
func True() Selector {
	return trueSelector{}
}

// And returns a selector which returns true only if all of it's sub-selectors return true
func And(lhs, rhs Selector, others ...Selector) Selector {
	s := andSelector{lhs: lhs, rhs: rhs}
	if len(others) == 0 {
		return s
	}
	return And(s, others[0], others[1:]...)
}

// Or returns a selector which returns true if any of it's sub-selectors return true
func Or(lhs, rhs Selector, others ...Selector) Selector {
	s := orSelector{lhs: lhs, rhs: rhs}
	if len(others) == 0 {
		return s
	}
	return Or(s, others[0], others[1:]...)
}

// Not returns a selector which returns the negation of the sub-selector's result
func Not(s Selector) Selector {
	return negSelector{s}
}