1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-17 19:22:28 +08:00

Merge branch 'main' of github.com:duke-git/lancet into main

This commit is contained in:
dudaodong
2021-12-31 10:18:11 +08:00
2 changed files with 32 additions and 24 deletions

View File

@@ -13,10 +13,12 @@ import (
"unsafe" "unsafe"
) )
// Contain check if the value is in the slice or not // Contain check if the value is in the iterable type or not
func Contain(slice interface{}, value interface{}) bool { func Contain(iterableType interface{}, value interface{}) bool {
v := reflect.ValueOf(slice)
switch reflect.TypeOf(slice).Kind() { v := reflect.ValueOf(iterableType)
switch kind := reflect.TypeOf(iterableType).Kind(); kind {
case reflect.Slice, reflect.Array: case reflect.Slice, reflect.Array:
for i := 0; i < v.Len(); i++ { for i := 0; i < v.Len(); i++ {
if v.Index(i).Interface() == value { if v.Index(i).Interface() == value {
@@ -29,9 +31,15 @@ func Contain(slice interface{}, value interface{}) bool {
return true return true
} }
case reflect.String: case reflect.String:
s := fmt.Sprintf("%v", slice) s := iterableType.(string)
ss := fmt.Sprintf("%v", value) ss, ok := value.(string)
if !ok {
panic("kind mismatch")
}
return strings.Contains(s, ss) return strings.Contains(s, ss)
default:
panic(fmt.Sprintf("kind %s is not support", iterableType))
} }
return false return false
@@ -252,32 +260,34 @@ func InterfaceSlice(slice interface{}) []interface{} {
// StringSlice convert param to slice of string. // StringSlice convert param to slice of string.
func StringSlice(slice interface{}) []string { func StringSlice(slice interface{}) []string {
var res []string
v := sliceValue(slice) v := sliceValue(slice)
out := make([]string, v.Len())
for i := 0; i < v.Len(); i++ { for i := 0; i < v.Len(); i++ {
res = append(res, fmt.Sprint(v.Index(i))) v, ok := v.Index(i).Interface().(string)
if !ok {
panic("invalid element type")
}
out[i] = v
} }
return res return out
} }
// IntSlice convert param to slice of int. // IntSlice convert param to slice of int.
func IntSlice(slice interface{}) ([]int, error) { func IntSlice(slice interface{}) []int {
var res []int
sv := sliceValue(slice) sv := sliceValue(slice)
out := make([]int, sv.Len())
for i := 0; i < sv.Len(); i++ { for i := 0; i < sv.Len(); i++ {
v := sv.Index(i).Interface() v, ok := sv.Index(i).Interface().(int)
switch v := v.(type) { if !ok {
case int: panic("invalid element type")
res = append(res, v)
default:
return nil, errors.New("InvalidSliceElementType")
} }
out[i] = v
} }
return res, nil return out
} }
// ConvertSlice convert original slice to new data type element of slice. // ConvertSlice convert original slice to new data type element of slice.

View File

@@ -237,10 +237,8 @@ func TestIntSlice(t *testing.T) {
} }
func intSlice(t *testing.T, test interface{}, expected []int) { func intSlice(t *testing.T, test interface{}, expected []int) {
res, err := IntSlice(test) res := IntSlice(test)
if err != nil {
t.Error("IntSlice Error: " + err.Error())
}
if !reflect.DeepEqual(res, expected) { if !reflect.DeepEqual(res, expected) {
utils.LogFailedTestInfo(t, "IntSlice", test, expected, res) utils.LogFailedTestInfo(t, "IntSlice", test, expected, res)
t.FailNow() t.FailNow()