forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconv.go
82 lines (68 loc) · 1.65 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
package goutil
import (
"reflect"
"strconv"
"github.com/gookit/goutil/internal/comfunc"
"github.com/gookit/goutil/mathutil"
"github.com/gookit/goutil/reflects"
"github.com/gookit/goutil/strutil"
)
// Bool convert value to bool
func Bool(v any) bool {
bl, _ := comfunc.ToBool(v)
return bl
}
// ToBool try to convert type to bool
func ToBool(v any) (bool, error) {
return comfunc.ToBool(v)
}
// String always convert value to string, will ignore error
func String(v any) string {
s, _ := strutil.AnyToString(v, false)
return s
}
// ToString convert value to string, will return error on fail.
func ToString(v any) (string, error) {
return strutil.AnyToString(v, true)
}
// Int convert value to int
func Int(v any) int {
iv, _ := mathutil.ToInt(v)
return iv
}
// ToInt try to convert value to int
func ToInt(v any) (int, error) {
return mathutil.ToInt(v)
}
// Int64 convert value to int64
func Int64(v any) int64 {
iv, _ := mathutil.ToInt64(v)
return iv
}
// ToInt64 try to convert value to int64
func ToInt64(v any) (int64, error) {
return mathutil.ToInt64(v)
}
// Uint convert value to uint64
func Uint(v any) uint64 {
iv, _ := mathutil.ToUint(v)
return iv
}
// ToUint try to convert value to uint64
func ToUint(v any) (uint64, error) {
return mathutil.ToUint(v)
}
// BaseTypeVal convert custom type or intX,uintX,floatX to generic base type.
//
// intX/unitX => int64
// floatX => float64
// string => string
//
// returns int64,string,float or error
func BaseTypeVal(val any) (value any, err error) {
return reflects.BaseTypeVal(reflect.ValueOf(val))
}
// BoolString convert
func BoolString(bl bool) string {
return strconv.FormatBool(bl)
}