-
Notifications
You must be signed in to change notification settings - Fork 3
/
type-cast.go
128 lines (110 loc) · 2.67 KB
/
type-cast.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
package config
import (
"reflect"
"time"
"github.com/H-BF/corlib/pkg/functional"
"github.com/pkg/errors"
"github.com/spf13/cast"
"go.uber.org/multierr"
)
// KnownValueTypes ...
type KnownValueTypes interface {
time.Time |
NetCIDR |
~[]NetCIDR |
IP |
~[]IP |
~int | ~uint |
~int64 | ~uint64 |
~int32 | ~uint32 |
~int16 | ~uint16 |
~int8 | ~uint8 |
~float32 | ~float64 |
~string | ~bool
}
func typeCast[T any](in any, ret *T) error {
var c typeCastFunc[T]
err := c.load()
if err == nil {
*ret, err = c(in)
}
return err
}
type typeCastFunc[T any] func(any) (T, error)
func (f *typeCastFunc[T]) load() error {
tyDest := reflect.TypeOf((*T)(nil)).Elem()
kindDest := tyDest.Kind()
if kindDest == reflect.Interface {
*f = func(in any) (r T, e error) {
reflect.ValueOf(&r).Elem().
Set(reflect.ValueOf(in))
return
}
return nil
}
*f = func(in any) (T, error) {
var ret T
if castInvoker := typeCastInvokers[tyDest]; castInvoker != nil {
var v interface{}
var e error
if e1 := castInvoker.InvokeNoResult(in, &v, &e); e1 != nil || e != nil {
return ret, multierr.Combine(e1, e)
}
reflect.ValueOf(&ret).Elem().
Set(
reflect.ValueOf(v),
)
} else {
x := reflect.ValueOf(in)
if !x.Type().ConvertibleTo(tyDest) {
return ret, errors.WithMessagef(ErrTypeCastNotSupported, "for-type('%s')", tyDest)
}
reflect.ValueOf(&ret).Elem().
Set(
x.Convert(tyDest),
)
}
return ret, nil
}
return nil
}
var (
typeCastInvokers = make(map[reflect.Type]functional.Callable)
)
func constructTypeCastInvoker[T KnownValueTypes](c typeCastFunc[T]) functional.Callable {
return functional.MustCallableOf(
func(in interface{}, ret *interface{}, err *error) {
*ret, *err = c(in)
},
)
}
func regTypeCastFunc[T KnownValueTypes](c typeCastFunc[T]) {
var a *T
ty := reflect.TypeOf(a).Elem()
if typeCastInvokers[ty] != nil {
panic(errors.Errorf("('%v') type cast is always registered", ty))
}
typeCastInvokers[ty] = constructTypeCastInvoker(c)
}
func init() {
regTypeCastFunc(cast.ToBoolE)
regTypeCastFunc(cast.ToInt8E)
regTypeCastFunc(cast.ToInt16E)
regTypeCastFunc(cast.ToInt32E)
regTypeCastFunc(cast.ToInt64E)
regTypeCastFunc(cast.ToUint8E)
regTypeCastFunc(cast.ToUint16E)
regTypeCastFunc(cast.ToUint32E)
regTypeCastFunc(cast.ToUint64E)
regTypeCastFunc(cast.ToIntE)
regTypeCastFunc(cast.ToUintE)
regTypeCastFunc(cast.ToStringE)
regTypeCastFunc(cast.ToFloat32E)
regTypeCastFunc(cast.ToFloat64E)
regTypeCastFunc(cast.ToDurationE)
regTypeCastFunc(cast.ToTimeE)
regTypeCastFunc(typeCastNetCIDR)
regTypeCastFunc(typeCastNetCIDRSlice)
regTypeCastFunc(typeCastIP)
regTypeCastFunc(typeCastIPSlice)
}