Skip to content

Commit

Permalink
add SliceFloat64E
Browse files Browse the repository at this point in the history
  • Loading branch information
shockerli committed Mar 24, 2021
1 parent 3066b99 commit a3d721f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cvte.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,24 @@ func SliceInt64E(val interface{}) (sl []int64, err error) {
return
}

// SliceFloat64E convert an interface to a []float64 type
func SliceFloat64E(val interface{}) (sl []float64, err error) {
list, err := SliceE(val)
if err != nil {
return
}

for _, v := range list {
vv, err := Float64E(v)
if err != nil {
return nil, err
}
sl = append(sl, vv)
}

return
}

// return the values of struct fields, and deep find the embedded fields
func deepStructValues(rt reflect.Type, rv reflect.Value) (sl []interface{}) {
for j := 0; j < rv.NumField(); j++ {
Expand Down
53 changes: 53 additions & 0 deletions cvte_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,7 @@ func TestSliceInt64E(t *testing.T) {
}{
{[]int{}, nil, false},
{[]int{1, 2, 3}, []int64{1, 2, 3}, false},
{[]float64{1, 2, 3.3, 4.8}, []int64{1, 2, 3, 4}, false},
{[]string{}, nil, false},
{[]interface{}{1, "-1", -1, nil}, []int64{1, -1, -1, 0}, false},
{[...]string{}, nil, false},
Expand Down Expand Up @@ -1513,6 +1514,58 @@ func TestSliceInt64E(t *testing.T) {
}
}

func TestSliceFloat64E(t *testing.T) {
tests := []struct {
input interface{}
expect []float64
isErr bool
}{
{[]int{}, nil, false},
{[]int{1, 2, 3}, []float64{1, 2, 3}, false},
{[]float64{1, 2, 3}, []float64{1, 2, 3}, false},
{[]float64{1.1, 2.2, 3}, []float64{1.1, 2.2, 3}, false},
{[]string{}, nil, false},
{[]interface{}{1, "-1.1", -1.7, nil}, []float64{1, -1.1, -1.7, 0}, false},
{[...]string{}, nil, false},
{[...]string{"1.01", "2.22", "3.30", "-1"}, []float64{1.01, 2.22, 3.3, -1}, false},

// sorted by key asc
{map[int]string{}, nil, false},
{map[int]string{2: "222", 1: "11.1"}, []float64{11.1, 222}, false},
{map[int]TestStructC{}, nil, false},
{map[interface{}]string{
1: "1",
0.9: "0.9",
-1: "-1",
-0.1: "-0.1",
}, []float64{-0.1, -1, 0.9, 1}, false},

{testing.T{}, nil, false},
{&testing.T{}, nil, false},

// errors
{int(123), nil, true},
{uint16(123), nil, true},
{float64(12.3), nil, true},
{func() {}, nil, true},
{nil, nil, true},
{[]string{"a", "b", "c"}, nil, true},
}

for i, tt := range tests {
msg := fmt.Sprintf("i = %d, input[%+v], expect[%+v], isErr[%v]", i, tt.input, tt.expect, tt.isErr)

v, err := cvt.SliceFloat64E(tt.input)
if tt.isErr {
assert.Error(t, err, msg)
continue
}

assert.NoError(t, err, msg)
assert.Equal(t, tt.expect, v, msg)
}
}

func TestFieldE(t *testing.T) {
tests := []struct {
input interface{}
Expand Down

0 comments on commit a3d721f

Please sign in to comment.