-
Notifications
You must be signed in to change notification settings - Fork 0
/
cast_int.go
328 lines (269 loc) · 7.25 KB
/
cast_int.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package pick
import (
"encoding/json"
"math"
"reflect"
"strconv"
"strings"
)
func (c DefaultCaster) AsInt(input any) (int, error) {
return c.intCaster.cast(input)
}
func (c DefaultCaster) AsInt8(input any) (int8, error) {
return c.int8Caster.cast(input)
}
func (c DefaultCaster) AsInt16(input any) (int16, error) {
return c.int16Caster.cast(input)
}
func (c DefaultCaster) AsInt32(input any) (int32, error) {
return c.int32Caster.cast(input)
}
func (c DefaultCaster) AsInt64(input any) (int64, error) {
return c.int64Caster.cast(input)
}
func (c DefaultCaster) AsUint(input any) (uint, error) {
return c.uintCaster.cast(input)
}
func (c DefaultCaster) AsUint8(input any) (uint8, error) {
return c.uint8Caster.cast(input)
}
func (c DefaultCaster) AsUint16(input any) (uint16, error) {
return c.uint16Caster.cast(input)
}
func (c DefaultCaster) AsUint32(input any) (uint32, error) {
return c.uint32Caster.cast(input)
}
func (c DefaultCaster) AsUint64(input any) (uint64, error) {
return c.uint64Caster.cast(input)
}
func (c DefaultCaster) AsIntSlice(input any) ([]int, error) {
return mapTo(input, mapOpFn(c.AsInt))
}
func (c DefaultCaster) AsInt8Slice(input any) ([]int8, error) {
return mapTo(input, mapOpFn(c.AsInt8))
}
func (c DefaultCaster) AsInt16Slice(input any) ([]int16, error) {
return mapTo(input, mapOpFn(c.AsInt16))
}
func (c DefaultCaster) AsInt32Slice(input any) ([]int32, error) {
return mapTo(input, mapOpFn(c.AsInt32))
}
func (c DefaultCaster) AsInt64Slice(input any) ([]int64, error) {
return mapTo(input, mapOpFn(c.AsInt64))
}
func (c DefaultCaster) AsUintSlice(input any) ([]uint, error) {
return mapTo(input, mapOpFn(c.AsUint))
}
func (c DefaultCaster) AsUint8Slice(input any) ([]uint8, error) {
return mapTo(input, mapOpFn(c.AsUint8))
}
func (c DefaultCaster) AsUint16Slice(input any) ([]uint16, error) {
return mapTo(input, mapOpFn(c.AsUint16))
}
func (c DefaultCaster) AsUint32Slice(input any) ([]uint32, error) {
return mapTo(input, mapOpFn(c.AsUint32))
}
func (c DefaultCaster) AsUint64Slice(input any) ([]uint64, error) {
return mapTo(input, mapOpFn(c.AsUint64))
}
type intCast[T Integer] struct {
signed bool
kind reflect.Kind
}
func newIntCast[T Integer]() intCast[T] {
ic := intCast[T]{}
var t T
ic.kind = reflect.TypeOf(t).Kind()
ic.signed = ic.kind >= reflect.Int && ic.kind <= reflect.Int64
return ic
}
//nolint:ireturn
func (ic intCast[T]) fromInt64(origin int64) (T, error) {
t := T(origin)
if !int64CastValid(origin, ic.kind) {
return t, newCastError(ErrCastOverFlow, origin)
}
return t, nil
}
//nolint:ireturn
func (ic intCast[T]) fromUint64(origin uint64) (T, error) {
t := T(origin)
if !uint64CastValid(origin, ic.kind) {
return t, newCastError(ErrCastOverFlow, origin)
}
return t, nil
}
//nolint:ireturn
func (ic intCast[T]) fromFloat64(origin float64) (T, error) {
casted, err := float64ToInt64(origin)
if err != nil {
return T(casted), err
}
return ic.fromInt64(casted)
}
//nolint:ireturn
func (ic intCast[T]) fromString(origin string) (T, error) {
if strings.ContainsAny(origin, ".e") {
v, err := strconv.ParseFloat(origin, 64)
if err != nil {
return T(v), newCastError(err, origin)
}
return ic.fromFloat64(v)
}
if ic.signed {
v, err := strconv.ParseInt(origin, 10, 64)
if err != nil {
return T(v), newCastError(err, origin)
}
return ic.fromInt64(v)
}
v, err := strconv.ParseUint(origin, 10, 64)
if err != nil {
return T(v), newCastError(err, origin)
}
return ic.fromUint64(v)
}
//nolint:ireturn
func (ic intCast[T]) fromBool(origin bool) T {
if origin {
return 1
}
return 0
}
//nolint:ireturn
func (ic intCast[T]) cast(input any) (T, error) {
switch origin := input.(type) {
case int:
return ic.fromInt64(int64(origin))
case int8:
return ic.fromInt64(int64(origin))
case int16:
return ic.fromInt64(int64(origin))
case int32:
return ic.fromInt64(int64(origin))
case int64:
return ic.fromInt64(origin)
case uint:
return ic.fromUint64(uint64(origin))
case uint8:
return ic.fromUint64(uint64(origin))
case uint16:
return ic.fromUint64(uint64(origin))
case uint32:
return ic.fromUint64(uint64(origin))
case uint64:
return ic.fromUint64(origin)
case float32:
return ic.fromFloat64(float64(origin))
case float64:
return ic.fromFloat64(origin)
case string:
return ic.fromString(origin)
case json.Number:
return ic.fromString(string(origin))
case []byte:
return ic.fromString(string(origin))
case bool:
return ic.fromBool(origin), nil
case nil:
return 0, nil
default:
// try to cast to basic (in case input is ~basic)
if basic, err := tryCastToBasicType(input); err == nil {
return ic.cast(basic)
}
return tryReflectConvert[T](input)
}
}
type sizeOfInteger int
// The int, uint types are usually 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems.
// https://go.dev/ref/spec#Numeric_types
const (
//nolint:mnd,gomnd // there are not magic numbers to be fair
sizeOfInt sizeOfInteger = 32 << (^uint(0) >> 63) // 32 or 64 // https://github.com/golang/go/blob/3d33437c450aa74014ea1d41cd986b6ee6266984/src/math/const.go#L40
sizeOfInt8 sizeOfInteger = 8
sizeOfInt16 sizeOfInteger = 16
sizeOfInt32 sizeOfInteger = 32
sizeOfInt64 sizeOfInteger = 64
sizeOfUInt8 sizeOfInteger = 8
sizeOfUInt16 sizeOfInteger = 16
sizeOfUInt32 sizeOfInteger = 32
sizeOfUInt64 sizeOfInteger = 64
)
//
// Casts range checks
//
func int64CastValid(origin int64, to reflect.Kind) bool {
switch to {
case reflect.Int:
return origin >= math.MinInt && origin <= math.MaxInt
case reflect.Int8:
return origin >= math.MinInt8 && origin <= math.MaxInt8
case reflect.Int16:
return origin >= math.MinInt16 && origin <= math.MaxInt16
case reflect.Int32:
return origin >= math.MinInt32 && origin <= math.MaxInt32
case reflect.Int64:
return true
case reflect.Uint:
if sizeOfInt == sizeOfInt32 {
return origin >= 0 && origin <= math.MaxUint32
}
return origin >= 0
case reflect.Uint8:
return origin >= 0 && origin <= math.MaxUint8
case reflect.Uint16:
return origin >= 0 && origin <= math.MaxUint16
case reflect.Uint32:
return origin >= 0 && origin <= math.MaxUint32
case reflect.Uint64:
return origin >= 0
default:
return false
}
}
func uint64CastValid(origin uint64, to reflect.Kind) bool {
switch to {
case reflect.Int:
return origin <= math.MaxInt
case reflect.Int8:
return origin <= math.MaxInt8
case reflect.Int16:
return origin <= math.MaxInt16
case reflect.Int32:
return origin <= math.MaxInt32
case reflect.Int64:
return origin <= math.MaxInt64
case reflect.Uint:
if sizeOfInt == sizeOfInt32 {
return origin <= math.MaxUint32
}
return true
case reflect.Uint8:
return origin <= math.MaxUint8
case reflect.Uint16:
return origin <= math.MaxUint16
case reflect.Uint32:
return origin <= math.MaxUint32
case reflect.Uint64:
return true
default:
return false
}
}
func floatIsWhole(num float64) bool {
return num == math.Trunc(num)
}
func float64ToInt64(origin float64) (int64, error) {
casted := int64(origin)
if origin > math.MaxInt64 {
return casted, newCastError(ErrCastOverFlow, origin)
}
if origin < math.MinInt64 {
return casted, newCastError(ErrCastOverFlow, origin)
}
if !floatIsWhole(origin) {
return casted, newCastError(ErrCastLostDecimals, origin)
}
return casted, nil
}