...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package label
17
18 import (
19 "fmt"
20 "strings"
21
22 "github.com/pkg/errors"
23 )
24
25
26 type Label map[string]string
27
28
29 func (l Label) String() string {
30 var arr []string
31
32 for k, v := range l {
33 if len(k) == 0 {
34 continue
35 }
36
37 arr = append(arr, fmt.Sprintf("%s=%s", k, v))
38 }
39
40 return strings.Join(arr, ",")
41 }
42
43 func ParseLabel(data string) (Label, error) {
44 if len(data) == 0 {
45 return Label{}, nil
46 }
47
48 labels := make(map[string]string)
49 for _, tok := range strings.Split(data, ",") {
50 kv := strings.Split(tok, "=")
51 if len(kv) != 2 {
52 return nil, errors.Errorf("invalid labels: %s", data)
53 }
54 labels[kv[0]] = kv[1]
55 }
56 return labels, nil
57 }
58