/
shortcut.go
363 lines (321 loc) · 11 KB
/
shortcut.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
package conv
import (
"reflect"
"time"
)
// Provides a group of shortcut methods for convenient use, to avoid initializing the Conv struct.
var _defaultConv = new(Conv)
// ConvertType is equivalent to new(Conv).ConvertType() .
func ConvertType(src interface{}, dstTyp reflect.Type) (interface{}, error) {
return _defaultConv.ConvertType(src, dstTyp)
}
// Convert is equivalent to new(Conv).Convert() .
func Convert(src interface{}, dstPtr interface{}) error {
return _defaultConv.Convert(src, dstPtr)
}
// Bool converts the given value to the corresponding value of bool.
// The value must be simple, for which IsSimpleType() returns true.
// It is equivalent to new(Conv).SimpleToBool(v) .
func Bool(v interface{}) (bool, error) {
return _defaultConv.SimpleToBool(v)
}
// String converts the given value to the corresponding value of string.
// The value must be a simple type, for which IsSimpleType() returns true.
// It is equivalent to new(Conv).SimpleToString(v) .
func String(v interface{}) (string, error) {
return _defaultConv.SimpleToString(v)
}
// Int converts the given value to the corresponding value of int.
// The value must be a simple type, for which IsSimpleType() returns true.
// It is equivalent to new(Conv).Convert(v, reflect.TypeOf(int(0))) .
func Int(v interface{}) (int, error) {
res, err := _defaultConv.simpleToPrimitive(v, reflect.Int)
if err != nil {
return 0, err
}
return res.(int), nil
}
// Int64 converts the given value to the corresponding value of int64.
// The value must be a simple type, for which IsSimpleType() returns true.
// It is equivalent to new(Conv).Convert(v, reflect.TypeOf(int64(0))) .
func Int64(v interface{}) (int64, error) {
res, err := _defaultConv.simpleToPrimitive(v, reflect.Int64)
if err != nil {
return 0, err
}
return res.(int64), nil
}
// Int32 converts the given value to the corresponding value of int32.
// The value must be a simple type, for which IsSimpleType() returns true.
// It is equivalent to new(Conv).Convert(v, reflect.TypeOf(int32(0))) .
func Int32(v interface{}) (int32, error) {
res, err := _defaultConv.simpleToPrimitive(v, reflect.Int32)
if err != nil {
return 0, err
}
return res.(int32), nil
}
// Int16 converts the given value to the corresponding value of int16.
// The value must be a simple type, for which IsSimpleType() returns true.
// It is equivalent to new(Conv).Convert(v, reflect.TypeOf(int16(0))) .
func Int16(v interface{}) (int16, error) {
res, err := _defaultConv.simpleToPrimitive(v, reflect.Int16)
if err != nil {
return 0, err
}
return res.(int16), nil
}
// Int8 converts the given value to the corresponding value of int8.
// The value must be a simple type, for which IsSimpleType() returns true.
// It is equivalent to new(Conv).Convert(v, reflect.TypeOf(int8(0))) .
func Int8(v interface{}) (int8, error) {
res, err := _defaultConv.simpleToPrimitive(v, reflect.Int8)
if err != nil {
return 0, err
}
return res.(int8), nil
}
// Uint converts the given value to the corresponding value of uint.
// The value must be a simple type, for which IsSimpleType() returns true.
// It is equivalent to new(Conv).Convert(v, reflect.TypeOf(uint(0))) .
func Uint(v interface{}) (uint, error) {
res, err := _defaultConv.simpleToPrimitive(v, reflect.Uint)
if err != nil {
return 0, err
}
return res.(uint), nil
}
// Uint64 converts the given value to the corresponding value of uint64.
// The value must be a simple type, for which IsSimpleType() returns true.
// It is equivalent to new(Conv).Convert(v, reflect.TypeOf(uint(0))) .
func Uint64(v interface{}) (uint64, error) {
res, err := _defaultConv.simpleToPrimitive(v, reflect.Uint64)
if err != nil {
return 0, err
}
return res.(uint64), nil
}
// Uint32 converts the given value to the corresponding value of uint32.
// The value must be a simple type, for which IsSimpleType() returns true.
// It is equivalent to new(Conv).Convert(v, reflect.TypeOf(uint(0))) .
func Uint32(v interface{}) (uint32, error) {
res, err := _defaultConv.simpleToPrimitive(v, reflect.Uint32)
if err != nil {
return 0, err
}
return res.(uint32), nil
}
// Uint16 converts the given value to the corresponding value of uint16.
// The value must be a simple type, for which IsSimpleType() returns true.
// It is equivalent to new(Conv).Convert(v, reflect.TypeOf(uint(0))) .
func Uint16(v interface{}) (uint16, error) {
res, err := _defaultConv.simpleToPrimitive(v, reflect.Uint16)
if err != nil {
return 0, err
}
return res.(uint16), nil
}
// Uint8 converts the given value to the corresponding value of uint8.
// The value must be a simple type, for which IsSimpleType() returns true.
// It is equivalent to new(Conv).Convert(v, reflect.TypeOf(uint(0))) .
func Uint8(v interface{}) (uint8, error) {
res, err := _defaultConv.simpleToPrimitive(v, reflect.Uint8)
if err != nil {
return 0, err
}
return res.(uint8), nil
}
// Float64 converts the given value to the corresponding value of float64.
// The value must be a simple type, for which IsSimpleType() returns true.
// It is equivalent to new(Conv).Convert(v, reflect.TypeOf(float64(0))) .
func Float64(v interface{}) (float64, error) {
res, err := _defaultConv.simpleToPrimitive(v, reflect.Float64)
if err != nil {
return 0, err
}
return res.(float64), nil
}
// Float32 converts the given value to the corresponding value of float32.
// The value must be a simple type, for which IsSimpleType() returns true.
// It is equivalent to new(Conv).Convert(v, reflect.TypeOf(float32(0))) .
func Float32(v interface{}) (float32, error) {
res, err := _defaultConv.simpleToPrimitive(v, reflect.Float32)
if err != nil {
return 0, err
}
return res.(float32), nil
}
// Complex128 converts the given value to the corresponding value of complex128.
// The value must be a simple type, for which IsSimpleType() returns true.
// It is equivalent to new(Conv).Convert(v, reflect.TypeOf(complex128(0+0i))) .
func Complex128(v interface{}) (complex128, error) {
res, err := _defaultConv.simpleToPrimitive(v, reflect.Complex128)
if err != nil {
return 0, err
}
return res.(complex128), nil
}
// Complex64 converts the given value to the corresponding value of complex64.
// The value must be a simple type, for which IsSimpleType() returns true.
// It is equivalent to new(Conv).Convert(v, reflect.TypeOf(complex64(0+0i))) .
func Complex64(v interface{}) (complex64, error) {
res, err := _defaultConv.simpleToPrimitive(v, reflect.Complex64)
if err != nil {
return 0, err
}
return res.(complex64), nil
}
// Time converts the given value to the corresponding value of time.Time.
// The value must be a simple type, for which IsSimpleType() returns true.
// It is equivalent to new(Conv).SimpleToSimple(v, reflect.TypeOf(time.Time{})) .
func Time(v interface{}) (time.Time, error) {
res, err := _defaultConv.SimpleToSimple(v, typTime)
if err != nil {
return zeroTime, err
}
return res.(time.Time), nil
}
// MapToStruct is equivalent to new(Conv).MapToStruct() .
func MapToStruct(m map[string]interface{}, dstTyp reflect.Type) (interface{}, error) {
return _defaultConv.MapToStruct(m, dstTyp)
}
// StructToMap is equivalent to new(Conv).StructToMap() .
func StructToMap(v interface{}) (map[string]interface{}, error) {
return _defaultConv.StructToMap(v)
}
// MustConvertType is equivalent to new(Conv).MustConvertType() .
func MustConvertType(src interface{}, dstTyp reflect.Type) interface{} {
return _defaultConv.MustConvertType(src, dstTyp)
}
// MustConvert is equivalent to new(Conv).MustConvert() .
func MustConvert(src interface{}, dstPtr interface{}) {
_defaultConv.MustConvert(src, dstPtr)
}
// MustBool is like Bool() but panics instead of returns an error.
func MustBool(v interface{}) bool {
res, err := _defaultConv.SimpleToBool(v)
if err != nil {
panic(err)
}
return res
}
// MustString is like String() but panics instead of returns an error.
func MustString(v interface{}) string {
res, err := _defaultConv.SimpleToString(v)
if err != nil {
panic(err)
}
return res
}
// MustInt is like Int() but panics instead of returns an error.
func MustInt(v interface{}) int {
res, err := _defaultConv.simpleToPrimitive(v, reflect.Int)
if err != nil {
panic(err)
}
return res.(int)
}
// MustInt64 is like Int64() but panics instead of returns an error.
func MustInt64(v interface{}) int64 {
res, err := _defaultConv.simpleToPrimitive(v, reflect.Int64)
if err != nil {
panic(err)
}
return res.(int64)
}
// MustInt32 is like Int32() but panics instead of returns an error.
func MustInt32(v interface{}) int32 {
res, err := _defaultConv.simpleToPrimitive(v, reflect.Int32)
if err != nil {
panic(err)
}
return res.(int32)
}
// MustInt16 is like Int16() but panics instead of returns an error.
func MustInt16(v interface{}) int16 {
res, err := _defaultConv.simpleToPrimitive(v, reflect.Int16)
if err != nil {
panic(err)
}
return res.(int16)
}
// MustInt8 is like Int8() but panics instead of returns an error.
func MustInt8(v interface{}) int8 {
res, err := _defaultConv.simpleToPrimitive(v, reflect.Int8)
if err != nil {
panic(err)
}
return res.(int8)
}
// MustUint is like Uint() but panics instead of returns an error.
func MustUint(v interface{}) uint {
res, err := _defaultConv.simpleToPrimitive(v, reflect.Uint)
if err != nil {
panic(err)
}
return res.(uint)
}
// MustUint64 is like Uint64() but panics instead of returns an error.
func MustUint64(v interface{}) uint64 {
res, err := _defaultConv.simpleToPrimitive(v, reflect.Uint64)
if err != nil {
panic(err)
}
return res.(uint64)
}
// MustUint32 is like Uint32() but panics instead of returns an error.
func MustUint32(v interface{}) uint32 {
res, err := _defaultConv.simpleToPrimitive(v, reflect.Uint32)
if err != nil {
panic(err)
}
return res.(uint32)
}
// MustUint16 is like Uint16() but panics instead of returns an error.
func MustUint16(v interface{}) uint16 {
res, err := _defaultConv.simpleToPrimitive(v, reflect.Uint16)
if err != nil {
panic(err)
}
return res.(uint16)
}
// MustUint8 is like Uint8() but panics instead of returns an error.
func MustUint8(v interface{}) uint8 {
res, err := _defaultConv.simpleToPrimitive(v, reflect.Uint8)
if err != nil {
panic(err)
}
return res.(uint8)
}
// MustFloat64 is like Float64() but panics instead of returns an error.
func MustFloat64(v interface{}) float64 {
res, err := _defaultConv.simpleToPrimitive(v, reflect.Float64)
if err != nil {
panic(err)
}
return res.(float64)
}
// MustFloat32 is like Float32() but panics instead of returns an error.
func MustFloat32(v interface{}) float32 {
res, err := _defaultConv.simpleToPrimitive(v, reflect.Float32)
if err != nil {
panic(err)
}
return res.(float32)
}
// MustMapToStruct is like MapToStruct() but panics instead of returns an error.
func MustMapToStruct(m map[string]interface{}, dstTyp reflect.Type) interface{} {
res, err := _defaultConv.MapToStruct(m, dstTyp)
if err != nil {
panic(err)
}
return res
}
// MustStructToMap is like StructToMap() but panics instead of returns an error.
func MustStructToMap(v interface{}) map[string]interface{} {
res, err := _defaultConv.StructToMap(v)
if err != nil {
panic(err)
}
return res
}