inprove package gconv for empty string converting to number array

This commit is contained in:
jflyfox 2020-12-09 13:39:09 +08:00
parent d2ae383b83
commit 569a953b43
4 changed files with 82 additions and 0 deletions

View File

@ -35,6 +35,11 @@ func Float32s(i interface{}) []float32 {
}
var array []float32
switch value := i.(type) {
case string:
if value == "" {
return []float32{}
}
return []float32{Float32(value)}
case []string:
array = make([]float32, len(value))
for k, v := range value {
@ -136,6 +141,11 @@ func Float64s(i interface{}) []float64 {
}
var array []float64
switch value := i.(type) {
case string:
if value == "" {
return []float64{}
}
return []float64{Float64(value)}
case []string:
array = make([]float64, len(value))
for k, v := range value {

View File

@ -30,6 +30,11 @@ func Ints(i interface{}) []int {
}
var array []int
switch value := i.(type) {
case string:
if value == "" {
return []int{}
}
return []int{Int(value)}
case []string:
array = make([]int, len(value))
for k, v := range value {
@ -141,6 +146,11 @@ func Int32s(i interface{}) []int32 {
}
var array []int32
switch value := i.(type) {
case string:
if value == "" {
return []int32{}
}
return []int32{Int32(value)}
case []string:
array = make([]int32, len(value))
for k, v := range value {
@ -241,6 +251,11 @@ func Int64s(i interface{}) []int64 {
}
var array []int64
switch value := i.(type) {
case string:
if value == "" {
return []int64{}
}
return []int64{Int64(value)}
case []string:
array = make([]int64, len(value))
for k, v := range value {

View File

@ -31,6 +31,11 @@ func Uints(i interface{}) []uint {
var array []uint
switch value := i.(type) {
case string:
if value == "" {
return []uint{}
}
return []uint{Uint(value)}
case []string:
array = make([]uint, len(value))
for k, v := range value {
@ -137,6 +142,11 @@ func Uint32s(i interface{}) []uint32 {
}
var array []uint32
switch value := i.(type) {
case string:
if value == "" {
return []uint32{}
}
return []uint32{Uint32(value)}
case []string:
array = make([]uint32, len(value))
for k, v := range value {
@ -243,6 +253,11 @@ func Uint64s(i interface{}) []uint64 {
}
var array []uint64
switch value := i.(type) {
case string:
if value == "" {
return []uint64{}
}
return []uint64{Uint64(value)}
case []string:
array = make([]uint64, len(value))
for k, v := range value {

View File

@ -25,6 +25,48 @@ func Test_Slice(t *testing.T) {
})
}
func Test_Slice_Empty(t *testing.T) {
// Int.
gtest.C(t, func(t *gtest.T) {
t.AssertEQ(gconv.Ints(""), []int{})
t.Assert(gconv.Ints(nil), nil)
})
gtest.C(t, func(t *gtest.T) {
t.AssertEQ(gconv.Int32s(""), []int32{})
t.Assert(gconv.Int32s(nil), nil)
})
gtest.C(t, func(t *gtest.T) {
t.AssertEQ(gconv.Int64s(""), []int64{})
t.Assert(gconv.Int64s(nil), nil)
})
// Uint.
gtest.C(t, func(t *gtest.T) {
t.AssertEQ(gconv.Uints(""), []uint{})
t.Assert(gconv.Uints(nil), nil)
})
gtest.C(t, func(t *gtest.T) {
t.AssertEQ(gconv.Uint32s(""), []uint32{})
t.Assert(gconv.Uint32s(nil), nil)
})
gtest.C(t, func(t *gtest.T) {
t.AssertEQ(gconv.Uint64s(""), []uint64{})
t.Assert(gconv.Uint64s(nil), nil)
})
// Float.
gtest.C(t, func(t *gtest.T) {
t.AssertEQ(gconv.Floats(""), []float64{})
t.Assert(gconv.Floats(nil), nil)
})
gtest.C(t, func(t *gtest.T) {
t.AssertEQ(gconv.Float32s(""), []float32{})
t.Assert(gconv.Float32s(nil), nil)
})
gtest.C(t, func(t *gtest.T) {
t.AssertEQ(gconv.Float64s(""), []float64{})
t.Assert(gconv.Float64s(nil), nil)
})
}
func Test_Strings(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
array := []*g.Var{