Skip to content

Commit d547b53

Browse files
authored
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 }) } ```
1 parent 8d14266 commit d547b53

16 files changed

+150
-184
lines changed

models/actions/run_job_list.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,9 @@ import (
1616
type ActionJobList []*ActionRunJob
1717

1818
func (jobs ActionJobList) GetRunIDs() []int64 {
19-
ids := make(container.Set[int64], len(jobs))
20-
for _, j := range jobs {
21-
if j.RunID == 0 {
22-
continue
23-
}
24-
ids.Add(j.RunID)
25-
}
26-
return ids.Values()
19+
return container.FilterSlice(jobs, func(j *ActionRunJob) (int64, bool) {
20+
return j.RunID, j.RunID != 0
21+
})
2722
}
2823

2924
func (jobs ActionJobList) LoadRuns(ctx context.Context, withRepo bool) error {

models/actions/run_list.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,15 @@ type RunList []*ActionRun
1919

2020
// GetUserIDs returns a slice of user's id
2121
func (runs RunList) GetUserIDs() []int64 {
22-
ids := make(container.Set[int64], len(runs))
23-
for _, run := range runs {
24-
ids.Add(run.TriggerUserID)
25-
}
26-
return ids.Values()
22+
return container.FilterSlice(runs, func(run *ActionRun) (int64, bool) {
23+
return run.TriggerUserID, true
24+
})
2725
}
2826

2927
func (runs RunList) GetRepoIDs() []int64 {
30-
ids := make(container.Set[int64], len(runs))
31-
for _, run := range runs {
32-
ids.Add(run.RepoID)
33-
}
34-
return ids.Values()
28+
return container.FilterSlice(runs, func(run *ActionRun) (int64, bool) {
29+
return run.RepoID, true
30+
})
3531
}
3632

3733
func (runs RunList) LoadTriggerUser(ctx context.Context) error {

models/actions/runner_list.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,9 @@ type RunnerList []*ActionRunner
1616

1717
// GetUserIDs returns a slice of user's id
1818
func (runners RunnerList) GetUserIDs() []int64 {
19-
ids := make(container.Set[int64], len(runners))
20-
for _, runner := range runners {
21-
if runner.OwnerID == 0 {
22-
continue
23-
}
24-
ids.Add(runner.OwnerID)
25-
}
26-
return ids.Values()
19+
return container.FilterSlice(runners, func(runner *ActionRunner) (int64, bool) {
20+
return runner.OwnerID, runner.OwnerID != 0
21+
})
2722
}
2823

2924
func (runners RunnerList) LoadOwners(ctx context.Context) error {

models/actions/schedule_list.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,15 @@ type ScheduleList []*ActionSchedule
1818

1919
// GetUserIDs returns a slice of user's id
2020
func (schedules ScheduleList) GetUserIDs() []int64 {
21-
ids := make(container.Set[int64], len(schedules))
22-
for _, schedule := range schedules {
23-
ids.Add(schedule.TriggerUserID)
24-
}
25-
return ids.Values()
21+
return container.FilterSlice(schedules, func(schedule *ActionSchedule) (int64, bool) {
22+
return schedule.TriggerUserID, true
23+
})
2624
}
2725

2826
func (schedules ScheduleList) GetRepoIDs() []int64 {
29-
ids := make(container.Set[int64], len(schedules))
30-
for _, schedule := range schedules {
31-
ids.Add(schedule.RepoID)
32-
}
33-
return ids.Values()
27+
return container.FilterSlice(schedules, func(schedule *ActionSchedule) (int64, bool) {
28+
return schedule.RepoID, true
29+
})
3430
}
3531

3632
func (schedules ScheduleList) LoadTriggerUser(ctx context.Context) error {

models/actions/schedule_spec_list.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@ import (
1616
type SpecList []*ActionScheduleSpec
1717

1818
func (specs SpecList) GetScheduleIDs() []int64 {
19-
ids := make(container.Set[int64], len(specs))
20-
for _, spec := range specs {
21-
ids.Add(spec.ScheduleID)
22-
}
23-
return ids.Values()
19+
return container.FilterSlice(specs, func(spec *ActionScheduleSpec) (int64, bool) {
20+
return spec.ScheduleID, true
21+
})
2422
}
2523

2624
func (specs SpecList) LoadSchedules(ctx context.Context) error {
@@ -46,11 +44,9 @@ func (specs SpecList) LoadSchedules(ctx context.Context) error {
4644
}
4745

4846
func (specs SpecList) GetRepoIDs() []int64 {
49-
ids := make(container.Set[int64], len(specs))
50-
for _, spec := range specs {
51-
ids.Add(spec.RepoID)
52-
}
53-
return ids.Values()
47+
return container.FilterSlice(specs, func(spec *ActionScheduleSpec) (int64, bool) {
48+
return spec.RepoID, true
49+
})
5450
}
5551

5652
func (specs SpecList) LoadRepos(ctx context.Context) error {

models/actions/task_list.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,9 @@ import (
1616
type TaskList []*ActionTask
1717

1818
func (tasks TaskList) GetJobIDs() []int64 {
19-
ids := make(container.Set[int64], len(tasks))
20-
for _, t := range tasks {
21-
if t.JobID == 0 {
22-
continue
23-
}
24-
ids.Add(t.JobID)
25-
}
26-
return ids.Values()
19+
return container.FilterSlice(tasks, func(t *ActionTask) (int64, bool) {
20+
return t.JobID, t.JobID != 0
21+
})
2722
}
2823

2924
func (tasks TaskList) LoadJobs(ctx context.Context) error {

models/activities/action_list.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@ import (
2222
type ActionList []*Action
2323

2424
func (actions ActionList) getUserIDs() []int64 {
25-
userIDs := make(container.Set[int64], len(actions))
26-
for _, action := range actions {
27-
userIDs.Add(action.ActUserID)
28-
}
29-
return userIDs.Values()
25+
return container.FilterSlice(actions, func(action *Action) (int64, bool) {
26+
return action.ActUserID, true
27+
})
3028
}
3129

3230
func (actions ActionList) LoadActUsers(ctx context.Context) (map[int64]*user_model.User, error) {
@@ -50,11 +48,9 @@ func (actions ActionList) LoadActUsers(ctx context.Context) (map[int64]*user_mod
5048
}
5149

5250
func (actions ActionList) getRepoIDs() []int64 {
53-
repoIDs := make(container.Set[int64], len(actions))
54-
for _, action := range actions {
55-
repoIDs.Add(action.RepoID)
56-
}
57-
return repoIDs.Values()
51+
return container.FilterSlice(actions, func(action *Action) (int64, bool) {
52+
return action.RepoID, true
53+
})
5854
}
5955

6056
func (actions ActionList) LoadRepositories(ctx context.Context) error {
@@ -80,18 +76,16 @@ func (actions ActionList) loadRepoOwner(ctx context.Context, userMap map[int64]*
8076
userMap = make(map[int64]*user_model.User)
8177
}
8278

83-
userSet := make(container.Set[int64], len(actions))
84-
for _, action := range actions {
79+
missingUserIDs := container.FilterSlice(actions, func(action *Action) (int64, bool) {
8580
if action.Repo == nil {
86-
continue
81+
return 0, false
8782
}
88-
if _, ok := userMap[action.Repo.OwnerID]; !ok {
89-
userSet.Add(action.Repo.OwnerID)
90-
}
91-
}
83+
_, alreadyLoaded := userMap[action.Repo.OwnerID]
84+
return action.Repo.OwnerID, !alreadyLoaded
85+
})
9286

9387
if err := db.GetEngine(ctx).
94-
In("id", userSet.Values()).
88+
In("id", missingUserIDs).
9589
Find(&userMap); err != nil {
9690
return fmt.Errorf("find user: %w", err)
9791
}

models/git/branch_list.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,12 @@ import (
1717
type BranchList []*Branch
1818

1919
func (branches BranchList) LoadDeletedBy(ctx context.Context) error {
20-
ids := container.Set[int64]{}
21-
for _, branch := range branches {
22-
if !branch.IsDeleted {
23-
continue
24-
}
25-
ids.Add(branch.DeletedByID)
26-
}
20+
ids := container.FilterSlice(branches, func(branch *Branch) (int64, bool) {
21+
return branch.DeletedByID, branch.IsDeleted
22+
})
23+
2724
usersMap := make(map[int64]*user_model.User, len(ids))
28-
if err := db.GetEngine(ctx).In("id", ids.Values()).Find(&usersMap); err != nil {
25+
if err := db.GetEngine(ctx).In("id", ids).Find(&usersMap); err != nil {
2926
return err
3027
}
3128
for _, branch := range branches {
@@ -41,14 +38,13 @@ func (branches BranchList) LoadDeletedBy(ctx context.Context) error {
4138
}
4239

4340
func (branches BranchList) LoadPusher(ctx context.Context) error {
44-
ids := container.Set[int64]{}
45-
for _, branch := range branches {
46-
if branch.PusherID > 0 { // pusher_id maybe zero because some branches are sync by backend with no pusher
47-
ids.Add(branch.PusherID)
48-
}
49-
}
41+
ids := container.FilterSlice(branches, func(branch *Branch) (int64, bool) {
42+
// pusher_id maybe zero because some branches are sync by backend with no pusher
43+
return branch.PusherID, branch.PusherID > 0
44+
})
45+
5046
usersMap := make(map[int64]*user_model.User, len(ids))
51-
if err := db.GetEngine(ctx).In("id", ids.Values()).Find(&usersMap); err != nil {
47+
if err := db.GetEngine(ctx).In("id", ids).Find(&usersMap); err != nil {
5248
return err
5349
}
5450
for _, branch := range branches {

models/issues/comment.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,10 +1272,9 @@ func InsertIssueComments(ctx context.Context, comments []*Comment) error {
12721272
return nil
12731273
}
12741274

1275-
issueIDs := make(container.Set[int64])
1276-
for _, comment := range comments {
1277-
issueIDs.Add(comment.IssueID)
1278-
}
1275+
issueIDs := container.FilterSlice(comments, func(comment *Comment) (int64, bool) {
1276+
return comment.IssueID, true
1277+
})
12791278

12801279
ctx, committer, err := db.TxContext(ctx)
12811280
if err != nil {
@@ -1298,7 +1297,7 @@ func InsertIssueComments(ctx context.Context, comments []*Comment) error {
12981297
}
12991298
}
13001299

1301-
for issueID := range issueIDs {
1300+
for _, issueID := range issueIDs {
13021301
if _, err := db.Exec(ctx, "UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ? AND `type`=?) WHERE id = ?",
13031302
issueID, CommentTypeComment, issueID); err != nil {
13041303
return err

0 commit comments

Comments
 (0)