-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
83 lines (68 loc) · 1.45 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 core
import (
"errors"
"strconv"
"time"
"unsafe"
)
type TaskWatch struct {
taskName string
startTime time.Time
totalTime time.Duration
isRunning bool
}
func NewTaskWatch() *TaskWatch {
return &TaskWatch{
taskName: "[empty_task]",
}
}
func NewTaskWatchWithName(taskName string) *TaskWatch {
return &TaskWatch{
taskName: taskName,
}
}
func (watch *TaskWatch) Start() error {
if watch.isRunning {
return errors.New("TaskWatch is already running")
}
watch.startTime = time.Now()
watch.isRunning = true
return nil
}
func (watch *TaskWatch) Stop() error {
if !watch.isRunning {
return errors.New("TaskWatch is not running")
}
watch.isRunning = false
watch.totalTime = time.Since(watch.startTime)
watch.taskName = ""
return nil
}
func (watch *TaskWatch) IsRunning() bool {
return watch.isRunning
}
func (watch *TaskWatch) GetTotalTime() int64 {
return watch.totalTime.Nanoseconds()
}
func BytesToStr(bytes []byte) string {
return *(*string)(unsafe.Pointer(&bytes))
}
func FlatMap(m map[string]interface{}) map[string]interface{} {
flattenMap := map[string]interface{}{}
for key, value := range m {
switch child := value.(type) {
case map[string]interface{}:
nm := FlatMap(child)
for nk, nv := range nm {
flattenMap[key+"."+nk] = nv
}
case []interface{}:
for i := 0; i < len(child); i++ {
flattenMap[key+"."+strconv.Itoa(i)] = child[i]
}
default:
flattenMap[key] = value
}
}
return flattenMap
}