-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber.go
163 lines (152 loc) · 3.1 KB
/
number.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package util
import (
"fmt"
"github.com/pkg/errors"
"reflect"
"strconv"
)
func ToUint64(value interface{}) (uint64Value uint64, err error) {
if value == nil {
return
}
v := reflect.ValueOf(value)
t := reflect.TypeOf(value)
switch t.Kind() {
case reflect.Int:
fallthrough
case reflect.Int8:
fallthrough
case reflect.Int16:
fallthrough
case reflect.Int32:
fallthrough
case reflect.Int64:
uint64Value = uint64(v.Int())
case reflect.Uint:
fallthrough
case reflect.Uint8:
fallthrough
case reflect.Uint16:
fallthrough
case reflect.Uint32:
fallthrough
case reflect.Uint64:
uint64Value = v.Uint()
case reflect.Float32:
fallthrough
case reflect.Float64:
uint64Value = uint64(v.Float())
case reflect.String:
uint64Value, err = strconv.ParseUint(reflect.ValueOf(value).String(), 10, 64)
case reflect.Ptr:
if !v.IsNil() {
uint64Value, err = ToUint64(v.Elem().Interface())
}
default:
err = errors.New(fmt.Sprintf("not support value %v %T", value, value))
}
return
}
func ToUint32(value interface{}) (uint32Value uint32, err error) {
uint64Value, err := ToUint64(value)
if err != nil {
err = errors.WithStack(err)
return
}
uint32Value = uint32(uint64Value)
return
}
func ToInt64(value interface{}) (int64Value int64, err error) {
uint64Value, err := ToUint64(value)
if err != nil {
err = errors.WithStack(err)
return
}
int64Value = int64(uint64Value)
return
}
func ToInt32(value interface{}) (int32Value int32, err error) {
uint64Value, err := ToUint64(value)
if err != nil {
err = errors.WithStack(err)
return
}
int32Value = int32(uint64Value)
return
}
func ToFloat64(value interface{}) (float64Value float64, err error) {
if value == nil {
return
}
v := reflect.ValueOf(value)
t := reflect.TypeOf(value)
switch t.Kind() {
case reflect.Int:
fallthrough
case reflect.Int8:
fallthrough
case reflect.Int16:
fallthrough
case reflect.Int32:
fallthrough
case reflect.Int64:
float64Value = float64(v.Int())
case reflect.Uint:
fallthrough
case reflect.Uint8:
fallthrough
case reflect.Uint16:
fallthrough
case reflect.Uint32:
fallthrough
case reflect.Uint64:
float64Value = float64(v.Uint())
case reflect.Float32:
fallthrough
case reflect.Float64:
float64Value = v.Float()
case reflect.String:
float64Value, err = strconv.ParseFloat(reflect.ValueOf(value).String(), 64)
case reflect.Ptr:
if !v.IsNil() {
float64Value, err = ToFloat64(v.Elem().Interface())
}
default:
err = errors.New(fmt.Sprintf("not support value %v %T", value, value))
break
}
return
}
func ToFloat32(value interface{}) (float32Value float32, err error) {
float64Value, err := ToFloat64(value)
if err != nil {
err = errors.WithStack(err)
return
}
float32Value = float32(float64Value)
return
}
func IntFallback(i, fallback int) int {
if i == 0 {
return fallback
}
return i
}
func UintFallback(i, fallback uint) uint {
if i == 0 {
return fallback
}
return i
}
func Int64Fallback(i, fallback int64) int64 {
if i == 0 {
return fallback
}
return i
}
func Uint64Fallback(i, fallback uint64) uint64 {
if i == 0 {
return fallback
}
return i
}