-
Notifications
You must be signed in to change notification settings - Fork 47
/
in.go
57 lines (49 loc) · 1.1 KB
/
in.go
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
50
51
52
53
54
55
56
57
/*
Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
Use of this source code is governed by a MIT license that can be found in the LICENSE file.
*/
package selector
import (
"fmt"
"strings"
)
// In returns if a key matches a set of values.
type In struct {
Key string
Values []string
}
// Matches returns the selector result.
func (i In) Matches(labels Labels) bool {
// if the labels has a given key
if value, hasValue := labels[i.Key]; hasValue {
// for each selector value
for _, iv := range i.Values {
// if they match, return true
if iv == value {
return true
}
}
return false
}
// in should be exclusive, that is
// we should fail if the in key isn't present
return false
}
// Validate validates the selector.
func (i In) Validate() (err error) {
err = CheckKey(i.Key)
if err != nil {
return
}
for _, v := range i.Values {
err = CheckValue(v)
if err != nil {
return
}
}
return
}
// String returns a string representation of the selector.
func (i In) String() string {
return fmt.Sprintf("%s in (%s)", i.Key, strings.Join(i.Values, ", "))
}