升级数组操作

This commit is contained in:
白茶清欢 2024-05-06 14:56:35 +08:00
parent 9ae0c8f4be
commit 3caad964bc
5 changed files with 47 additions and 252 deletions

248
array.go
View File

@ -9,8 +9,7 @@ package wrapper
import ( import (
"encoding/json" "encoding/json"
"errors" "git.zhangdeman.cn/zhangdeman/op_type"
"fmt"
"reflect" "reflect"
"strings" "strings"
) )
@ -20,11 +19,10 @@ 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 any) *Array { func ArrayType[Bt op_type.BaseType](value []Bt) *Array[Bt] {
at := &Array{ at := &Array[Bt]{
value: value, value: value,
} }
_, _ = at.Convert()
return at return at
} }
@ -33,11 +31,9 @@ func ArrayType(value any) *Array {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 21:05 2023/6/11 // Date : 21:05 2023/6/11
type Array struct { type Array[Bt op_type.BaseType] struct {
value any value []Bt
convertResult []any
convertErr error convertErr error
isSimpleSlice bool // 是否简单list, 即数据的每一项类型相同, 且都是基础内置数据类型
itemType reflect.Kind // 简单list场景下, 每一项的数据类型 itemType reflect.Kind // 简单list场景下, 每一项的数据类型
} }
@ -46,201 +42,20 @@ type Array struct {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 21:11 2023/6/11 // Date : 21:11 2023/6/11
func (at *Array) IsNil() bool { func (at *Array[Bt]) IsNil() bool {
return at.value == nil return at.value == nil
} }
// IsValid 检测是否为数组类型
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:06 2023/6/11
func (at *Array) 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), "]")
}
// ItemIsInterface 数组每一项是否为interface
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:20 2023/6/11
func (at *Array) ItemIsInterface() bool {
if !at.IsValid() {
return false
}
if _, ok := at.value.([]any); ok {
return true
}
return false
}
// Convert 类型转换
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:59 2023/6/12
func (at *Array) Convert() ([]any, error) {
if at.IsNil() {
// 空指针
at.convertResult = make([]any, 0)
return at.convertResult, nil
}
if !at.IsValid() {
// 无效slice
at.convertErr = errors.New("input slice is invalid")
return nil, at.convertErr
}
switch val := at.value.(type) {
case []int8:
at.isSimpleSlice = true
at.itemType = reflect.Int8
at.convertResult = make([]any, 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([]any, 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([]any, 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([]any, 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([]any, 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([]any, 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([]any, 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([]any, 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([]any, 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([]any, 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([]any, 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([]any, 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([]any, 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([]any, len(val))
for i := 0; i < len(val); i++ {
at.convertResult[i] = val[i]
}
case []any:
at.isSimpleSlice = false
at.itemType = reflect.Interface
at.convertResult = make([]any, len(val))
copy(at.convertResult, val)
case []struct{}:
at.isSimpleSlice = false
at.itemType = reflect.Interface
at.convertResult = make([]any, len(val))
for i := 0; i < len(val); i++ {
at.convertResult[i] = val[i]
}
case []map[string]any:
at.isSimpleSlice = false
at.itemType = reflect.Interface
at.convertResult = make([]any, len(val))
for i := 0; i < len(val); i++ {
at.convertResult[i] = val[i]
}
default:
}
return at.convertResult, nil
}
// ConvertIgnoreError 类型转换并忽略异常
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:07 2023/6/26
func (at *Array) ConvertIgnoreError() []any {
res, _ := at.Convert()
return res
}
// ToStringSlice ... // ToStringSlice ...
// //
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 11:42 2024/4/22 // Date : 11:42 2024/4/22
func (at *Array) ToStringSlice() []string { func (at *Array[Bt]) ToStringSlice() []string {
res := at.ConvertIgnoreError()
list := make([]string, 0) list := make([]string, 0)
for _, item := range res { for _, item := range at.value {
list = append(list, fmt.Sprintf("%v", item)) byteData, _ := json.Marshal(item)
list = append(list, strings.Trim(string(byteData), "\""))
} }
return list return list
} }
@ -250,19 +65,15 @@ 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() StringResult { func (at *Array[Bt]) Unique() StringResult {
result := make([]any, 0) result := make([]any, 0)
dataTable := make(map[string]bool) dataTable := make(map[string]bool)
for _, item := range at.convertResult { for _, item := range at.value {
k := ""
if at.isSimpleSlice {
// 简单数据类型
k = fmt.Sprintf("%v", item)
} else {
// 非简单类型
byteData, _ := json.Marshal(item) byteData, _ := json.Marshal(item)
k = string(byteData) k := string(byteData)
if strings.HasPrefix(k, "\"\"") && strings.HasSuffix(k, "\"\"") {
k = string(byteData[1 : len(byteData)-1])
} }
if _, exist := dataTable[k]; exist { if _, exist := dataTable[k]; exist {
continue continue
@ -283,22 +94,13 @@ func (at *Array) Unique() StringResult {
// 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 any) int { func (at *Array[Bt]) Has(input Bt) int {
for idx := 0; idx < len(at.convertResult); idx++ { for idx := 0; idx < len(at.value); idx++ {
if nil == input { if reflect.TypeOf(at.value[idx]).String() != reflect.TypeOf(input).String() {
if nil != at.convertResult[idx] {
continue
}
return idx
}
if at.convertResult[idx] == nil {
continue
}
if reflect.TypeOf(at.convertResult[idx]).String() != reflect.TypeOf(input).String() {
// 类型不同 // 类型不同
continue continue
} }
sourceByte, _ := json.Marshal(at.convertResult[idx]) sourceByte, _ := json.Marshal(at.value[idx])
inputByte, _ := json.Marshal(input) inputByte, _ := json.Marshal(input)
if string(sourceByte) != string(inputByte) { if string(sourceByte) != string(inputByte) {
continue continue
@ -313,14 +115,14 @@ func (at *Array) Has(input any) int {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 16:57 2023/9/28 // Date : 16:57 2023/9/28
func (at *Array) ToString() StringResult { func (at *Array[Bt]) ToString() StringResult {
if at.IsNil() { if at.IsNil() {
return StringResult{ return StringResult{
Value: "", Value: "",
Err: nil, Err: nil,
} }
} }
byteData, err := json.Marshal(at.convertResult) byteData, err := json.Marshal(at.value)
return StringResult{ return StringResult{
Value: string(byteData), Value: string(byteData),
Err: err, Err: err,
@ -332,19 +134,15 @@ func (at *Array) ToString() StringResult {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 17:42 2023/10/25 // Date : 17:42 2023/10/25
func (at *Array) ToStringWithSplit(split string) StringResult { func (at *Array[Bt]) ToStringWithSplit(split string) StringResult {
if at.IsNil() { if at.IsNil() {
return StringResult{ return StringResult{
Value: "", Value: "",
Err: nil, Err: nil,
} }
} }
strList := make([]string, 0)
for _, item := range at.convertResult {
strList = append(strList, fmt.Sprintf("%v", item))
}
return StringResult{ return StringResult{
Value: strings.Join(strList, split), Value: strings.Join(at.ToStringSlice(), split),
Err: nil, Err: nil,
} }
} }

18
array_test.go Normal file
View File

@ -0,0 +1,18 @@
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-05-06 下午2:48
package wrapper
import (
"fmt"
"testing"
)
func TestArray_Unique(t *testing.T) {
fmt.Println(ArrayType([]any{"1", 1, 1, "1", 2, 3}).Unique().Value)
fmt.Println(ArrayType([]int{1, 1, 2, 3}).Unique().Value)
}

1
go.mod
View File

@ -13,6 +13,7 @@ require (
require ( require (
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240501142503-e31a270e50cc // indirect git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240501142503-e31a270e50cc // indirect
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 // indirect
git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253 // indirect git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253 // indirect
github.com/BurntSushi/toml v1.3.2 // indirect github.com/BurntSushi/toml v1.3.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect

2
go.sum
View File

@ -8,6 +8,8 @@ git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240130062251-a87a97b0e8d4 h1:93JYY
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240130062251-a87a97b0e8d4/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U= git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240130062251-a87a97b0e8d4/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0= git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U= git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U=
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 h1:gUDlQMuJ4xNfP2Abl1Msmpa3fASLWYkNlqDFF/6GN0Y=
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0/go.mod h1:VHb9qmhaPDAQDcS6vUiDCamYjZ4R5lD1XtVsh55KsMI=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240110090803-399e964daa0c h1:k7VCn9GfRGTilvdF/TcTFVMDBfKLe3VeGAtMTiDSnS0= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240110090803-399e964daa0c h1:k7VCn9GfRGTilvdF/TcTFVMDBfKLe3VeGAtMTiDSnS0=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240110090803-399e964daa0c/go.mod h1:w7kG4zyTJ1uPFaTWhze+OQuaUBINT2XnDxpyiM6ctc0= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240110090803-399e964daa0c/go.mod h1:w7kG4zyTJ1uPFaTWhze+OQuaUBINT2XnDxpyiM6ctc0=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687 h1:uQcGqdzi4UdpZlp4f4FUPeBqoygP58pEKJkmN3ROsE0= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687 h1:uQcGqdzi4UdpZlp4f4FUPeBqoygP58pEKJkmN3ROsE0=

View File

@ -108,30 +108,6 @@ func (to *ternaryOperator) StringWithFunc(condFunc CondFunc, trueVal String, fal
return to.String(condFunc(), trueVal, falseVal) return to.String(condFunc(), trueVal, falseVal)
} }
// Array ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:22 2023/11/28
func (to *ternaryOperator) Array(cond bool, trueVal *Array, falseVal *Array) *Array {
if cond {
return trueVal
}
return falseVal
}
// ArrayWithFunc ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:22 2023/11/28
func (to *ternaryOperator) ArrayWithFunc(condFunc CondFunc, trueVal *Array, falseVal *Array) *Array {
if nil == condFunc {
condFunc = defaultCondFunc
}
return to.Array(condFunc(), trueVal, falseVal)
}
// Map ... // Map ...
// //
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>