From 0cb251f7b8db0ccf2e4b58cf539ad812547e83cd Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 27 Jul 2022 15:12:14 +0800 Subject: [PATCH] feat: add ToSlice and ToSlicePointer and AppendIfAbsent --- slice/slice.go | 55 ++++++++++++++++++++++++++++----------------- slice/slice_test.go | 32 ++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 21 deletions(-) diff --git a/slice/slice.go b/slice/slice.go index 3d7476b..88f5fe8 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -641,7 +641,6 @@ func InsertByIndex(slice interface{}, index int, value interface{}) (interface{} return slice, errors.New("InvalidSliceIndex") } - // value is slice vv := reflect.ValueOf(value) if vv.Kind() == reflect.Slice { if reflect.TypeOf(slice).Elem() != reflect.TypeOf(value).Elem() { @@ -651,7 +650,6 @@ func InsertByIndex(slice interface{}, index int, value interface{}) (interface{} return v.Interface(), nil } - // value is not slice if reflect.TypeOf(slice).Elem() != reflect.TypeOf(value) { return slice, errors.New("InvalidValueType") } @@ -709,21 +707,8 @@ func Unique(slice interface{}) interface{} { for i := 0; i < len(temp); i++ { res.Index(i).Set(reflect.ValueOf(temp[i])) } + return res.Interface() - - // if use map filter, the result slice element order is random, not same as origin slice - //mp := make(map[interface{}]bool) - //for i := 0; i < sv.Len(); i++ { - // v := sv.Index(i).Interface() - // mp[v] = true - //} - // - //var res []interface{} - //for k := range mp { - // res = append(res, mp[k]) - //} - //return res - } // UniqueBy call iteratee func with every item of slice, then remove duplicated. @@ -732,11 +717,6 @@ func UniqueBy(slice, iteratee interface{}) interface{} { sv := sliceValue(slice) fn := functionValue(iteratee) - // elemType := sv.Type().Elem() - // if !checkCallbackFuncSignature2(fn, elemType, elemType) { - // panic("iteratee function signature should be of type func(" + elemType.String() + ")" + elemType.String()) - // } - if sv.Len() == 0 { return slice } @@ -955,3 +935,36 @@ func LastIndexOf(slice, value interface{}) int { return -1 } + +// ToSlicePointer returns a pointer to the slices of a variable parameter transformation +func ToSlicePointer(value ...interface{}) []*interface{} { + out := make([]*interface{}, len(value)) + for i := range value { + out[i] = &value[i] + } + + return out +} + +// ToSlice returns a slices of a variable parameter transformation +func ToSlice(value ...interface{}) interface{} { + rv := reflect.ValueOf(value) + out := reflect.MakeSlice(rv.Type(), len(value), len(value)) + for i := range value { + out.Index(i).Set(reflect.ValueOf(value[i])) + } + return out.Interface() +} + +// AppendIfAbsent only absent append the value +func AppendIfAbsent(slice interface{}, value interface{}) interface{} { + sv := sliceValue(slice) + out := reflect.MakeSlice(sv.Type(), sv.Len(), sv.Len()) + for i := 0; i < sv.Len(); i++ { + out.Index(i).Set(sv.Index(i)) + } + if !Contain(slice, value) { + out = reflect.Append(out, reflect.ValueOf(value)) + } + return out.Interface() +} diff --git a/slice/slice_test.go b/slice/slice_test.go index c801be1..c724d29 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -592,3 +592,35 @@ func TestLastIndexOf(t *testing.T) { assert.Equal(1, LastIndexOf(arr, "a")) assert.Equal(-1, LastIndexOf(arr, "d")) } + +func TestToSlice(t *testing.T) { + assert := internal.NewAssert(t, "TestToSlice") + + str1 := "a" + str2 := "b" + out1 := ToSlice(str1) + out2 := ToSlice(str1, str2) + + assert.Equal([]string{"a"}, StringSlice(out1)) + assert.Equal([]string{"a", "b"}, StringSlice(out2)) +} + +func TestToSlicePointer(t *testing.T) { + assert := internal.NewAssert(t, "TestToSlicePointer") + + var str1 interface{} + str1 = "a" + var str2 interface{} + str2 = "b" + + assert.Equal([]*interface{}{&str1}, ToSlicePointer(str1)) + assert.Equal([]*interface{}{&str1, &str2}, ToSlicePointer(str1, str2)) +} + +func TestToAppendIfAbsent(t *testing.T) { + assert := internal.NewAssert(t, "TestToAppendIfAbsent") + + str1 := []string{"a", "b"} + assert.Equal([]string{"a", "b"}, AppendIfAbsent(str1, "a")) + assert.Equal([]string{"a", "b", "c"}, AppendIfAbsent(str1, "c")) +}