-
Notifications
You must be signed in to change notification settings - Fork 0
/
uint.go
166 lines (143 loc) · 3.74 KB
/
uint.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"
)
// ToUint converts an interface to a uint type.
func ToUint(i any) uint {
v, _ := ToUintE(i)
return v
}
// ToUint64 converts an interface to a uint64 type.
func ToUint64(i any) uint64 {
v, _ := ToUint64E(i)
return v
}
// ToUint32 converts an interface to a uint32 type.
func ToUint32(i any) uint32 {
v, _ := ToUint32E(i)
return v
}
// ToUint16 converts an interface to a uint16 type.
func ToUint16(i any) uint16 {
v, _ := ToUint16E(i)
return v
}
// ToUint8 converts an interface to a uint8 type.
func ToUint8(i any) uint8 {
v, _ := ToUint8E(i)
return v
}
// ToUintE converts an interface to a uint type.
func ToUintE(i any) (uint, error) {
return ToUnsignedE[uint](i)
}
// ToUint64E converts an interface to a uint64 type.
func ToUint64E(i any) (uint64, error) {
return ToUnsignedE[uint64](i)
}
// ToUint32E converts an interface to a uint32 type.
func ToUint32E(i any) (uint32, error) {
return ToUnsignedE[uint32](i)
}
// ToUint16E converts an interface to a uint16 type.
func ToUint16E(i any) (uint16, error) {
return ToUnsignedE[uint16](i)
}
// ToUint8E converts an interface to a uint type.
func ToUint8E(i any) (uint8, error) {
return ToUnsignedE[uint8](i)
}
// ToUintSlice casts an interface to a []uint type.
func ToUintSlice(i interface{}) []uint {
v, _ := ToUintSliceE(i)
return v
}
// ToUint64Slice casts an interface to a []uint64 type.
func ToUint64Slice(i interface{}) []uint64 {
v, _ := ToUint64SliceE(i)
return v
}
// ToUint32Slice casts an interface to a []uint32 type.
func ToUint32Slice(i interface{}) []uint32 {
v, _ := ToUint32SliceE(i)
return v
}
// ToUintSliceE casts an interface to a []uint type.
func ToUintSliceE(i interface{}) ([]uint, error) {
if i == nil {
return []uint{}, fmt.Errorf("unable to cast %#v of type %T to []uint", i, i)
}
switch v := i.(type) {
case []uint:
return v, nil
}
kind := reflect.TypeOf(i).Kind()
switch kind {
case reflect.Slice, reflect.Array:
s := reflect.ValueOf(i)
a := make([]uint, s.Len())
for j := 0; j < s.Len(); j++ {
val, err := ToUintE(s.Index(j).Interface())
if err != nil {
return []uint{}, fmt.Errorf("unable to cast %#v of type %T to []int", i, i)
}
a[j] = val
}
return a, nil
default:
return []uint{}, fmt.Errorf("unable to cast %#v of type %T to []int", i, i)
}
}
// ToUint64SliceE casts an interface to a []uint64 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 []int64", i, i)
}
a[j] = val
}
return a, nil
default:
return []uint64{}, fmt.Errorf("unable to cast %#v of type %T to []int64", i, i)
}
}
// ToUint32SliceE casts an interface to a []int32 type.
func ToUint32SliceE(i interface{}) ([]uint32, error) {
if i == nil {
return []uint32{}, fmt.Errorf("unable to cast %#v of type %T to []int32", i, i)
}
switch v := i.(type) {
case []uint32:
return v, nil
}
kind := reflect.TypeOf(i).Kind()
switch kind {
case reflect.Slice, reflect.Array:
s := reflect.ValueOf(i)
a := make([]uint32, s.Len())
for j := 0; j < s.Len(); j++ {
val, err := ToUint32E(s.Index(j).Interface())
if err != nil {
return []uint32{}, fmt.Errorf("unable to cast %#v of type %T to []int32", i, i)
}
a[j] = val
}
return a, nil
default:
return []uint32{}, fmt.Errorf("unable to cast %#v of type %T to []int32", i, i)
}
}