-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.go
98 lines (85 loc) · 2.64 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
package structure
import (
"errors"
"fmt"
"reflect"
)
func Must[T any](t T, err error) T {
if err != nil {
panic(err)
}
return t
}
func ConvertTo[T any](from any) (T, error) {
return ConvertToWithOption[T](from, defaultOption)
}
func MustConvertTo[T any](from any) T {
return Must(ConvertToWithOption[T](from, defaultOption))
}
func ConvertToWithOption[T any](from any, option *Option) (t T, e error) {
if result, err := ConvertToTypeWithOption(from, reflect.TypeOf((*T)(nil)).Elem(), option); err == nil {
return result.(T), nil
} else {
return t, err
}
}
func ConvertToType(from any, toType reflect.Type) (any, error) {
return ConvertToTypeWithOption(from, toType, defaultOption)
}
func MustConvertToType(from any, toType reflect.Type) any {
return Must(ConvertToType(from, toType))
}
func ConvertToTypeWithOption(from any, toType reflect.Type, option *Option) (any, error) {
if option == nil {
option = defaultOption
}
result := reflect.New(toType).Elem()
err := MapToValueWithOption(from, result, option)
if err != nil {
return nil, err
}
return result.Interface(), nil
}
func ConvertToKind(from any, toKind reflect.Kind) (any, error) {
return ConvertToKindWithOption(from, toKind, defaultOption)
}
func MustConvertToKind(from any, toKind reflect.Kind) any {
return Must(ConvertToKind(from, toKind))
}
func ConvertToKindWithOption(from any, toKind reflect.Kind, option *Option) (any, error) {
if option == nil {
option = defaultOption
}
switch toKind {
case reflect.Bool:
return ConvertToTypeWithOption(from, boolType, option)
case reflect.Int:
return ConvertToTypeWithOption(from, intType, option)
case reflect.Int8:
return ConvertToTypeWithOption(from, int8Type, option)
case reflect.Int16:
return ConvertToTypeWithOption(from, int16Type, option)
case reflect.Int32:
return ConvertToTypeWithOption(from, int32Type, option)
case reflect.Int64:
return ConvertToTypeWithOption(from, int64Type, option)
case reflect.Uint:
return ConvertToTypeWithOption(from, uintType, option)
case reflect.Uint8:
return ConvertToTypeWithOption(from, uint8Type, option)
case reflect.Uint16:
return ConvertToTypeWithOption(from, uint16Type, option)
case reflect.Uint32:
return ConvertToTypeWithOption(from, uint32Type, option)
case reflect.Uint64:
return ConvertToTypeWithOption(from, uint64Type, option)
case reflect.Float32:
return ConvertToTypeWithOption(from, float32Type, option)
case reflect.Float64:
return ConvertToTypeWithOption(from, float64Type, option)
case reflect.String:
return ConvertToTypeWithOption(from, stringType, option)
default:
return nil, errors.New(fmt.Sprintf("unsupported kind %s", toKind))
}
}