forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conv.go
161 lines (149 loc) · 4.03 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
package reflects
import (
"fmt"
"reflect"
"strconv"
"github.com/gookit/goutil/comdef"
"github.com/gookit/goutil/internal/comfunc"
"github.com/gookit/goutil/mathutil"
"github.com/gookit/goutil/strutil"
)
// 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(v reflect.Value) (value any, err error) {
v = reflect.Indirect(v)
switch v.Kind() {
case reflect.String:
value = v.String()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
value = v.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
value = int64(v.Uint()) // always return int64
case reflect.Float32, reflect.Float64:
value = v.Float()
default:
err = comdef.ErrConvType
}
return
}
// ValueByType create reflect.Value by give reflect.Type
func ValueByType(val any, typ reflect.Type) (rv reflect.Value, err error) {
if typ.Kind() <= reflect.Float64 {
return ValueByKind(val, typ.Kind())
}
// check type. like map, slice
newRv := reflect.ValueOf(val)
if newRv.Type() == typ {
return newRv, nil
}
err = comdef.ErrConvType
return
}
// ValueByKind create reflect.Value by give reflect.Kind
//
// TIPs:
//
// Only support kind: string, bool, intX, uintX, floatX
func ValueByKind(val any, kind reflect.Kind) (rv reflect.Value, err error) {
switch kind {
case reflect.Int:
if dstV, err1 := mathutil.ToInt(val); err1 == nil {
rv = reflect.ValueOf(dstV)
}
case reflect.Int8:
if dstV, err1 := mathutil.ToInt(val); err1 == nil {
rv = reflect.ValueOf(int8(dstV))
}
case reflect.Int16:
if dstV, err1 := mathutil.ToInt(val); err1 == nil {
rv = reflect.ValueOf(int16(dstV))
}
case reflect.Int32:
if dstV, err1 := mathutil.ToInt(val); err1 == nil {
rv = reflect.ValueOf(int32(dstV))
}
case reflect.Int64:
if dstV, err1 := mathutil.ToInt64(val); err1 == nil {
rv = reflect.ValueOf(dstV)
}
case reflect.Uint:
if dstV, err1 := mathutil.ToUint(val); err1 == nil {
rv = reflect.ValueOf(uint(dstV))
}
case reflect.Uint8:
if dstV, err1 := mathutil.ToUint(val); err1 == nil {
rv = reflect.ValueOf(uint8(dstV))
}
case reflect.Uint16:
if dstV, err1 := mathutil.ToUint(val); err1 == nil {
rv = reflect.ValueOf(uint16(dstV))
}
case reflect.Uint32:
if dstV, err1 := mathutil.ToUint(val); err1 == nil {
rv = reflect.ValueOf(uint32(dstV))
}
case reflect.Uint64:
if dstV, err1 := mathutil.ToUint(val); err1 == nil {
rv = reflect.ValueOf(dstV)
}
case reflect.Float32:
if dstV, err1 := mathutil.ToFloat(val); err1 == nil {
rv = reflect.ValueOf(float32(dstV))
}
case reflect.Float64:
if dstV, err1 := mathutil.ToFloat(val); err1 == nil {
rv = reflect.ValueOf(dstV)
}
case reflect.String:
if dstV, err1 := strutil.ToString(val); err1 == nil {
rv = reflect.ValueOf(dstV)
}
case reflect.Bool:
if bl, err := comfunc.ToBool(val); err == nil {
rv = reflect.ValueOf(bl)
}
}
if !rv.IsValid() {
err = comdef.ErrConvType
}
return
}
// String convert
func String(rv reflect.Value) string {
s, _ := ValToString(rv, false)
return s
}
// ToString convert
func ToString(rv reflect.Value) (str string, err error) {
return ValToString(rv, true)
}
// ValToString convert handle
func ValToString(rv reflect.Value, defaultAsErr bool) (str string, err error) {
rv = Indirect(rv)
switch rv.Kind() {
case reflect.Invalid:
str = ""
case reflect.Bool:
str = strconv.FormatBool(rv.Bool())
case reflect.String:
str = rv.String()
case reflect.Float32, reflect.Float64:
str = strconv.FormatFloat(rv.Float(), 'f', -1, 64)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
str = strconv.FormatInt(rv.Int(), 10)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
str = strconv.FormatUint(rv.Uint(), 10)
default:
if defaultAsErr {
err = comdef.ErrConvType
} else {
str = fmt.Sprint(rv.Interface())
}
}
return
}