github.com/blend/go-sdk@v1.20220411.3/selector/not_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 // NotIn returns if a key does not match a set of values. 16 type NotIn struct { 17 Key string 18 Values []string 19 } 20 21 // Matches returns the selector result. 22 func (ni NotIn) Matches(labels Labels) bool { 23 if value, hasValue := labels[ni.Key]; hasValue { 24 for _, iv := range ni.Values { 25 if iv == value { 26 // the key does not equal any of the values 27 return false 28 } 29 } 30 } 31 // the value doesn't exist. 32 return true 33 } 34 35 // Validate validates the selector. 36 func (ni NotIn) Validate() (err error) { 37 err = CheckKey(ni.Key) 38 if err != nil { 39 return 40 } 41 for _, v := range ni.Values { 42 err = CheckValue(v) 43 if err != nil { 44 return 45 } 46 } 47 return 48 } 49 50 // String returns a string representation of the selector. 51 func (ni NotIn) String() string { 52 return fmt.Sprintf("%s notin (%s)", ni.Key, strings.Join(ni.Values, ", ")) 53 }