-
Notifications
You must be signed in to change notification settings - Fork 1
/
slice.go
240 lines (213 loc) · 5.77 KB
/
slice.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
package cast
import (
"fmt"
"reflect"
"strings"
"time"
)
// ToSlice casts an interface to a []interface{} type, discarding any errors.
func ToSlice(i interface{}) []interface{} {
ret, _ := ToSliceE(i)
return ret
}
// ToSliceE casts an interface to a []interface{} type.
func ToSliceE(i interface{}) ([]interface{}, error) {
var s []interface{}
switch t := i.(type) {
case []interface{}:
return append(s, t...), nil
case []map[string]interface{}:
for _, u := range t {
s = append(s, u)
}
return s, nil
default:
return s, fmt.Errorf("unable to cast %#v of type %T to []interface{}", i, i)
}
}
// ToBoolSlice casts an interface to a []bool type, discarding any errors.
func ToBoolSlice(i interface{}) []bool {
ret, _ := ToBoolSliceE(i)
return ret
}
// ToBoolSliceE casts an interface to a []bool type.
func ToBoolSliceE(i interface{}) ([]bool, error) {
if i == nil {
return []bool{}, fmt.Errorf("unable to cast %#v of type %T to []bool", i, i)
}
switch v := i.(type) {
case []bool:
return v, nil
}
kind := reflect.TypeOf(i).Kind()
switch kind {
case reflect.Slice, reflect.Array:
s := reflect.ValueOf(i)
a := make([]bool, s.Len())
for j := 0; j < s.Len(); j++ {
val, err := ToBoolE(s.Index(j).Interface())
if err != nil {
return []bool{}, fmt.Errorf("unable to cast %#v of type %T to []bool", i, i)
}
a[j] = val
}
return a, nil
default:
return []bool{}, fmt.Errorf("unable to cast %#v of type %T to []bool", i, i)
}
}
// ToStringSlice casts an interface to a []string type, discarding any
// errors.
func ToStringSlice(i interface{}) []string {
ret, _ := ToStringSliceE(i)
return ret
}
// ToStringSliceE casts an interface to a []string type.
func ToStringSliceE(i interface{}) ([]string, error) {
var a []string
switch t := i.(type) {
case []interface{}:
for _, val := range t {
v := ToString(val)
a = append(a, v)
}
return a, nil
case []string:
return t, nil
case string:
return strings.Fields(t), nil
case interface{}:
str, err := ToStringE(t)
if err != nil {
return a, fmt.Errorf("unable to cast %#v of type %T to []string", i, i)
}
return []string{str}, nil
default:
return a, fmt.Errorf("unable to cast %#v of type %T to []string", i, i)
}
}
// ToIntSlice casts an interface to a []int type, discarding any errors.
func ToIntSlice(i interface{}) []int {
ret, _ := ToIntSliceE(i)
return ret
}
// ToIntSliceE casts an interface to a []int type.
func ToIntSliceE(i interface{}) ([]int, error) {
if i == nil {
return []int{}, fmt.Errorf("unable to cast %#v of type %T to []int", i, i)
}
switch v := i.(type) {
case []int:
return v, nil
}
kind := reflect.TypeOf(i).Kind()
switch kind {
case reflect.Slice, reflect.Array:
s := reflect.ValueOf(i)
a := make([]int, s.Len())
for j := 0; j < s.Len(); j++ {
val, err := ToIntE(s.Index(j).Interface())
if err != nil {
return []int{}, fmt.Errorf("unable to cast %#v of type %T to []int", i, i)
}
a[j] = val
}
return a, nil
default:
return []int{}, fmt.Errorf("unable to cast %#v of type %T to []int", i, i)
}
}
// ToInt64Slice casts an interface to a []int type, discarding any errors.
func ToInt64Slice(i interface{}) []int64 {
ret, _ := ToInt64SliceE(i)
return ret
}
// ToInt64SliceE casts an interface to a []int type.
func ToInt64SliceE(i interface{}) ([]int64, error) {
if i == nil {
return []int64{}, fmt.Errorf("unable to cast %#v of type %T to []int64", i, i)
}
switch v := i.(type) {
case []int64:
return v, nil
}
kind := reflect.TypeOf(i).Kind()
switch kind {
case reflect.Slice, reflect.Array:
s := reflect.ValueOf(i)
a := make([]int64, s.Len())
for j := 0; j < s.Len(); j++ {
val, err := ToInt64E(s.Index(j).Interface())
if err != nil {
return []int64{}, fmt.Errorf("unable to cast %#v of type %T to []int64", i, i)
}
a[j] = val
}
return a, nil
default:
return []int64{}, fmt.Errorf("unable to cast %#v of type %T to []int64", i, i)
}
}
// ToUint64Slice casts an interface to a []int type, discarding any errors.
func ToUint64Slice(i interface{}) []uint64 {
ret, _ := ToUint64SliceE(i)
return ret
}
// ToUint64SliceE casts an interface to a []int type.
func ToUint64SliceE(i interface{}) ([]uint64, error) {
if i == nil {
return []uint64{}, fmt.Errorf("unable to cast %#v of type %T to []uint64", i, i)
}
switch v := i.(type) {
case []uint64:
return v, nil
}
kind := reflect.TypeOf(i).Kind()
switch kind {
case reflect.Slice, reflect.Array:
s := reflect.ValueOf(i)
a := make([]uint64, s.Len())
for j := 0; j < s.Len(); j++ {
val, err := ToUint64E(s.Index(j).Interface())
if err != nil {
return []uint64{}, fmt.Errorf("unable to cast %#v of type %T to []uint64", i, i)
}
a[j] = val
}
return a, nil
default:
return []uint64{}, fmt.Errorf("unable to cast %#v of type %T to []uint64", i, i)
}
}
// ToDurationSlice casts an interface to a []time.Duration type, discarding
// any errors.
func ToDurationSlice(i interface{}) []time.Duration {
ret, _ := ToDurationSliceE(i)
return ret
}
// ToDurationSliceE casts an interface to a []time.Duration type.
func ToDurationSliceE(i interface{}) ([]time.Duration, error) {
if i == nil {
return []time.Duration{}, fmt.Errorf("unable to cast %#v of type %T to []time.Duration", i, i)
}
switch v := i.(type) {
case []time.Duration:
return v, nil
}
kind := reflect.TypeOf(i).Kind()
switch kind {
case reflect.Slice, reflect.Array:
s := reflect.ValueOf(i)
a := make([]time.Duration, s.Len())
for j := 0; j < s.Len(); j++ {
val, err := ToDurationE(s.Index(j).Interface())
if err != nil {
return []time.Duration{}, fmt.Errorf("unable to cast %#v of type %T to []time.Duration", i, i)
}
a[j] = val
}
return a, nil
default:
return []time.Duration{}, fmt.Errorf("unable to cast %#v of type %T to []time.Duration", i, i)
}
}