aboutsummaryrefslogtreecommitdiffstats
path: root/models/repo/repo_list.go
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/repo/repo_list.go
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/repo/repo_list.go')
-rw-r--r--models/repo/repo_list.go9
1 files changed, 5 insertions, 4 deletions
diff --git a/models/repo/repo_list.go b/models/repo/repo_list.go
index cb7cd47a8d..987c7df9b0 100644
--- a/models/repo/repo_list.go
+++ b/models/repo/repo_list.go
@@ -104,18 +104,19 @@ func (repos RepositoryList) LoadAttributes(ctx context.Context) error {
return nil
}
- set := make(container.Set[int64])
+ userIDs := container.FilterSlice(repos, func(repo *Repository) (int64, bool) {
+ return repo.OwnerID, true
+ })
repoIDs := make([]int64, len(repos))
for i := range repos {
- set.Add(repos[i].OwnerID)
repoIDs[i] = repos[i].ID
}
// Load owners.
- users := make(map[int64]*user_model.User, len(set))
+ users := make(map[int64]*user_model.User, len(userIDs))
if err := db.GetEngine(ctx).
Where("id > 0").
- In("id", set.Values()).
+ In("id", userIDs).
Find(&users); err != nil {
return fmt.Errorf("find users: %w", err)
}