-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathstring_map.go
More file actions
40 lines (31 loc) · 764 Bytes
/
string_map.go
File metadata and controls
40 lines (31 loc) · 764 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"fmt"
"strings"
)
// StringMap is a map of key value pairs.
type StringMap map[string]string
func (s StringMap) String() string {
elements := make([]string, 0, len(s))
for k, v := range s {
elements = append(elements, fmt.Sprintf("%s=%s", k, v))
}
return strings.Join(elements, ",")
}
// Set parses a string of the format: `key=value`.
func (s StringMap) Set(value string) error {
if s == nil {
s = StringMap(map[string]string{})
}
kv := strings.Split(value, "=")
if len(kv) != 2 {
return fmt.Errorf("invalid key=value format: %s", value)
}
s[kv[0]] = kv[1]
return nil
}
// IsCumulative always return true because it's allowed to call Set multiple
// times.
func (_ StringMap) IsCumulative() bool {
return true
}