github.com/blend/go-sdk@v1.20220411.3/selector/in.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package selector 9 10 import ( 11 "fmt" 12 "strings" 13 ) 14 15 // In returns if a key matches a set of values. 16 type In struct { 17 Key string 18 Values []string 19 } 20 21 // Matches returns the selector result. 22 func (i In) Matches(labels Labels) bool { 23 // if the labels has a given key 24 if value, hasValue := labels[i.Key]; hasValue { 25 // for each selector value 26 for _, iv := range i.Values { 27 // if they match, return true 28 if iv == value { 29 return true 30 } 31 } 32 return false 33 } 34 // in should be exclusive, that is 35 // we should fail if the in key isn't present 36 return false 37 } 38 39 // Validate validates the selector. 40 func (i In) Validate() (err error) { 41 err = CheckKey(i.Key) 42 if err != nil { 43 return 44 } 45 for _, v := range i.Values { 46 err = CheckValue(v) 47 if err != nil { 48 return 49 } 50 } 51 return 52 } 53 54 // String returns a string representation of the selector. 55 func (i In) String() string { 56 return fmt.Sprintf("%s in (%s)", i.Key, strings.Join(i.Values, ", ")) 57 }