Skip to content

Commit 0e57ff7

Browse files
KN4CK3Rwxiaoguang
andauthored
Add generic set type (#21408)
This PR adds a generic set type to get rid of maps used as sets. Co-authored-by: wxiaoguang <[email protected]>
1 parent e84558b commit 0e57ff7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+328
-324
lines changed

models/activities/action_list.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ import (
1818
type ActionList []*Action
1919

2020
func (actions ActionList) getUserIDs() []int64 {
21-
userIDs := make(map[int64]struct{}, len(actions))
21+
userIDs := make(container.Set[int64], len(actions))
2222
for _, action := range actions {
23-
if _, ok := userIDs[action.ActUserID]; !ok {
24-
userIDs[action.ActUserID] = struct{}{}
25-
}
23+
userIDs.Add(action.ActUserID)
2624
}
27-
return container.KeysInt64(userIDs)
25+
return userIDs.Values()
2826
}
2927

3028
func (actions ActionList) loadUsers(ctx context.Context) (map[int64]*user_model.User, error) {
@@ -48,13 +46,11 @@ func (actions ActionList) loadUsers(ctx context.Context) (map[int64]*user_model.
4846
}
4947

5048
func (actions ActionList) getRepoIDs() []int64 {
51-
repoIDs := make(map[int64]struct{}, len(actions))
49+
repoIDs := make(container.Set[int64], len(actions))
5250
for _, action := range actions {
53-
if _, ok := repoIDs[action.RepoID]; !ok {
54-
repoIDs[action.RepoID] = struct{}{}
55-
}
51+
repoIDs.Add(action.RepoID)
5652
}
57-
return container.KeysInt64(repoIDs)
53+
return repoIDs.Values()
5854
}
5955

6056
func (actions ActionList) loadRepositories(ctx context.Context) error {

models/activities/notification.go

+17-29
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func CreateOrUpdateIssueNotifications(issueID, commentID, notificationAuthorID,
200200

201201
func createOrUpdateIssueNotifications(ctx context.Context, issueID, commentID, notificationAuthorID, receiverID int64) error {
202202
// init
203-
var toNotify map[int64]struct{}
203+
var toNotify container.Set[int64]
204204
notifications, err := getNotificationsByIssueID(ctx, issueID)
205205
if err != nil {
206206
return err
@@ -212,33 +212,27 @@ func createOrUpdateIssueNotifications(ctx context.Context, issueID, commentID, n
212212
}
213213

214214
if receiverID > 0 {
215-
toNotify = make(map[int64]struct{}, 1)
216-
toNotify[receiverID] = struct{}{}
215+
toNotify = make(container.Set[int64], 1)
216+
toNotify.Add(receiverID)
217217
} else {
218-
toNotify = make(map[int64]struct{}, 32)
218+
toNotify = make(container.Set[int64], 32)
219219
issueWatches, err := issues_model.GetIssueWatchersIDs(ctx, issueID, true)
220220
if err != nil {
221221
return err
222222
}
223-
for _, id := range issueWatches {
224-
toNotify[id] = struct{}{}
225-
}
223+
toNotify.AddMultiple(issueWatches...)
226224
if !(issue.IsPull && issues_model.HasWorkInProgressPrefix(issue.Title)) {
227225
repoWatches, err := repo_model.GetRepoWatchersIDs(ctx, issue.RepoID)
228226
if err != nil {
229227
return err
230228
}
231-
for _, id := range repoWatches {
232-
toNotify[id] = struct{}{}
233-
}
229+
toNotify.AddMultiple(repoWatches...)
234230
}
235231
issueParticipants, err := issue.GetParticipantIDsByIssue(ctx)
236232
if err != nil {
237233
return err
238234
}
239-
for _, id := range issueParticipants {
240-
toNotify[id] = struct{}{}
241-
}
235+
toNotify.AddMultiple(issueParticipants...)
242236

243237
// dont notify user who cause notification
244238
delete(toNotify, notificationAuthorID)
@@ -248,7 +242,7 @@ func createOrUpdateIssueNotifications(ctx context.Context, issueID, commentID, n
248242
return err
249243
}
250244
for _, id := range issueUnWatches {
251-
delete(toNotify, id)
245+
toNotify.Remove(id)
252246
}
253247
}
254248

@@ -499,16 +493,14 @@ func (nl NotificationList) LoadAttributes() error {
499493
}
500494

501495
func (nl NotificationList) getPendingRepoIDs() []int64 {
502-
ids := make(map[int64]struct{}, len(nl))
496+
ids := make(container.Set[int64], len(nl))
503497
for _, notification := range nl {
504498
if notification.Repository != nil {
505499
continue
506500
}
507-
if _, ok := ids[notification.RepoID]; !ok {
508-
ids[notification.RepoID] = struct{}{}
509-
}
501+
ids.Add(notification.RepoID)
510502
}
511-
return container.KeysInt64(ids)
503+
return ids.Values()
512504
}
513505

514506
// LoadRepos loads repositories from database
@@ -575,16 +567,14 @@ func (nl NotificationList) LoadRepos() (repo_model.RepositoryList, []int, error)
575567
}
576568

577569
func (nl NotificationList) getPendingIssueIDs() []int64 {
578-
ids := make(map[int64]struct{}, len(nl))
570+
ids := make(container.Set[int64], len(nl))
579571
for _, notification := range nl {
580572
if notification.Issue != nil {
581573
continue
582574
}
583-
if _, ok := ids[notification.IssueID]; !ok {
584-
ids[notification.IssueID] = struct{}{}
585-
}
575+
ids.Add(notification.IssueID)
586576
}
587-
return container.KeysInt64(ids)
577+
return ids.Values()
588578
}
589579

590580
// LoadIssues loads issues from database
@@ -661,16 +651,14 @@ func (nl NotificationList) Without(failures []int) NotificationList {
661651
}
662652

663653
func (nl NotificationList) getPendingCommentIDs() []int64 {
664-
ids := make(map[int64]struct{}, len(nl))
654+
ids := make(container.Set[int64], len(nl))
665655
for _, notification := range nl {
666656
if notification.CommentID == 0 || notification.Comment != nil {
667657
continue
668658
}
669-
if _, ok := ids[notification.CommentID]; !ok {
670-
ids[notification.CommentID] = struct{}{}
671-
}
659+
ids.Add(notification.CommentID)
672660
}
673-
return container.KeysInt64(ids)
661+
return ids.Values()
674662
}
675663

676664
// LoadComments loads comments from database

models/issues/comment_list.go

+24-40
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ import (
1717
type CommentList []*Comment
1818

1919
func (comments CommentList) getPosterIDs() []int64 {
20-
posterIDs := make(map[int64]struct{}, len(comments))
20+
posterIDs := make(container.Set[int64], len(comments))
2121
for _, comment := range comments {
22-
if _, ok := posterIDs[comment.PosterID]; !ok {
23-
posterIDs[comment.PosterID] = struct{}{}
24-
}
22+
posterIDs.Add(comment.PosterID)
2523
}
26-
return container.KeysInt64(posterIDs)
24+
return posterIDs.Values()
2725
}
2826

2927
func (comments CommentList) loadPosters(ctx context.Context) error {
@@ -70,13 +68,11 @@ func (comments CommentList) getCommentIDs() []int64 {
7068
}
7169

7270
func (comments CommentList) getLabelIDs() []int64 {
73-
ids := make(map[int64]struct{}, len(comments))
71+
ids := make(container.Set[int64], len(comments))
7472
for _, comment := range comments {
75-
if _, ok := ids[comment.LabelID]; !ok {
76-
ids[comment.LabelID] = struct{}{}
77-
}
73+
ids.Add(comment.LabelID)
7874
}
79-
return container.KeysInt64(ids)
75+
return ids.Values()
8076
}
8177

8278
func (comments CommentList) loadLabels(ctx context.Context) error { //nolint
@@ -120,13 +116,11 @@ func (comments CommentList) loadLabels(ctx context.Context) error { //nolint
120116
}
121117

122118
func (comments CommentList) getMilestoneIDs() []int64 {
123-
ids := make(map[int64]struct{}, len(comments))
119+
ids := make(container.Set[int64], len(comments))
124120
for _, comment := range comments {
125-
if _, ok := ids[comment.MilestoneID]; !ok {
126-
ids[comment.MilestoneID] = struct{}{}
127-
}
121+
ids.Add(comment.MilestoneID)
128122
}
129-
return container.KeysInt64(ids)
123+
return ids.Values()
130124
}
131125

132126
func (comments CommentList) loadMilestones(ctx context.Context) error {
@@ -163,13 +157,11 @@ func (comments CommentList) loadMilestones(ctx context.Context) error {
163157
}
164158

165159
func (comments CommentList) getOldMilestoneIDs() []int64 {
166-
ids := make(map[int64]struct{}, len(comments))
160+
ids := make(container.Set[int64], len(comments))
167161
for _, comment := range comments {
168-
if _, ok := ids[comment.OldMilestoneID]; !ok {
169-
ids[comment.OldMilestoneID] = struct{}{}
170-
}
162+
ids.Add(comment.OldMilestoneID)
171163
}
172-
return container.KeysInt64(ids)
164+
return ids.Values()
173165
}
174166

175167
func (comments CommentList) loadOldMilestones(ctx context.Context) error {
@@ -206,13 +198,11 @@ func (comments CommentList) loadOldMilestones(ctx context.Context) error {
206198
}
207199

208200
func (comments CommentList) getAssigneeIDs() []int64 {
209-
ids := make(map[int64]struct{}, len(comments))
201+
ids := make(container.Set[int64], len(comments))
210202
for _, comment := range comments {
211-
if _, ok := ids[comment.AssigneeID]; !ok {
212-
ids[comment.AssigneeID] = struct{}{}
213-
}
203+
ids.Add(comment.AssigneeID)
214204
}
215-
return container.KeysInt64(ids)
205+
return ids.Values()
216206
}
217207

218208
func (comments CommentList) loadAssignees(ctx context.Context) error {
@@ -259,16 +249,14 @@ func (comments CommentList) loadAssignees(ctx context.Context) error {
259249

260250
// getIssueIDs returns all the issue ids on this comment list which issue hasn't been loaded
261251
func (comments CommentList) getIssueIDs() []int64 {
262-
ids := make(map[int64]struct{}, len(comments))
252+
ids := make(container.Set[int64], len(comments))
263253
for _, comment := range comments {
264254
if comment.Issue != nil {
265255
continue
266256
}
267-
if _, ok := ids[comment.IssueID]; !ok {
268-
ids[comment.IssueID] = struct{}{}
269-
}
257+
ids.Add(comment.IssueID)
270258
}
271-
return container.KeysInt64(ids)
259+
return ids.Values()
272260
}
273261

274262
// Issues returns all the issues of comments
@@ -334,16 +322,14 @@ func (comments CommentList) loadIssues(ctx context.Context) error {
334322
}
335323

336324
func (comments CommentList) getDependentIssueIDs() []int64 {
337-
ids := make(map[int64]struct{}, len(comments))
325+
ids := make(container.Set[int64], len(comments))
338326
for _, comment := range comments {
339327
if comment.DependentIssue != nil {
340328
continue
341329
}
342-
if _, ok := ids[comment.DependentIssueID]; !ok {
343-
ids[comment.DependentIssueID] = struct{}{}
344-
}
330+
ids.Add(comment.DependentIssueID)
345331
}
346-
return container.KeysInt64(ids)
332+
return ids.Values()
347333
}
348334

349335
func (comments CommentList) loadDependentIssues(ctx context.Context) error {
@@ -439,13 +425,11 @@ func (comments CommentList) loadAttachments(ctx context.Context) (err error) {
439425
}
440426

441427
func (comments CommentList) getReviewIDs() []int64 {
442-
ids := make(map[int64]struct{}, len(comments))
428+
ids := make(container.Set[int64], len(comments))
443429
for _, comment := range comments {
444-
if _, ok := ids[comment.ReviewID]; !ok {
445-
ids[comment.ReviewID] = struct{}{}
446-
}
430+
ids.Add(comment.ReviewID)
447431
}
448-
return container.KeysInt64(ids)
432+
return ids.Values()
449433
}
450434

451435
func (comments CommentList) loadReviews(ctx context.Context) error { //nolint

models/issues/issue_list.go

+13-20
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ type IssueList []*Issue
2222

2323
// get the repo IDs to be loaded later, these IDs are for issue.Repo and issue.PullRequest.HeadRepo
2424
func (issues IssueList) getRepoIDs() []int64 {
25-
repoIDs := make(map[int64]struct{}, len(issues))
25+
repoIDs := make(container.Set[int64], len(issues))
2626
for _, issue := range issues {
2727
if issue.Repo == nil {
28-
repoIDs[issue.RepoID] = struct{}{}
28+
repoIDs.Add(issue.RepoID)
2929
}
3030
if issue.PullRequest != nil && issue.PullRequest.HeadRepo == nil {
31-
repoIDs[issue.PullRequest.HeadRepoID] = struct{}{}
31+
repoIDs.Add(issue.PullRequest.HeadRepoID)
3232
}
3333
}
34-
return container.KeysInt64(repoIDs)
34+
return repoIDs.Values()
3535
}
3636

3737
func (issues IssueList) loadRepositories(ctx context.Context) ([]*repo_model.Repository, error) {
@@ -79,13 +79,11 @@ func (issues IssueList) LoadRepositories() ([]*repo_model.Repository, error) {
7979
}
8080

8181
func (issues IssueList) getPosterIDs() []int64 {
82-
posterIDs := make(map[int64]struct{}, len(issues))
82+
posterIDs := make(container.Set[int64], len(issues))
8383
for _, issue := range issues {
84-
if _, ok := posterIDs[issue.PosterID]; !ok {
85-
posterIDs[issue.PosterID] = struct{}{}
86-
}
84+
posterIDs.Add(issue.PosterID)
8785
}
88-
return container.KeysInt64(posterIDs)
86+
return posterIDs.Values()
8987
}
9088

9189
func (issues IssueList) loadPosters(ctx context.Context) error {
@@ -185,13 +183,11 @@ func (issues IssueList) loadLabels(ctx context.Context) error {
185183
}
186184

187185
func (issues IssueList) getMilestoneIDs() []int64 {
188-
ids := make(map[int64]struct{}, len(issues))
186+
ids := make(container.Set[int64], len(issues))
189187
for _, issue := range issues {
190-
if _, ok := ids[issue.MilestoneID]; !ok {
191-
ids[issue.MilestoneID] = struct{}{}
192-
}
188+
ids.Add(issue.MilestoneID)
193189
}
194-
return container.KeysInt64(ids)
190+
return ids.Values()
195191
}
196192

197193
func (issues IssueList) loadMilestones(ctx context.Context) error {
@@ -224,14 +220,11 @@ func (issues IssueList) loadMilestones(ctx context.Context) error {
224220
}
225221

226222
func (issues IssueList) getProjectIDs() []int64 {
227-
ids := make(map[int64]struct{}, len(issues))
223+
ids := make(container.Set[int64], len(issues))
228224
for _, issue := range issues {
229-
projectID := issue.ProjectID()
230-
if _, ok := ids[projectID]; !ok {
231-
ids[projectID] = struct{}{}
232-
}
225+
ids.Add(issue.ProjectID())
233226
}
234-
return container.KeysInt64(ids)
227+
return ids.Values()
235228
}
236229

237230
func (issues IssueList) loadProjects(ctx context.Context) error {

models/issues/reaction.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ type ReactionOptions struct {
211211

212212
// CreateReaction creates reaction for issue or comment.
213213
func CreateReaction(opts *ReactionOptions) (*Reaction, error) {
214-
if !setting.UI.ReactionsMap[opts.Type] {
214+
if !setting.UI.ReactionsLookup.Contains(opts.Type) {
215215
return nil, ErrForbiddenIssueReaction{opts.Type}
216216
}
217217

@@ -316,16 +316,14 @@ func (list ReactionList) GroupByType() map[string]ReactionList {
316316
}
317317

318318
func (list ReactionList) getUserIDs() []int64 {
319-
userIDs := make(map[int64]struct{}, len(list))
319+
userIDs := make(container.Set[int64], len(list))
320320
for _, reaction := range list {
321321
if reaction.OriginalAuthor != "" {
322322
continue
323323
}
324-
if _, ok := userIDs[reaction.UserID]; !ok {
325-
userIDs[reaction.UserID] = struct{}{}
326-
}
324+
userIDs.Add(reaction.UserID)
327325
}
328-
return container.KeysInt64(userIDs)
326+
return userIDs.Values()
329327
}
330328

331329
func valuesUser(m map[int64]*user_model.User) []*user_model.User {

0 commit comments

Comments
 (0)