mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-04 00:55:59 +08:00
Merge branch 'errcode' of github.com:OpenIMSDK/Open-IM-Server into errcode
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/db/relation"
|
||||
"Open_IM/pkg/common/db/table"
|
||||
"context"
|
||||
@@ -9,11 +10,11 @@ import (
|
||||
|
||||
type FriendInterface interface {
|
||||
// CheckIn 检查user2是否在user1的好友列表中(inUser1Friends==true) 检查user1是否在user2的好友列表中(inUser2Friends==true)
|
||||
CheckIn(ctx context.Context, user1, user2 string) (err error, inUser1Friends bool, inUser2Friends bool)
|
||||
CheckIn(ctx context.Context, user1, user2 string) (inUser1Friends bool, inUser2Friends bool, err error)
|
||||
// AddFriendRequest 增加或者更新好友申请
|
||||
AddFriendRequest(ctx context.Context, fromUserID, toUserID string, reqMsg string, ex string) (err error)
|
||||
// BecomeFriend 先判断是否在好友表,如果在则不插入
|
||||
BecomeFriend(ctx context.Context, friends []*table.FriendModel) (err error)
|
||||
BecomeFriend(ctx context.Context, friends []*table.FriendModel, revFriends []*table.FriendModel) (err error)
|
||||
// RefuseFriendRequest 拒绝好友申请
|
||||
RefuseFriendRequest(ctx context.Context, friendRequest *table.FriendRequestModel) (err error)
|
||||
// AgreeFriendRequest 同意好友申请
|
||||
@@ -23,14 +24,14 @@ type FriendInterface interface {
|
||||
// UpdateRemark 更新好友备注
|
||||
UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error)
|
||||
// FindOwnerFriends 获取ownerUserID的好友列表
|
||||
FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*table.FriendModel, err error)
|
||||
FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*table.FriendModel, total int64, err error)
|
||||
// FindInWhoseFriends friendUserID在哪些人的好友列表中
|
||||
FindInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*table.FriendModel, err error)
|
||||
FindInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*table.FriendModel, total int64, err error)
|
||||
// FindFriendRequestFromMe 获取我发出去的好友申请
|
||||
FindFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*table.FriendRequestModel, err error)
|
||||
FindFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*table.FriendRequestModel, total int64, err error)
|
||||
// FindFriendRequestToMe 获取我收到的的好友申请
|
||||
FindFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*table.FriendRequestModel, err error)
|
||||
// FindFriends 获取某人指定好友的信息
|
||||
FindFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*table.FriendRequestModel, total int64, err error)
|
||||
// FindFriends 获取某人指定好友的信息 如果有一个不存在也返回错误
|
||||
FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*table.FriendModel, err error)
|
||||
}
|
||||
|
||||
@@ -92,7 +93,7 @@ func (f *FriendController) FindFriends(ctx context.Context, ownerUserID string,
|
||||
|
||||
type FriendDatabaseInterface interface {
|
||||
// CheckIn 检查user2是否在user1的好友列表中(inUser1Friends==true) 检查user1是否在user2的好友列表中(inUser2Friends==true)
|
||||
CheckIn(ctx context.Context, user1, user2 string) (err error, inUser1Friends bool, inUser2Friends bool)
|
||||
CheckIn(ctx context.Context, user1, user2 string) (inUser1Friends bool, inUser2Friends bool, err error)
|
||||
// AddFriendRequest 增加或者更新好友申请
|
||||
AddFriendRequest(ctx context.Context, fromUserID, toUserID string, reqMsg string, ex string) (err error)
|
||||
// BecomeFriend 先判断是否在好友表,如果在则不插入
|
||||
@@ -127,15 +128,44 @@ func NewFriendDatabase(db *gorm.DB) *FriendDatabase {
|
||||
}
|
||||
|
||||
// CheckIn 检查user2是否在user1的好友列表中(inUser1Friends==true) 检查user1是否在user2的好友列表中(inUser2Friends==true)
|
||||
func (f *FriendDatabase) CheckIn(ctx context.Context, user1, user2 string) (err error, inUser1Friends bool, inUser2Friends bool) {
|
||||
func (f *FriendDatabase) CheckIn(ctx context.Context, userID1, userID2 string) (inUser1Friends bool, inUser2Friends bool, err error) {
|
||||
friends, err := f.friend.FindUserState(ctx, userID1, userID2)
|
||||
for _, v := range friends {
|
||||
if v.OwnerUserID == userID1 && v.FriendUserID == userID2 {
|
||||
inUser1Friends = true
|
||||
}
|
||||
if v.OwnerUserID == userID2 && v.FriendUserID == userID1 {
|
||||
inUser2Friends = true
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// AddFriendRequest 增加或者更新好友申请
|
||||
func (f *FriendDatabase) AddFriendRequest(ctx context.Context, fromUserID, toUserID string, reqMsg string, ex string) (err error) {
|
||||
|
||||
}
|
||||
|
||||
// BecomeFriend 先判断是否在好友表,如果在则不插入
|
||||
func (f *FriendDatabase) BecomeFriend(ctx context.Context, friends []*table.FriendModel) (err error) {
|
||||
func (f *FriendDatabase) BecomeFriend(ctx context.Context, ownerUserID string, friends []*table.FriendModel) (err error) {
|
||||
return f.friend.DB.Transaction(func(tx *gorm.DB) error {
|
||||
//先find 找出重复的 去掉重复的
|
||||
friendUserIDs := make([]string, 0, len(friends))
|
||||
for _, v := range friends {
|
||||
friendUserIDs = append(friendUserIDs, v.FriendUserID)
|
||||
}
|
||||
fs1, err := f.friend.FindFriends(ctx, ownerUserID, friendUserIDs, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fs2, err := f.friend.FindReversalFriends(ctx, ownerUserID, friendUserIDs, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// RefuseFriendRequest 拒绝好友申请
|
||||
@@ -170,6 +200,14 @@ func (f *FriendDatabase) FindFriendRequestFromMe(ctx context.Context, userID str
|
||||
func (f *FriendDatabase) FindFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*table.FriendRequestModel, err error) {
|
||||
}
|
||||
|
||||
// FindFriends 获取某人指定好友的信息
|
||||
// FindFriends 获取某人指定好友的信息 如果有一个不存在也返回错误
|
||||
func (f *FriendDatabase) FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*table.FriendModel, err error) {
|
||||
friends, err = f.friend.Find(ctx, ownerUserID, friendUserIDs)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if len(friends) != len(friendUserIDs) {
|
||||
err = constant.ErrRecordNotFound.Wrap()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/db/relation"
|
||||
"Open_IM/pkg/common/db/table"
|
||||
"context"
|
||||
@@ -10,11 +11,17 @@ import (
|
||||
type UserInterface interface {
|
||||
//获取指定用户的信息 如果有记录未找到 也返回错误
|
||||
Find(ctx context.Context, userIDs []string) (users []*table.UserModel, err error)
|
||||
//插入
|
||||
Create(ctx context.Context, users []*table.UserModel) error
|
||||
//更新
|
||||
Update(ctx context.Context, users []*table.UserModel) (err error)
|
||||
//更新带零值的
|
||||
UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error)
|
||||
//通过名字搜索
|
||||
GetByName(ctx context.Context, userName string, showNumber, pageNumber int32) (users []*table.UserModel, count int64, err error)
|
||||
//通过名字和id搜索
|
||||
GetByNameAndID(ctx context.Context, content string, showNumber, pageNumber int32) (users []*table.UserModel, count int64, err error)
|
||||
//获取,如果没找到,不不返回错误
|
||||
Get(ctx context.Context, showNumber, pageNumber int32) (users []*table.UserModel, count int64, err error)
|
||||
//userIDs是否存在 只要有一个存在就为true
|
||||
IsExist(ctx context.Context, userIDs []string) (exist bool, err error)
|
||||
@@ -30,24 +37,29 @@ func (u *UserController) Find(ctx context.Context, userIDs []string) (users []*t
|
||||
func (u *UserController) Create(ctx context.Context, users []*table.UserModel) error {
|
||||
return u.database.Create(ctx, users)
|
||||
}
|
||||
func (u *UserController) Take(ctx context.Context, userID string) (user *table.UserModel, err error) {
|
||||
return u.database.Take(ctx, userID)
|
||||
}
|
||||
|
||||
func (u *UserController) Update(ctx context.Context, users []*table.UserModel) (err error) {
|
||||
return u.database.Update(ctx, users)
|
||||
}
|
||||
func (u *UserController) UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error) {
|
||||
return u.database.UpdateByMap(ctx, userID, args)
|
||||
}
|
||||
|
||||
func (u *UserController) GetByName(ctx context.Context, userName string, showNumber, pageNumber int32) (users []*table.UserModel, count int64, err error) {
|
||||
return u.database.GetByName(ctx, userName, showNumber, pageNumber)
|
||||
}
|
||||
|
||||
func (u *UserController) GetByNameAndID(ctx context.Context, content string, showNumber, pageNumber int32) (users []*table.UserModel, count int64, err error) {
|
||||
return u.database.GetByNameAndID(ctx, content, showNumber, pageNumber)
|
||||
}
|
||||
|
||||
func (u *UserController) Get(ctx context.Context, showNumber, pageNumber int32) (users []*table.UserModel, count int64, err error) {
|
||||
return u.database.Get(ctx, showNumber, pageNumber)
|
||||
}
|
||||
|
||||
func (u *UserController) IsExist(ctx context.Context, userIDs []string) (exist bool, err error) {
|
||||
return u.IsExist(ctx, userIDs)
|
||||
}
|
||||
func NewUserController(db *gorm.DB) *UserController {
|
||||
controller := &UserController{database: newUserDatabase(db)}
|
||||
return controller
|
||||
@@ -56,12 +68,12 @@ func NewUserController(db *gorm.DB) *UserController {
|
||||
type UserDatabaseInterface interface {
|
||||
Find(ctx context.Context, userIDs []string) (users []*table.UserModel, err error)
|
||||
Create(ctx context.Context, users []*table.UserModel) error
|
||||
Take(ctx context.Context, userID string) (user *table.UserModel, err error)
|
||||
Update(ctx context.Context, users []*table.UserModel) (err error)
|
||||
UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error)
|
||||
GetByName(ctx context.Context, userName string, showNumber, pageNumber int32) (users []*table.UserModel, count int64, err error)
|
||||
GetByNameAndID(ctx context.Context, content string, showNumber, pageNumber int32) (users []*table.UserModel, count int64, err error)
|
||||
Get(ctx context.Context, showNumber, pageNumber int32) (users []*table.UserModel, count int64, err error)
|
||||
IsExist(ctx context.Context, userIDs []string) (exist bool, err error)
|
||||
}
|
||||
|
||||
type UserDatabase struct {
|
||||
@@ -76,16 +88,22 @@ func newUserDatabase(db *gorm.DB) *UserDatabase {
|
||||
return database
|
||||
}
|
||||
|
||||
// 获取指定用户的信息 如果有记录未找到 也返回错误
|
||||
func (u *UserDatabase) Find(ctx context.Context, userIDs []string) (users []*table.UserModel, err error) {
|
||||
return u.sqlDB.Find(ctx, userIDs)
|
||||
users, err = u.sqlDB.Find(ctx, userIDs)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if len(users) != len(userIDs) {
|
||||
err = constant.ErrRecordNotFound.Wrap()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (u *UserDatabase) Create(ctx context.Context, users []*table.UserModel) error {
|
||||
func (u *UserDatabase) Create(ctx context.Context, users []*table.UserModel) (err error) {
|
||||
return u.sqlDB.Create(ctx, users)
|
||||
}
|
||||
func (u *UserDatabase) Take(ctx context.Context, userID string) (user *table.UserModel, err error) {
|
||||
return u.sqlDB.Take(ctx, userID)
|
||||
}
|
||||
|
||||
func (u *UserDatabase) Update(ctx context.Context, users []*table.UserModel) (err error) {
|
||||
return u.sqlDB.Update(ctx, users)
|
||||
}
|
||||
@@ -98,6 +116,20 @@ func (u *UserDatabase) GetByName(ctx context.Context, userName string, showNumbe
|
||||
func (u *UserDatabase) GetByNameAndID(ctx context.Context, content string, showNumber, pageNumber int32) (users []*table.UserModel, count int64, err error) {
|
||||
return u.sqlDB.GetByNameAndID(ctx, content, showNumber, pageNumber)
|
||||
}
|
||||
|
||||
// 获取,如果没找到,不返回错误
|
||||
func (u *UserDatabase) Get(ctx context.Context, showNumber, pageNumber int32) (users []*table.UserModel, count int64, err error) {
|
||||
return u.sqlDB.Get(ctx, showNumber, pageNumber)
|
||||
}
|
||||
|
||||
// userIDs是否存在 只要有一个存在就为true
|
||||
func (u *UserDatabase) IsExist(ctx context.Context, userIDs []string) (exist bool, err error) {
|
||||
users, err := u.sqlDB.Find(ctx, userIDs)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if len(users) > 0 {
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
@@ -30,11 +30,11 @@ type FriendUser struct {
|
||||
Nickname string `gorm:"column:name;size:255"`
|
||||
}
|
||||
|
||||
func (f *FriendGorm) Create(ctx context.Context, friends []*table.FriendModel) (err error) {
|
||||
func (f *FriendGorm) Create(ctx context.Context, friends []*table.FriendModel, tx ...*gorm.DB) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friends", friends)
|
||||
}()
|
||||
return utils.Wrap(f.DB.Model(&table.FriendModel{}).Create(&friends).Error, "")
|
||||
return utils.Wrap(getDBConn(f.DB, tx).Model(&table.FriendModel{}).Create(&friends).Error, "")
|
||||
}
|
||||
|
||||
func (f *FriendGorm) Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error) {
|
||||
@@ -52,7 +52,7 @@ func (f *FriendGorm) UpdateByMap(ctx context.Context, ownerUserID string, args m
|
||||
return utils.Wrap(f.DB.Model(&table.FriendModel{}).Where("owner_user_id = ?", ownerUserID).Updates(args).Error, "")
|
||||
}
|
||||
|
||||
func (f *FriendGorm) Update(ctx context.Context, friends []*table.FriendModel) (err error) {
|
||||
func (f *FriendGorm) Update(ctx context.Context, friends []*table.FriendModel, tx ...*gorm.DB) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friends", friends)
|
||||
}()
|
||||
@@ -92,3 +92,19 @@ func (f *FriendGorm) FindUserState(ctx context.Context, userID1, userID2 string)
|
||||
}()
|
||||
return friends, utils.Wrap(f.DB.Model(&table.FriendModel{}).Where("(owner_user_id = ? and friend_user_id = ?) or (owner_user_id = ? and friend_user_id = ?)", userID1, userID2, userID2, userID1).Find(&friends).Error, "")
|
||||
}
|
||||
|
||||
// 获取 owner的好友列表
|
||||
func (f *FriendGorm) FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string, tx ...*gorm.DB) (friends []*table.FriendModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friendUserIDs", friendUserIDs, "friends", friends)
|
||||
}()
|
||||
return friends, utils.Wrap(getDBConn(f.DB, tx).Where("owner_user_id = ? AND friend_user_id in (?)", ownerUserID, friendUserIDs).Find(&friends).Error, "")
|
||||
}
|
||||
|
||||
// 获取哪些人添加了friendUserID
|
||||
func (f *FriendGorm) FindReversalFriends(ctx context.Context, friendUserID string, ownerUserIDs []string, tx ...*gorm.DB) (friends []*table.FriendModel, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friendUserID", friendUserID, "friends", friends)
|
||||
}()
|
||||
return friends, utils.Wrap(getDBConn(f.DB, tx).Where("friend_user_id = ? AND owner_user_id in (?)", friendUserID, ownerUserIDs).Find(&friends).Error, "")
|
||||
}
|
||||
|
||||
@@ -19,12 +19,11 @@ func NewUserGorm(db *gorm.DB) *UserGorm {
|
||||
return &user
|
||||
}
|
||||
|
||||
func (u *UserGorm) Create(ctx context.Context, users []*table.UserModel) (err error) {
|
||||
func (u *UserGorm) Create(ctx context.Context, users []*table.UserModel, tx ...*gorm.DB) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "users", users)
|
||||
}()
|
||||
err = utils.Wrap(u.DB.Model(&table.UserModel{}).Create(&users).Error, "")
|
||||
return err
|
||||
return utils.Wrap(getDBConn(u.DB, tx).Model(&table.UserModel{}).Create(&users).Error, "")
|
||||
}
|
||||
|
||||
func (u *UserGorm) UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error) {
|
||||
|
||||
@@ -0,0 +1,292 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
// DistinctAny remove duplicate elements
|
||||
func DistinctAny[E any, K comparable](es []E, fn func(e E) K) []E {
|
||||
v := make([]E, 0, len(es))
|
||||
tmp := map[K]struct{}{}
|
||||
for i := 0; i < len(es); i++ {
|
||||
t := es[i]
|
||||
k := fn(t)
|
||||
if _, ok := tmp[k]; !ok {
|
||||
tmp[k] = struct{}{}
|
||||
v = append(v, t)
|
||||
}
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// Distinct remove duplicate elements
|
||||
func Distinct[T comparable](ts []T) []T {
|
||||
return DistinctAny(ts, func(t T) T {
|
||||
return t
|
||||
})
|
||||
}
|
||||
|
||||
// Delete delete slice element, support negative number to delete the penultimate
|
||||
func Delete[E any](es []E, index ...int) []E {
|
||||
switch len(index) {
|
||||
case 0:
|
||||
return es
|
||||
case 1:
|
||||
i := index[0]
|
||||
if i < 0 {
|
||||
i = len(es) + i
|
||||
}
|
||||
if len(es) <= i {
|
||||
return es
|
||||
}
|
||||
return append(es[:i], es[i+1:]...)
|
||||
default:
|
||||
tmp := make(map[int]struct{})
|
||||
for _, i := range index {
|
||||
if i < 0 {
|
||||
i = len(es) + i
|
||||
}
|
||||
tmp[i] = struct{}{}
|
||||
}
|
||||
v := make([]E, 0, len(es))
|
||||
for i := 0; i < len(es); i++ {
|
||||
if _, ok := tmp[i]; !ok {
|
||||
v = append(v, es[i])
|
||||
}
|
||||
}
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteAt delete slice element, support negative number to delete the penultimate
|
||||
func DeleteAt[E any](es *[]E, index ...int) []E {
|
||||
v := Delete(*es, index...)
|
||||
*es = v
|
||||
return v
|
||||
}
|
||||
|
||||
// IndexAny get the index of the element
|
||||
func IndexAny[E any, K comparable](es []E, e E, fn func(e E) K) int {
|
||||
k := fn(e)
|
||||
for i := 0; i < len(es); i++ {
|
||||
if fn(es[i]) == k {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// IndexOf get the index of the element
|
||||
func IndexOf[E comparable](es []E, e E) int {
|
||||
return IndexAny(es, e, func(t E) E {
|
||||
return t
|
||||
})
|
||||
}
|
||||
|
||||
// Contain include element or not
|
||||
func Contain[E comparable](es []E, e E) bool {
|
||||
return IndexOf(es, e) >= 0
|
||||
}
|
||||
|
||||
// SliceToMapOkAny slice to map
|
||||
func SliceToMapOkAny[E any, K comparable, V any](es []E, fn func(e E) (K, V, bool)) map[K]V {
|
||||
kv := make(map[K]V)
|
||||
for i := 0; i < len(es); i++ {
|
||||
t := es[i]
|
||||
if k, v, ok := fn(t); ok {
|
||||
kv[k] = v
|
||||
}
|
||||
}
|
||||
return kv
|
||||
}
|
||||
|
||||
// SliceToMapAny slice to map
|
||||
func SliceToMapAny[E any, K comparable, V any](es []E, fn func(e E) (K, V)) map[K]V {
|
||||
return SliceToMapOkAny(es, func(e E) (K, V, bool) {
|
||||
k, v := fn(e)
|
||||
return k, v, true
|
||||
})
|
||||
}
|
||||
|
||||
// SliceToMap slice to map
|
||||
func SliceToMap[E any, K comparable](es []E, fn func(e E) K) map[K]E {
|
||||
return SliceToMapOkAny[E, K, E](es, func(e E) (K, E, bool) {
|
||||
k := fn(e)
|
||||
return k, e, true
|
||||
})
|
||||
}
|
||||
|
||||
// SliceSetAny slice to map[K]struct{}
|
||||
func SliceSetAny[E any, K comparable](es []E, fn func(e E) K) map[K]struct{} {
|
||||
return SliceToMapAny(es, func(e E) (K, struct{}) {
|
||||
return fn(e), struct{}{}
|
||||
})
|
||||
}
|
||||
|
||||
// SliceSet slice to map[E]struct{}
|
||||
func SliceSet[E comparable](es []E) map[E]struct{} {
|
||||
return SliceSetAny(es, func(e E) E {
|
||||
return e
|
||||
})
|
||||
}
|
||||
|
||||
// HasKey get whether the map contains key
|
||||
func HasKey[K comparable, V any](m map[K]V, k K) bool {
|
||||
if m == nil {
|
||||
return false
|
||||
}
|
||||
_, ok := m[k]
|
||||
return ok
|
||||
}
|
||||
|
||||
// Min get minimum value
|
||||
func Min[E Ordered](e ...E) E {
|
||||
v := e[0]
|
||||
for _, t := range e[1:] {
|
||||
if v > t {
|
||||
v = t
|
||||
}
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// Max get maximum value
|
||||
func Max[E Ordered](e ...E) E {
|
||||
v := e[0]
|
||||
for _, t := range e[1:] {
|
||||
if v < t {
|
||||
v = t
|
||||
}
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// BothExistAny get elements common to multiple slices
|
||||
func BothExistAny[E any, K comparable](es [][]E, fn func(e E) K) []E {
|
||||
if len(es) == 0 {
|
||||
return []E{}
|
||||
}
|
||||
var idx int
|
||||
ei := make([]map[K]E, len(es))
|
||||
for i := 0; i < len(ei); i++ {
|
||||
e := es[i]
|
||||
if len(e) == 0 {
|
||||
return []E{}
|
||||
}
|
||||
kv := make(map[K]E)
|
||||
for j := 0; j < len(e); j++ {
|
||||
t := e[j]
|
||||
k := fn(t)
|
||||
kv[k] = t
|
||||
}
|
||||
ei[i] = kv
|
||||
if len(kv) < len(ei[idx]) {
|
||||
idx = i
|
||||
}
|
||||
}
|
||||
v := make([]E, 0, len(ei[idx]))
|
||||
for k := range ei[idx] {
|
||||
all := true
|
||||
for i := 0; i < len(ei); i++ {
|
||||
if i == idx {
|
||||
continue
|
||||
}
|
||||
if _, ok := ei[i][k]; !ok {
|
||||
all = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if !all {
|
||||
continue
|
||||
}
|
||||
v = append(v, ei[idx][k])
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// BothExist get elements common to multiple slices
|
||||
func BothExist[E comparable](es ...[]E) []E {
|
||||
return BothExistAny(es, func(e E) E {
|
||||
return e
|
||||
})
|
||||
}
|
||||
|
||||
// CompleteAny complete inclusion
|
||||
func CompleteAny[K comparable, E any](ks []K, es []E, fn func(e E) K) bool {
|
||||
a := SliceSetAny(es, fn)
|
||||
for k := range SliceSet(ks) {
|
||||
if !HasKey(a, k) {
|
||||
return false
|
||||
}
|
||||
delete(a, k)
|
||||
}
|
||||
return len(a) == 0
|
||||
}
|
||||
|
||||
// MapKey get map keys
|
||||
func MapKey[K comparable, V any](kv map[K]V) []K {
|
||||
ks := make([]K, 0, len(kv))
|
||||
for k := range kv {
|
||||
ks = append(ks, k)
|
||||
}
|
||||
return ks
|
||||
}
|
||||
|
||||
// MapValue get map values
|
||||
func MapValue[K comparable, V any](kv map[K]V) []V {
|
||||
vs := make([]V, 0, len(kv))
|
||||
for k := range kv {
|
||||
vs = append(vs, kv[k])
|
||||
}
|
||||
return vs
|
||||
}
|
||||
|
||||
// Sort basic type sorting
|
||||
func Sort[E Ordered](es []E, asc bool) []E {
|
||||
SortAny(es, func(a, b E) bool {
|
||||
if asc {
|
||||
return a < b
|
||||
} else {
|
||||
return a > b
|
||||
}
|
||||
})
|
||||
return es
|
||||
}
|
||||
|
||||
// SortAny custom sort method
|
||||
func SortAny[E any](es []E, fn func(a, b E) bool) {
|
||||
sort.Sort(&sortSlice[E]{
|
||||
ts: es,
|
||||
fn: fn,
|
||||
})
|
||||
}
|
||||
|
||||
// If true -> a, false -> b
|
||||
func If[T any](isa bool, a, b T) T {
|
||||
if isa {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
type sortSlice[E any] struct {
|
||||
ts []E
|
||||
fn func(a, b E) bool
|
||||
}
|
||||
|
||||
func (o *sortSlice[E]) Len() int {
|
||||
return len(o.ts)
|
||||
}
|
||||
|
||||
func (o *sortSlice[E]) Less(i, j int) bool {
|
||||
return o.fn(o.ts[i], o.ts[j])
|
||||
}
|
||||
|
||||
func (o *sortSlice[E]) Swap(i, j int) {
|
||||
o.ts[i], o.ts[j] = o.ts[j], o.ts[i]
|
||||
}
|
||||
|
||||
// Ordered types that can be sorted
|
||||
type Ordered interface {
|
||||
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64 | ~string
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package utilsv2
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -12,9 +12,9 @@ func TestDistinct(t *testing.T) {
|
||||
|
||||
func TestDeleteAt(t *testing.T) {
|
||||
arr := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
|
||||
fmt.Println(DeleteAt(arr, 0, 1, -1, -2))
|
||||
fmt.Println(DeleteAt(arr))
|
||||
fmt.Println(DeleteAt(arr, 1))
|
||||
fmt.Println(Delete(arr, 0, 1, -1, -2))
|
||||
fmt.Println(Delete(arr))
|
||||
fmt.Println(Delete(arr, 1))
|
||||
}
|
||||
|
||||
func TestSliceToMap(t *testing.T) {
|
||||
@@ -46,5 +46,39 @@ func TestIndexOf(t *testing.T) {
|
||||
func TestSort(t *testing.T) {
|
||||
arr := []int{1, 1, 1, 4, 4, 5, 2, 3, 3, 3, 6}
|
||||
fmt.Println(Sort(arr, false))
|
||||
}
|
||||
|
||||
func TestBothExist(t *testing.T) {
|
||||
arr1 := []int{1, 1, 1, 4, 4, 5, 2, 3, 3, 3, 6}
|
||||
arr2 := []int{6, 1, 3}
|
||||
arr3 := []int{5, 1, 3, 6}
|
||||
fmt.Println(BothExist(arr1, arr2, arr3))
|
||||
}
|
||||
|
||||
func TestCompleteAny(t *testing.T) {
|
||||
type Item struct {
|
||||
ID int
|
||||
Value string
|
||||
}
|
||||
|
||||
ids := []int{1, 2, 3, 4, 5, 6, 7, 8}
|
||||
|
||||
var list []Item
|
||||
|
||||
for _, id := range ids {
|
||||
list = append(list, Item{
|
||||
ID: id,
|
||||
Value: fmt.Sprintf("%d", id*1000),
|
||||
})
|
||||
}
|
||||
|
||||
DeleteAt(&list, -1)
|
||||
DeleteAt(&ids, -1)
|
||||
|
||||
ok := CompleteAny(ids, list, func(t Item) int {
|
||||
return t.ID
|
||||
})
|
||||
|
||||
fmt.Printf("%+v\n", ok)
|
||||
|
||||
}
|
||||
@@ -1,152 +0,0 @@
|
||||
package utilsv2
|
||||
|
||||
import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
// DistinctAny 切片去重
|
||||
func DistinctAny[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
|
||||
}
|
||||
|
||||
// Distinct 切片去重
|
||||
func Distinct[T comparable](ts []T) []T {
|
||||
return DistinctAny(ts, func(t T) T {
|
||||
return t
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteAt 删除切片元素, 支持负数删除倒数第几个
|
||||
func DeleteAt[T any](ts []T, index ...int) []T {
|
||||
switch len(index) {
|
||||
case 0:
|
||||
return ts
|
||||
case 1:
|
||||
i := index[0]
|
||||
if i < 0 {
|
||||
i = len(ts) + i
|
||||
}
|
||||
if len(ts) <= i {
|
||||
return ts
|
||||
}
|
||||
return append(ts[:i], ts[i+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
|
||||
}
|
||||
}
|
||||
|
||||
// IndexAny 获取元素所在的下标
|
||||
func IndexAny[T any, V comparable](ts []T, t T, fn func(t T) V) int {
|
||||
k := fn(t)
|
||||
for i := 0; i < len(ts); i++ {
|
||||
if fn(ts[i]) == k {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// IndexOf 可比较的元素index下标
|
||||
func IndexOf[T comparable](ts []T, t T) int {
|
||||
return IndexAny(ts, t, func(t T) T {
|
||||
return t
|
||||
})
|
||||
}
|
||||
|
||||
// IsContain 是否包含元素
|
||||
func IsContain[T comparable](ts []T, t T) bool {
|
||||
return IndexOf(ts, t) >= 0
|
||||
}
|
||||
|
||||
// SliceToMap 切片转map
|
||||
func SliceToMap[T any, K comparable](ts []T, fn func(t T) K) map[K]T {
|
||||
kv := make(map[K]T)
|
||||
for i := 0; i < len(ts); i++ {
|
||||
t := ts[i]
|
||||
k := fn(t)
|
||||
kv[k] = t
|
||||
}
|
||||
return kv
|
||||
}
|
||||
|
||||
// MapKey map获取所有key
|
||||
func MapKey[K comparable, V any](kv map[K]V) []K {
|
||||
ks := make([]K, 0, len(kv))
|
||||
for k := range kv {
|
||||
ks = append(ks, k)
|
||||
}
|
||||
return ks
|
||||
}
|
||||
|
||||
// MapValue map获取所有key
|
||||
func MapValue[K comparable, V any](kv map[K]V) []V {
|
||||
vs := make([]V, 0, len(kv))
|
||||
for k := range kv {
|
||||
vs = append(vs, kv[k])
|
||||
}
|
||||
return vs
|
||||
}
|
||||
|
||||
// Sort 排序
|
||||
func Sort[T Ordered](ts []T, asc bool) []T {
|
||||
SortAny(ts, func(a, b T) bool {
|
||||
if asc {
|
||||
return a < b
|
||||
} else {
|
||||
return a > b
|
||||
}
|
||||
})
|
||||
return ts
|
||||
}
|
||||
|
||||
// SortAny 排序
|
||||
func SortAny[T any](ts []T, fn func(a, b T) bool) {
|
||||
sort.Sort(&sortSlice[T]{
|
||||
ts: ts,
|
||||
fn: fn,
|
||||
})
|
||||
}
|
||||
|
||||
type sortSlice[T any] struct {
|
||||
ts []T
|
||||
fn func(a, b T) bool
|
||||
}
|
||||
|
||||
func (o *sortSlice[T]) Len() int {
|
||||
return len(o.ts)
|
||||
}
|
||||
|
||||
func (o *sortSlice[T]) Less(i, j int) bool {
|
||||
return o.fn(o.ts[i], o.ts[j])
|
||||
}
|
||||
|
||||
func (o *sortSlice[T]) Swap(i, j int) {
|
||||
o.ts[i], o.ts[j] = o.ts[j], o.ts[i]
|
||||
}
|
||||
|
||||
type Ordered interface {
|
||||
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64 | ~string
|
||||
}
|
||||
Reference in New Issue
Block a user