forked from issue9/conv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conv.go
633 lines (572 loc) · 14.3 KB
/
conv.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
// SPDX-License-Identifier: MIT
package conv
import (
"fmt"
"strconv"
"strings"
)
// 抛出一个类型无法转换的错误
// val当前值;t目标类型。
func typeError(val interface{}, t string) error {
return fmt.Errorf("[%T:%v]无法转换成[%v]类型", val, val, t)
}
// 字符串转 bool 值,供 Bool() 函数调用。
// 添加了一些 strconv.ParseFloat 不支持但又比较常用的字符串转换
func str2Bool(str string) (bool, error) {
if val, err := strconv.ParseBool(str); err == nil {
return val, nil
} else if val, err := strconv.ParseFloat(str, 32); err == nil {
return val != 0, nil
}
switch strings.ToLower(strings.TrimSpace(str)) {
case "on":
return true, nil
case "off":
return false, nil
case "true":
return true, nil
case "false":
return false, nil
case "yes":
return true, nil
case "no":
return false, nil
case "1":
return true, nil
case "0":
return false, nil
default:
return false, typeError(str, "bool")
}
}
// Bool 将 val 转换成 bool 类型或是在无法转换的情况下返回 error。
// 以下值被可以被正确转换:
// 123(true), 0(false),"-123"(true), "on"(true), "off"(false), "true"(true), "false"(false)
func Bool(val interface{}) (bool, error) {
switch ret := val.(type) {
case bool:
return ret, nil
case int:
return ret != 0, nil
case int8:
return ret != 0, nil
case int32:
return ret != 0, nil
case int64:
return ret != 0, nil
case float32:
return ret != 0, nil
case float64:
return ret != 0, nil
case uint:
return ret != 0, nil
case uint8:
return ret != 0, nil
case uint32:
return ret != 0, nil
case uint64:
return ret != 0, nil
case []byte:
return str2Bool(string(ret))
case string:
return str2Bool(ret)
default:
return false, typeError(val, "bool")
}
}
// MustBool 将 val 转换成 bool 类型或是在无法转换的情况下返回 def 参数。
func MustBool(val interface{}, def ...bool) bool {
if ret, err := Bool(val); err == nil {
return ret
}
if len(def) == 0 {
panic(typeError(val, "bool"))
}
return def[0]
}
// Uint64 将 val 转换成 uint64 类型或是在无法转换的情况下返回 error。
// 将一个有符号整数转换成无符号整数,负数将返回错误,正数和零正常转换
func Uint64(val interface{}) (uint64, error) {
switch ret := val.(type) {
case uint64:
return ret, nil
case int:
if ret < 0 {
return 0, typeError(ret, "uint64")
}
return uint64(ret), nil
case int8:
if ret < 0 {
return 0, typeError(ret, "uint64")
}
return uint64(ret), nil
case int32:
if ret < 0 {
return 0, typeError(ret, "uint64")
}
return uint64(ret), nil
case int64:
if ret < 0 {
return 0, typeError(ret, "uint64")
}
return uint64(ret), nil
case uint:
return uint64(ret), nil
case uint8:
return uint64(ret), nil
case uint32:
return uint64(ret), nil
case float32:
if ret < 0 {
return 0, typeError(ret, "uint64")
}
return uint64(ret), nil
case float64:
if ret < 0 {
return 0, typeError(ret, "uint64")
}
return uint64(ret), nil
case bool:
if ret {
return 1, nil
}
return 0, nil
case []byte:
if val, err := strconv.ParseFloat(string(ret), 32); err == nil {
return uint64(val), nil
}
return 0, typeError(val, "uint64")
case string:
if val, err := strconv.ParseFloat(ret, 32); err == nil {
return uint64(val), nil
}
return 0, typeError(val, "uint64")
default:
return 0, typeError(ret, "uint64")
}
}
// MustUint64 将 val 转换成 uint64 类型或是在无法转换的情况下返回 def 参数。
func MustUint64(val interface{}, def ...uint64) uint64 {
if ret, err := Uint64(val); err == nil {
return ret
}
if len(def) == 0 {
panic(typeError(val, "uint64"))
}
return def[0]
}
// Uint 将 val 转换成 uint 类型或是在无法转换的情况下返回 error。
func Uint(val interface{}) (uint, error) {
ret, err := Uint64(val)
if err != nil {
return 0, err
}
return uint(ret), nil
}
// MustUint 将 val 转换成 uint 类型或是在无法转换的情况下返回 def 参数。
func MustUint(val interface{}, def ...uint) uint {
if ret, err := Uint64(val); err == nil {
return uint(ret)
}
if len(def) == 0 {
panic(typeError(val, "uint"))
}
return def[0]
}
// Uint8 将 val 转换成 uint8 类型或是在无法转换的情况下返回 error。
func Uint8(val interface{}) (uint8, error) {
ret, err := Uint64(val)
if err != nil {
return 0, err
}
return uint8(ret), nil
}
// MustUint8 将 val 转换成 uint8 类型或是在无法转换的情况下返回 def 参数。
func MustUint8(val interface{}, def ...uint8) uint8 {
if ret, err := Uint64(val); err == nil {
return uint8(ret)
}
if len(def) == 0 {
panic(typeError(val, "uint8"))
}
return def[0]
}
// Uint32 将 val 转换成 uint32 类型或是在无法转换的情况下返回 error。
func Uint32(val interface{}) (uint32, error) {
ret, err := Uint64(val)
if err != nil {
return 0, err
}
return uint32(ret), nil
}
// MustUint32 将 val 转换成 uint32 类型或是在无法转换的情况下返回 def 参数。
func MustUint32(val interface{}, def ...uint32) uint32 {
if ret, err := Uint64(val); err == nil {
return uint32(ret)
}
if len(def) == 0 {
panic(typeError(val, "uint32"))
}
return def[0]
}
// Int64 将 val 转换成 int64 类型或是在无法转换的情况下返回 error。
func Int64(val interface{}) (int64, error) {
switch ret := val.(type) {
case int64:
return ret, nil
case int:
return int64(ret), nil
case int8:
return int64(ret), nil
case int32:
return int64(ret), nil
case uint:
return int64(ret), nil
case uint8:
return int64(ret), nil
case uint32:
return int64(ret), nil
case uint64:
return int64(ret), nil
case float32:
return int64(ret), nil
case float64:
return int64(ret), nil
case bool:
if ret {
return 1, nil
}
return 0, nil
case []byte:
val, err := strconv.ParseFloat(string(ret), 32)
if err == nil {
return int64(val), nil
}
return -1, typeError(val, "int64")
case string:
val, err := strconv.ParseFloat(ret, 32)
if err == nil {
return int64(val), nil
}
return -1, typeError(val, "int64")
default:
return -1, typeError(ret, "int64")
}
}
// MustInt64 将 val 转换成 int64 类型或是在无法转换的情况下返回 def 参数。
func MustInt64(val interface{}, def ...int64) int64 {
if ret, err := Int64(val); err == nil {
return ret
}
if len(def) == 0 {
panic(typeError(val, "int64"))
}
return def[0]
}
// Int 将 val 转换成 int 类型或是在无法转换的情况下返回 error。
func Int(val interface{}) (int, error) {
ret, err := Int64(val)
if err != nil {
return -1, err
}
return int(ret), err
}
// MustInt 将 val 转换成 int 类型或是在无法转换的情况下返回 def 参数。
func MustInt(val interface{}, def ...int) int {
if ret, err := Int64(val); err == nil {
return int(ret)
}
if len(def) == 0 {
panic(typeError(val, "int"))
}
return def[0]
}
// Int8 将 val 转换成 int8 类型或是在无法转换的情况下返回 error。
func Int8(val interface{}) (int8, error) {
ret, err := Int64(val)
if err != nil {
return -1, err
}
return int8(ret), err
}
// MustInt8 将 val 转换成 int8 类型或是在无法转换的情况下返回 def 参数。
func MustInt8(val interface{}, def ...int8) int8 {
if ret, err := Int64(val); err == nil {
return int8(ret)
}
if len(def) == 0 {
panic(typeError(val, "int8"))
}
return def[0]
}
// Int32 将 val 转换成 int32 类型或是在无法转换的情况下返回 error。
func Int32(val interface{}) (int32, error) {
ret, err := Int64(val)
if err != nil {
return -1, err
}
return int32(ret), err
}
// MustInt32 将 val 转换成 int32 类型或是在无法转换的情况下返回 def 参数。
func MustInt32(val interface{}, def ...int32) int32 {
if ret, err := Int64(val); err != nil {
return int32(ret)
}
if len(def) == 0 {
panic(typeError(val, "int32"))
}
return def[0]
}
// Float64 将 val 转换成 float64 类型或是在无法转换的情况下返回 error。
func Float64(val interface{}) (float64, error) {
switch ret := val.(type) {
case float64:
return ret, nil
case int:
return float64(ret), nil
case int8:
return float64(ret), nil
case int32:
return float64(ret), nil
case int64:
return float64(ret), nil
case uint:
return float64(ret), nil
case uint8:
return float64(ret), nil
case uint32:
return float64(ret), nil
case uint64:
return float64(ret), nil
case float32:
return float64(ret), nil
case bool:
if ret {
return 1.0, nil
}
return 0.0, nil
case []byte:
val, err := strconv.ParseFloat(string(ret), 64)
if err == nil {
return float64(val), nil
}
return -1, typeError(val, "float64")
case string:
val, err := strconv.ParseFloat(ret, 64)
if err == nil {
return float64(val), nil
}
return -1, typeError(val, "float64")
default:
return -1, typeError(ret, "float64")
}
}
// MustFloat64 将 val 转换成 float64 类型或是在无法转换的情况下返回 def 参数。
func MustFloat64(val interface{}, def ...float64) float64 {
if ret, err := Float64(val); err == nil {
return ret
}
if len(def) == 0 {
panic(typeError(val, "float64"))
}
return def[0]
}
// Float32 将 val 转换成 float32 类型或是在无法转换的情况下返回 error。
func Float32(val interface{}) (float32, error) {
ret, err := Float64(val)
if err != nil {
return -1.0, err
}
return float32(ret), nil
}
// MustFloat32 将 val 转换成 float32 类型或是在无法转换的情况下返回 def 参数。
func MustFloat32(val interface{}, def ...float32) float32 {
if ret, err := Float64(val); err == nil {
return float32(ret)
}
if len(def) == 0 {
panic(typeError(val, "float32"))
}
return def[0]
}
// String 将 val 转换成 string 类型或是在无法转换的情况下返回 error。
func String(val interface{}) (string, error) {
switch ret := val.(type) {
case string:
return ret, nil
case []byte:
return string(ret), nil
case []rune:
return string(ret), nil
case int64:
return strconv.FormatInt(ret, 10), nil
case int:
return strconv.FormatInt(int64(ret), 10), nil
case int8:
return strconv.FormatInt(int64(ret), 10), nil
case int32:
return strconv.FormatInt(int64(ret), 10), nil
case uint:
return strconv.FormatInt(int64(ret), 10), nil
case uint8:
return strconv.FormatInt(int64(ret), 10), nil
case uint32:
return strconv.FormatInt(int64(ret), 10), nil
case uint64:
return strconv.FormatInt(int64(ret), 10), nil
case float32:
return strconv.FormatFloat(float64(ret), 'f', -1, 32), nil
case float64:
return strconv.FormatFloat(ret, 'f', -1, 64), nil
case bool:
return strconv.FormatBool(ret), nil
case fmt.Stringer:
return ret.String(), nil
case error:
return ret.Error(), nil
default:
return "", typeError(ret, "string")
}
}
// MustString 将 val 转换成 string 类型或是在无法转换的情况下返回 def 参数。
func MustString(val interface{}, def ...string) string {
if ret, err := String(val); err == nil {
return ret
}
if len(def) == 0 {
panic(typeError(val, "string"))
}
return def[0]
}
// Bytes 将 val 转换成 []byte 类型或是在无法转换的情况下返回 error。
func Bytes(val interface{}) ([]byte, error) {
switch ret := val.(type) {
case []byte:
return ret, nil
case string:
return []byte(ret), nil
case int64:
return []byte(strconv.FormatInt(ret, 10)), nil
case int:
return []byte(strconv.FormatInt(int64(ret), 10)), nil
case int8:
return []byte(strconv.FormatInt(int64(ret), 10)), nil
case int32:
return []byte(strconv.FormatInt(int64(ret), 10)), nil
case uint:
return []byte(strconv.FormatInt(int64(ret), 10)), nil
case uint8:
return []byte(strconv.FormatInt(int64(ret), 10)), nil
case uint32:
return []byte(strconv.FormatInt(int64(ret), 10)), nil
case uint64:
return []byte(strconv.FormatInt(int64(ret), 10)), nil
case float32:
return []byte(strconv.FormatFloat(float64(ret), 'f', 5, 32)), nil
case float64:
return []byte(strconv.FormatFloat(ret, 'f', 5, 64)), nil
case bool:
return []byte(strconv.FormatBool(ret)), nil
default:
return nil, typeError(ret, "[]byte")
}
}
// MustBytes 将 val 转换成 []byte 类型或是在无法转换的情况下返回 def 参数。
func MustBytes(val interface{}, def ...[]byte) []byte {
if ret, err := Bytes(val); err == nil {
return ret
}
if len(def) == 0 {
panic(typeError(val, "bytes"))
}
return def[0]
}
// Slice 将 val 转换成 slice 类型或是在无法转换的情况下返回 error。
//
// []int, []interface{} 以及数组都可以转换。
// []byte("123") 返回 []interface{}{byte(49),byte(50),byte(51)}
// "123" 返回 []interface{}{rune(49),rune(50),rune(51)}
func Slice(val interface{}) ([]interface{}, error) {
switch data := val.(type) {
case []interface{}:
return data, nil
case []int:
ret := make([]interface{}, len(data))
for k, v := range data {
ret[k] = v
}
return ret, nil
case []int8:
ret := make([]interface{}, len(data))
for k, v := range data {
ret[k] = v
}
return ret, nil
case []int32:
ret := make([]interface{}, len(data))
for k, v := range data {
ret[k] = v
}
return ret, nil
case []int64:
ret := make([]interface{}, len(data))
for k, v := range data {
ret[k] = v
}
return ret, nil
case []uint:
ret := make([]interface{}, len(data))
for k, v := range data {
ret[k] = v
}
return ret, nil
case []uint8:
ret := make([]interface{}, len(data))
for k, v := range data {
ret[k] = v
}
return ret, nil
case []uint32:
ret := make([]interface{}, len(data))
for k, v := range data {
ret[k] = v
}
return ret, nil
case []uint64:
ret := make([]interface{}, len(data))
for k, v := range data {
ret[k] = v
}
return ret, nil
case []float32:
ret := make([]interface{}, len(data))
for k, v := range data {
ret[k] = v
}
return ret, nil
case []string:
ret := make([]interface{}, len(data))
for k, v := range data {
ret[k] = v
}
return ret, nil
case string:
ret := make([]interface{}, len(data))
for k, v := range data {
ret[k] = v
}
return ret, nil
default:
return nil, typeError(data, "slice")
}
}
// MustSlice 将 val 转换成 slice 类型或是在无法转换的情况下返回 def 参数。
func MustSlice(val interface{}, def ...[]interface{}) []interface{} {
if ret, err := Slice(val); err == nil {
return ret
}
if len(def) == 0 {
panic(typeError(val, "slice"))
}
return def[0]
}