parent
fa673d63c1
commit
60e7a69a42
5
cast.go
5
cast.go
@ -67,6 +67,11 @@ func ToSlice(i interface{}) []interface{} {
|
|||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ToBoolSlice(i interface{}) []bool {
|
||||||
|
v, _ := ToBoolSliceE(i)
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
func ToStringSlice(i interface{}) []string {
|
func ToStringSlice(i interface{}) []string {
|
||||||
v, _ := ToStringSliceE(i)
|
v, _ := ToStringSliceE(i)
|
||||||
return v
|
return v
|
||||||
|
@ -141,6 +141,10 @@ func TestSlices(t *testing.T) {
|
|||||||
assert.Equal(t, []int{1, 3}, ToIntSlice([]interface{}{1.2, 3.2}))
|
assert.Equal(t, []int{1, 3}, ToIntSlice([]interface{}{1.2, 3.2}))
|
||||||
assert.Equal(t, []int{2, 3}, ToIntSlice([]string{"2", "3"}))
|
assert.Equal(t, []int{2, 3}, ToIntSlice([]string{"2", "3"}))
|
||||||
assert.Equal(t, []int{2, 3}, ToIntSlice([2]string{"2", "3"}))
|
assert.Equal(t, []int{2, 3}, ToIntSlice([2]string{"2", "3"}))
|
||||||
|
assert.Equal(t, []bool{true, false, true}, ToBoolSlice([]bool{true, false, true}))
|
||||||
|
assert.Equal(t, []bool{true, false, true}, ToBoolSlice([]interface{}{true, false, true}))
|
||||||
|
assert.Equal(t, []bool{true, false, true}, ToBoolSlice([]int{1,0,1}))
|
||||||
|
assert.Equal(t, []bool{true, false, true}, ToBoolSlice([]string{"true","false","true"}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestToBool(t *testing.T) {
|
func TestToBool(t *testing.T) {
|
||||||
|
32
caste.go
32
caste.go
@ -422,6 +422,38 @@ func ToSliceE(i interface{}) ([]interface{}, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToBoolSliceE casts an empty interface to a []bool.
|
||||||
|
func ToBoolSliceE(i interface{}) ([]bool, error) {
|
||||||
|
jww.DEBUG.Println("ToBoolSliceE called on type:", reflect.TypeOf(i))
|
||||||
|
|
||||||
|
if i == nil {
|
||||||
|
return []bool{}, fmt.Errorf("Unable to Cast %#v to []bool", i)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch v := i.(type) {
|
||||||
|
case []bool:
|
||||||
|
return v, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
kind := reflect.TypeOf(i).Kind()
|
||||||
|
switch kind {
|
||||||
|
case reflect.Slice, reflect.Array:
|
||||||
|
s := reflect.ValueOf(i)
|
||||||
|
a := make([]bool, s.Len())
|
||||||
|
for j := 0; j < s.Len(); j++ {
|
||||||
|
val, err := ToBoolE(s.Index(j).Interface())
|
||||||
|
if err != nil {
|
||||||
|
return []bool{}, fmt.Errorf("Unable to Cast %#v to []bool", i)
|
||||||
|
}
|
||||||
|
a[j] = val
|
||||||
|
}
|
||||||
|
return a, nil
|
||||||
|
default:
|
||||||
|
return []bool{}, fmt.Errorf("Unable to Cast %#v to []bool", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// ToStringSliceE casts an empty interface to a []string.
|
// ToStringSliceE casts an empty interface to a []string.
|
||||||
func ToStringSliceE(i interface{}) ([]string, error) {
|
func ToStringSliceE(i interface{}) ([]string, error) {
|
||||||
jww.TRACE.Println("ToStringSliceE called on type:", reflect.TypeOf(i))
|
jww.TRACE.Println("ToStringSliceE called on type:", reflect.TypeOf(i))
|
||||||
|
Loading…
Reference in New Issue
Block a user