go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/scheduler/appengine/task/utils/kvlist.go (about) 1 // Copyright 2015 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package utils 16 17 import ( 18 "fmt" 19 "sort" 20 "strings" 21 ) 22 23 // KV is key and value strings. 24 type KV struct { 25 Key string 26 Value string 27 } 28 29 // KVList if list of KV pairs. 30 type KVList []KV 31 32 // ValidateKVList makes sure each string in the list is valid key-value pair. 33 func ValidateKVList(kind string, list []string, sep rune) error { 34 for _, item := range list { 35 if !strings.ContainsRune(item, sep) { 36 return fmt.Errorf("bad %s, not a 'key%svalue' pair: %q", kind, string(sep), item) 37 } 38 } 39 return nil 40 } 41 42 // KVListFromMap converts a map to KVList. 43 func KVListFromMap(m map[string]string) KVList { 44 keys := make([]string, 0, len(m)) 45 for k := range m { 46 keys = append(keys, k) 47 } 48 sort.Strings(keys) 49 kvList := make([]KV, len(keys)) 50 for i, k := range keys { 51 kvList[i] = KV{ 52 Key: k, 53 Value: m[k], 54 } 55 } 56 return kvList 57 } 58 59 // UnpackKVList takes validated list of k-v pair strings and returns list of 60 // structs. 61 // 62 // Silently skips malformed strings. Use ValidateKVList to detect them before 63 // calling this function. 64 func UnpackKVList(list []string, sep rune) (out KVList) { 65 for _, item := range list { 66 idx := strings.IndexRune(item, sep) 67 if idx == -1 { 68 continue 69 } 70 out = append(out, KV{ 71 Key: item[:idx], 72 Value: item[idx+1:], 73 }) 74 } 75 return out 76 } 77 78 // Pack converts KV list to a list of strings. 79 func (l KVList) Pack(sep rune) []string { 80 out := make([]string, len(l)) 81 for i, kv := range l { 82 out[i] = kv.Key + string(sep) + kv.Value 83 } 84 return out 85 }