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