aboutsummaryrefslogtreecommitdiffstats
path: root/models/git
diff options
context:
space:
mode:
authoroliverpool <3864879+oliverpool@users.noreply.github.com>2024-04-09 14:27:30 +0200
committerGitHub <noreply@github.com>2024-04-09 20:27:30 +0800
commitd547b53cca8a9a7ac96449910bae5d811728c251 (patch)
tree2d356c8a7e06855b16ecc289ba9dce92ceeb5f77 /models/git
parent8d14266269f1b4fd5e13d701830919c1a1613444 (diff)
downloadgitea-d547b53cca8a9a7ac96449910bae5d811728c251.tar.gz
gitea-d547b53cca8a9a7ac96449910bae5d811728c251.zip
Add container.FilterSlice function (#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 }) } ```
Diffstat (limited to 'models/git')
-rw-r--r--models/git/branch_list.go26
1 files changed, 11 insertions, 15 deletions
diff --git a/models/git/branch_list.go b/models/git/branch_list.go
index 8319e5ecd0..980bd7b4c9 100644
--- a/models/git/branch_list.go
+++ b/models/git/branch_list.go
@@ -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 {