增加数组去重

This commit is contained in:
白茶清欢 2024-05-06 14:29:33 +08:00
parent d1b9779946
commit 9ae0c8f4be

View File

@ -20,7 +20,7 @@ import (
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 21:03 2023/6/11 // Date : 21:03 2023/6/11
func ArrayType(value interface{}) *Array { func ArrayType(value any) *Array {
at := &Array{ at := &Array{
value: value, value: value,
} }
@ -34,8 +34,8 @@ func ArrayType(value interface{}) *Array {
// //
// Date : 21:05 2023/6/11 // Date : 21:05 2023/6/11
type Array struct { type Array struct {
value interface{} value any
convertResult []interface{} convertResult []any
convertErr error convertErr error
isSimpleSlice bool // 是否简单list, 即数据的每一项类型相同, 且都是基础内置数据类型 isSimpleSlice bool // 是否简单list, 即数据的每一项类型相同, 且都是基础内置数据类型
itemType reflect.Kind // 简单list场景下, 每一项的数据类型 itemType reflect.Kind // 简单list场景下, 每一项的数据类型
@ -75,7 +75,7 @@ func (at *Array) ItemIsInterface() bool {
if !at.IsValid() { if !at.IsValid() {
return false return false
} }
if _, ok := at.value.([]interface{}); ok { if _, ok := at.value.([]any); ok {
return true return true
} }
return false return false
@ -86,10 +86,10 @@ func (at *Array) ItemIsInterface() bool {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 17:59 2023/6/12 // Date : 17:59 2023/6/12
func (at *Array) Convert() ([]interface{}, error) { func (at *Array) Convert() ([]any, error) {
if at.IsNil() { if at.IsNil() {
// 空指针 // 空指针
at.convertResult = make([]interface{}, 0) at.convertResult = make([]any, 0)
return at.convertResult, nil return at.convertResult, nil
} }
if !at.IsValid() { if !at.IsValid() {
@ -101,117 +101,117 @@ func (at *Array) Convert() ([]interface{}, error) {
case []int8: case []int8:
at.isSimpleSlice = true at.isSimpleSlice = true
at.itemType = reflect.Int8 at.itemType = reflect.Int8
at.convertResult = make([]interface{}, len(val)) at.convertResult = make([]any, len(val))
for i := 0; i < len(val); i++ { for i := 0; i < len(val); i++ {
at.convertResult[i] = val[i] at.convertResult[i] = val[i]
} }
case []int16: case []int16:
at.isSimpleSlice = true at.isSimpleSlice = true
at.itemType = reflect.Int16 at.itemType = reflect.Int16
at.convertResult = make([]interface{}, len(val)) at.convertResult = make([]any, len(val))
for i := 0; i < len(val); i++ { for i := 0; i < len(val); i++ {
at.convertResult[i] = val[i] at.convertResult[i] = val[i]
} }
case []int32: case []int32:
at.isSimpleSlice = true at.isSimpleSlice = true
at.itemType = reflect.Int32 at.itemType = reflect.Int32
at.convertResult = make([]interface{}, len(val)) at.convertResult = make([]any, len(val))
for i := 0; i < len(val); i++ { for i := 0; i < len(val); i++ {
at.convertResult[i] = val[i] at.convertResult[i] = val[i]
} }
case []int64: case []int64:
at.isSimpleSlice = true at.isSimpleSlice = true
at.itemType = reflect.Int64 at.itemType = reflect.Int64
at.convertResult = make([]interface{}, len(val)) at.convertResult = make([]any, len(val))
for i := 0; i < len(val); i++ { for i := 0; i < len(val); i++ {
at.convertResult[i] = val[i] at.convertResult[i] = val[i]
} }
case []int: case []int:
at.isSimpleSlice = true at.isSimpleSlice = true
at.itemType = reflect.Int at.itemType = reflect.Int
at.convertResult = make([]interface{}, len(val)) at.convertResult = make([]any, len(val))
for i := 0; i < len(val); i++ { for i := 0; i < len(val); i++ {
at.convertResult[i] = val[i] at.convertResult[i] = val[i]
} }
case []uint: case []uint:
at.isSimpleSlice = true at.isSimpleSlice = true
at.itemType = reflect.Uint at.itemType = reflect.Uint
at.convertResult = make([]interface{}, len(val)) at.convertResult = make([]any, len(val))
for i := 0; i < len(val); i++ { for i := 0; i < len(val); i++ {
at.convertResult[i] = val[i] at.convertResult[i] = val[i]
} }
case []uint8: case []uint8:
at.isSimpleSlice = true at.isSimpleSlice = true
at.itemType = reflect.Uint8 at.itemType = reflect.Uint8
at.convertResult = make([]interface{}, len(val)) at.convertResult = make([]any, len(val))
for i := 0; i < len(val); i++ { for i := 0; i < len(val); i++ {
at.convertResult[i] = val[i] at.convertResult[i] = val[i]
} }
case []uint16: case []uint16:
at.isSimpleSlice = true at.isSimpleSlice = true
at.itemType = reflect.Uint16 at.itemType = reflect.Uint16
at.convertResult = make([]interface{}, len(val)) at.convertResult = make([]any, len(val))
for i := 0; i < len(val); i++ { for i := 0; i < len(val); i++ {
at.convertResult[i] = val[i] at.convertResult[i] = val[i]
} }
case []uint32: case []uint32:
at.isSimpleSlice = true at.isSimpleSlice = true
at.itemType = reflect.Int32 at.itemType = reflect.Int32
at.convertResult = make([]interface{}, len(val)) at.convertResult = make([]any, len(val))
for i := 0; i < len(val); i++ { for i := 0; i < len(val); i++ {
at.convertResult[i] = val[i] at.convertResult[i] = val[i]
} }
case []uint64: case []uint64:
at.isSimpleSlice = true at.isSimpleSlice = true
at.itemType = reflect.Int64 at.itemType = reflect.Int64
at.convertResult = make([]interface{}, len(val)) at.convertResult = make([]any, len(val))
for i := 0; i < len(val); i++ { for i := 0; i < len(val); i++ {
at.convertResult[i] = val[i] at.convertResult[i] = val[i]
} }
case []float32: case []float32:
at.isSimpleSlice = true at.isSimpleSlice = true
at.itemType = reflect.Float32 at.itemType = reflect.Float32
at.convertResult = make([]interface{}, len(val)) at.convertResult = make([]any, len(val))
for i := 0; i < len(val); i++ { for i := 0; i < len(val); i++ {
at.convertResult[i] = val[i] at.convertResult[i] = val[i]
} }
case []float64: case []float64:
at.isSimpleSlice = true at.isSimpleSlice = true
at.itemType = reflect.Float64 at.itemType = reflect.Float64
at.convertResult = make([]interface{}, len(val)) at.convertResult = make([]any, len(val))
for i := 0; i < len(val); i++ { for i := 0; i < len(val); i++ {
at.convertResult[i] = val[i] at.convertResult[i] = val[i]
} }
case []bool: case []bool:
at.isSimpleSlice = true at.isSimpleSlice = true
at.itemType = reflect.Bool at.itemType = reflect.Bool
at.convertResult = make([]interface{}, len(val)) at.convertResult = make([]any, len(val))
for i := 0; i < len(val); i++ { for i := 0; i < len(val); i++ {
at.convertResult[i] = val[i] at.convertResult[i] = val[i]
} }
case []string: case []string:
at.isSimpleSlice = true at.isSimpleSlice = true
at.itemType = reflect.String at.itemType = reflect.String
at.convertResult = make([]interface{}, len(val)) at.convertResult = make([]any, len(val))
for i := 0; i < len(val); i++ { for i := 0; i < len(val); i++ {
at.convertResult[i] = val[i] at.convertResult[i] = val[i]
} }
case []interface{}: case []any:
at.isSimpleSlice = false at.isSimpleSlice = false
at.itemType = reflect.Interface at.itemType = reflect.Interface
at.convertResult = make([]interface{}, len(val)) at.convertResult = make([]any, len(val))
copy(at.convertResult, val) copy(at.convertResult, val)
case []struct{}: case []struct{}:
at.isSimpleSlice = false at.isSimpleSlice = false
at.itemType = reflect.Interface at.itemType = reflect.Interface
at.convertResult = make([]interface{}, len(val)) at.convertResult = make([]any, len(val))
for i := 0; i < len(val); i++ { for i := 0; i < len(val); i++ {
at.convertResult[i] = val[i] at.convertResult[i] = val[i]
} }
case []map[string]interface{}: case []map[string]any:
at.isSimpleSlice = false at.isSimpleSlice = false
at.itemType = reflect.Interface at.itemType = reflect.Interface
at.convertResult = make([]interface{}, len(val)) at.convertResult = make([]any, len(val))
for i := 0; i < len(val); i++ { for i := 0; i < len(val); i++ {
at.convertResult[i] = val[i] at.convertResult[i] = val[i]
} }
@ -226,7 +226,7 @@ func (at *Array) Convert() ([]interface{}, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 18:07 2023/6/26 // Date : 18:07 2023/6/26
func (at *Array) ConvertIgnoreError() []interface{} { func (at *Array) ConvertIgnoreError() []any {
res, _ := at.Convert() res, _ := at.Convert()
return res return res
} }
@ -250,22 +250,32 @@ func (at *Array) ToStringSlice() []string {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 17:43 2023/6/12 // Date : 17:43 2023/6/12
func (at *Array) Unique() []interface{} { func (at *Array) Unique() StringResult {
result := make([]interface{}, 0) result := make([]any, 0)
if at.isSimpleSlice { dataTable := make(map[string]bool)
// 简单数据类型
dataTable := make(map[string]bool) for _, item := range at.convertResult {
for _, item := range at.convertResult { k := ""
k := fmt.Sprintf("%v", item) if at.isSimpleSlice {
if _, exist := dataTable[k]; exist { // 简单数据类型
continue k = fmt.Sprintf("%v", item)
} } else {
dataTable[k] = true // 非简单类型
result = append(result, item) byteData, _ := json.Marshal(item)
k = string(byteData)
} }
return result if _, exist := dataTable[k]; exist {
continue
}
dataTable[k] = true
result = append(result, item)
} }
return []interface{}{} byteData, _ := json.Marshal(result)
return StringResult{
Value: string(byteData),
Err: nil,
}
} }
// Has 查询一个值是否在列表里, 在的话, 返回首次出现的索引, 不在返回-1 // Has 查询一个值是否在列表里, 在的话, 返回首次出现的索引, 不在返回-1
@ -273,7 +283,7 @@ func (at *Array) Unique() []interface{} {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 16:28 2023/7/31 // Date : 16:28 2023/7/31
func (at *Array) Has(input interface{}) int { func (at *Array) Has(input any) int {
for idx := 0; idx < len(at.convertResult); idx++ { for idx := 0; idx < len(at.convertResult); idx++ {
if nil == input { if nil == input {
if nil != at.convertResult[idx] { if nil != at.convertResult[idx] {