forked from reiver/go-cast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloat64.go
153 lines (147 loc) · 2.97 KB
/
float64.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
package cast
// Float64 will return an float64 when `v` is of type float64, float32, int32, int16, int8, uint32, uint16, uint8 or has a method:
//
// type interface {
// Float64() (float64, error)
// }
//
// ... that returns successfully, or has a method:
//
// type interface {
// Float32() (float32, 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 {
// 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 Float64(v interface{}) (float64, error) {
switch value := v.(type) {
case float64er:
return value.Float64()
case float32er:
return func()(float64, error){
casted, err := value.Float32()
if nil != err {
return 0, err
}
return float64(casted), nil
}()
case int32er:
return func()(float64, error){
casted, err := value.Int32()
if nil != err {
return 0, err
}
return float64(casted), nil
}()
case int16er:
return func()(float64, error){
casted, err := value.Int16()
if nil != err {
return 0, err
}
return float64(casted), nil
}()
case int8er:
return func()(float64, error){
casted, err := value.Int8()
if nil != err {
return 0, err
}
return float64(casted), nil
}()
case uint32er:
return func()(float64, error){
casted, err := value.Uint32()
if nil != err {
return 0, err
}
return float64(casted), nil
}()
case uint16er:
return func()(float64, error){
casted, err := value.Uint16()
if nil != err {
return 0, err
}
return float64(casted), nil
}()
case uint8er:
return func()(float64, error){
casted, err := value.Uint8()
if nil != err {
return 0, err
}
return float64(casted), nil
}()
case float64:
return float64(value), nil
case float32:
return float64(value), nil
case int64:
return float64(value), nil
case int32:
return float64(value), nil
case int16:
return float64(value), nil
case int8:
return float64(value), nil
case uint64:
return float64(value), nil
case uint32:
return float64(value), nil
case uint16:
return float64(value), nil
case uint8:
return float64(value), nil
default:
return 0, internalCannotCastComplainer{expectedType:"float64", actualType:typeof(value)}
}
}
// MustFloat64 is like Float64, expect panic()s on an error.
func MustFloat64(v interface{}) float64 {
x, err := Float64(v)
if nil != err {
panic(err)
}
return x
}
type float64er interface {
Float64() (float64, error)
}