-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconv_primitive.go
434 lines (347 loc) · 9.41 KB
/
conv_primitive.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package conv
import (
"fmt"
"math"
"reflect"
"strconv"
)
// Implements conversions between booleans, strings and numbers.
var primitive primitiveConv
type primitiveConv struct{}
func (c primitiveConv) toPrimitive(v interface{}, dstKind reflect.Kind) (interface{}, error) {
switch dstKind {
case reflect.Bool:
return c.toBool(v)
case reflect.String:
return c.toString(v), nil
case reflect.Int:
return c.toInt(v)
case reflect.Int8:
return c.toInt8(v)
case reflect.Int16:
return c.toInt16(v)
case reflect.Int32:
return c.toInt32(v)
case reflect.Int64:
return c.toInt64(v)
case reflect.Uint:
return c.toUint(v)
case reflect.Uint8:
return c.toUint8(v)
case reflect.Uint16:
return c.toUint16(v)
case reflect.Uint32:
return c.toUint32(v)
case reflect.Uint64:
return c.toUint64(v)
case reflect.Float32:
return c.toFloat32(v)
case reflect.Float64:
return c.toFloat64(v)
case reflect.Complex64:
return c.toComplex64(v)
case reflect.Complex128:
return c.toComplex128(v)
}
// This should never run.
panic("not a primitive type")
}
// toBool convert zero values to false, non-zero values to true.
func (c primitiveConv) toBool(v interface{}) (bool, error) {
val := reflect.ValueOf(v)
kind := val.Kind()
switch {
case kind == reflect.String:
return strconv.ParseBool(val.String())
case kind == reflect.Bool:
return val.Bool(), nil
case isKindInt(kind):
return val.Int() != 0, nil
case isKindUint(kind):
return val.Uint() != 0, nil
case isKindFloat(kind):
return val.Float() != 0, nil
case isKindComplex(kind):
return val.Complex() != 0, nil
}
return false, errCantConvertTo(v, "bool")
}
func (c primitiveConv) toString(v interface{}) string {
switch vv := v.(type) {
case bool:
// The default string representation for booleans are true/false, which is not compatible
// to other number types. To increase compatibility, we use 0/1 instead, they can be recognized
// by strconv.ParseBool() , and can be converted to other number types.
if vv {
return "1"
}
return "0"
case string:
return vv
// By default fmt.Sprint() will convert float numbers to scientific notation when the number is large.
// e.g., fmt.Sprint(1234567890123.456) will result in "1.234567890123456e+12".
// To increase compatibility, we convert float numbers to string using strconv.FormatFloat().
case float64:
return strconv.FormatFloat(vv, 'f', -1, 64)
case float32:
return strconv.FormatFloat(float64(vv), 'f', -1, 32)
case complex64:
// Ignore the imaginary part of a complex number when it is zero, thus the value can be converted
// to some other real number.
// e.g., When converting (3+0i) to int, it is converted to "3" then converted to 3. If convert directly
// from "(3+0i)" to int, it will result in an error.
if imag(vv) == 0 {
return c.toString(real(vv))
}
case complex128:
if imag(vv) == 0 {
return c.toString(real(vv))
}
}
return fmt.Sprint(v)
}
func (c primitiveConv) doPrimitiveToInt64(v interface{}, dstType string) (int64, error) {
val := reflect.ValueOf(v)
kind := val.Kind()
switch {
case kind == reflect.String:
return strconv.ParseInt(val.String(), 0, 64)
case kind == reflect.Bool:
if val.Bool() {
return 1, nil
}
return 0, nil
case isKindInt(kind):
return val.Int(), nil
case isKindUint(kind):
u := val.Uint()
if u > math.MaxInt64 {
return 0, errValueOverflow(v, dstType)
}
return int64(val.Uint()), nil
case isKindFloat(kind):
f := val.Float()
return c.doFloat64ToInt64(f, dstType)
case isKindComplex(kind):
// Prevent data loss, ensure the imaginary part is zero.
cpl := val.Complex()
partImag := imag(cpl)
if partImag != 0 {
return 0, errImaginaryPartLoss(v, dstType)
}
partReal := real(cpl)
return c.doFloat64ToInt64(partReal, dstType)
}
return 0, errCantConvertTo(v, dstType)
}
func (c primitiveConv) doFloat64ToInt64(f float64, dstType string) (int64, error) {
if f < math.MinInt64 || f > math.MaxInt64 {
return 0, errValueOverflow(f, dstType)
}
if f != math.Trunc(f) {
return 0, errPrecisionLoss(f, dstType)
}
return int64(f), nil
}
func (c primitiveConv) toInt64(v interface{}) (int64, error) {
return c.doPrimitiveToInt64(v, "int64")
}
func (c primitiveConv) toInt(v interface{}) (int, error) {
num, err := c.doPrimitiveToInt64(v, "int")
if err != nil {
return 0, err
}
if num < minInt || num > maxInt {
return 0, errValueOverflow(v, "int")
}
return int(num), nil
}
func (c primitiveConv) toInt32(v interface{}) (int32, error) {
num, err := c.doPrimitiveToInt64(v, "int32")
if err != nil {
return 0, err
}
if num < math.MinInt32 || num > math.MaxInt32 {
return 0, errValueOverflow(v, "int32")
}
return int32(num), nil
}
func (c primitiveConv) toInt16(v interface{}) (int16, error) {
num, err := c.doPrimitiveToInt64(v, "int16")
if err != nil {
return 0, err
}
if num < math.MinInt16 || num > math.MaxInt16 {
return 0, errValueOverflow(v, "int16")
}
return int16(num), nil
}
func (c primitiveConv) toInt8(v interface{}) (int8, error) {
num, err := c.doPrimitiveToInt64(v, "int8")
if err != nil {
return 0, err
}
if num < math.MinInt8 || num > math.MaxInt8 {
return 0, errValueOverflow(v, "int8")
}
return int8(num), nil
}
func (c primitiveConv) doPrimitiveToUint64(v interface{}, dstType string) (uint64, error) {
val := reflect.ValueOf(v)
kind := val.Kind()
switch {
case kind == reflect.String:
return strconv.ParseUint(val.String(), 0, 64)
case kind == reflect.Bool:
if val.Bool() {
return 1, nil
}
return 0, nil
case isKindInt(kind):
num := val.Int()
if num < 0 {
return 0, errValueOverflow(v, dstType)
}
return uint64(num), nil
case isKindUint(kind):
return val.Uint(), nil
case isKindFloat(kind):
f := val.Float()
return c.doFloatToUint(f, dstType)
case isKindComplex(kind):
// Prevent data loss, ensure the imaginary part is zero.
cpl := val.Complex()
partImag := imag(cpl)
if partImag != 0 {
return 0, errImaginaryPartLoss(v, dstType)
}
partReal := real(cpl)
return c.doFloatToUint(partReal, dstType)
}
return 0, errCantConvertTo(v, dstType)
}
func (c primitiveConv) doFloatToUint(f float64, dstType string) (uint64, error) {
if f < 0 || f > math.MaxUint64 {
return 0, errValueOverflow(f, dstType)
}
if f != math.Trunc(f) {
return 0, errPrecisionLoss(f, dstType)
}
return uint64(f), nil
}
func (c primitiveConv) toUint64(v interface{}) (uint64, error) {
return c.doPrimitiveToUint64(v, "uint64")
}
func (c primitiveConv) toUint(v interface{}) (uint, error) {
num, err := c.doPrimitiveToUint64(v, "uint")
if err != nil {
return 0, err
}
if num > maxUint {
return 0, errValueOverflow(v, "uint")
}
return uint(num), nil
}
func (c primitiveConv) toUint32(v interface{}) (uint32, error) {
num, err := c.doPrimitiveToUint64(v, "uint32")
if err != nil {
return 0, err
}
if num > math.MaxUint32 {
return 0, errValueOverflow(v, "uint32")
}
return uint32(num), nil
}
func (c primitiveConv) toUint16(v interface{}) (uint16, error) {
num, err := c.doPrimitiveToUint64(v, "uint16")
if err != nil {
return 0, err
}
if num > math.MaxUint16 {
return 0, errValueOverflow(v, "uint16")
}
return uint16(num), nil
}
func (c primitiveConv) toUint8(v interface{}) (uint8, error) {
num, err := c.doPrimitiveToUint64(v, "uint8")
if err != nil {
return 0, err
}
if num > math.MaxUint8 {
return 0, errValueOverflow(v, "uint8")
}
return uint8(num), nil
}
func (c primitiveConv) doPrimitiveToFloat64(v interface{}, dstType string) (float64, error) {
val := reflect.ValueOf(v)
kind := val.Kind()
switch {
case kind == reflect.String:
return strconv.ParseFloat(val.String(), 64)
case kind == reflect.Bool:
if val.Bool() {
return 1, nil
}
return 0, nil
case isKindInt(kind):
return float64(val.Int()), nil
case isKindUint(kind):
return float64(val.Uint()), nil
case isKindFloat(kind):
return val.Float(), nil
case isKindComplex(kind):
// Prevent data loss, ensure the imaginary part is zero.
cpl := val.Complex()
partImag := imag(cpl)
if partImag != 0 {
return 0, errImaginaryPartLoss(v, dstType)
}
return real(cpl), nil
}
return 0, errCantConvertTo(v, dstType)
}
func (c primitiveConv) toFloat64(v interface{}) (float64, error) {
return c.doPrimitiveToFloat64(v, "float64")
}
func (c primitiveConv) toFloat32(v interface{}) (float32, error) {
num, err := c.doPrimitiveToFloat64(v, "float32")
if err != nil {
return 0, err
}
if num < -math.MaxFloat32 || num > math.MaxFloat32 {
return 0, errValueOverflow(v, "float32")
}
return float32(num), nil
}
func (c primitiveConv) doPrimitiveToComplex128(v interface{}, dstType string) (complex128, error) {
val := reflect.ValueOf(v)
kind := val.Kind()
switch {
case kind == reflect.String:
return strconv.ParseComplex(val.String(), 128)
case kind == reflect.Bool:
if val.Bool() {
return 1, nil
}
return 0, nil
case isKindInt(kind):
return complex(float64(val.Int()), 0), nil
case isKindUint(kind):
return complex(float64(val.Uint()), 0), nil
case isKindFloat(kind):
return complex(val.Float(), 0), nil
case isKindComplex(kind):
return val.Complex(), nil
}
return 0, errCantConvertTo(v, dstType)
}
func (c primitiveConv) toComplex128(v interface{}) (complex128, error) {
return c.doPrimitiveToComplex128(v, "complex128")
}
func (c primitiveConv) toComplex64(v interface{}) (complex64, error) {
num, err := c.doPrimitiveToComplex128(v, "complex64")
if err != nil {
return 0, err
}
return complex64(num), nil
}