From dbb5a448570f4b4168b65260675477e713ae0eb3 Mon Sep 17 00:00:00 2001 From: seekwe Date: Sat, 16 Mar 2024 16:22:49 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20Update=20Slice=20function?= =?UTF-8?q?=20parameters,=20add=20noConv=20to=20control=20type=20conversio?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ztype/slice.go | 46 ++++++++++++++++++++++++++++++++------------- ztype/slice_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++- ztype/type.go | 16 ++++++++-------- 3 files changed, 86 insertions(+), 22 deletions(-) diff --git a/ztype/slice.go b/ztype/slice.go index fe46e6c..47c174d 100644 --- a/ztype/slice.go +++ b/ztype/slice.go @@ -2,6 +2,7 @@ package ztype import ( "encoding/json" + "reflect" "github.com/sohaha/zlsgo/zreflect" ) @@ -72,8 +73,8 @@ func (s SliceType) Maps() Maps { // } // Deprecated: please use ToSlice -func Slice(value interface{}) SliceType { - return ToSlice(value) +func Slice(value interface{}, noConv ...bool) SliceType { + return ToSlice(value, noConv...) } // SliceStrToAny []string to []interface{} @@ -85,10 +86,10 @@ func SliceStrToAny(slice []string) []interface{} { return ss } -func ToSlice(value interface{}) SliceType { - s := SliceType{} +func ToSlice(value interface{}, noConv ...bool) (s SliceType) { + s = SliceType{} if value == nil { - return s + return } switch val := value.(type) { @@ -102,19 +103,38 @@ func ToSlice(value interface{}) SliceType { for i := range val { s = append(s, New(val[i])) } - case string: - if val != "" { - s = append(s, New(val)) + case []int: + s = make(SliceType, 0, len(val)) + for i := range val { + s = append(s, New(val[i])) + } + case []int64: + s = make(SliceType, 0, len(val)) + for i := range val { + s = append(s, New(val[i])) } default: var nval []interface{} - if conv.to("", value, zreflect.ValueOf(&nval)) == nil { - s = make(SliceType, 0, len(nval)) - for i := range nval { - s = append(s, New(nval[i])) + vof := zreflect.ValueOf(&nval) + to := func() { + if conv.to("", value, vof) == nil { + s = make(SliceType, 0, len(nval)) + for i := range nval { + s = append(s, New(nval[i])) + } + } + } + + switch vof.Type().Kind() { + case reflect.Slice: + to() + default: + if len(noConv) > 0 && noConv[0] { + return } + to() } } - return s + return } diff --git a/ztype/slice_test.go b/ztype/slice_test.go index 139b121..8f48632 100644 --- a/ztype/slice_test.go +++ b/ztype/slice_test.go @@ -1,6 +1,7 @@ package ztype import ( + "encoding/json" "testing" "github.com/sohaha/zlsgo" @@ -9,7 +10,7 @@ import ( func TestSlice(t *testing.T) { tt := zlsgo.NewTest(t) value := "ddd" - res := Slice(value) + res := Slice(value, false) tt.Log(res) res = Slice([]interface{}{"1", 2, 3.0}) @@ -23,11 +24,18 @@ func TestSlice(t *testing.T) { tt.Equal([]int{0}, res.Int()) tt.Equal([]string{`{"h":"ddd"}`}, res.String()) tt.Equal(map[string]interface{}{"h": "ddd"}, res.Index(0).Value()) + tt.Equal("10086", res.Index(110).String("10086")) + + tt.Equal([]string{"1", "2"}, ToSlice([]int{1, 2}).String()) + tt.Equal([]string{"1", "2"}, ToSlice([]int64{1, 2}).String()) rmaps := res.Maps() tt.Equal(Maps{{"h": "ddd"}}, rmaps) tt.Equal("[]interface {}", GetType(res.Value())) + + j, _ := json.Marshal(res) + tt.Equal(`[{"h":"ddd"}]`, string(j)) } func TestSliceStrToIface(t *testing.T) { @@ -37,3 +45,39 @@ func TestSliceStrToIface(t *testing.T) { tt.Equal(len(value), len(res)) t.Log(res) } + +func TestSliceForce(t *testing.T) { + tt := zlsgo.NewTest(t) + + value := "test" + + tt.Equal([]string{"test"}, ToSlice(value).String()) + tt.Equal([]string{}, ToSlice(value, true).String()) +} + +func BenchmarkSlice(b *testing.B) { + + b.Run("str", func(b *testing.B) { + for i := 0; i < b.N; i++ { + ToSlice("test").String() + } + }) + + b.Run("str_no", func(b *testing.B) { + for i := 0; i < b.N; i++ { + ToSlice("test", true).String() + } + }) + + b.Run("int", func(b *testing.B) { + for i := 0; i < b.N; i++ { + ToSlice([]int{1, 2, 3}).String() + } + }) + + b.Run("any", func(b *testing.B) { + for i := 0; i < b.N; i++ { + ToSlice([]interface{}{1, 2, 3}).String() + } + }) +} diff --git a/ztype/type.go b/ztype/type.go index d02580e..6ae119a 100644 --- a/ztype/type.go +++ b/ztype/type.go @@ -138,23 +138,23 @@ func (t Type) Maps() Maps { return t.Slice().Maps() } -func (t Type) Slice() SliceType { +func (t Type) Slice(noConv ...bool) SliceType { if t.v == nil { return SliceType{} } - return ToSlice(t.v) + return ToSlice(t.v, noConv...) } -func (t Type) SliceValue() []interface{} { - return t.Slice().Value() +func (t Type) SliceValue(noConv ...bool) []interface{} { + return t.Slice(noConv...).Value() } -func (t Type) SliceString() []string { - return t.Slice().String() +func (t Type) SliceString(noConv ...bool) []string { + return t.Slice(noConv...).String() } -func (t Type) SliceInt() []int { - return t.Slice().Int() +func (t Type) SliceInt(noConv ...bool) []int { + return t.Slice(noConv...).Int() } func (t Type) Exists() bool {