-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert.go
239 lines (219 loc) · 5.7 KB
/
convert.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
package goutils
import (
"errors"
"reflect"
"strconv"
"time"
"unsafe"
"github.com/goccy/go-json"
)
// Convert string to any type by [reflect.Type].
func ReflectStrConv(val string, reflectType reflect.Type) (r interface{}, err error) {
switch reflectType {
case reflect.TypeOf(time.Time{}):
r, err = ParseTime(val)
case reflect.TypeOf(time.Duration(0)):
r, err = time.ParseDuration(val)
default:
switch reflectType.Kind() {
case reflect.String:
r = val
case reflect.Int:
r, err = strconv.Atoi(val)
case reflect.Int8:
r, err = strconv.ParseInt(val, 10, 8)
case reflect.Int16:
r, err = strconv.ParseInt(val, 10, 16)
case reflect.Int32:
r, err = strconv.ParseInt(val, 10, 32)
case reflect.Int64:
r, err = strconv.ParseInt(val, 10, 64)
case reflect.Uint:
r, err = strconv.ParseUint(val, 10, 0)
case reflect.Uint8:
r, err = strconv.ParseUint(val, 10, 8)
case reflect.Uint16:
r, err = strconv.ParseUint(val, 10, 16)
case reflect.Uint32:
r, err = strconv.ParseUint(val, 10, 32)
case reflect.Uint64:
r, err = strconv.ParseUint(val, 10, 64)
case reflect.Float32:
r, err = strconv.ParseFloat(val, 32)
case reflect.Float64:
r, err = strconv.ParseFloat(val, 64)
case reflect.Bool:
r, err = strconv.ParseBool(val)
case reflect.Slice:
r = reflect.MakeSlice(reflectType, 0, 0).Interface()
err = json.Unmarshal([]byte(val), &r)
case reflect.Map:
r = reflect.MakeMap(reflectType).Interface()
err = json.Unmarshal([]byte(val), &r)
case reflect.Struct:
r = reflect.New(reflectType).Interface()
err = json.Unmarshal([]byte(val), &r)
case reflect.Interface:
err = errors.New("`reflectType` must be a specific type")
default:
err = errors.New("`reflectType` is not supported")
}
}
return r, err
}
// Convert string to any type
func StrConv[T any](val string) (t T, err error) {
temp, err := ReflectStrConv(val, reflect.TypeOf(t))
if err != nil {
return t, err
}
return Unmarshal[T](temp)
}
// Convert []string to []T by [ReflectStrConv].
func ReflectSliceStrConv(s []string, reflectType reflect.Type) (result []interface{}, err error) {
result = make([]interface{}, len(s))
for i, v := range s {
temp, err := ReflectStrConv(v, reflectType)
if err != nil {
return nil, err
}
result[i] = temp
}
return result, nil
}
// Convert map[string]string to map[string][reflect.Type] by [ReflectStrConv].
func ReflectMapStrConv(m map[string]string, elemType reflect.Type) (result map[string]interface{}, err error) {
result = make(map[string]interface{})
for k, v := range m {
temp, err := ReflectStrConv(v, elemType)
if err != nil {
return nil, err
}
result[k] = temp
}
return result, nil
}
// Convert map[string]string to T by [ReflectMapStrConv], with T is a map[string]any.
func MapStrConv[T any](m map[string]string) (result T, err error) {
var t T
if reflect.TypeOf(m) == reflect.TypeOf(t) {
return reflect.ValueOf(m).Interface().(T), nil
}
temp, err := ReflectMapStrConv(m, reflect.TypeOf(t).Elem())
if err != nil {
return result, err
}
return Unmarshal[T](temp)
}
// Convert map[string]*string to map[string]*T. T can be any type.
// T will be convert by [StrConv].
func MapPtrStrConv[T any](m map[string]*string) (result map[string]*T, err error) {
result = make(map[string]*T)
for k, v := range m {
if v == nil {
result[k] = nil
continue
}
t, err := StrConv[T](*v)
if err != nil {
return nil, err
}
result[k] = &t
}
return result, nil
}
// Convert map[string]interface{} to a struct.
func MapStrAnyToStruct(source map[string]interface{}, dest interface{}) error {
// Convert map to json string
bytes, err := json.Marshal(source)
if err != nil {
return err
}
// Convert json string to struct
if err := json.Unmarshal(bytes, dest); err != nil {
return err
}
return nil
}
// Convert []string to T. T is a slice of any type.
// T will be convert by [StrConv].
func SliceStrConv[T any](s []string) (result []interface{}, err error) {
result = make([]interface{}, len(s))
for i, v := range s {
t, err := StrConv[T](v)
if err != nil {
return nil, err
}
result[i] = t
}
return result, nil
}
// Convert []*string to []*T. T can be any type.
// T will be convert by [StrConv].
func SlicePtrStrConv[T any](s []*string) (result []*T, err error) {
if reflect.TypeOf(new(T)) == reflect.TypeOf(s) {
return *(*[]*T)(unsafe.Pointer(&s)), nil
}
result = make([]*T, len(s))
for i, v := range s {
if v == nil {
result[i] = nil
continue
}
t, err := StrConv[T](*v)
if err != nil {
return nil, err
}
result[i] = &t
}
return result, nil
}
// Convert any to string, throw error
func AnyToStr(val interface{}) (string, error) {
switch val := val.(type) {
case string:
return val, nil
case []byte:
return string(val), nil
case int:
return strconv.Itoa(val), nil
case int8:
return strconv.Itoa(int(val)), nil
case int16:
return strconv.Itoa(int(val)), nil
case int32:
return strconv.Itoa(int(val)), nil
case int64:
return strconv.Itoa(int(val)), nil
case uint:
return strconv.Itoa(int(val)), nil
case uint8:
return strconv.Itoa(int(val)), nil
case uint16:
return strconv.Itoa(int(val)), nil
case uint32:
return strconv.Itoa(int(val)), nil
case uint64:
return strconv.Itoa(int(val)), nil
case float32:
return strconv.FormatFloat(float64(val), 'f', -1, 32), nil
case float64:
return strconv.FormatFloat(val, 'f', -1, 64), nil
case bool:
return strconv.FormatBool(val), nil
case time.Time:
return TimeStr(val), nil
case time.Duration:
return val.String(), nil
default:
return Marshal(val)
}
}
// Convert any to string with error as a result.
func ToStr(val interface{}) string {
str, err := AnyToStr(val)
if err != nil {
return err.Error()
}
return str
}