-
Notifications
You must be signed in to change notification settings - Fork 0
/
int.go
166 lines (143 loc) · 3.65 KB
/
int.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
package convx
import (
"fmt"
"reflect"
)
// ToInt converts an interface to an int type.
func ToInt(i any) int {
v, _ := ToIntE(i)
return v
}
// ToInt64 converts an interface to an int64 type.
func ToInt64(i any) int64 {
v, _ := ToInt64E(i)
return v
}
// ToInt32 converts an interface to an int32 type.
func ToInt32(i any) int32 {
v, _ := ToInt32E(i)
return v
}
// ToInt16 converts an interface to an int16 type.
func ToInt16(i any) int16 {
v, _ := ToInt16E(i)
return v
}
// ToInt8 converts an interface to an int8 type.
func ToInt8(i any) int8 {
v, _ := ToInt8E(i)
return v
}
// ToIntE converts an interface to an int type.
func ToIntE(i any) (int, error) {
return ToSignedE[int](i)
}
// ToInt64E converts an interface to an int64 type.
func ToInt64E(i any) (int64, error) {
return ToSignedE[int64](i)
}
// ToInt32E converts an interface to an int32 type.
func ToInt32E(i any) (int32, error) {
return ToSignedE[int32](i)
}
// ToInt16E converts an interface to an int16 type.
func ToInt16E(i any) (int16, error) {
return ToSignedE[int16](i)
}
// ToInt8E converts an interface to an int8 type.
func ToInt8E(i any) (int8, error) {
return ToSignedE[int8](i)
}
// ToIntSlice casts an interface to a []int type.
func ToIntSlice(i interface{}) []int {
v, _ := ToIntSliceE(i)
return v
}
// ToInt64Slice casts an interface to a []int64 type.
func ToInt64Slice(i interface{}) []int64 {
v, _ := ToInt64SliceE(i)
return v
}
// ToInt32Slice casts an interface to a []int32 type.
func ToInt32Slice(i interface{}) []int32 {
v, _ := ToInt32SliceE(i)
return v
}
// 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)
}
}
// ToInt64SliceE casts an interface to a []int64 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)
}
}
// ToInt32SliceE casts an interface to a []int32 type.
func ToInt32SliceE(i interface{}) ([]int32, error) {
if i == nil {
return []int32{}, fmt.Errorf("unable to cast %#v of type %T to []int32", i, i)
}
switch v := i.(type) {
case []int32:
return v, nil
}
kind := reflect.TypeOf(i).Kind()
switch kind {
case reflect.Slice, reflect.Array:
s := reflect.ValueOf(i)
a := make([]int32, s.Len())
for j := 0; j < s.Len(); j++ {
val, err := ToInt32E(s.Index(j).Interface())
if err != nil {
return []int32{}, fmt.Errorf("unable to cast %#v of type %T to []int32", i, i)
}
a[j] = val
}
return a, nil
default:
return []int32{}, fmt.Errorf("unable to cast %#v of type %T to []int32", i, i)
}
}