Skip to content

Commit 1aa3193

Browse files
author
Thomas Charlot
committed
Add uint converters
1 parent e818efa commit 1aa3193

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

cast.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,57 @@ func AsIntArray(values ...interface{}) ([]int64, bool) {
139139
return arr, b
140140
}
141141

142+
// AsUInt to convert as a uint64
143+
func AsUInt(v interface{}) (uint64, bool) {
144+
switch d := v.(type) {
145+
case int, int8, int16, int32, int64:
146+
n := reflect.ValueOf(d).Int()
147+
if n < 0 {
148+
return 0, false
149+
}
150+
return uint64(n), true
151+
case float32, float64:
152+
n := reflect.ValueOf(d).Float()
153+
if n < 0 {
154+
return 0, false
155+
}
156+
return uint64(n), true
157+
case uint, uint8, uint16, uint32, uint64:
158+
return reflect.ValueOf(d).Uint(), true
159+
case json.Number:
160+
if n, err := d.Int64(); err == nil && n >= 0 {
161+
return uint64(n), true
162+
}
163+
return 0, false
164+
case string:
165+
if i, err := strconv.ParseUint(d, 10, 64); err == nil {
166+
return i, true
167+
}
168+
return 0, false
169+
case bool:
170+
if d {
171+
return 1, true
172+
}
173+
return 0, true
174+
default:
175+
return 0, false
176+
}
177+
}
178+
179+
// AsUIntArray to convert as an array of uint64
180+
func AsUIntArray(values ...interface{}) ([]uint64, bool) {
181+
arr := make([]uint64, len(values))
182+
b := true
183+
for i, v := range values {
184+
if cv, ok := AsUInt(v); ok {
185+
arr[i] = cv
186+
continue
187+
}
188+
b = false
189+
}
190+
return arr, b
191+
}
192+
142193
// AsFloat to convert as a float64
143194
func AsFloat(v interface{}) (float64, bool) {
144195
switch d := v.(type) {

cast_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,42 @@ func TestAsInt(t *testing.T) {
8585
testInt(t, float64(123), 123, true)
8686
}
8787

88+
func TestAsUIntArray(t *testing.T) {
89+
arr, ok := cast.AsUIntArray(1, 2, true)
90+
assert.True(t, ok)
91+
assert.Equal(t, []uint64{1, 2, 1}, arr)
92+
93+
arr, ok = cast.AsUIntArray(1, 2, true, "no", -2)
94+
assert.False(t, ok)
95+
assert.Equal(t, []uint64{1, 2, 1, 0, 0}, arr)
96+
}
97+
98+
func TestAsUInt(t *testing.T) {
99+
testUInt(t, "123", 123, true)
100+
testUInt(t, "-123", 0, false)
101+
testUInt(t, "wrong", 0, false)
102+
testUInt(t, true, 1, true)
103+
testUInt(t, false, 0, true)
104+
testUInt(t, 123, 123, true)
105+
testUInt(t, int8(123), 123, true)
106+
testUInt(t, int8(-123), 0, false)
107+
testUInt(t, int16(123), 123, true)
108+
testUInt(t, int16(-123), 0, false)
109+
testUInt(t, int32(123), 123, true)
110+
testUInt(t, int32(-123), 0, false)
111+
testUInt(t, int64(123), 123, true)
112+
testUInt(t, int64(-123), 0, false)
113+
testUInt(t, uint(123), 123, true)
114+
testUInt(t, uint8(123), 123, true)
115+
testUInt(t, uint16(123), 123, true)
116+
testUInt(t, uint32(123), 123, true)
117+
testUInt(t, uint64(123), 123, true)
118+
testUInt(t, float32(123), 123, true)
119+
testUInt(t, float32(-123), 0, false)
120+
testUInt(t, float64(123), 123, true)
121+
testUInt(t, float64(-123), 0, false)
122+
}
123+
88124
func TestAsIntArray(t *testing.T) {
89125
arr, ok := cast.AsIntArray(1, 2, true)
90126
assert.True(t, ok)
@@ -196,6 +232,12 @@ func testInt(t *testing.T, value interface{}, expected int64, ok bool) {
196232
assert.Equal(t, ok, o)
197233
}
198234

235+
func testUInt(t *testing.T, value interface{}, expected uint64, ok bool) {
236+
b, o := cast.AsUInt(value)
237+
assert.Equal(t, expected, b)
238+
assert.Equal(t, ok, o)
239+
}
240+
199241
func testFloat(t *testing.T, value interface{}, expected float64, ok bool) {
200242
b, o := cast.AsFloat(value)
201243
assert.Equal(t, expected, b)

0 commit comments

Comments
 (0)