Skip to content

Commit fc29686

Browse files
committed
Implement cast method ToFloat64Slice
1 parent 3416201 commit fc29686

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

cast.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@ func ToUintSlice(i interface{}) []uint {
175175
return v
176176
}
177177

178+
// ToFloat64Slice casts an interface to a []float64 type.
179+
func ToFloat64Slice(i interface{}) []float64 {
180+
v, _ := ToFloat64SliceE(i)
181+
return v
182+
}
183+
178184
// ToDurationSlice casts an interface to a []time.Duration type.
179185
func ToDurationSlice(i interface{}) []time.Duration {
180186
v, _ := ToDurationSliceE(i)

cast_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,45 @@ func TestToUintSliceE(t *testing.T) {
719719
}
720720
}
721721

722+
func TestToFloat64SliceE(t *testing.T) {
723+
c := qt.New(t)
724+
725+
tests := []struct {
726+
input interface{}
727+
expect []float64
728+
iserr bool
729+
}{
730+
{[]float64{1.0, 3.0}, []float64{1.0, 3.0}, false},
731+
{[]interface{}{1.0, 3.0}, []float64{1.0, 3.0}, false},
732+
{[]string{"2.0", "3.0"}, []float64{2.0, 3.0}, false},
733+
{[]int{1, 3}, []float64{1.0, 3.0}, false},
734+
{[]int32{1, 3}, []float64{1.0, 3.0}, false},
735+
{[]int64{1, 3}, []float64{1.0, 3.0}, false},
736+
{[]bool{true, false}, []float64{1.0, 0.0}, false},
737+
// errors
738+
{nil, nil, true},
739+
{testing.T{}, nil, true},
740+
{[]string{"foo", "bar"}, nil, true},
741+
}
742+
743+
for i, test := range tests {
744+
errmsg := qt.Commentf("i = %d", i) // assert helper message
745+
746+
v, err := ToFloat64SliceE(test.input)
747+
if test.iserr {
748+
c.Assert(err, qt.IsNotNil)
749+
continue
750+
}
751+
752+
c.Assert(err, qt.IsNil)
753+
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
754+
755+
// Non-E test
756+
v = ToFloat64Slice(test.input)
757+
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
758+
}
759+
}
760+
722761
func TestToSliceE(t *testing.T) {
723762
c := qt.New(t)
724763

caste.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,34 @@ func ToUintSliceE(i interface{}) ([]uint, error) {
13831383
}
13841384
}
13851385

1386+
func ToFloat64SliceE(i interface{}) ([]float64, error) {
1387+
if i == nil {
1388+
return []float64{}, fmt.Errorf("unable to cast %#v of type %T to []float64", i, i)
1389+
}
1390+
1391+
switch v := i.(type) {
1392+
case []float64:
1393+
return v, nil
1394+
}
1395+
1396+
kind := reflect.TypeOf(i).Kind()
1397+
switch kind {
1398+
case reflect.Slice, reflect.Array:
1399+
s := reflect.ValueOf(i)
1400+
a := make([]float64, s.Len())
1401+
for j := 0; j < s.Len(); j++ {
1402+
val, err := ToFloat64E(s.Index(j).Interface())
1403+
if err != nil {
1404+
return []float64{}, fmt.Errorf("unable to cast %#v of type %T to []float64", i, i)
1405+
}
1406+
a[j] = val
1407+
}
1408+
return a, nil
1409+
default:
1410+
return []float64{}, fmt.Errorf("unable to cast %#v of type %T to []float64", i, i)
1411+
}
1412+
}
1413+
13861414
// ToDurationSliceE casts an interface to a []time.Duration type.
13871415
func ToDurationSliceE(i interface{}) ([]time.Duration, error) {
13881416
if i == nil {

0 commit comments

Comments
 (0)