Skip to content

Commit e929a71

Browse files
Merge pull request #236 from nmvalera/master
Add cast methods ToUintSlice
2 parents 77d67b0 + 64a8e08 commit e929a71

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
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+
// ToUintSlice casts an interface to a []uint type.
173+
func ToUintSlice(i interface{}) []uint {
174+
v, _ := ToUintSliceE(i)
175+
return v
176+
}
177+
172178
// ToFloat64Slice casts an interface to a []float64 type.
173179
func ToFloat64Slice(i interface{}) []float64 {
174180
v, _ := ToFloat64SliceE(i)

cast_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,9 @@ func TestToFloat64SliceE(t *testing.T) {
693693
{[]string{"1.2", "3.2"}, []float64{1.2, 3.2}, false},
694694
{[2]string{"2", "3"}, []float64{2, 3}, false},
695695
{[2]string{"1.2", "3.2"}, []float64{1.2, 3.2}, false},
696+
{[]int32{1, 3}, []float64{1.0, 3.0}, false},
697+
{[]int64{1, 3}, []float64{1.0, 3.0}, false},
698+
{[]bool{true, false}, []float64{1.0, 0.0}, false},
696699
// errors
697700
{nil, nil, true},
698701
{testing.T{}, nil, true},
@@ -717,6 +720,47 @@ func TestToFloat64SliceE(t *testing.T) {
717720
}
718721
}
719722

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

caste.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -615,9 +615,6 @@ func ToUint64E(i interface{}) (uint64, error) {
615615
case string:
616616
v, err := strconv.ParseUint(trimZeroDecimal(s), 0, 0)
617617
if err == nil {
618-
if v < 0 {
619-
return 0, errNegativeNotAllowed
620-
}
621618
return v, nil
622619
}
623620
return 0, fmt.Errorf("unable to cast %#v of type %T to uint64", i, i)
@@ -1329,6 +1326,11 @@ func ToIntSliceE(i interface{}) ([]int, error) {
13291326
return toSliceE(i, ToIntE)
13301327
}
13311328

1329+
// ToUintSliceE casts an interface to a []uint type.
1330+
func ToUintSliceE(i interface{}) ([]uint, error) {
1331+
return toSliceE(i, ToUintE)
1332+
}
1333+
13321334
// ToFloat64SliceE casts an interface to a []float64 type.
13331335
func ToFloat64SliceE(i interface{}) ([]float64, error) {
13341336
return toSliceE(i, ToFloat64E)

0 commit comments

Comments
 (0)