-
Notifications
You must be signed in to change notification settings - Fork 0
/
int_slice.go
125 lines (119 loc) · 2.92 KB
/
int_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
package cast
// IntSlicer is the interface that wraps the IntSlice method.
// IntSlice method return []int64
type IntSlicer interface {
IntSlice() []int64
}
//IntSlice cast slice input to int64 slice
func IntSlice(input interface{}) (output []int64, err error) {
var castError error
switch castValue := input.(type) {
case IntSlicer:
output = castValue.IntSlice()
return
case []string:
output = make([]int64, len(castValue))
for index := range castValue {
if output[index], castError = Int(castValue[index]); castError != nil {
//todo. return better error
err = NewCastError("Could not convert to int64")
}
}
return
case []interface{}:
output = make([]int64, len(castValue))
for index := range castValue {
if output[index], castError = Int(castValue[index]); castError != nil {
//todo. return better error
err = NewCastError("Could not convert to int64")
}
}
return
case []int:
output = make([]int64, len(castValue))
for index := range castValue {
output[index], _ = Int(castValue[index])
}
return
case []int8:
output = make([]int64, len(castValue))
for index := range castValue {
output[index], _ = Int(castValue[index])
}
return
case []int16:
output = make([]int64, len(castValue))
for index := range castValue {
output[index], _ = Int(castValue[index])
}
return
case []int32:
output = make([]int64, len(castValue))
for index := range castValue {
output[index], _ = Int(castValue[index])
}
return
case []int64:
output = castValue
return
case []uint:
output = make([]int64, len(castValue))
for index := range castValue {
output[index], _ = Int(castValue[index])
}
return
case []uint8:
output = make([]int64, len(castValue))
for index := range castValue {
output[index], _ = Int(castValue[index])
}
return
case []uint16:
output = make([]int64, len(castValue))
for index := range castValue {
output[index], _ = Int(castValue[index])
}
return
case []uint32:
output = make([]int64, len(castValue))
for index := range castValue {
output[index], _ = Int(castValue[index])
}
return
case []uint64:
output = make([]int64, len(castValue))
for index := range castValue {
output[index], _ = Int(castValue[index])
}
return
case []float32:
output = make([]int64, len(castValue))
for index := range castValue {
output[index], _ = Int(castValue[index])
}
return
case []float64:
output = make([]int64, len(castValue))
for index := range castValue {
output[index], _ = Int(castValue[index])
}
return
case []bool:
output = make([]int64, len(castValue))
for index := range castValue {
output[index], _ = Int(castValue[index])
}
return
default:
err = NewCastError("Could not convert to string")
}
return
}
//MustIntSlice cast input slice to int64 slice and panic if error
func MustIntSlice(input interface{}) []int64 {
output, err := IntSlice(input)
if err != nil {
panic(err)
}
return output
}