github.com/felipejfc/helm@v2.1.2+incompatible/pkg/storage/driver/labels.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package driver 18 19 import ( 20 "bytes" 21 "fmt" 22 "io" 23 ) 24 25 // labels is a map of key value pairs to be included as metadata in a configmap object. 26 type labels map[string]string 27 28 func (lbs *labels) init() { *lbs = labels(make(map[string]string)) } 29 func (lbs labels) get(key string) string { return lbs[key] } 30 func (lbs labels) set(key, val string) { lbs[key] = val } 31 32 func (lbs labels) keys() (ls []string) { 33 for key := range lbs { 34 ls = append(ls, key) 35 } 36 return 37 } 38 39 func (lbs labels) match(set labels) bool { 40 for _, key := range set.keys() { 41 if lbs.get(key) != set.get(key) { 42 return false 43 } 44 } 45 return true 46 } 47 48 func (lbs labels) toMap() map[string]string { return lbs } 49 50 func (lbs *labels) fromMap(kvs map[string]string) { 51 for k, v := range kvs { 52 lbs.set(k, v) 53 } 54 } 55 56 func (lbs labels) dump(w io.Writer) error { 57 var b bytes.Buffer 58 59 fmt.Fprintln(&b, "labels:") 60 for k, v := range lbs { 61 fmt.Fprintf(&b, "\t- %q -> %q\n", k, v) 62 } 63 64 _, err := w.Write(b.Bytes()) 65 return err 66 }