From f9e09d67bb0a598125d64f5ef75a6a2661b70502 Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Tue, 21 Feb 2023 19:20:09 +0800 Subject: [PATCH] utils --- internal/rpc/group/group.go | 2 +- pkg/utils/utils_v2.go | 27 +++++---------------------- 2 files changed, 6 insertions(+), 23 deletions(-) diff --git a/internal/rpc/group/group.go b/internal/rpc/group/group.go index dd6fbda13..3d6b3c1f9 100644 --- a/internal/rpc/group/group.go +++ b/internal/rpc/group/group.go @@ -547,7 +547,7 @@ func (s *groupServer) GetGroupsInfo(ctx context.Context, req *pbGroup.GetGroupsI return e.GroupID }) resp.GroupInfos = utils.Slice(groups, func(e *relationTb.GroupModel) *sdkws.GroupInfo { - return DbToPbGroupInfo(e, ownerMap[e.GroupID].UserID, uint32(groupMemberNumMap[e.GroupID])) + return DbToPbGroupInfo(e, ownerMap[e.GroupID].UserID, groupMemberNumMap[e.GroupID]) }) return resp, nil } diff --git a/pkg/utils/utils_v2.go b/pkg/utils/utils_v2.go index a28efcf14..38fae8424 100644 --- a/pkg/utils/utils_v2.go +++ b/pkg/utils/utils_v2.go @@ -27,6 +27,11 @@ func SliceSub[E comparable](a, b []E) []E { return rs } +// SliceSubAny a中存在,b中不存在 (a-b) +func SliceSubAny[E comparable, T any](a []E, b []T, fn func(t T) E) []E { + return SliceSub(a, Slice(b, fn)) +} + // DistinctAny 去重 func DistinctAny[E any, K comparable](es []E, fn func(e E) K) []E { v := make([]E, 0, len(es)) @@ -380,28 +385,6 @@ func Single[E comparable](a, b []E) []E { return v } -// SliceSub a中存在,b中不存在 (a-b) -func SliceSub[E comparable](a, b []E) []E { - k := make(map[E]struct{}) - for i := 0; i < len(b); i++ { - k[b[i]] = struct{}{} - } - t := make(map[E]struct{}) - rs := make([]E, 0, len(a)) - for i := 0; i < len(a); i++ { - e := a[i] - if _, ok := t[e]; ok { - continue - } - if _, ok := k[e]; ok { - continue - } - rs = append(rs, e) - t[e] = struct{}{} - } - return rs -} - // Order 将ts按es排序 func Order[E comparable, T any](es []E, ts []T, fn func(t T) E) []T { if len(es) == 0 || len(ts) == 0 {