Skip to content

Commit 01004f2

Browse files
Merge pull request #234 from arui1628/master
Add ToInt64Slice() and ToInt64SliceE()
2 parents 76b8370 + 4f997d9 commit 01004f2

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

cast.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ func ToIntSlice(i interface{}) []int {
169169
return v
170170
}
171171

172+
// ToInt64Slice casts an interface to a []int64 type.
173+
func ToInt64Slice(i interface{}) []int64 {
174+
v, _ := ToInt64SliceE(i)
175+
return v
176+
}
177+
172178
// ToUintSlice casts an interface to a []uint type.
173179
func ToUintSlice(i interface{}) []uint {
174180
v, _ := ToUintSliceE(i)

cast_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,42 @@ func TestToIntSliceE(t *testing.T) {
678678
}
679679
}
680680

681+
func TestToInt64SliceE(t *testing.T) {
682+
c := qt.New(t)
683+
684+
tests := []struct {
685+
input interface{}
686+
expect []int64
687+
iserr bool
688+
}{
689+
{[]int{1, 3}, []int64{1, 3}, false},
690+
{[]interface{}{1.2, 3.2}, []int64{1, 3}, false},
691+
{[]string{"2", "3"}, []int64{2, 3}, false},
692+
{[2]string{"2", "3"}, []int64{2, 3}, false},
693+
// errors
694+
{nil, nil, true},
695+
{testing.T{}, nil, true},
696+
{[]string{"foo", "bar"}, nil, true},
697+
}
698+
699+
for i, test := range tests {
700+
errmsg := qt.Commentf("i = %d", i) // assert helper message
701+
702+
v, err := ToInt64SliceE(test.input)
703+
if test.iserr {
704+
c.Assert(err, qt.IsNotNil)
705+
continue
706+
}
707+
708+
c.Assert(err, qt.IsNil)
709+
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
710+
711+
// Non-E test
712+
v = ToInt64Slice(test.input)
713+
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
714+
}
715+
}
716+
681717
func TestToFloat64SliceE(t *testing.T) {
682718
c := qt.New(t)
683719

caste.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,11 @@ func ToFloat64SliceE(i interface{}) ([]float64, error) {
13361336
return toSliceE(i, ToFloat64E)
13371337
}
13381338

1339+
// ToInt64SliceE casts an interface to a []int64 type.
1340+
func ToInt64SliceE(i interface{}) ([]int64, error) {
1341+
return toSliceE(i, ToInt64E)
1342+
}
1343+
13391344
// ToDurationSliceE casts an interface to a []time.Duration type.
13401345
func ToDurationSliceE(i interface{}) ([]time.Duration, error) {
13411346
return toSliceE(i, ToDurationE)

0 commit comments

Comments
 (0)