File name should have changed during query->selector rename.
This commit is contained in:
123
pkg/labels/selector.go
Normal file
123
pkg/labels/selector.go
Normal file
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
Copyright 2014 Google Inc. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package labels
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Represents a selector.
|
||||
type Selector interface {
|
||||
// Returns true if this selector matches the given set of labels.
|
||||
Matches(Labels) bool
|
||||
|
||||
// Prints a human readable version of this selector.
|
||||
String() string
|
||||
}
|
||||
|
||||
// Everything returns a selector that matches all labels.
|
||||
func Everything() Selector {
|
||||
return andTerm{}
|
||||
}
|
||||
|
||||
type hasTerm struct {
|
||||
label, value string
|
||||
}
|
||||
|
||||
func (t *hasTerm) Matches(ls Labels) bool {
|
||||
return ls.Get(t.label) == t.value
|
||||
}
|
||||
|
||||
func (t *hasTerm) String() string {
|
||||
return fmt.Sprintf("%v=%v", t.label, t.value)
|
||||
}
|
||||
|
||||
type notHasTerm struct {
|
||||
label, value string
|
||||
}
|
||||
|
||||
func (t *notHasTerm) Matches(ls Labels) bool {
|
||||
return ls.Get(t.label) != t.value
|
||||
}
|
||||
|
||||
func (t *notHasTerm) String() string {
|
||||
return fmt.Sprintf("%v!=%v", t.label, t.value)
|
||||
}
|
||||
|
||||
type andTerm []Selector
|
||||
|
||||
func (t andTerm) Matches(ls Labels) bool {
|
||||
for _, q := range t {
|
||||
if !q.Matches(ls) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (t andTerm) String() string {
|
||||
var terms []string
|
||||
for _, q := range t {
|
||||
terms = append(terms, q.String())
|
||||
}
|
||||
return strings.Join(terms, ",")
|
||||
}
|
||||
|
||||
func try(selectorPiece, op string) (lhs, rhs string, ok bool) {
|
||||
pieces := strings.Split(selectorPiece, op)
|
||||
if len(pieces) == 2 {
|
||||
return pieces[0], pieces[1], true
|
||||
}
|
||||
return "", "", false
|
||||
}
|
||||
|
||||
// Given a Set, return a Selector which will match exactly that Set.
|
||||
func SelectorFromSet(ls Set) Selector {
|
||||
var items []Selector
|
||||
for label, value := range ls {
|
||||
items = append(items, &hasTerm{label: label, value: value})
|
||||
}
|
||||
if len(items) == 1 {
|
||||
return items[0]
|
||||
}
|
||||
return andTerm(items)
|
||||
}
|
||||
|
||||
// Takes a string repsenting a selector and returns an object suitable for matching, or an error.
|
||||
func ParseSelector(selector string) (Selector, error) {
|
||||
parts := strings.Split(selector, ",")
|
||||
var items []Selector
|
||||
for _, part := range parts {
|
||||
if part == "" {
|
||||
continue
|
||||
}
|
||||
if lhs, rhs, ok := try(part, "!="); ok {
|
||||
items = append(items, ¬HasTerm{label: lhs, value: rhs})
|
||||
} else if lhs, rhs, ok := try(part, "=="); ok {
|
||||
items = append(items, &hasTerm{label: lhs, value: rhs})
|
||||
} else if lhs, rhs, ok := try(part, "="); ok {
|
||||
items = append(items, &hasTerm{label: lhs, value: rhs})
|
||||
} else {
|
||||
return nil, fmt.Errorf("invalid selector: '%s'; can't understand '%s'", selector, part)
|
||||
}
|
||||
}
|
||||
if len(items) == 1 {
|
||||
return items[0], nil
|
||||
}
|
||||
return andTerm(items), nil
|
||||
}
|
Reference in New Issue
Block a user