Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ func ToInt(i interface{}) int {
return v
}

// ToIntBase10 casts an interface to an int type.
func ToIntBase10(i interface{}) int {
v, _ := ToIntEBase10(i)
return v
}

// ToUint casts an interface to a uint type.
func ToUint(i interface{}) uint {
v, _ := ToUintE(i)
Expand Down
47 changes: 47 additions & 0 deletions cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,52 @@ func TestToUint8E(t *testing.T) {
}
}

func TestToIntEBase10(t *testing.T) {
tests := []struct {
input interface{}
expect int
iserr bool
}{
{"091", 91, false},
{int(8), 8, false},
{int8(8), 8, false},
{int16(8), 8, false},
{int32(8), 8, false},
{int64(8), 8, false},
{uint(8), 8, false},
{uint8(8), 8, false},
{uint16(8), 8, false},
{uint32(8), 8, false},
{uint64(8), 8, false},
{float32(8.31), 8, false},
{float64(8.31), 8, false},
{true, 1, false},
{false, 0, false},
{"8", 8, false},
{nil, 0, false},
// errors
{"test", 0, true},
{testing.T{}, 0, true},
}

for i, test := range tests {
errmsg := fmt.Sprintf("i = %d", i) // assert helper message

v, err := ToIntEBase10(test.input)
if test.iserr {
assert.Error(t, err, errmsg)
continue
}

assert.NoError(t, err, errmsg)
assert.Equal(t, test.expect, v, errmsg)

// Non-E test
v = ToIntBase10(test.input)
assert.Equal(t, test.expect, v, errmsg)
}
}

func TestToIntE(t *testing.T) {
tests := []struct {
input interface{}
Expand All @@ -302,6 +348,7 @@ func TestToIntE(t *testing.T) {
{"8", 8, false},
{nil, 0, false},
// errors
{"091", 0, true},
{"test", 0, true},
{testing.T{}, 0, true},
}
Expand Down
47 changes: 47 additions & 0 deletions caste.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,53 @@ func ToInt8E(i interface{}) (int8, error) {
}
}

// ToIntE casts an interface to an int type.
func ToIntEBase10(i interface{}) (int, error) {
i = indirect(i)

switch s := i.(type) {
case int:
return s, nil
case int64:
return int(s), nil
case int32:
return int(s), nil
case int16:
return int(s), nil
case int8:
return int(s), nil
case uint:
return int(s), nil
case uint64:
return int(s), nil
case uint32:
return int(s), nil
case uint16:
return int(s), nil
case uint8:
return int(s), nil
case float64:
return int(s), nil
case float32:
return int(s), nil
case string:
v, err := strconv.ParseInt(s, 10, 0)
if err == nil {
return int(v), nil
}
return 0, fmt.Errorf("unable to cast %#v of type %T to int", i, i)
case bool:
if s {
return 1, nil
}
return 0, nil
case nil:
return 0, nil
default:
return 0, fmt.Errorf("unable to cast %#v of type %T to int", i, i)
}
}

// ToIntE casts an interface to an int type.
func ToIntE(i interface{}) (int, error) {
i = indirect(i)
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2
)

go 1.13