-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
74 lines (60 loc) · 1.28 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
package maatq
import (
"encoding/json"
"errors"
"reflect"
"runtime"
"sort"
"strconv"
)
type Int8Slice []int8
func (s Int8Slice) Len() int {
return len(s)
}
func (s Int8Slice) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s Int8Slice) Less(i, j int) bool {
return s[i] < s[j]
}
func AtoString(val interface{}) (string, error) {
sval, ok := val.(string)
if ok {
return sval, nil
}
fval, ok := val.(float64)
if ok {
return strconv.FormatInt(int64(fval), 10), nil
}
bval, ok := val.(bool)
if ok {
return strconv.FormatBool(bval), nil
}
jval, ok := val.(map[string]interface{})
if ok {
tmpVal, err := json.Marshal(jval)
if err == nil {
return string(tmpVal), nil
}
return "", err
}
return "", errors.New("Unknown type")
}
func GetFunctionName(i interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}
func makeRangeOfInt8(min, max int8, step int) []int8 {
s := make([]int8, 0)
for i := min; i <= max; i += int8(step) {
s = append(s, i)
}
return s
}
func inInt8Slice(n int8, data []int8) bool {
i := sort.Search(len(data), func(i int) bool { return data[i] >= n })
return i < len(data) && data[i] == n
}
// 生成完整的队列名称,例如 default => maatq:default
func queueName(q string) string {
return "maatq:" + q
}