From 569a953b43662eff5fdfd5cac609de3c86b59a1e Mon Sep 17 00:00:00 2001 From: jflyfox Date: Wed, 9 Dec 2020 13:39:09 +0800 Subject: [PATCH] inprove package gconv for empty string converting to number array --- util/gconv/gconv_slice_float.go | 10 +++++++ util/gconv/gconv_slice_int.go | 15 ++++++++++ util/gconv/gconv_slice_uint.go | 15 ++++++++++ util/gconv/gconv_z_unit_slice_test.go | 42 +++++++++++++++++++++++++++ 4 files changed, 82 insertions(+) diff --git a/util/gconv/gconv_slice_float.go b/util/gconv/gconv_slice_float.go index 5a35e36f5..0ade14af5 100644 --- a/util/gconv/gconv_slice_float.go +++ b/util/gconv/gconv_slice_float.go @@ -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 { diff --git a/util/gconv/gconv_slice_int.go b/util/gconv/gconv_slice_int.go index f07749c58..7bb461bd5 100644 --- a/util/gconv/gconv_slice_int.go +++ b/util/gconv/gconv_slice_int.go @@ -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 { diff --git a/util/gconv/gconv_slice_uint.go b/util/gconv/gconv_slice_uint.go index ed46ba0f6..60b102dea 100644 --- a/util/gconv/gconv_slice_uint.go +++ b/util/gconv/gconv_slice_uint.go @@ -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 { diff --git a/util/gconv/gconv_z_unit_slice_test.go b/util/gconv/gconv_z_unit_slice_test.go index 9f8e140ef..85fe200ee 100644 --- a/util/gconv/gconv_z_unit_slice_test.go +++ b/util/gconv/gconv_z_unit_slice_test.go @@ -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{