Skip to content

Commit

Permalink
✨ feat: Update Slice function parameters, add noConv to control type …
Browse files Browse the repository at this point in the history
…conversion
  • Loading branch information
sohaha committed Mar 16, 2024
1 parent 60832eb commit dbb5a44
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 22 deletions.
46 changes: 33 additions & 13 deletions ztype/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ztype

import (
"encoding/json"
"reflect"

"github.com/sohaha/zlsgo/zreflect"
)
Expand Down Expand Up @@ -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{}
Expand All @@ -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) {
Expand All @@ -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
}
46 changes: 45 additions & 1 deletion ztype/slice_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ztype

import (
"encoding/json"
"testing"

"github.com/sohaha/zlsgo"
Expand All @@ -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})
Expand All @@ -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) {
Expand All @@ -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()
}
})
}
16 changes: 8 additions & 8 deletions ztype/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit dbb5a44

Please sign in to comment.