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