-
Notifications
You must be signed in to change notification settings - Fork 0
/
cast.go
287 lines (255 loc) · 8.31 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
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
package pick
import (
"errors"
"reflect"
"github.com/moukoublen/pick/internal/errorsx"
)
type DefaultCaster struct {
directCastFunctionsTypes directCastFunctionsTypes
intCaster intCast[int]
int8Caster intCast[int8]
int16Caster intCast[int16]
int32Caster intCast[int32]
int64Caster intCast[int64]
uintCaster intCast[uint]
uint8Caster intCast[uint8]
uint16Caster intCast[uint16]
uint32Caster intCast[uint32]
uint64Caster intCast[uint64]
}
func NewDefaultCaster() DefaultCaster {
return DefaultCaster{
directCastFunctionsTypes: castFunctionTypes,
intCaster: newIntCast[int](),
int8Caster: newIntCast[int8](),
int16Caster: newIntCast[int16](),
int32Caster: newIntCast[int32](),
int64Caster: newIntCast[int64](),
uintCaster: newIntCast[uint](),
uint8Caster: newIntCast[uint8](),
uint16Caster: newIntCast[uint16](),
uint32Caster: newIntCast[uint32](),
uint64Caster: newIntCast[uint64](),
}
}
//nolint:gocyclo
func (c DefaultCaster) ByType(input any, asType reflect.Type) (any, error) {
// check if direct function is available
switch asType {
case c.directCastFunctionsTypes.typeOfBool:
return c.AsBool(input)
// case c.directCastFunctionsTypes.typeOfByte: // there is no distinguish type for byte. Its only uint8.
// return c.AsByte(input)
case c.directCastFunctionsTypes.typeOfInt8:
return c.AsInt8(input)
case c.directCastFunctionsTypes.typeOfInt16:
return c.AsInt16(input)
case c.directCastFunctionsTypes.typeOfInt32:
return c.AsInt32(input)
case c.directCastFunctionsTypes.typeOfInt64:
return c.AsInt64(input)
case c.directCastFunctionsTypes.typeOfInt:
return c.AsInt(input)
case c.directCastFunctionsTypes.typeOfUint8:
return c.AsUint8(input)
case c.directCastFunctionsTypes.typeOfUint16:
return c.AsUint16(input)
case c.directCastFunctionsTypes.typeOfUint32:
return c.AsUint32(input)
case c.directCastFunctionsTypes.typeOfUint64:
return c.AsUint64(input)
case c.directCastFunctionsTypes.typeOfUint:
return c.AsUint(input)
case c.directCastFunctionsTypes.typeOfFloat32:
return c.AsFloat32(input)
case c.directCastFunctionsTypes.typeOfFloat64:
return c.AsFloat64(input)
case c.directCastFunctionsTypes.typeOfString:
return c.AsString(input)
case c.directCastFunctionsTypes.typeOfTime:
return c.AsTime(input)
case c.directCastFunctionsTypes.typeOfDuration:
return c.AsDuration(input)
case c.directCastFunctionsTypes.typeOfSliceBool:
return c.AsBoolSlice(input)
// case c.directCastFunctionsTypes.typeOfSliceByte: // there is no distinguish type for byte. Its only uint8.
// return c.AsByteSlice(input)
case c.directCastFunctionsTypes.typeOfSliceInt8:
return c.AsInt8Slice(input)
case c.directCastFunctionsTypes.typeOfSliceInt16:
return c.AsInt16Slice(input)
case c.directCastFunctionsTypes.typeOfSliceInt32:
return c.AsInt32Slice(input)
case c.directCastFunctionsTypes.typeOfSliceInt64:
return c.AsInt64Slice(input)
case c.directCastFunctionsTypes.typeOfSliceInt:
return c.AsIntSlice(input)
case c.directCastFunctionsTypes.typeOfSliceUint8:
return c.AsUint8Slice(input)
case c.directCastFunctionsTypes.typeOfSliceUint16:
return c.AsUint16Slice(input)
case c.directCastFunctionsTypes.typeOfSliceUint32:
return c.AsUint32Slice(input)
case c.directCastFunctionsTypes.typeOfSliceUint64:
return c.AsUint64Slice(input)
case c.directCastFunctionsTypes.typeOfSliceUint:
return c.AsUintSlice(input)
case c.directCastFunctionsTypes.typeOfSliceFloat32:
return c.AsFloat32Slice(input)
case c.directCastFunctionsTypes.typeOfSliceFloat64:
return c.AsFloat64Slice(input)
case c.directCastFunctionsTypes.typeOfSliceString:
return c.AsStringSlice(input)
case c.directCastFunctionsTypes.typeOfSliceTime:
return c.AsTimeSlice(input)
case c.directCastFunctionsTypes.typeOfSliceDuration:
return c.AsDurationSlice(input)
}
// check basic types aliases
asKind := asType.Kind()
_, isBasicKind := c.directCastFunctionsTypes.basicKindTypeMap[asKind]
if isBasicKind {
v, err := c.As(input, asKind)
if err != nil {
return nil, err
}
val := reflect.ValueOf(v)
if !val.CanConvert(asType) {
return nil, ErrCastInvalidType
}
return val.Convert(asType).Interface(), nil
}
// slice / array
if asKind == reflect.Array || asKind == reflect.Slice {
return c.sliceByType(input, asType.Elem())
}
// TODO: reflect.Map
// TODO: reflect.Pointer
// TODO: reflect.Interface
// fallback attempt to reflect convert
val := reflect.ValueOf(input)
if !val.CanConvert(asType) {
return nil, ErrCastInvalidType
}
return val.Convert(asType).Interface(), nil
}
func (c DefaultCaster) sliceByType(input any, asSliceElemType reflect.Type) (any, error) {
inputValue := reflect.ValueOf(input)
sc := 1
switch inputValue.Kind() {
case reflect.Array, reflect.Slice:
sc = inputValue.Len()
}
sliceValue := reflect.MakeSlice(reflect.SliceOf(asSliceElemType), sc, sc)
err := forEach(input, func(item any, meta iterationOpMeta) error {
casted, cerr := c.ByType(item, asSliceElemType)
if cerr != nil {
return cerr
}
castedValue := reflect.ValueOf(casted)
sliceValue.Index(meta.Index).Set(castedValue)
return nil
})
if err != nil {
return nil, err
}
return sliceValue.Interface(), nil
}
func (c DefaultCaster) As(input any, asKind reflect.Kind) (any, error) {
//nolint:exhaustive
switch asKind {
case reflect.Float32:
return c.AsFloat32(input)
case reflect.Float64:
return c.AsFloat64(input)
case reflect.Int:
return c.AsInt(input)
case reflect.Int8:
return c.AsInt8(input)
case reflect.Int16:
return c.AsInt16(input)
case reflect.Int32:
return c.AsInt32(input)
case reflect.Int64:
return c.AsInt64(input)
case reflect.Uint:
return c.AsUint(input)
case reflect.Uint8:
return c.AsUint8(input)
case reflect.Uint16:
return c.AsUint16(input)
case reflect.Uint32:
return c.AsUint32(input)
case reflect.Uint64:
return c.AsUint64(input)
case reflect.Bool:
return c.AsBool(input)
case reflect.String:
return c.AsString(input)
}
return nil, newCastError(ErrCastInvalidType, input)
}
// tryCastToBasicType checks input's Kind to identify if it can be casted as a basic type.
// If it can, it casts it and returns it.
// If not, it returns `ErrCannotBeCastedToBasic`.
func tryCastToBasicType(input any) (any, error) {
if input == nil {
return nil, newCastError(ErrCannotBeCastedToBasic, input)
}
t := reflect.TypeOf(input)
k := t.Kind()
if t.String() == k.String() {
return input, newCastError(ErrAlreadyBasicType, input)
}
switch k {
case reflect.Bool:
return tryReflectConvert[bool](input)
case reflect.Int:
return tryReflectConvert[int](input)
case reflect.Int8:
return tryReflectConvert[int8](input)
case reflect.Int16:
return tryReflectConvert[int16](input)
case reflect.Int32:
return tryReflectConvert[int32](input)
case reflect.Int64:
return tryReflectConvert[int64](input)
case reflect.Uint:
return tryReflectConvert[uint](input)
case reflect.Uint8:
return tryReflectConvert[uint8](input)
case reflect.Uint16:
return tryReflectConvert[uint16](input)
case reflect.Uint32:
return tryReflectConvert[uint32](input)
case reflect.Uint64:
return tryReflectConvert[uint64](input)
case reflect.Float32:
return tryReflectConvert[float32](input)
case reflect.Float64:
return tryReflectConvert[float64](input)
case reflect.String:
return tryReflectConvert[string](input)
}
return nil, newCastError(ErrCannotBeCastedToBasic, input)
}
//nolint:ireturn
func tryReflectConvert[Out any](input any) (output Out, err error) {
defer errorsx.RecoverPanicToError(&err)
if input == nil {
return output, newCastError(ErrCastInvalidType, input)
}
typeOfInput := reflect.TypeOf(input)
valueOfInput := reflect.ValueOf(input)
typeOfOutput := reflect.TypeOf(output)
if !typeOfInput.ConvertibleTo(typeOfOutput) {
return output, newCastError(ErrCastInvalidType, input)
}
convertedValue := valueOfInput.Convert(typeOfOutput)
//nolint:forcetypeassert // if we get here we can safely assert.
return convertedValue.Interface().(Out), nil
}
var (
ErrCannotBeCastedToBasic = errors.New("value cannot be casted to basic type")
ErrAlreadyBasicType = errors.New("value is already basic type")
)