-
Notifications
You must be signed in to change notification settings - Fork 3
/
cast.go
251 lines (229 loc) · 8.56 KB
/
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
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
// Copyright (c) 2014 Dmitry Ponomarev <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package gocast
import (
"context"
"fmt"
"reflect"
)
// TryTo cast any input type into the target
func TryTo(v, to any, tags ...string) (any, error) {
return TryToContext(context.Background(), v, to, tags...)
}
// TryToContext cast any input type into the target
func TryToContext(ctx context.Context, v, to any, tags ...string) (any, error) {
if v == nil {
if vl := reflect.ValueOf(v); !vl.IsValid() || vl.IsNil() {
return nil, nil
}
return nil, wrapError(ErrInvalidParams, `TryToContext: "v" is nil`)
}
return TryToTypeContext(ctx, v, reflect.TypeOf(to), tags...)
}
// TryTo cast any input type into the target
func To(v, to any, tags ...string) any {
val, _ := TryTo(v, to, tags...)
return val
}
// TryToType cast any input type into the target reflection
func TryToType(v any, t reflect.Type, tags ...string) (any, error) {
return TryToTypeContext(context.Background(), v, t, tags...)
}
// TryToTypeContext cast any input type into the target reflection
func TryToTypeContext(ctx context.Context, v any, t reflect.Type, tags ...string) (any, error) {
if v == nil {
if vl := reflect.ValueOf(v); !vl.IsValid() || vl.IsNil() {
return nil, nil
}
return nil, wrapError(ErrInvalidParams, `TryToTypeContext: "v" is nil`)
}
val := reflect.ValueOf(v)
if t == nil { // In case of type is ANY make a copy of data
if k := val.Kind(); k == reflect.Struct || k == reflect.Map || k == reflect.Slice || k == reflect.Array {
return ReflectTryToTypeContext(ctx, val, val.Type(), true, tags...)
}
return v, nil
}
return ReflectTryToTypeContext(ctx, val, t, true, tags...)
}
// ToType cast any input type into the target reflection
func ToType(v any, t reflect.Type, tags ...string) any {
val, _ := TryToType(v, t, tags...)
return val
}
// ReflectTryToType converts reflection value to reflection type or returns error
func ReflectTryToType(v reflect.Value, t reflect.Type, recursive bool, tags ...string) (any, error) {
return ReflectTryToTypeContext(context.Background(), v, t, recursive, tags...)
}
// ReflectTryToTypeContext converts reflection value to reflection type or returns error
func ReflectTryToTypeContext(ctx context.Context, srcVal reflect.Value, t reflect.Type, recursive bool, tags ...string) (any, error) {
v := reflectTarget(srcVal)
if !v.IsValid() {
switch t.Kind() {
case reflect.Ptr, reflect.Interface, reflect.Map, reflect.Slice, reflect.Array, reflect.Chan, reflect.Func:
return nil, nil
default:
return nil, wrapError(ErrInvalidParams, "ReflectTryToTypeContext: `srcVal` is invalid")
}
}
if v.Type() == t {
if k := t.Kind(); k != reflect.Struct &&
k != reflect.Map &&
k != reflect.Array && k != reflect.Slice &&
k != reflect.Interface && k != reflect.Pointer {
return v.Interface(), nil
}
}
var err error
switch t.Kind() {
case reflect.String:
if stringer, _ := srcVal.Interface().(fmt.Stringer); stringer != nil {
return stringer.String(), nil
}
return TryStr(v.Interface())
case reflect.Bool:
return Bool(v.Interface()), nil
case reflect.Int:
return TryNumber[int](v.Interface())
case reflect.Int8:
return TryNumber[int8](v.Interface())
case reflect.Int16:
return TryNumber[int16](v.Interface())
case reflect.Int32:
return TryNumber[int32](v.Interface())
case reflect.Int64:
return TryNumber[int64](v.Interface())
case reflect.Uint:
return TryNumber[uint](v.Interface())
case reflect.Uint8:
return TryNumber[uint8](v.Interface())
case reflect.Uint16:
return TryNumber[uint16](v.Interface())
case reflect.Uint32:
return TryNumber[uint32](v.Interface())
case reflect.Uint64:
return TryNumber[uint64](v.Interface())
case reflect.Float32:
return TryNumber[float32](v.Interface())
case reflect.Float64:
return TryNumber[float64](v.Interface())
case reflect.Slice, reflect.Array:
slice := reflect.New(t)
if err = TryToAnySliceContext(ctx, slice.Interface(), v.Interface(), tags...); err == nil {
return slice.Elem().Interface(), nil
}
case reflect.Map:
mp := reflect.MakeMap(t).Interface()
if err = ToMapContext(ctx, mp, v.Interface(), recursive, tags...); err == nil {
return mp, nil
}
case reflect.Interface:
return v.Interface(), nil
case reflect.Pointer:
var (
vl any
tElem = t.Elem()
)
if tElem.Kind() == reflect.Struct {
newVal := reflect.New(tElem)
if err = TryCopyStructContext(ctx, newVal.Interface(), v.Interface(), tags...); err == nil {
return newVal.Interface(), nil
}
} else if vl, err = ReflectTryToTypeContext(ctx, v, tElem, true, tags...); err == nil {
return reflect.ValueOf(vl).Addr().Interface(), nil
}
case reflect.Struct:
newVal := reflect.New(t)
if err = TryCopyStructContext(ctx, newVal.Interface(), v.Interface(), tags...); err == nil {
return newVal.Elem().Interface(), nil
}
default:
if v.Type() == t {
newVal := reflect.New(t)
reflect.Copy(newVal.Addr(), v.Addr())
return newVal.Interface(), nil
}
}
return nil, err
}
// ReflectToType converts reflection valut to reflection type or returns nil
func ReflectToType(v reflect.Value, t reflect.Type, tags ...string) any {
return ReflectToTypeContext(context.Background(), v, t, tags...)
}
// ReflectToType converts reflection valut to reflection type or returns nil
func ReflectToTypeContext(ctx context.Context, v reflect.Value, t reflect.Type, tags ...string) any {
val, _ := ReflectTryToTypeContext(ctx, v, t, true, tags...)
return val
}
// TryCastValue source type into the target type
func TryCastValue[R any, T any](v T, recursive bool, tags ...string) (R, error) {
return TryCastValueContext[R](context.Background(), v, recursive, tags...)
}
// TryCastValueContext source type into the target type
func TryCastValueContext[R any, T any](ctx context.Context, v T, recursive bool, tags ...string) (R, error) {
var (
rVal R
val, err = TryToContext(ctx, v, rVal, tags...)
)
if err != nil {
return rVal, err
}
switch nval := val.(type) {
case *R:
return *nval, nil
case nil:
return rVal, nil
default:
return val.(R), nil
}
}
// TryCastRecursive source type into the target type with recursive data converting
func TryCastRecursive[R any, T any](v T, tags ...string) (R, error) {
return TryCastRecursiveContext[R](context.Background(), v, tags...)
}
// TryCastRecursiveContext source type into the target type with recursive data converting
func TryCastRecursiveContext[R any, T any](ctx context.Context, v T, tags ...string) (R, error) {
return TryCastValueContext[R](ctx, v, true, tags...)
}
// TryCast source type into the target type
func TryCast[R any, T any](v T, tags ...string) (R, error) {
return TryCastContext[R](context.Background(), v, tags...)
}
// TryCastContext source type into the target type
func TryCastContext[R any, T any](ctx context.Context, v T, tags ...string) (R, error) {
return TryCastValueContext[R](ctx, v, false, tags...)
}
// Cast source type into the target type
func Cast[R any, T any](v T, tags ...string) R {
return CastContext[R](context.Background(), v, tags...)
}
// CastContext source type into the target type
func CastContext[R any, T any](ctx context.Context, v T, tags ...string) R {
val, _ := TryCastContext[R](ctx, v, tags...)
return val
}
// CastRecursive source type into the target type
func CastRecursive[R any, T any](v T, tags ...string) R {
return CastRecursiveContext[R](context.Background(), v, tags...)
}
// CastRecursiveContext source type into the target type
func CastRecursiveContext[R any, T any](ctx context.Context, v T, tags ...string) R {
val, _ := TryCastRecursiveContext[R](ctx, v, tags...)
return val
}