forked from ostafen/clover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
83 lines (74 loc) · 1.55 KB
/
util.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package clover
import (
"os"
)
const defaultPermDir = 0777
func makeDirIfNotExists(dir string) error {
if err := os.Mkdir(dir, defaultPermDir); err != nil && !os.IsExist(err) {
return err
}
return nil
}
func copyMap(m map[string]interface{}) map[string]interface{} {
mapCopy := make(map[string]interface{})
for k, v := range m {
mapValue, ok := v.(map[string]interface{})
if ok {
mapCopy[k] = copyMap(mapValue)
} else {
mapCopy[k] = v
}
}
return mapCopy
}
func boolToInt(v bool) int {
if v {
return 1
}
return 0
}
func isNumber(v interface{}) bool {
switch v.(type) {
case int, uint, uint8, uint16, uint32, uint64,
int8, int16, int32, int64, float32, float64:
return true
default:
return false
}
}
func toFloat64(v interface{}) float64 {
switch vType := v.(type) {
case uint64:
return float64(vType)
case int64:
return float64(vType)
case float64:
return vType
}
panic("not a number")
}
func toInt64(v interface{}) int64 {
switch vType := v.(type) {
case uint64:
return int64(vType)
case int64:
return vType
}
panic("not a number")
}
// Returns a flat list of all keys of the map, including sub-maps, in which case the dot notation is used (ex. a.b.c)
func getAllKeys(fields map[string]interface{}) []string {
result := []string{}
for key, value := range fields {
subMap, isMap := value.(map[string]interface{})
if isMap {
subFields := getAllKeys(subMap)
for _, subKey := range subFields {
result = append(result, key + "." + subKey)
}
} else {
result = append(result, key)
}
}
return result
}