2023-06-11 21:17:27 +08:00
|
|
|
// Package wrapper ...
|
|
|
|
//
|
|
|
|
// Description : wrapper ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2023-06-11 21:02
|
|
|
|
package wrapper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-06-12 18:18:16 +08:00
|
|
|
"errors"
|
2023-06-12 18:47:28 +08:00
|
|
|
"fmt"
|
2023-06-12 18:18:16 +08:00
|
|
|
"reflect"
|
2023-06-11 21:17:27 +08:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Array 数组实例
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:03 2023/6/11
|
|
|
|
func Array(value interface{}) *ArrayType {
|
2023-06-12 18:18:16 +08:00
|
|
|
at := &ArrayType{
|
2023-06-11 21:17:27 +08:00
|
|
|
value: value,
|
|
|
|
}
|
2023-06-12 18:18:16 +08:00
|
|
|
at.Convert()
|
|
|
|
return at
|
2023-06-11 21:17:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ArrayType ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:05 2023/6/11
|
|
|
|
type ArrayType struct {
|
2023-06-12 18:18:16 +08:00
|
|
|
value interface{}
|
|
|
|
convertResult []interface{}
|
|
|
|
convertErr error
|
2023-06-12 18:43:14 +08:00
|
|
|
isSimpleSlice bool // 是否简单list, 即数据的每一项类型相同, 且都是基础内置数据类型
|
|
|
|
itemType reflect.Kind // 简单list场景下, 每一项的数据类型
|
2023-06-11 21:17:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsNil 输入是否为nil
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:11 2023/6/11
|
|
|
|
func (at *ArrayType) IsNil() bool {
|
|
|
|
return at.value == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsValid 检测是否为数组类型
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:06 2023/6/11
|
|
|
|
func (at *ArrayType) IsValid() bool {
|
|
|
|
if at.IsNil() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
byteData, err := json.Marshal(at.value)
|
|
|
|
if nil != err {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return strings.HasPrefix(string(byteData), "[") && strings.HasSuffix(string(byteData), "]")
|
|
|
|
}
|
2023-06-11 21:22:08 +08:00
|
|
|
|
|
|
|
// ItemIsInterface 数组每一项是否为interface
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:20 2023/6/11
|
|
|
|
func (at *ArrayType) ItemIsInterface() bool {
|
|
|
|
if !at.IsValid() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if _, ok := at.value.([]interface{}); ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2023-06-12 18:18:16 +08:00
|
|
|
|
|
|
|
// Convert 类型转换
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:59 2023/6/12
|
2023-06-26 18:08:18 +08:00
|
|
|
func (at *ArrayType) Convert() ([]interface{}, error) {
|
2023-06-12 18:18:16 +08:00
|
|
|
if at.IsNil() {
|
|
|
|
// 空指针
|
|
|
|
at.convertResult = make([]interface{}, 0)
|
2023-06-26 18:08:18 +08:00
|
|
|
return at.convertResult, nil
|
2023-06-12 18:18:16 +08:00
|
|
|
}
|
|
|
|
if !at.IsValid() {
|
|
|
|
// 无效slice
|
|
|
|
at.convertErr = errors.New("input slice is invalid")
|
2023-06-26 18:08:18 +08:00
|
|
|
return nil, at.convertErr
|
2023-06-12 18:18:16 +08:00
|
|
|
}
|
|
|
|
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]
|
|
|
|
}
|
|
|
|
}
|
2023-06-26 18:08:18 +08:00
|
|
|
return at.convertResult, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConvertIgnoreError 类型转换并忽略异常
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:07 2023/6/26
|
|
|
|
func (at *ArrayType) ConvertIgnoreError() []interface{} {
|
|
|
|
res, _ := at.Convert()
|
|
|
|
return res
|
2023-06-12 18:18:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unique 对数据结果进行去重
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:43 2023/6/12
|
|
|
|
func (at *ArrayType) Unique() []interface{} {
|
2023-06-12 18:47:28 +08:00
|
|
|
result := make([]interface{}, 0)
|
|
|
|
if at.isSimpleSlice {
|
|
|
|
// 简单数据类型
|
|
|
|
dataTable := make(map[string]bool)
|
|
|
|
for _, item := range at.convertResult {
|
|
|
|
k := fmt.Sprintf("%v", item)
|
|
|
|
if _, exist := dataTable[k]; exist {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dataTable[k] = true
|
|
|
|
result = append(result, item)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2023-06-12 18:18:16 +08:00
|
|
|
return []interface{}{}
|
|
|
|
}
|