forked from reiver/go-cast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint64.go
173 lines (162 loc) · 3.56 KB
/
int64.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
164
165
166
167
168
169
170
171
172
173
package cast
import "math"
// Int64 will return an int64 when `v` is of type int64, int32, int16, int8, int, uint32, uint16, uint8 or has a method:
//
// type interface {
// Int64() (int64, error)
// }
//
// ... that returns successfully, or has a method:
//
// type interface {
// Int32() (int32, error)
// }
//
// ... that returns successfully, or has a method:
//
// type interface {
// Int16() (int16, error)
// }
//
// ... that returns successfully, or has a method:
//
// type interface {
// Int8() (int8, error)
// }
//
// ... that returns successfully, or has a method:
//
// type interface {
// Int() (int, error)
// }
//
// ... that returns successfully, or has a method:
//
// type interface {
// Uint32() (uint32, error)
// }
//
// ... that returns successfully, or has a method:
//
// type interface {
// Uint16() (uint16, error)
// }
//
// ... that returns successfully, or has a method:
//
// type interface {
// Uint8() (uint8, error)
// }
//
// ... that returns successfully.
//
// Else it will return an error.
func Int64(v interface{}) (int64, error) {
switch value := v.(type) {
case int64er:
return value.Int64()
case int32er:
return func()(int64, error){
casted, err := value.Int32()
if nil != err {
return 0, err
}
return int64(casted), nil
}()
case int16er:
return func()(int64, error){
casted, err := value.Int16()
if nil != err {
return 0, err
}
return int64(casted), nil
}()
case int8er:
return func()(int64, error){
casted, err := value.Int8()
if nil != err {
return 0, err
}
return int64(casted), nil
}()
case inter:
return func()(int64, error){
casted, err := value.Int()
if nil != err {
return 0, err
}
return int64(casted), nil
}()
case uint32er:
return func()(int64, error){
casted, err := value.Uint32()
if nil != err {
return 0, err
}
return int64(casted), nil
}()
case uint16er:
return func()(int64, error){
casted, err := value.Uint16()
if nil != err {
return 0, err
}
return int64(casted), nil
}()
case uint8er:
return func()(int64, error){
casted, err := value.Uint8()
if nil != err {
return 0, err
}
return int64(casted), nil
}()
case int64:
return int64(value), nil
case int32:
return int64(value), nil
case int16:
return int64(value), nil
case int8:
return int64(value), nil
case int:
return int64(value), nil
case uint32:
return int64(value), nil
case uint16:
return int64(value), nil
case uint8:
return int64(value), nil
case uint64:
if value > math.MaxInt64 {
return 0, internalCannotCastComplainer{expectedType:"int64", actualType:typeof(value), reason: "overflow"}
}
return int64(value), nil
case float32:
if math.Trunc(float64(value)) != float64(value) {
return 0, internalCannotCastComplainer{expectedType:"int64", actualType:typeof(value), reason: "decimal part found"}
}
return int64(value), nil
case float64:
if value < math.MinInt64 || value > math.MaxInt64 {
return 0, internalCannotCastComplainer{expectedType:"int64", actualType:typeof(value), reason: "overflow"}
}
if math.Trunc(value) != value {
return 0, internalCannotCastComplainer{expectedType:"int64", actualType:typeof(value), reason: "decimal part found"}
}
return int64(value), nil
default:
return 0, internalCannotCastComplainer{expectedType:"int64", actualType:typeof(value)}
}
}
// MustInt64 is like Int64, expect panic()s on an error.
func MustInt64(v interface{}) int64 {
x, err := Int64(v)
if nil != err {
panic(err)
}
return x
}
type int64er interface {
Int64() (int64, error)
}