Add `ToInt8`, `ToInt16` and `ToInt32`

This commit is contained in:
chyeh 2017-02-10 15:00:46 +08:00 committed by Bjørn Erik Pedersen
parent f820543c35
commit e4f8127058
3 changed files with 153 additions and 0 deletions

15
cast.go
View File

@ -32,6 +32,21 @@ func ToInt64(i interface{}) int64 {
return v return v
} }
func ToInt32(i interface{}) int32 {
v, _ := ToInt32E(i)
return v
}
func ToInt16(i interface{}) int16 {
v, _ := ToInt16E(i)
return v
}
func ToInt8(i interface{}) int8 {
v, _ := ToInt8E(i)
return v
}
func ToInt(i interface{}) int { func ToInt(i interface{}) int {
v, _ := ToIntE(i) v, _ := ToIntE(i)
return v return v

View File

@ -34,6 +34,39 @@ func TestToInt64(t *testing.T) {
assert.Equal(t, ToInt64(eight), int64(8)) assert.Equal(t, ToInt64(eight), int64(8))
} }
func TestToInt32(t *testing.T) {
var eight interface{} = 8
assert.Equal(t, ToInt32(int32(8)), int32(8))
assert.Equal(t, ToInt32(8), int32(8))
assert.Equal(t, ToInt32(8.31), int32(8))
assert.Equal(t, ToInt32("8"), int32(8))
assert.Equal(t, ToInt32(true), int32(1))
assert.Equal(t, ToInt32(false), int32(0))
assert.Equal(t, ToInt32(eight), int32(8))
}
func TestToInt16(t *testing.T) {
var eight interface{} = 8
assert.Equal(t, ToInt16(int16(8)), int16(8))
assert.Equal(t, ToInt16(8), int16(8))
assert.Equal(t, ToInt16(8.31), int16(8))
assert.Equal(t, ToInt16("8"), int16(8))
assert.Equal(t, ToInt16(true), int16(1))
assert.Equal(t, ToInt16(false), int16(0))
assert.Equal(t, ToInt16(eight), int16(8))
}
func TestToInt8(t *testing.T) {
var eight interface{} = 8
assert.Equal(t, ToInt8(int8(8)), int8(8))
assert.Equal(t, ToInt8(8), int8(8))
assert.Equal(t, ToInt8(8.31), int8(8))
assert.Equal(t, ToInt8("8"), int8(8))
assert.Equal(t, ToInt8(true), int8(1))
assert.Equal(t, ToInt8(false), int8(0))
assert.Equal(t, ToInt8(eight), int8(8))
}
func TestToFloat64(t *testing.T) { func TestToFloat64(t *testing.T) {
var eight interface{} = 8 var eight interface{} = 8
assert.Equal(t, ToFloat64(8), 8.00) assert.Equal(t, ToFloat64(8), 8.00)

105
caste.go
View File

@ -151,6 +151,111 @@ func ToInt64E(i interface{}) (int64, error) {
} }
} }
// ToInt32E casts an empty interface to an int32.
func ToInt32E(i interface{}) (int32, error) {
i = indirect(i)
switch s := i.(type) {
case int64:
return int32(s), nil
case int:
return int32(s), nil
case int32:
return s, nil
case int16:
return int32(s), nil
case int8:
return int32(s), nil
case string:
v, err := strconv.ParseInt(s, 0, 0)
if err == nil {
return int32(v), nil
}
return 0, fmt.Errorf("Unable to Cast %#v to int32", i)
case float64:
return int32(s), nil
case bool:
if bool(s) {
return int32(1), nil
}
return int32(0), nil
case nil:
return int32(0), nil
default:
return int32(0), fmt.Errorf("Unable to Cast %#v to int32", i)
}
}
// ToInt16E casts an empty interface to an int16.
func ToInt16E(i interface{}) (int16, error) {
i = indirect(i)
switch s := i.(type) {
case int64:
return int16(s), nil
case int:
return int16(s), nil
case int32:
return int16(s), nil
case int16:
return s, nil
case int8:
return int16(s), nil
case string:
v, err := strconv.ParseInt(s, 0, 0)
if err == nil {
return int16(v), nil
}
return 0, fmt.Errorf("Unable to Cast %#v to int16", i)
case float64:
return int16(s), nil
case bool:
if bool(s) {
return int16(1), nil
}
return int16(0), nil
case nil:
return int16(0), nil
default:
return int16(0), fmt.Errorf("Unable to Cast %#v to int16", i)
}
}
// ToInt8E casts an empty interface to an int8.
func ToInt8E(i interface{}) (int8, error) {
i = indirect(i)
switch s := i.(type) {
case int64:
return int8(s), nil
case int:
return int8(s), nil
case int32:
return int8(s), nil
case int16:
return int8(s), nil
case int8:
return s, nil
case string:
v, err := strconv.ParseInt(s, 0, 0)
if err == nil {
return int8(v), nil
}
return 0, fmt.Errorf("Unable to Cast %#v to int8", i)
case float64:
return int8(s), nil
case bool:
if bool(s) {
return int8(1), nil
}
return int8(0), nil
case nil:
return int8(0), nil
default:
return int8(0), fmt.Errorf("Unable to Cast %#v to int8", i)
}
}
// ToIntE casts an empty interface to an int. // ToIntE casts an empty interface to an int.
func ToIntE(i interface{}) (int, error) { func ToIntE(i interface{}) (int, error) {
i = indirect(i) i = indirect(i)