From 7524ba931f51523b1ae3abcc9c6b0a81ea2f37a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 12 Jun 2023 18:18:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0array=20convert=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- array.go | 175 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 173 insertions(+), 2 deletions(-) diff --git a/array.go b/array.go index 27e49a7..01d6553 100644 --- a/array.go +++ b/array.go @@ -9,6 +9,8 @@ package wrapper import ( "encoding/json" + "errors" + "reflect" "strings" ) @@ -18,9 +20,11 @@ import ( // // Date : 21:03 2023/6/11 func Array(value interface{}) *ArrayType { - return &ArrayType{ + at := &ArrayType{ value: value, } + at.Convert() + return at } // ArrayType ... @@ -29,7 +33,11 @@ func Array(value interface{}) *ArrayType { // // Date : 21:05 2023/6/11 type ArrayType struct { - value interface{} + value interface{} + convertResult []interface{} + convertErr error + isSimpleSlice bool + itemType reflect.Kind } // IsNil 输入是否为nil @@ -71,3 +79,166 @@ func (at *ArrayType) ItemIsInterface() bool { } return false } + +// Convert 类型转换 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:59 2023/6/12 +func (at *ArrayType) Convert() { + if at.IsNil() { + // 空指针 + at.convertResult = make([]interface{}, 0) + return + } + if !at.IsValid() { + // 无效slice + at.convertErr = errors.New("input slice is invalid") + return + } + switch val := at.value.(type) { + case []int8: + at.isSimpleSlice = true + at.itemType = reflect.Int8 + at.convertResult = make([]interface{}, len(val)) + for i := 0; i < len(val); i++ { + at.convertResult[i] = val[i] + } + case []int16: + at.isSimpleSlice = true + at.itemType = reflect.Int16 + at.convertResult = make([]interface{}, len(val)) + for i := 0; i < len(val); i++ { + at.convertResult[i] = val[i] + } + case []int32: + at.isSimpleSlice = true + at.itemType = reflect.Int32 + at.convertResult = make([]interface{}, len(val)) + for i := 0; i < len(val); i++ { + at.convertResult[i] = val[i] + } + case []int64: + at.isSimpleSlice = true + at.itemType = reflect.Int64 + at.convertResult = make([]interface{}, len(val)) + for i := 0; i < len(val); i++ { + at.convertResult[i] = val[i] + } + case []int: + at.isSimpleSlice = true + at.itemType = reflect.Int + at.convertResult = make([]interface{}, len(val)) + for i := 0; i < len(val); i++ { + at.convertResult[i] = val[i] + } + case []uint: + at.isSimpleSlice = true + at.itemType = reflect.Uint + at.convertResult = make([]interface{}, len(val)) + for i := 0; i < len(val); i++ { + at.convertResult[i] = val[i] + } + case []uint8: + at.isSimpleSlice = true + at.itemType = reflect.Uint8 + at.convertResult = make([]interface{}, len(val)) + for i := 0; i < len(val); i++ { + at.convertResult[i] = val[i] + } + case []uint16: + at.isSimpleSlice = true + at.itemType = reflect.Uint16 + at.convertResult = make([]interface{}, len(val)) + for i := 0; i < len(val); i++ { + at.convertResult[i] = val[i] + } + case []uint32: + at.isSimpleSlice = true + at.itemType = reflect.Int32 + at.convertResult = make([]interface{}, len(val)) + for i := 0; i < len(val); i++ { + at.convertResult[i] = val[i] + } + case []uint64: + at.isSimpleSlice = true + at.itemType = reflect.Int64 + at.convertResult = make([]interface{}, len(val)) + for i := 0; i < len(val); i++ { + at.convertResult[i] = val[i] + } + case []float32: + at.isSimpleSlice = true + at.itemType = reflect.Float32 + at.convertResult = make([]interface{}, len(val)) + for i := 0; i < len(val); i++ { + at.convertResult[i] = val[i] + } + case []float64: + at.isSimpleSlice = true + at.itemType = reflect.Float64 + at.convertResult = make([]interface{}, len(val)) + for i := 0; i < len(val); i++ { + at.convertResult[i] = val[i] + } + case []bool: + at.isSimpleSlice = true + at.itemType = reflect.Bool + at.convertResult = make([]interface{}, len(val)) + for i := 0; i < len(val); i++ { + at.convertResult[i] = val[i] + } + case []string: + at.isSimpleSlice = true + at.itemType = reflect.String + at.convertResult = make([]interface{}, len(val)) + for i := 0; i < len(val); i++ { + at.convertResult[i] = val[i] + } + case []interface{}: + at.isSimpleSlice = false + at.itemType = reflect.Interface + at.convertResult = make([]interface{}, len(val)) + for i := 0; i < len(val); i++ { + at.convertResult[i] = val[i] + } + } +} + +// IsSimpleSlice 是否简单list, 即数据的每一项类型相同, 且都是基础内置数据类型 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:50 2023/6/12 +func (at *ArrayType) IsSimpleSlice() bool { + if at.ItemIsInterface() { + // 每一项都是interface + return false + } + switch at.value.(type) { + case []int8: + case []int16: + case []int32: + case []int64: + case []int: + case []uint: + case []uint8: + case []uint16: + case []uint32: + case []uint64: + case []float32: + case []float64: + case []bool: + + } + return false +} + +// Unique 对数据结果进行去重 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:43 2023/6/12 +func (at *ArrayType) Unique() []interface{} { + return []interface{}{} +}