-
Notifications
You must be signed in to change notification settings - Fork 1
/
to.array.go
178 lines (169 loc) · 3.97 KB
/
to.array.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
package cast
import (
"reflect"
"github.com/bdlm/errors/v2"
)
// toArray returns a slice containing the specified reflect.Value type
// containing the from value.
//
// Options:
// - DEFAULT: slice, default return value on error.
// - LENGTH: int, size of the backing array length, default 1. Must be greater
// than 0.
func toArray(to reflect.Value, from any, ops Ops) (any, error) {
var ret any
var ok bool
if _, ok = ops[DEFAULT]; ok {
ret = ops[DEFAULT]
}
size := 1
if _, ok = ops[LENGTH]; ok {
size = To[int](ops[LENGTH])
}
if !ok || size < 0 {
return ret, errors.Errorf("invalid channel buffer size %d", size)
}
switch to.Type().Elem().Kind() {
//case reflect.Invalid:
//case reflect.Array:
//case reflect.Func:
//case reflect.Chan:
// return toChan(to.Elem(), from)
//case reflect.Struct:
//case reflect.UnsafePointer:
//case reflect.Map:
//case reflect.Pointer:
//case reflect.Slice:
default:
return ret, errors.Errorf("unable to cast %#.10v of type %T to %T", from, from, to.Interface())
case reflect.Interface:
// v := to.Interface()
// tval, err := ToE[interface{}](from)
// v = append(v, tval)
// if err != nil {
// return ret, err
// }
// ret = make([]interface{}, 1)
// ret = append(ret.([]interface{}), tval)
case reflect.Bool:
tval, err := ToE[bool](from)
if err != nil {
return ret, err
}
ret = make([]bool, size)
ret = append(ret.([]bool), tval)
case reflect.Complex64:
tval, err := ToE[complex64](from)
if err != nil {
return ret, err
}
ret = make([]complex64, size)
ret = append(ret.([]complex64), tval)
case reflect.Complex128:
tval, err := ToE[complex128](from)
if err != nil {
return ret, err
}
ret = make([]complex128, size)
ret = append(ret.([]complex128), tval)
case reflect.Float32:
tval, err := ToE[float32](from)
if err != nil {
return ret, err
}
ret = make([]float32, size)
ret = append(ret.([]float32), tval)
case reflect.Float64:
tval, err := ToE[float64](from)
if err != nil {
return ret, err
}
ret = make([]float64, size)
ret = append(ret.([]float64), tval)
case reflect.Int:
tval, err := ToE[int](from)
if err != nil {
return ret, err
}
ret = make([]int, size)
ret = append(ret.([]int), tval)
case reflect.Int8:
tval, err := ToE[int8](from)
if err != nil {
return ret, err
}
ret = make([]int8, size)
ret = append(ret.([]int8), tval)
case reflect.Int16:
tval, err := ToE[int16](from)
if err != nil {
return ret, err
}
ret = make([]int16, size)
ret = append(ret.([]int16), tval)
case reflect.Int32:
tval, err := ToE[int32](from)
if err != nil {
return ret, err
}
ret = make([]int32, size)
ret = append(ret.([]int32), tval)
case reflect.Int64:
tval, err := ToE[int64](from)
if err != nil {
return ret, err
}
ret = make([]int64, size)
ret = append(ret.([]int64), tval)
case reflect.Uint:
tval, err := ToE[uint](from)
if err != nil {
return ret, err
}
ret = make([]uint, size)
ret = append(ret.([]uint), tval)
case reflect.Uint8:
tval, err := ToE[uint8](from)
if err != nil {
return ret, err
}
ret = make([]uint8, size)
ret = append(ret.([]uint8), tval)
case reflect.Uint16:
tval, err := ToE[uint16](from)
if err != nil {
return ret, err
}
ret = make([]uint16, size)
ret = append(ret.([]uint16), tval)
case reflect.Uint32:
tval, err := ToE[uint32](from)
if err != nil {
return ret, err
}
ret = make([]uint32, size)
ret = append(ret.([]uint32), tval)
case reflect.Uint64:
tval, err := ToE[uint64](from)
if err != nil {
return ret, err
}
ret = make([]uint64, size)
ret = append(ret.([]uint64), tval)
case reflect.Uintptr:
tval, err := ToE[uintptr](from)
if err != nil {
return ret, err
}
ret = make([]uintptr, size)
ret = append(ret.([]uintptr), tval)
case reflect.String:
tval, err := ToE[string](from)
if err != nil {
return ret, err
}
ret = make([]string, size)
ret = append(ret.([]string), tval)
}
return ret, nil
}