-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.go
120 lines (94 loc) · 2.14 KB
/
common.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
package redisutil
import (
"reflect"
"strconv"
"github.com/pkg/errors"
)
var ErrInvalidNum = errors.New("Invalid num")
// 为了以后升级可以统一让所有key失效
func keyPatch(key string) string {
return key
}
func isNum(i interface{}) (string, bool) {
switch vi := i.(type) {
case int8:
v := int64(vi)
s := strconv.FormatInt(v, 10)
return s, true
case int16:
v := int64(vi)
s := strconv.FormatInt(v, 10)
return s, true
case int32:
v := int64(vi)
s := strconv.FormatInt(v, 10)
return s, true
case int:
v := int64(vi)
s := strconv.FormatInt(v, 10)
return s, true
case int64:
s := strconv.FormatInt(vi, 10)
return s, true
case uint8:
v := uint64(vi)
s := strconv.FormatUint(v, 10)
return s, true
case uint16:
v := uint64(vi)
s := strconv.FormatUint(v, 10)
return s, true
case uint32:
v := uint64(vi)
s := strconv.FormatUint(v, 10)
return s, true
case uint:
v := uint64(vi)
s := strconv.FormatUint(v, 10)
return s, true
case uint64:
s := strconv.FormatUint(vi, 10)
return s, true
default:
return "", false
}
}
func isNumPtr(i interface{}) bool {
val := reflect.ValueOf(i)
if val.Kind() != reflect.Ptr {
return false
}
v := val.Elem().Kind()
switch v {
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int, reflect.Int64:
return true
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint, reflect.Uint64:
return true
default:
return false
}
}
// 注:此方法溢出不会报error
func bytesToNum(b []byte, num interface{}) error {
val := reflect.ValueOf(num)
if val.Kind() != reflect.Ptr {
return ErrInvalidNum
}
v := val.Elem().Kind()
s := string(b)
switch v {
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int, reflect.Int64:
n, err := strconv.ParseInt(s, 0, 64)
if err != nil {
return errors.WithMessagef(err, "%s convert to int err", s)
}
val.Elem().SetInt(n)
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint, reflect.Uint64:
n, err := strconv.ParseUint(s, 0, 64)
if err != nil {
return errors.WithMessagef(err, "%s convert to uint err", s)
}
val.Elem().SetUint(n)
}
return nil
}