Skip to content

Commit

Permalink
Add uint converters
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Charlot committed Jun 25, 2019
1 parent e818efa commit 1aa3193
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
51 changes: 51 additions & 0 deletions cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,57 @@ func AsIntArray(values ...interface{}) ([]int64, bool) {
return arr, b
}

// AsUInt to convert as a uint64
func AsUInt(v interface{}) (uint64, bool) {
switch d := v.(type) {
case int, int8, int16, int32, int64:
n := reflect.ValueOf(d).Int()
if n < 0 {
return 0, false
}
return uint64(n), true
case float32, float64:
n := reflect.ValueOf(d).Float()
if n < 0 {
return 0, false
}
return uint64(n), true
case uint, uint8, uint16, uint32, uint64:
return reflect.ValueOf(d).Uint(), true
case json.Number:
if n, err := d.Int64(); err == nil && n >= 0 {
return uint64(n), true
}
return 0, false
case string:
if i, err := strconv.ParseUint(d, 10, 64); err == nil {
return i, true
}
return 0, false
case bool:
if d {
return 1, true
}
return 0, true
default:
return 0, false
}
}

// AsUIntArray to convert as an array of uint64
func AsUIntArray(values ...interface{}) ([]uint64, bool) {
arr := make([]uint64, len(values))
b := true
for i, v := range values {
if cv, ok := AsUInt(v); ok {
arr[i] = cv
continue
}
b = false
}
return arr, b
}

// AsFloat to convert as a float64
func AsFloat(v interface{}) (float64, bool) {
switch d := v.(type) {
Expand Down
42 changes: 42 additions & 0 deletions cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,42 @@ func TestAsInt(t *testing.T) {
testInt(t, float64(123), 123, true)
}

func TestAsUIntArray(t *testing.T) {
arr, ok := cast.AsUIntArray(1, 2, true)
assert.True(t, ok)
assert.Equal(t, []uint64{1, 2, 1}, arr)

arr, ok = cast.AsUIntArray(1, 2, true, "no", -2)
assert.False(t, ok)
assert.Equal(t, []uint64{1, 2, 1, 0, 0}, arr)
}

func TestAsUInt(t *testing.T) {
testUInt(t, "123", 123, true)
testUInt(t, "-123", 0, false)
testUInt(t, "wrong", 0, false)
testUInt(t, true, 1, true)
testUInt(t, false, 0, true)
testUInt(t, 123, 123, true)
testUInt(t, int8(123), 123, true)
testUInt(t, int8(-123), 0, false)
testUInt(t, int16(123), 123, true)
testUInt(t, int16(-123), 0, false)
testUInt(t, int32(123), 123, true)
testUInt(t, int32(-123), 0, false)
testUInt(t, int64(123), 123, true)
testUInt(t, int64(-123), 0, false)
testUInt(t, uint(123), 123, true)
testUInt(t, uint8(123), 123, true)
testUInt(t, uint16(123), 123, true)
testUInt(t, uint32(123), 123, true)
testUInt(t, uint64(123), 123, true)
testUInt(t, float32(123), 123, true)
testUInt(t, float32(-123), 0, false)
testUInt(t, float64(123), 123, true)
testUInt(t, float64(-123), 0, false)
}

func TestAsIntArray(t *testing.T) {
arr, ok := cast.AsIntArray(1, 2, true)
assert.True(t, ok)
Expand Down Expand Up @@ -196,6 +232,12 @@ func testInt(t *testing.T, value interface{}, expected int64, ok bool) {
assert.Equal(t, ok, o)
}

func testUInt(t *testing.T, value interface{}, expected uint64, ok bool) {
b, o := cast.AsUInt(value)
assert.Equal(t, expected, b)
assert.Equal(t, ok, o)
}

func testFloat(t *testing.T, value interface{}, expected float64, ok bool) {
b, o := cast.AsFloat(value)
assert.Equal(t, expected, b)
Expand Down

0 comments on commit 1aa3193

Please sign in to comment.