提交 f8b1262b authored 作者: mooncake9527's avatar mooncake9527

1. xslice修改为泛型

上级 0e9756e9
......@@ -2,11 +2,11 @@ package xslice
import (
"fmt"
"gitlab.wanzhuangkj.com/tush/xpkg/xtype"
"strings"
"github.com/jinzhu/copier"
"github.com/spf13/cast"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/xsf"
"gitlab.wanzhuangkj.com/tush/xpkg/xerrors/xerror"
"gitlab.wanzhuangkj.com/tush/xpkg/xset"
)
......@@ -48,12 +48,12 @@ func StrToUint64(rs []string) []uint64 {
return IDs
}
type IID interface {
GetID() xsf.ID
type IID[ID xtype.Key] interface {
GetID() ID
}
type IIDTable interface {
GetID() xsf.ID
type IIDTable[ID xtype.Key] interface {
GetID() ID
TableName() string
}
......@@ -61,27 +61,19 @@ type ITable interface {
TableName() string
}
func GetIDs[T IID](rs []*T) []xsf.ID {
func GetIDs[ID xtype.Key, T IID[ID]](rs []*T) []ID {
if len(rs) == 0 {
return nil
}
set := xset.NewSet[xsf.ID]()
set := xset.NewSet[ID]()
for _, r := range rs {
set.Add((*r).GetID())
}
return set.ToList()
}
func SliceToIDMap[T IID](rs []*T) map[xsf.ID]*T {
m := make(map[xsf.ID]*T)
for _, r := range rs {
m[(*r).GetID()] = r
}
return m
}
func SliceToIdMap[T IId](rs []*T) map[uint]*T {
m := make(map[uint]*T)
func SliceToIDMap[ID xtype.Key, T IID[ID]](rs []*T) map[ID]*T {
m := make(map[ID]*T)
for _, r := range rs {
m[(*r).GetID()] = r
}
......@@ -178,15 +170,15 @@ func StrMapToSlice[T any](m map[string]*T) []*T {
return rs
}
func GetStrMapIDs[T IID](m map[string]*T) []xsf.ID {
st := xset.NewSet[xsf.ID]()
func GetStrMapIDs[ID xtype.Key, T IID[ID]](m map[string]*T) []ID {
st := xset.NewSet[ID]()
for _, v := range m {
st.Add((*v).GetID())
}
return st.ToList()
}
func IDMapToSlice[T any](m map[xsf.ID]*T) []*T {
func IDMapToSlice[ID xtype.Key, T any](m map[ID]*T) []*T {
var rs []*T
for _, r := range m {
rs = append(rs, r)
......@@ -194,8 +186,8 @@ func IDMapToSlice[T any](m map[xsf.ID]*T) []*T {
return rs
}
func GetIDMapIDs[T IID](m map[xsf.ID]*T) []xsf.ID {
st := xset.NewSet[xsf.ID]()
func GetIDMapIDs[ID xtype.Key, T IID[ID]](m map[ID]*T) []ID {
st := xset.NewSet[ID]()
for _, v := range m {
st.Add((*v).GetID())
}
......@@ -210,8 +202,8 @@ func StrMapSliceToSlice[T any](m map[string][]*T) []*T {
return rs
}
func GetStrMapSliceIDs[T IID](m map[string][]*T) []xsf.ID {
st := xset.NewSet[xsf.ID]()
func GetStrMapSliceIDs[ID xtype.Key, T IID[ID]](m map[string][]*T) []ID {
st := xset.NewSet[ID]()
for _, rs := range m {
for _, r := range rs {
st.Add((*r).GetID())
......@@ -220,7 +212,7 @@ func GetStrMapSliceIDs[T IID](m map[string][]*T) []xsf.ID {
return st.ToList()
}
func IDMapSliceToSlice[T any](m map[xsf.ID][]*T) []*T {
func IDMapSliceToSlice[ID xtype.Key, T any](m map[ID][]*T) []*T {
var rs []*T
for _, r := range m {
rs = append(rs, r...)
......@@ -228,8 +220,8 @@ func IDMapSliceToSlice[T any](m map[xsf.ID][]*T) []*T {
return rs
}
func GetIDMapSliceIDs[T IID](m map[xsf.ID][]*T) []xsf.ID {
st := xset.NewSet[xsf.ID]()
func GetIDMapSliceIDs[ID xtype.Key, T IID[ID]](m map[ID][]*T) []ID {
st := xset.NewSet[ID]()
for _, rs := range m {
for _, r := range rs {
st.Add((*r).GetID())
......@@ -238,7 +230,7 @@ func GetIDMapSliceIDs[T IID](m map[xsf.ID][]*T) []xsf.ID {
return st.ToList()
}
func CompareSlice[T IIDTable, BizSlice ~[]*T](ids []xsf.ID, bizSlice BizSlice, errCode int) error {
func CompareSlice[ID xtype.Key, T IIDTable[ID], BizSlice ~[]*T](ids []ID, bizSlice BizSlice, errCode int) error {
if len(ids) == 0 {
return nil
}
......@@ -247,7 +239,7 @@ func CompareSlice[T IIDTable, BizSlice ~[]*T](ids []xsf.ID, bizSlice BizSlice, e
var t T
return xerror.NewC(errCode, fmt.Sprintf("未查询到%s", t.TableName()))
}
deltaIDs := SetDifference(ids, GetIDs(bizSlice))
deltaIDs := SetDifference(ids, GetIDs[ID](bizSlice))
if len(deltaIDs) > 0 {
var t T
return xerror.NewC(errCode, fmt.Sprintf("未查询到%s[ids:%s]", t.TableName(), Join(deltaIDs, ",")))
......@@ -270,17 +262,26 @@ func ArrayUnique[T comparable](array []T) []T {
return array[:idx]
}
type IId interface {
GetID() uint
}
func GetIds[T IId](rs []*T) []uint {
if len(rs) == 0 {
return nil
}
set := xset.NewSet[uint]()
for _, r := range rs {
set.Add((*r).GetID())
}
return set.ToList()
}
//
//type IId[id xtype.Key] interface {
// GetID() id
//}
//
//func GetIds[id xtype.Key, T IId[id]](rs []*T) []id {
// if len(rs) == 0 {
// return nil
// }
// set := xset.NewSet[id]()
// for _, r := range rs {
// set.Add((*r).GetID())
// }
// return set.ToList()
//}
//
//func SliceToIdMap[id xtype.Key, T IId[id]](rs []*T) map[id]*T {
// m := make(map[id]*T)
// for _, r := range rs {
// m[(*r).GetID()] = r
// }
// return m
//}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论