forked from reiver/go-cast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint32.go
144 lines (133 loc) · 3.31 KB
/
int32.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
package cast
import "math"
// Int32 will return an int32 when `v` is of type int32, int16, int8, uint16, uint8 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 {
// 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 Int32(v interface{}) (int32, error) {
switch value := v.(type) {
case int32er:
return func()(int32, error){
casted, err := value.Int32()
if nil != err {
return 0, err
}
return int32(casted), nil
}()
case int16er:
return func()(int32, error){
casted, err := value.Int16()
if nil != err {
return 0, err
}
return int32(casted), nil
}()
case int8er:
return func()(int32, error){
casted, err := value.Int8()
if nil != err {
return 0, err
}
return int32(casted), nil
}()
case uint16er:
return func()(int32, error){
casted, err := value.Uint16()
if nil != err {
return 0, err
}
return int32(casted), nil
}()
case uint8er:
return func()(int32, error){
casted, err := value.Uint8()
if nil != err {
return 0, err
}
return int32(casted), nil
}()
case float32:
if value < math.MinInt32 || value > math.MaxInt32 {
return 0, internalCannotCastComplainer{expectedType:"int32", actualType:typeof(value), reason: "overflow"}
}
if math.Trunc(float64(value)) != float64(value) {
return 0, internalCannotCastComplainer{expectedType:"int32", actualType:typeof(value), reason: "decimal part found"}
}
return int32(value), nil
case float64:
if value < math.MinInt32 || value > math.MaxInt32 {
return 0, internalCannotCastComplainer{expectedType:"int32", actualType:typeof(value), reason: "overflow"}
}
if math.Trunc(value) != value {
return 0, internalCannotCastComplainer{expectedType:"int32", actualType:typeof(value), reason: "decimal part found"}
}
return int32(value), nil
case int64:
if value < math.MinInt32 || value > math.MaxInt32 {
return 0, internalCannotCastComplainer{expectedType:"int32", actualType:typeof(value), reason: "overflow"}
}
return int32(value), nil
case uint32:
if value > math.MaxInt32 {
return 0, internalCannotCastComplainer{expectedType:"int32", actualType:typeof(value), reason: "overflow"}
}
return int32(value), nil
case uint64:
if value > math.MaxInt32 {
return 0, internalCannotCastComplainer{expectedType:"int32", actualType:typeof(value), reason: "overflow"}
}
return int32(value), nil
case int32:
return int32(value), nil
case int16:
return int32(value), nil
case int8:
return int32(value), nil
case uint16:
return int32(value), nil
case uint8:
return int32(value), nil
default:
return 0, internalCannotCastComplainer{expectedType:"int32", actualType:typeof(value)}
}
}
// MustInt32 is like Int32, expect panic()s on an error.
func MustInt32(v interface{}) int32 {
x, err := Int32(v)
if nil != err {
panic(err)
}
return x
}
type int32er interface {
Int32() (int32, error)
}