-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
62 lines (51 loc) · 1.1 KB
/
map.go
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package utils
import (
"fmt"
"strings"
)
func MapGetKeyValuesEx(s, sequenceDelim, keyValueDelim string) map[string]string {
pairs := strings.Split(s, sequenceDelim)
m := make(map[string]string)
for _, p := range pairs {
if IsEmpty(p) {
continue
}
kv := strings.SplitN(p, keyValueDelim, 2)
k := strings.TrimSpace(kv[0])
if len(kv) > 1 {
v := strings.TrimSpace(kv[1])
if strings.HasPrefix(v, "${") && strings.HasSuffix(v, "}") {
ed := strings.SplitN(v[2:len(v)-1], ":", 2)
e, d := ed[0], ed[1]
v = EnvGet(e, "").(string)
if v == "" && d != "" {
v = d
}
}
m[k] = v
} else {
m[k] = ""
}
}
return m
}
func MapGetKeyValues(s string) map[string]string {
return MapGetKeyValuesEx(s, ",", "=")
}
func MapToArrayWithSeparator(m map[string]string, s string) []string {
var arr []string
if m == nil {
return arr
}
for k, v := range m {
if IsEmpty(v) {
arr = append(arr, k)
} else {
arr = append(arr, fmt.Sprintf("%s%s%v", k, s, v))
}
}
return arr
}
func MapToArray(m map[string]string) []string {
return MapToArrayWithSeparator(m, "=")
}