Compare commits
0
Commits
master
..
7c4db86ea2
No files matched your search
@@ -7,7 +7,7 @@ require (
|
|||||||
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20251013024601-da007da2fb42
|
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20251013024601-da007da2fb42
|
||||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20251013044511-86c1a4a3a9dd
|
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20251013044511-86c1a4a3a9dd
|
||||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e
|
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e
|
||||||
github.com/duke-git/lancet/v2 v2.3.9
|
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394
|
||||||
github.com/smartystreets/goconvey v1.8.1
|
github.com/smartystreets/goconvey v1.8.1
|
||||||
github.com/spaolacci/murmur3 v1.1.0
|
github.com/spaolacci/murmur3 v1.1.0
|
||||||
github.com/stretchr/testify v1.11.1
|
github.com/stretchr/testify v1.11.1
|
||||||
@@ -27,7 +27,8 @@ require (
|
|||||||
github.com/smarty/assertions v1.15.0 // indirect
|
github.com/smarty/assertions v1.15.0 // indirect
|
||||||
github.com/tidwall/match v1.2.0 // indirect
|
github.com/tidwall/match v1.2.0 // indirect
|
||||||
github.com/tidwall/pretty v1.2.1 // indirect
|
github.com/tidwall/pretty v1.2.1 // indirect
|
||||||
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a // indirect
|
golang.org/x/mod v0.9.0 // indirect
|
||||||
golang.org/x/text v0.9.0 // indirect
|
golang.org/x/sys v0.6.0 // indirect
|
||||||
|
golang.org/x/tools v0.7.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
@@ -1,17 +1,23 @@
|
|||||||
|
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250916024308-d378e6c57772 h1:Yo1ur3LnDF5s7F7tpJsNrdUSF8LwYKnN9TdQU32F3eU=
|
||||||
|
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250916024308-d378e6c57772/go.mod h1:5p8CEKGBxi7qPtTXDI3HDmqKAfIm5i/aBWdrbkbdNjc=
|
||||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20251101092813-3dd9fb807f1c h1:H0Tlh7i5UtCLwwhPFEqIO7V+vydQ7YgMtPQI7iCxiSI=
|
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20251101092813-3dd9fb807f1c h1:H0Tlh7i5UtCLwwhPFEqIO7V+vydQ7YgMtPQI7iCxiSI=
|
||||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20251101092813-3dd9fb807f1c/go.mod h1:5p8CEKGBxi7qPtTXDI3HDmqKAfIm5i/aBWdrbkbdNjc=
|
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20251101092813-3dd9fb807f1c/go.mod h1:5p8CEKGBxi7qPtTXDI3HDmqKAfIm5i/aBWdrbkbdNjc=
|
||||||
|
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/op_type v0.0.0-20251013024601-da007da2fb42 h1:VjYrb4adud7FHeiYS9XA0B/tOaJjfRejzQAlwimrrDc=
|
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20251013024601-da007da2fb42 h1:VjYrb4adud7FHeiYS9XA0B/tOaJjfRejzQAlwimrrDc=
|
||||||
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20251013024601-da007da2fb42/go.mod h1:VHb9qmhaPDAQDcS6vUiDCamYjZ4R5lD1XtVsh55KsMI=
|
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20251013024601-da007da2fb42/go.mod h1:VHb9qmhaPDAQDcS6vUiDCamYjZ4R5lD1XtVsh55KsMI=
|
||||||
|
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20250504055908-8d68e6106ea9 h1:/GLQaFoLb+ciHOtAS2BIyPNnf4O5ME3AC5PUaJY9kfs=
|
||||||
|
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20250504055908-8d68e6106ea9/go.mod h1:ABJ655C5QenQNOzf7LjCe4sSB52CXvaWLX2Zg4uwDJY=
|
||||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20251013044511-86c1a4a3a9dd h1:kTZOpR8iHx27sUufMWVYhDZx9Q4h80j7RWlaR8GIBiU=
|
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20251013044511-86c1a4a3a9dd h1:kTZOpR8iHx27sUufMWVYhDZx9Q4h80j7RWlaR8GIBiU=
|
||||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20251013044511-86c1a4a3a9dd/go.mod h1:pLrQ63JICi81/3w2BrD26QZiu+IpddvEVfMJ6No3Xb4=
|
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20251013044511-86c1a4a3a9dd/go.mod h1:pLrQ63JICi81/3w2BrD26QZiu+IpddvEVfMJ6No3Xb4=
|
||||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e h1:Q973S6CcWr1ICZhFI1STFOJ+KUImCl2BaIXm6YppBqI=
|
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e h1:Q973S6CcWr1ICZhFI1STFOJ+KUImCl2BaIXm6YppBqI=
|
||||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI=
|
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI=
|
||||||
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
|
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
|
||||||
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||||
|
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
|
||||||
|
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/duke-git/lancet/v2 v2.3.9 h1:ZxUvfoEY7YbsGIeoXRxHWIkRCAt6VN7UBKWgCCqBB3U=
|
|
||||||
github.com/duke-git/lancet/v2 v2.3.9/go.mod h1:zGa2R4xswg6EG9I6WnyubDbFO/+A/RROxIbXcwryTsc=
|
|
||||||
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
|
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
|
||||||
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
|
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
|
||||||
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
|
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
|
||||||
@@ -22,6 +28,8 @@ github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7
|
|||||||
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
||||||
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
|
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
|
||||||
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||||
|
github.com/mozillazg/go-pinyin v0.20.0 h1:BtR3DsxpApHfKReaPO1fCqF4pThRwH9uwvXzm+GnMFQ=
|
||||||
|
github.com/mozillazg/go-pinyin v0.20.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
|
||||||
github.com/mozillazg/go-pinyin v0.21.0 h1:Wo8/NT45z7P3er/9YSLHA3/kjZzbLz5hR7i+jGeIGao=
|
github.com/mozillazg/go-pinyin v0.21.0 h1:Wo8/NT45z7P3er/9YSLHA3/kjZzbLz5hR7i+jGeIGao=
|
||||||
github.com/mozillazg/go-pinyin v0.21.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
|
github.com/mozillazg/go-pinyin v0.21.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
@@ -44,10 +52,12 @@ github.com/tidwall/match v1.2.0/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JT
|
|||||||
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||||
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
|
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
|
||||||
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||||
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a h1:4iLhBPcpqFmylhnkbY3W0ONLUYYkDAW9xMFLfxgsvCw=
|
golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=
|
||||||
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
|
golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||||
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
|
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
|
||||||
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4=
|
||||||
|
golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
|||||||
@@ -1,121 +0,0 @@
|
|||||||
// Package op_array ...
|
|
||||||
//
|
|
||||||
// Description : op_array ...
|
|
||||||
//
|
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
|
||||||
//
|
|
||||||
// Date : 2025-11-25 11:30
|
|
||||||
package op_array
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/duke-git/lancet/v2/convertor"
|
|
||||||
"github.com/duke-git/lancet/v2/slice"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ToMap 数组转map
|
|
||||||
func ToMap[Key comparable, Value any](dataList []Value, keyFormat func(item Value) Key) map[Key]Value {
|
|
||||||
return convertor.ToMap(dataList, func(item Value) (Key, Value) {
|
|
||||||
return keyFormat(item), item
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// ToCustomMap 数组转map
|
|
||||||
func ToCustomMap[Key comparable, Value any, CustomValue any](dataList []Value, keyFormat func(item Value) Key, formatCustomValue func(item Value) CustomValue) map[Key]CustomValue {
|
|
||||||
return convertor.ToMap(dataList, func(item Value) (Key, CustomValue) {
|
|
||||||
return keyFormat(item), formatCustomValue(item)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// ExtractField 提取数组指定字段, 并构建成一个新的数组
|
|
||||||
func ExtractField[FieldValue any, Value any](dataList []Value, fieldValue func(item Value) FieldValue) []FieldValue {
|
|
||||||
return slice.Map(dataList, func(_ int, item Value) FieldValue {
|
|
||||||
return fieldValue(item)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Filter 过滤列表数据
|
|
||||||
func Filter[Value any](dataList []Value, filterValue func(item Value) bool) []Value {
|
|
||||||
return slice.Filter(dataList, func(_ int, item Value) bool {
|
|
||||||
return filterValue(item)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Group 按照指定字段进行分组
|
|
||||||
func Group[Key comparable, Value any](dataList []Value, keyFormat func(item Value) Key) [][]Value {
|
|
||||||
keyList := make([]string, 0)
|
|
||||||
dataTable := make(map[string][]Value)
|
|
||||||
res := make([][]Value, 0)
|
|
||||||
for _, item := range dataList {
|
|
||||||
key := fmt.Sprintf("%v", keyFormat(item))
|
|
||||||
if _, ok := dataTable[key]; !ok {
|
|
||||||
keyList = append(keyList, key)
|
|
||||||
}
|
|
||||||
dataTable[key] = append(dataTable[key], item)
|
|
||||||
}
|
|
||||||
formatList := make([][]Value, 0)
|
|
||||||
for _, key := range keyList {
|
|
||||||
formatList = append(formatList, dataTable[key])
|
|
||||||
}
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
||||||
// GroupToMap 数组数据按照指定 key 进行分组
|
|
||||||
func GroupToMap[Key comparable, Value any](dataList []Value, keyFormat func(item Value) Key) map[Key][]Value {
|
|
||||||
return slice.GroupWith(dataList, func(item Value) Key {
|
|
||||||
return keyFormat(item)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// TreeItem 数据结构
|
|
||||||
type TreeItem[ID comparable, Value any] struct {
|
|
||||||
ID ID `json:"id" dc:"数据ID"`
|
|
||||||
ParentID ID `json:"parent_id" dc:"父级ID"`
|
|
||||||
Value Value `json:"value" dc:"数据值"`
|
|
||||||
ChildList []*TreeItem[ID, Value] `json:"child_list" dc:"子级数据列表"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tree 将列表数据转为属性结构
|
|
||||||
func Tree[ID comparable, Value any](dataList []Value, formatParentID func(v Value) ID, formatID func(v Value) ID) *TreeItem[ID, Value] {
|
|
||||||
// list 转table
|
|
||||||
dataTable := make(map[ID]*TreeItem[ID, Value])
|
|
||||||
for _, item := range dataList {
|
|
||||||
dataID := formatID(item)
|
|
||||||
dataTable[dataID] = &TreeItem[ID, Value]{
|
|
||||||
ID: dataID,
|
|
||||||
ParentID: formatParentID(item),
|
|
||||||
Value: item,
|
|
||||||
ChildList: make([]*TreeItem[ID, Value], 0),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 构建树(保证一定是一棵树)
|
|
||||||
rootData := &TreeItem[ID, Value]{
|
|
||||||
ID: *new(ID),
|
|
||||||
ParentID: *new(ID),
|
|
||||||
Value: *new(Value),
|
|
||||||
ChildList: make([]*TreeItem[ID, Value], 0), // 子数据列表
|
|
||||||
}
|
|
||||||
for _, item := range dataList {
|
|
||||||
dataID := formatID(item)
|
|
||||||
parentID := formatParentID(item)
|
|
||||||
if _, exist := dataTable[parentID]; exist {
|
|
||||||
// 归属某一个父级节点
|
|
||||||
dataTable[parentID].ChildList = append(dataTable[parentID].ChildList, dataTable[dataID])
|
|
||||||
} else {
|
|
||||||
// 部署于任何父节点, 则归属于虚拟的根节点
|
|
||||||
rootData.ChildList = append(rootData.ChildList, dataTable[dataID])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return rootData
|
|
||||||
}
|
|
||||||
|
|
||||||
// DeepClone 深度克隆一个数组, 每一个元素的地址均重新分配
|
|
||||||
func DeepClone[Value any](dataList []Value) []Value {
|
|
||||||
return convertor.DeepClone(dataList)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Shard 将数组按照指定数量进行分片
|
|
||||||
func Shard[Value any](dataList []Value, itemShardCount int) [][]Value {
|
|
||||||
return slice.Chunk(dataList, itemShardCount)
|
|
||||||
}
|
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
// Package op_array ...
|
|
||||||
//
|
|
||||||
// Description : op_array ...
|
|
||||||
//
|
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
|
||||||
//
|
|
||||||
// Date : 2025-11-27 12:40
|
|
||||||
package op_array
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestTree(t *testing.T) {
|
|
||||||
type Data struct {
|
|
||||||
ID int `json:"id" dc:"数据ID"`
|
|
||||||
ParentID int `json:"parent_id" dc:"父级ID"`
|
|
||||||
Value string `json:"value" dc:"数据值"`
|
|
||||||
}
|
|
||||||
|
|
||||||
dataList := []Data{
|
|
||||||
{ID: 1, ParentID: 0, Value: "1"},
|
|
||||||
{ID: 2, ParentID: 1, Value: "2"},
|
|
||||||
{ID: 3, ParentID: 1, Value: "3"},
|
|
||||||
{ID: 4, ParentID: 2, Value: "4"},
|
|
||||||
{ID: 5, ParentID: 2, Value: "5"},
|
|
||||||
{ID: 6, ParentID: 3, Value: "6"},
|
|
||||||
{ID: 7, ParentID: 3, Value: "7"},
|
|
||||||
}
|
|
||||||
res := Tree(dataList, func(v Data) int { return v.ParentID }, func(v Data) int { return v.ID })
|
|
||||||
serialize.JSON.ConsoleOutput(res)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestClone(t *testing.T) {
|
|
||||||
type Data struct {
|
|
||||||
ID int `json:"id" dc:"数据ID"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// 字面结构体
|
|
||||||
dataList := []Data{
|
|
||||||
{ID: 1},
|
|
||||||
{ID: 2},
|
|
||||||
}
|
|
||||||
res := DeepClone(dataList)
|
|
||||||
fmt.Println(fmt.Sprintf("%p", &res[0]), fmt.Sprintf("%p", &dataList[0]))
|
|
||||||
fmt.Println(fmt.Sprintf("%p", &res[1]), fmt.Sprintf("%p", &dataList[1]))
|
|
||||||
|
|
||||||
// 结构体指针
|
|
||||||
dataList1 := []*Data{
|
|
||||||
{ID: 1},
|
|
||||||
{ID: 2},
|
|
||||||
}
|
|
||||||
res1 := DeepClone(dataList1)
|
|
||||||
fmt.Println(fmt.Sprintf("%p", &res1[0]), fmt.Sprintf("%p", &dataList1[0]))
|
|
||||||
fmt.Println(fmt.Sprintf("%p", &res1[1]), fmt.Sprintf("%p", &dataList1[1]))
|
|
||||||
|
|
||||||
// Map数据
|
|
||||||
dataList2 := []map[string]any{
|
|
||||||
{"id": 1},
|
|
||||||
{"id": 2},
|
|
||||||
}
|
|
||||||
res2 := DeepClone(dataList2)
|
|
||||||
fmt.Println(fmt.Sprintf("%p", &res2[0]), fmt.Sprintf("%p", &dataList2[0]))
|
|
||||||
fmt.Println(fmt.Sprintf("%p", &res2[1]), fmt.Sprintf("%p", &dataList2[1]))
|
|
||||||
serialize.JSON.ConsoleOutput(res2)
|
|
||||||
serialize.JSON.ConsoleOutput(dataList2)
|
|
||||||
}
|
|
||||||
+22
-10
@@ -8,13 +8,14 @@
|
|||||||
package op_string
|
package op_string
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/md5"
|
||||||
|
"encoding/hex"
|
||||||
|
"io"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.zhangdeman.cn/zhangdeman/wrapper/define"
|
"git.zhangdeman.cn/zhangdeman/wrapper/define"
|
||||||
"github.com/duke-git/lancet/v2/cryptor"
|
|
||||||
"github.com/duke-git/lancet/v2/strutil"
|
|
||||||
"github.com/spaolacci/murmur3"
|
"github.com/spaolacci/murmur3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -57,8 +58,16 @@ func SnakeCaseToCamel(str string) string {
|
|||||||
|
|
||||||
// Md5 计算Md5值
|
// Md5 计算Md5值
|
||||||
func Md5(str string) define.BaseValueResult[string] {
|
func Md5(str string) define.BaseValueResult[string] {
|
||||||
|
h := md5.New()
|
||||||
|
_, err := io.WriteString(h, str)
|
||||||
|
if nil != err {
|
||||||
return define.BaseValueResult[string]{
|
return define.BaseValueResult[string]{
|
||||||
Value: cryptor.Md5String(str),
|
Value: "",
|
||||||
|
Err: err,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return define.BaseValueResult[string]{
|
||||||
|
Value: hex.EncodeToString(h.Sum(nil)),
|
||||||
Err: nil,
|
Err: nil,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -99,7 +108,10 @@ func ReplaceChar(str string, charTable map[string]string) string {
|
|||||||
if len(charTable) == 0 {
|
if len(charTable) == 0 {
|
||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
return strutil.ReplaceWithMap(str, charTable)
|
for k, v := range charTable {
|
||||||
|
str = strings.ReplaceAll(str, k, v)
|
||||||
|
}
|
||||||
|
return str
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasSubStr 是否包含指定的子串
|
// HasSubStr 是否包含指定的子串
|
||||||
@@ -107,7 +119,12 @@ func HasSubStr(str string, subStrList []string) bool {
|
|||||||
if len(subStrList) == 0 {
|
if len(subStrList) == 0 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return strutil.ContainsAny(str, subStrList)
|
for _, item := range subStrList {
|
||||||
|
if strings.Contains(str, item) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// HashNumber 生成字符串哈希值
|
// HashNumber 生成字符串哈希值
|
||||||
@@ -135,8 +152,3 @@ func Random(length int, sourceCharList string) string {
|
|||||||
}
|
}
|
||||||
return string(genStrByte)
|
return string(genStrByte)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ValidateNotEmpty 验证字符串去前后空格之后不为空
|
|
||||||
func ValidateNotEmpty(str string) bool {
|
|
||||||
return strings.TrimSpace(str) != ""
|
|
||||||
}
|
|
||||||
@@ -106,11 +106,3 @@ func (t *Time) getTimeFormat(defaultFormat string, layout ...string) string {
|
|||||||
}
|
}
|
||||||
return defaultFormat
|
return defaultFormat
|
||||||
}
|
}
|
||||||
|
|
||||||
func DefaultZeroTime() time.Time {
|
|
||||||
return time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local)
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsZeroTime(t time.Time) bool {
|
|
||||||
return t.IsZero() || t.Year() == 1970
|
|
||||||
}
|
|
||||||