Add container.FilterSlice function (gitea#30339)
Many places have the following logic: ```go func (jobs ActionJobList) GetRunIDs() []int64 { ids := make(container.Set[int64], len(jobs)) for _, j := range jobs { if j.RunID == 0 { continue } ids.Add(j.RunID) } return ids.Values() } ``` this introduces a `container.FilterMapUnique` function, which reduces the code above to: ```go func (jobs ActionJobList) GetRunIDs() []int64 { return container.FilterMapUnique(jobs, func(j *ActionRunJob) (int64, bool) { return j.RunID, j.RunID != 0 }) } ``` Conflicts: models/issues/comment_list.go due to premature refactor in #3116
This commit is contained in:
parent
5a10eec50f
commit
525accfae6
16 changed files with 150 additions and 182 deletions
|
@ -17,15 +17,12 @@ import (
|
|||
type BranchList []*Branch
|
||||
|
||||
func (branches BranchList) LoadDeletedBy(ctx context.Context) error {
|
||||
ids := container.Set[int64]{}
|
||||
for _, branch := range branches {
|
||||
if !branch.IsDeleted {
|
||||
continue
|
||||
}
|
||||
ids.Add(branch.DeletedByID)
|
||||
}
|
||||
ids := container.FilterSlice(branches, func(branch *Branch) (int64, bool) {
|
||||
return branch.DeletedByID, branch.IsDeleted
|
||||
})
|
||||
|
||||
usersMap := make(map[int64]*user_model.User, len(ids))
|
||||
if err := db.GetEngine(ctx).In("id", ids.Values()).Find(&usersMap); err != nil {
|
||||
if err := db.GetEngine(ctx).In("id", ids).Find(&usersMap); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, branch := range branches {
|
||||
|
@ -41,14 +38,13 @@ func (branches BranchList) LoadDeletedBy(ctx context.Context) error {
|
|||
}
|
||||
|
||||
func (branches BranchList) LoadPusher(ctx context.Context) error {
|
||||
ids := container.Set[int64]{}
|
||||
for _, branch := range branches {
|
||||
if branch.PusherID > 0 { // pusher_id maybe zero because some branches are sync by backend with no pusher
|
||||
ids.Add(branch.PusherID)
|
||||
}
|
||||
}
|
||||
ids := container.FilterSlice(branches, func(branch *Branch) (int64, bool) {
|
||||
// pusher_id maybe zero because some branches are sync by backend with no pusher
|
||||
return branch.PusherID, branch.PusherID > 0
|
||||
})
|
||||
|
||||
usersMap := make(map[int64]*user_model.User, len(ids))
|
||||
if err := db.GetEngine(ctx).In("id", ids.Values()).Find(&usersMap); err != nil {
|
||||
if err := db.GetEngine(ctx).In("id", ids).Find(&usersMap); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, branch := range branches {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue