aboutsummaryrefslogtreecommitdiffstats
path: root/models/issues
diff options
context:
space:
mode:
authorKN4CK3R <admin@oldschoolhack.me>2022-10-12 07:18:26 +0200
committerGitHub <noreply@github.com>2022-10-12 13:18:26 +0800
commit0e57ff7eee4ac71d923f970d15889ad4d50f97a9 (patch)
treee5a92c55af5366924bd40ae14b4bf12842239193 /models/issues
parente84558b0931309cf1f4f2767bc47296483b9b3e1 (diff)
downloadgitea-0e57ff7eee4ac71d923f970d15889ad4d50f97a9.tar.gz
gitea-0e57ff7eee4ac71d923f970d15889ad4d50f97a9.zip
Add generic set type (#21408)
This PR adds a generic set type to get rid of maps used as sets. Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Diffstat (limited to 'models/issues')
-rw-r--r--models/issues/comment_list.go64
-rw-r--r--models/issues/issue_list.go33
-rw-r--r--models/issues/reaction.go10
3 files changed, 41 insertions, 66 deletions
diff --git a/models/issues/comment_list.go b/models/issues/comment_list.go
index e3406a5cbe..70105d7ff0 100644
--- a/models/issues/comment_list.go
+++ b/models/issues/comment_list.go
@@ -17,13 +17,11 @@ import (
type CommentList []*Comment
func (comments CommentList) getPosterIDs() []int64 {
- posterIDs := make(map[int64]struct{}, len(comments))
+ posterIDs := make(container.Set[int64], len(comments))
for _, comment := range comments {
- if _, ok := posterIDs[comment.PosterID]; !ok {
- posterIDs[comment.PosterID] = struct{}{}
- }
+ posterIDs.Add(comment.PosterID)
}
- return container.KeysInt64(posterIDs)
+ return posterIDs.Values()
}
func (comments CommentList) loadPosters(ctx context.Context) error {
@@ -70,13 +68,11 @@ func (comments CommentList) getCommentIDs() []int64 {
}
func (comments CommentList) getLabelIDs() []int64 {
- ids := make(map[int64]struct{}, len(comments))
+ ids := make(container.Set[int64], len(comments))
for _, comment := range comments {
- if _, ok := ids[comment.LabelID]; !ok {
- ids[comment.LabelID] = struct{}{}
- }
+ ids.Add(comment.LabelID)
}
- return container.KeysInt64(ids)
+ return ids.Values()
}
func (comments CommentList) loadLabels(ctx context.Context) error { //nolint
@@ -120,13 +116,11 @@ func (comments CommentList) loadLabels(ctx context.Context) error { //nolint
}
func (comments CommentList) getMilestoneIDs() []int64 {
- ids := make(map[int64]struct{}, len(comments))
+ ids := make(container.Set[int64], len(comments))
for _, comment := range comments {
- if _, ok := ids[comment.MilestoneID]; !ok {
- ids[comment.MilestoneID] = struct{}{}
- }
+ ids.Add(comment.MilestoneID)
}
- return container.KeysInt64(ids)
+ return ids.Values()
}
func (comments CommentList) loadMilestones(ctx context.Context) error {
@@ -163,13 +157,11 @@ func (comments CommentList) loadMilestones(ctx context.Context) error {
}
func (comments CommentList) getOldMilestoneIDs() []int64 {
- ids := make(map[int64]struct{}, len(comments))
+ ids := make(container.Set[int64], len(comments))
for _, comment := range comments {
- if _, ok := ids[comment.OldMilestoneID]; !ok {
- ids[comment.OldMilestoneID] = struct{}{}
- }
+ ids.Add(comment.OldMilestoneID)
}
- return container.KeysInt64(ids)
+ return ids.Values()
}
func (comments CommentList) loadOldMilestones(ctx context.Context) error {
@@ -206,13 +198,11 @@ func (comments CommentList) loadOldMilestones(ctx context.Context) error {
}
func (comments CommentList) getAssigneeIDs() []int64 {
- ids := make(map[int64]struct{}, len(comments))
+ ids := make(container.Set[int64], len(comments))
for _, comment := range comments {
- if _, ok := ids[comment.AssigneeID]; !ok {
- ids[comment.AssigneeID] = struct{}{}
- }
+ ids.Add(comment.AssigneeID)
}
- return container.KeysInt64(ids)
+ return ids.Values()
}
func (comments CommentList) loadAssignees(ctx context.Context) error {
@@ -259,16 +249,14 @@ func (comments CommentList) loadAssignees(ctx context.Context) error {
// getIssueIDs returns all the issue ids on this comment list which issue hasn't been loaded
func (comments CommentList) getIssueIDs() []int64 {
- ids := make(map[int64]struct{}, len(comments))
+ ids := make(container.Set[int64], len(comments))
for _, comment := range comments {
if comment.Issue != nil {
continue
}
- if _, ok := ids[comment.IssueID]; !ok {
- ids[comment.IssueID] = struct{}{}
- }
+ ids.Add(comment.IssueID)
}
- return container.KeysInt64(ids)
+ return ids.Values()
}
// Issues returns all the issues of comments
@@ -334,16 +322,14 @@ func (comments CommentList) loadIssues(ctx context.Context) error {
}
func (comments CommentList) getDependentIssueIDs() []int64 {
- ids := make(map[int64]struct{}, len(comments))
+ ids := make(container.Set[int64], len(comments))
for _, comment := range comments {
if comment.DependentIssue != nil {
continue
}
- if _, ok := ids[comment.DependentIssueID]; !ok {
- ids[comment.DependentIssueID] = struct{}{}
- }
+ ids.Add(comment.DependentIssueID)
}
- return container.KeysInt64(ids)
+ return ids.Values()
}
func (comments CommentList) loadDependentIssues(ctx context.Context) error {
@@ -439,13 +425,11 @@ func (comments CommentList) loadAttachments(ctx context.Context) (err error) {
}
func (comments CommentList) getReviewIDs() []int64 {
- ids := make(map[int64]struct{}, len(comments))
+ ids := make(container.Set[int64], len(comments))
for _, comment := range comments {
- if _, ok := ids[comment.ReviewID]; !ok {
- ids[comment.ReviewID] = struct{}{}
- }
+ ids.Add(comment.ReviewID)
}
- return container.KeysInt64(ids)
+ return ids.Values()
}
func (comments CommentList) loadReviews(ctx context.Context) error { //nolint
diff --git a/models/issues/issue_list.go b/models/issues/issue_list.go
index 874f2a6368..deadb6a564 100644
--- a/models/issues/issue_list.go
+++ b/models/issues/issue_list.go
@@ -22,16 +22,16 @@ type IssueList []*Issue
// get the repo IDs to be loaded later, these IDs are for issue.Repo and issue.PullRequest.HeadRepo
func (issues IssueList) getRepoIDs() []int64 {
- repoIDs := make(map[int64]struct{}, len(issues))
+ repoIDs := make(container.Set[int64], len(issues))
for _, issue := range issues {
if issue.Repo == nil {
- repoIDs[issue.RepoID] = struct{}{}
+ repoIDs.Add(issue.RepoID)
}
if issue.PullRequest != nil && issue.PullRequest.HeadRepo == nil {
- repoIDs[issue.PullRequest.HeadRepoID] = struct{}{}
+ repoIDs.Add(issue.PullRequest.HeadRepoID)
}
}
- return container.KeysInt64(repoIDs)
+ return repoIDs.Values()
}
func (issues IssueList) loadRepositories(ctx context.Context) ([]*repo_model.Repository, error) {
@@ -79,13 +79,11 @@ func (issues IssueList) LoadRepositories() ([]*repo_model.Repository, error) {
}
func (issues IssueList) getPosterIDs() []int64 {
- posterIDs := make(map[int64]struct{}, len(issues))
+ posterIDs := make(container.Set[int64], len(issues))
for _, issue := range issues {
- if _, ok := posterIDs[issue.PosterID]; !ok {
- posterIDs[issue.PosterID] = struct{}{}
- }
+ posterIDs.Add(issue.PosterID)
}
- return container.KeysInt64(posterIDs)
+ return posterIDs.Values()
}
func (issues IssueList) loadPosters(ctx context.Context) error {
@@ -185,13 +183,11 @@ func (issues IssueList) loadLabels(ctx context.Context) error {
}
func (issues IssueList) getMilestoneIDs() []int64 {
- ids := make(map[int64]struct{}, len(issues))
+ ids := make(container.Set[int64], len(issues))
for _, issue := range issues {
- if _, ok := ids[issue.MilestoneID]; !ok {
- ids[issue.MilestoneID] = struct{}{}
- }
+ ids.Add(issue.MilestoneID)
}
- return container.KeysInt64(ids)
+ return ids.Values()
}
func (issues IssueList) loadMilestones(ctx context.Context) error {
@@ -224,14 +220,11 @@ func (issues IssueList) loadMilestones(ctx context.Context) error {
}
func (issues IssueList) getProjectIDs() []int64 {
- ids := make(map[int64]struct{}, len(issues))
+ ids := make(container.Set[int64], len(issues))
for _, issue := range issues {
- projectID := issue.ProjectID()
- if _, ok := ids[projectID]; !ok {
- ids[projectID] = struct{}{}
- }
+ ids.Add(issue.ProjectID())
}
- return container.KeysInt64(ids)
+ return ids.Values()
}
func (issues IssueList) loadProjects(ctx context.Context) error {
diff --git a/models/issues/reaction.go b/models/issues/reaction.go
index e7295c8af8..ccda10be2c 100644
--- a/models/issues/reaction.go
+++ b/models/issues/reaction.go
@@ -211,7 +211,7 @@ type ReactionOptions struct {
// CreateReaction creates reaction for issue or comment.
func CreateReaction(opts *ReactionOptions) (*Reaction, error) {
- if !setting.UI.ReactionsMap[opts.Type] {
+ if !setting.UI.ReactionsLookup.Contains(opts.Type) {
return nil, ErrForbiddenIssueReaction{opts.Type}
}
@@ -316,16 +316,14 @@ func (list ReactionList) GroupByType() map[string]ReactionList {
}
func (list ReactionList) getUserIDs() []int64 {
- userIDs := make(map[int64]struct{}, len(list))
+ userIDs := make(container.Set[int64], len(list))
for _, reaction := range list {
if reaction.OriginalAuthor != "" {
continue
}
- if _, ok := userIDs[reaction.UserID]; !ok {
- userIDs[reaction.UserID] = struct{}{}
- }
+ userIDs.Add(reaction.UserID)
}
- return container.KeysInt64(userIDs)
+ return userIDs.Values()
}
func valuesUser(m map[int64]*user_model.User) []*user_model.User {