-
Notifications
You must be signed in to change notification settings - Fork 1
/
converter.go
69 lines (56 loc) · 1.22 KB
/
converter.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
63
64
65
66
67
68
69
package helpers
import (
"bufio"
"bytes"
"github.com/BurntSushi/toml"
"gopkg.in/yaml.v2"
)
func ToYAML(src interface{}) string {
data, err := yaml.Marshal(src)
if err == nil {
return string(data)
}
return ""
}
func ToTOML(src interface{}) string {
var data bytes.Buffer
buffer := bufio.NewWriter(&data)
if err := toml.NewEncoder(buffer).Encode(src); err != nil {
return ""
}
if err := buffer.Flush(); err != nil {
return ""
}
return data.String()
}
func ToConfigMap(list interface{}) (map[string]interface{}, bool) {
x, ok := list.(map[string]interface{})
if ok {
return x, ok
}
y, ok := list.(map[interface{}]interface{})
if !ok {
return nil, false
}
result := make(map[string]interface{})
for k, v := range y {
result[k.(string)] = v
}
return result, true
}
func GetMapKey(value map[string]interface{}, keys ...string) (result interface{}, ok bool) {
result = value
for _, key := range keys {
if stringMap, ok := result.(map[string]interface{}); ok {
if result, ok = stringMap[key]; ok {
continue
}
} else if interfaceMap, ok := result.(map[interface{}]interface{}); ok {
if result, ok = interfaceMap[key]; ok {
continue
}
}
return nil, false
}
return result, true
}