Merge remote-tracking branch 'origin/errcode' into errcode

This commit is contained in:
Gordon 2023-02-02 12:06:55 +08:00
commit eb810f8dca
3 changed files with 91 additions and 0 deletions

View File

@ -13,6 +13,7 @@ type FriendModel struct {
OperatorUserID string `gorm:"column:operator_user_id;size:64"`
Ex string `gorm:"column:ex;size:1024"`
}
type ConversationModel struct {
OwnerUserID string `gorm:"column:owner_user_id;primary_key;type:char(128)" json:"OwnerUserID"`
ConversationID string `gorm:"column:conversation_id;primary_key;type:char(128)" json:"conversationID"`
@ -50,6 +51,31 @@ type GroupModel struct {
NotificationUserID string `gorm:"column:notification_user_id;size:64"`
}
func (f *GroupModel) EqID(i interface{}) bool {
switch v := i.(type) {
case GroupModel:
return f.GroupID == v.GroupID
case *GroupModel:
return f.GroupID == v.GroupID
default:
return false
}
}
func DuplicateRemoval[T any](arr []T, fn func(t T) string) {
}
func aaa() {
DuplicateRemoval([]GroupModel{}, func(t GroupModel) string {
return t.GroupID
})
DuplicateRemoval([]*GroupModel{}, func(t *GroupModel) string {
return t.GroupID
})
}
type FriendRequestModel struct {
FromUserID string `gorm:"column:from_user_id;primary_key;size:64"`
ToUserID string `gorm:"column:to_user_id;primary_key;size:64"`

13
pkg/utilsv2/demo.go Normal file
View File

@ -0,0 +1,13 @@
package utilsv2
import "Open_IM/pkg/common/db/table"
func demo() {
groups := []*table.GroupModel{}
groups = DuplicateRemovalAny(groups, func(t *table.GroupModel) string {
return t.GroupID
})
}

52
pkg/utilsv2/slice.go Normal file
View File

@ -0,0 +1,52 @@
package utilsv2
func DuplicateRemovalAny[T any, V comparable](ts []T, fn func(t T) V) []T {
v := make([]T, 0, len(ts))
tmp := map[V]struct{}{}
for i := 0; i < len(ts); i++ {
t := ts[i]
k := fn(t)
if _, ok := tmp[k]; !ok {
tmp[k] = struct{}{}
v = append(v, t)
}
}
return v
}
func DuplicateRemoval[T comparable](ts []T) []T {
return DuplicateRemovalAny(ts, func(t T) T {
return t
})
}
func DeleteAt[T any](ts []T, index ...int) []T {
switch len(index) {
case 0:
return ts
case 1:
i := index[0]
if len(ts) <= i || len(ts) < -i {
return ts
}
if i < 0 {
i = len(ts) + i
}
return append(ts[:index[0]], ts[index[0]+1:]...)
default:
tmp := make(map[int]struct{})
for _, i := range index {
if i < 0 {
i = len(ts) + i
}
tmp[i] = struct{}{}
}
v := make([]T, 0, len(ts))
for i := 0; i < len(ts); i++ {
if _, ok := tmp[i]; !ok {
v = append(v, ts[i])
}
}
return v
}
}