aboutsummaryrefslogtreecommitdiffstats
path: root/models/repo
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/repo
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/repo')
-rw-r--r--models/repo/repo_list.go6
-rw-r--r--models/repo/topic.go7
-rw-r--r--models/repo/user_repo.go31
3 files changed, 14 insertions, 30 deletions
diff --git a/models/repo/repo_list.go b/models/repo/repo_list.go
index cc524a417e..0cd0a3c8e3 100644
--- a/models/repo/repo_list.go
+++ b/models/repo/repo_list.go
@@ -68,10 +68,10 @@ func (repos RepositoryList) loadAttributes(ctx context.Context) error {
return nil
}
- set := make(map[int64]struct{})
+ set := make(container.Set[int64])
repoIDs := make([]int64, len(repos))
for i := range repos {
- set[repos[i].OwnerID] = struct{}{}
+ set.Add(repos[i].OwnerID)
repoIDs[i] = repos[i].ID
}
@@ -79,7 +79,7 @@ func (repos RepositoryList) loadAttributes(ctx context.Context) error {
users := make(map[int64]*user_model.User, len(set))
if err := db.GetEngine(ctx).
Where("id > 0").
- In("id", container.KeysInt64(set)).
+ In("id", set.Values()).
Find(&users); err != nil {
return fmt.Errorf("find users: %v", err)
}
diff --git a/models/repo/topic.go b/models/repo/topic.go
index 2a16467215..7ba9a49e89 100644
--- a/models/repo/topic.go
+++ b/models/repo/topic.go
@@ -11,6 +11,7 @@ import (
"strings"
"code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/timeutil"
"xorm.io/builder"
@@ -62,7 +63,7 @@ func ValidateTopic(topic string) bool {
// SanitizeAndValidateTopics sanitizes and checks an array or topics
func SanitizeAndValidateTopics(topics []string) (validTopics, invalidTopics []string) {
validTopics = make([]string, 0)
- mValidTopics := make(map[string]struct{})
+ mValidTopics := make(container.Set[string])
invalidTopics = make([]string, 0)
for _, topic := range topics {
@@ -72,12 +73,12 @@ func SanitizeAndValidateTopics(topics []string) (validTopics, invalidTopics []st
continue
}
// ignore same topic twice
- if _, ok := mValidTopics[topic]; ok {
+ if mValidTopics.Contains(topic) {
continue
}
if ValidateTopic(topic) {
validTopics = append(validTopics, topic)
- mValidTopics[topic] = struct{}{}
+ mValidTopics.Add(topic)
} else {
invalidTopics = append(invalidTopics, topic)
}
diff --git a/models/repo/user_repo.go b/models/repo/user_repo.go
index 6c0a241dc5..e7125f70f8 100644
--- a/models/repo/user_repo.go
+++ b/models/repo/user_repo.go
@@ -10,6 +10,7 @@ import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/perm"
user_model "code.gitea.io/gitea/models/user"
+ "code.gitea.io/gitea/modules/container"
api "code.gitea.io/gitea/modules/structs"
"xorm.io/builder"
@@ -83,37 +84,19 @@ func GetRepoAssignees(ctx context.Context, repo *Repository) (_ []*user_model.Us
return nil, err
}
- uidMap := map[int64]bool{}
- i := 0
- for _, uid := range userIDs {
- if uidMap[uid] {
- continue
- }
- uidMap[uid] = true
- userIDs[i] = uid
- i++
- }
- userIDs = userIDs[:i]
- userIDs = append(userIDs, additionalUserIDs...)
-
- for _, uid := range additionalUserIDs {
- if uidMap[uid] {
- continue
- }
- userIDs[i] = uid
- i++
- }
- userIDs = userIDs[:i]
+ uniqueUserIDs := make(container.Set[int64])
+ uniqueUserIDs.AddMultiple(userIDs...)
+ uniqueUserIDs.AddMultiple(additionalUserIDs...)
// Leave a seat for owner itself to append later, but if owner is an organization
// and just waste 1 unit is cheaper than re-allocate memory once.
- users := make([]*user_model.User, 0, len(userIDs)+1)
+ users := make([]*user_model.User, 0, len(uniqueUserIDs)+1)
if len(userIDs) > 0 {
- if err = e.In("id", userIDs).OrderBy(user_model.GetOrderByName()).Find(&users); err != nil {
+ if err = e.In("id", uniqueUserIDs.Values()).OrderBy(user_model.GetOrderByName()).Find(&users); err != nil {
return nil, err
}
}
- if !repo.Owner.IsOrganization() && !uidMap[repo.OwnerID] {
+ if !repo.Owner.IsOrganization() && !uniqueUserIDs.Contains(repo.OwnerID) {
users = append(users, repo.Owner)
}