| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package selector |
| 4 | |
| 5 | import ( |
| 6 | "github.com/prometheus/prometheus/model/labels" |
| 7 | ) |
| 8 | |
| 9 | type ( |
| 10 | trueSelector struct{} |
| 11 | falseSelector struct{} |
| 12 | negSelector struct{ s Selector } |
| 13 | andSelector struct{ lhs, rhs Selector } |
| 14 | orSelector struct{ lhs, rhs Selector } |
| 15 | ) |
| 16 | |
| 17 | func (trueSelector) Matches(_ labels.Labels) bool { return true } |
| 18 | func (falseSelector) Matches(_ labels.Labels) bool { return false } |
| 19 | func (s negSelector) Matches(lbs labels.Labels) bool { return !s.s.Matches(lbs) } |
| 20 | func (s andSelector) Matches(lbs labels.Labels) bool { return s.lhs.Matches(lbs) && s.rhs.Matches(lbs) } |
| 21 | func (s orSelector) Matches(lbs labels.Labels) bool { return s.lhs.Matches(lbs) || s.rhs.Matches(lbs) } |
| 22 | |
| 23 | // True returns a selector which always returns true |
| 24 | func True() Selector { |
| 25 | return trueSelector{} |
| 26 | } |
| 27 | |
| 28 | // And returns a selector which returns true only if all of it's sub-selectors return true |
| 29 | func And(lhs, rhs Selector, others ...Selector) Selector { |
| 30 | s := andSelector{lhs: lhs, rhs: rhs} |
| 31 | if len(others) == 0 { |
| 32 | return s |
| 33 | } |
| 34 | return And(s, others[0], others[1:]...) |
| 35 | } |
| 36 | |
| 37 | // Or returns a selector which returns true if any of it's sub-selectors return true |
| 38 | func Or(lhs, rhs Selector, others ...Selector) Selector { |
| 39 | s := orSelector{lhs: lhs, rhs: rhs} |
| 40 | if len(others) == 0 { |
| 41 | return s |
| 42 | } |
| 43 | return Or(s, others[0], others[1:]...) |
| 44 | } |
| 45 | |
| 46 | // Not returns a selector which returns the negation of the sub-selector's result |
| 47 | func Not(s Selector) Selector { |
| 48 | return negSelector{s} |
| 49 | } |