forked from thoas/bokchoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
133 lines (104 loc) · 2.66 KB
/
utils.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package bokchoy
import (
"fmt"
"math/rand"
"strconv"
"time"
"github.com/oklog/ulid"
"github.com/pkg/errors"
)
func ID() string {
t := time.Now().UTC()
entropy := rand.New(rand.NewSource(t.UnixNano()))
return ulid.MustNew(ulid.Timestamp(t), entropy).String()
}
func reverseDurations(durations []time.Duration) []time.Duration {
results := make([]time.Duration, len(durations))
j := len(durations) - 1
for i := 0; i < len(durations); i++ {
results[i] = durations[j]
j--
}
return results
}
func mapDuration(values map[string]interface{}, key string, optional bool) (time.Duration, error) {
raw, err := mapString(values, key, optional)
if err != nil {
return 0, err
}
if raw != "" {
value, err := strconv.ParseInt(raw, 10, 64)
if err != nil {
return 0, errors.Wrapf(ErrAttributeError, "cannot parse `%s` to integer", key)
}
timeValue := time.Duration(value) * time.Second
return timeValue, nil
}
return 0, nil
}
func mapTime(values map[string]interface{}, key string, optional bool) (time.Time, error) {
raw, err := mapString(values, key, optional)
if err != nil {
return time.Time{}, err
}
if raw != "" {
value, err := strconv.ParseInt(raw, 10, 64)
if err != nil {
return time.Time{}, errors.Wrapf(ErrAttributeError, "cannot parse `%s` to integer", key)
}
timeValue := time.Unix(value, 0).UTC()
return timeValue, nil
}
return time.Time{}, nil
}
func mapString(values map[string]interface{}, key string, optional bool) (string, error) {
raw, ok := values[key]
if !ok && !optional {
return "", errors.Wrapf(ErrAttributeError, "cannot cast `%s`", key)
}
switch raw := raw.(type) {
case string:
return raw, nil
case int, int64:
return fmt.Sprintf("%d", raw), nil
}
return "", nil
}
func mapInt(values map[string]interface{}, key string, optional bool) (int, error) {
raw, err := mapString(values, key, optional)
if err != nil {
return 0, err
}
if raw != "" {
value, err := strconv.ParseInt(raw, 10, 64)
if err != nil {
return 0, errors.Wrapf(ErrAttributeError, "cannot parse `%s` to integer", key)
}
return int(value), nil
}
return 0, nil
}
func mapFloat(values map[string]interface{}, key string, optional bool) (float64, error) {
raw, err := mapString(values, key, optional)
if err != nil {
return 0, err
}
if raw != "" {
value, err := strconv.ParseFloat(raw, 10)
if err != nil {
return 0, errors.Wrapf(ErrAttributeError, "cannot parse `%s` to float", key)
}
return value, nil
}
return 0, nil
}
func unpack(fields map[string]interface{}) []interface{} {
args := make([]interface{}, len(fields)*2)
i := 0
for k, v := range fields {
args[i] = k
args[i+1] = v
i += 2
}
return args
}