Skip to content

Commit 5315153

Browse files
authored
Use Set[Type] instead of map[Type]bool/struct{}. (#26804)
1 parent 815d267 commit 5315153

File tree

9 files changed

+36
-48
lines changed

9 files changed

+36
-48
lines changed

build/backport-locales.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"path/filepath"
1313
"strings"
1414

15+
"code.gitea.io/gitea/modules/container"
1516
"code.gitea.io/gitea/modules/setting"
1617
)
1718

@@ -58,7 +59,7 @@ func main() {
5859

5960
// use old en-US as the base, and copy the new translations to the old locales
6061
enUsOld := inisOld["options/locale/locale_en-US.ini"]
61-
brokenWarned := map[string]bool{}
62+
brokenWarned := make(container.Set[string])
6263
for path, iniOld := range inisOld {
6364
if iniOld == enUsOld {
6465
continue
@@ -77,7 +78,7 @@ func main() {
7778
broken := oldStr != "" && strings.Count(oldStr, "%") != strings.Count(newStr, "%")
7879
broken = broken || strings.Contains(oldStr, "\n") || strings.Contains(oldStr, "\n")
7980
if broken {
80-
brokenWarned[secOld.Name()+"."+keyEnUs.Name()] = true
81+
brokenWarned.Add(secOld.Name() + "." + keyEnUs.Name())
8182
fmt.Println("----")
8283
fmt.Printf("WARNING: skip broken locale: %s , [%s] %s\n", path, secEnUS.Name(), keyEnUs.Name())
8384
fmt.Printf("\told: %s\n", strings.ReplaceAll(oldStr, "\n", "\\n"))
@@ -103,7 +104,7 @@ func main() {
103104
broken = broken || strings.HasPrefix(str, "`\"")
104105
broken = broken || strings.Count(str, `"`)%2 == 1
105106
broken = broken || strings.Count(str, "`")%2 == 1
106-
if broken && !brokenWarned[sec.Name()+"."+key.Name()] {
107+
if broken && !brokenWarned.Contains(sec.Name()+"."+key.Name()) {
107108
fmt.Printf("WARNING: found broken locale: %s , [%s] %s\n", path, sec.Name(), key.Name())
108109
fmt.Printf("\tstr: %s\n", strings.ReplaceAll(str, "\n", "\\n"))
109110
fmt.Println("----")

build/generate-go-licenses.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"regexp"
1616
"sort"
1717
"strings"
18+
19+
"code.gitea.io/gitea/modules/container"
1820
)
1921

2022
// regexp is based on go-license, excluding README and NOTICE
@@ -55,20 +57,14 @@ func main() {
5557
// yml
5658
//
5759
// It could be removed once we have a better regex.
58-
excludedExt := map[string]bool{
59-
".gitignore": true,
60-
".go": true,
61-
".mod": true,
62-
".sum": true,
63-
".toml": true,
64-
".yml": true,
65-
}
60+
excludedExt := container.SetOf(".gitignore", ".go", ".mod", ".sum", ".toml", ".yml")
61+
6662
var paths []string
6763
err := filepath.WalkDir(base, func(path string, entry fs.DirEntry, err error) error {
6864
if err != nil {
6965
return err
7066
}
71-
if entry.IsDir() || !licenseRe.MatchString(entry.Name()) || excludedExt[filepath.Ext(entry.Name())] {
67+
if entry.IsDir() || !licenseRe.MatchString(entry.Name()) || excludedExt.Contains(filepath.Ext(entry.Name())) {
7268
return nil
7369
}
7470
paths = append(paths, path)

models/unit/unit.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010

1111
"code.gitea.io/gitea/models/perm"
12+
"code.gitea.io/gitea/modules/container"
1213
"code.gitea.io/gitea/modules/log"
1314
"code.gitea.io/gitea/modules/setting"
1415
)
@@ -318,14 +319,13 @@ var (
318319

319320
// FindUnitTypes give the unit key names and return valid unique units and invalid keys
320321
func FindUnitTypes(nameKeys ...string) (res []Type, invalidKeys []string) {
321-
m := map[Type]struct{}{}
322+
m := make(container.Set[Type])
322323
for _, key := range nameKeys {
323324
t := TypeFromKey(key)
324325
if t == TypeInvalid {
325326
invalidKeys = append(invalidKeys, key)
326-
} else if _, ok := m[t]; !ok {
327+
} else if m.Add(t) {
327328
res = append(res, t)
328-
m[t] = struct{}{}
329329
}
330330
}
331331
return res, invalidKeys

modules/assetfs/layered.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"sort"
1515
"time"
1616

17+
"code.gitea.io/gitea/modules/container"
1718
"code.gitea.io/gitea/modules/log"
1819
"code.gitea.io/gitea/modules/process"
1920
"code.gitea.io/gitea/modules/util"
@@ -130,22 +131,19 @@ func readDir(layer *Layer, name string) ([]fs.FileInfo, error) {
130131
// * false: only directories will be returned.
131132
// The returned files are sorted by name.
132133
func (l *LayeredFS) ListFiles(name string, fileMode ...bool) ([]string, error) {
133-
fileMap := map[string]bool{}
134+
fileSet := make(container.Set[string])
134135
for _, layer := range l.layers {
135136
infos, err := readDir(layer, name)
136137
if err != nil {
137138
return nil, err
138139
}
139140
for _, info := range infos {
140141
if shouldInclude(info, fileMode...) {
141-
fileMap[info.Name()] = true
142+
fileSet.Add(info.Name())
142143
}
143144
}
144145
}
145-
files := make([]string, 0, len(fileMap))
146-
for file := range fileMap {
147-
files = append(files, file)
148-
}
146+
files := fileSet.Values()
149147
sort.Strings(files)
150148
return files, nil
151149
}
@@ -161,7 +159,7 @@ func (l *LayeredFS) ListAllFiles(name string, fileMode ...bool) ([]string, error
161159
}
162160

163161
func listAllFiles(layers []*Layer, name string, fileMode ...bool) ([]string, error) {
164-
fileMap := map[string]bool{}
162+
fileSet := make(container.Set[string])
165163
var list func(dir string) error
166164
list = func(dir string) error {
167165
for _, layer := range layers {
@@ -172,7 +170,7 @@ func listAllFiles(layers []*Layer, name string, fileMode ...bool) ([]string, err
172170
for _, info := range infos {
173171
path := util.PathJoinRelX(dir, info.Name())
174172
if shouldInclude(info, fileMode...) {
175-
fileMap[path] = true
173+
fileSet.Add(path)
176174
}
177175
if info.IsDir() {
178176
if err = list(path); err != nil {
@@ -186,10 +184,7 @@ func listAllFiles(layers []*Layer, name string, fileMode ...bool) ([]string, err
186184
if err := list(name); err != nil {
187185
return nil, err
188186
}
189-
var files []string
190-
for file := range fileMap {
191-
files = append(files, file)
192-
}
187+
files := fileSet.Values()
193188
sort.Strings(files)
194189
return files, nil
195190
}

modules/templates/util_dict.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"html/template"
1010
"reflect"
1111

12+
"code.gitea.io/gitea/modules/container"
1213
"code.gitea.io/gitea/modules/json"
1314
"code.gitea.io/gitea/modules/setting"
1415
)
@@ -51,7 +52,7 @@ func dict(args ...any) (map[string]any, error) {
5152
return m, nil
5253
}
5354

54-
func dumpVarMarshalable(v any, dumped map[uintptr]bool) (ret any, ok bool) {
55+
func dumpVarMarshalable(v any, dumped container.Set[uintptr]) (ret any, ok bool) {
5556
if v == nil {
5657
return nil, true
5758
}
@@ -61,11 +62,10 @@ func dumpVarMarshalable(v any, dumped map[uintptr]bool) (ret any, ok bool) {
6162
}
6263
if e.CanAddr() {
6364
addr := e.UnsafeAddr()
64-
if dumped[addr] {
65+
if !dumped.Add(addr) {
6566
return "[dumped]", false
6667
}
67-
dumped[addr] = true
68-
defer delete(dumped, addr)
68+
defer dumped.Remove(addr)
6969
}
7070
switch e.Kind() {
7171
case reflect.Bool, reflect.String,
@@ -107,7 +107,7 @@ func dumpVar(v any) template.HTML {
107107
if setting.IsProd {
108108
return "<pre>dumpVar: only available in dev mode</pre>"
109109
}
110-
m, ok := dumpVarMarshalable(v, map[uintptr]bool{})
110+
m, ok := dumpVarMarshalable(v, make(container.Set[uintptr]))
111111
var dumpStr string
112112
jsonBytes, err := json.MarshalIndent(m, "", " ")
113113
if err != nil {

routers/api/actions/runner/utils.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
actions_model "code.gitea.io/gitea/models/actions"
1111
secret_model "code.gitea.io/gitea/models/secret"
1212
actions_module "code.gitea.io/gitea/modules/actions"
13+
"code.gitea.io/gitea/modules/container"
1314
"code.gitea.io/gitea/modules/git"
1415
"code.gitea.io/gitea/modules/json"
1516
"code.gitea.io/gitea/modules/log"
@@ -197,10 +198,7 @@ func findTaskNeeds(ctx context.Context, task *actions_model.ActionTask) (map[str
197198
if len(task.Job.Needs) == 0 {
198199
return nil, nil
199200
}
200-
needs := map[string]struct{}{}
201-
for _, v := range task.Job.Needs {
202-
needs[v] = struct{}{}
203-
}
201+
needs := container.SetOf(task.Job.Needs...)
204202

205203
jobs, _, err := actions_model.FindRunJobs(ctx, actions_model.FindRunJobOptions{RunID: task.Job.RunID})
206204
if err != nil {
@@ -209,7 +207,7 @@ func findTaskNeeds(ctx context.Context, task *actions_model.ActionTask) (map[str
209207

210208
ret := make(map[string]*runnerv1.TaskNeed, len(needs))
211209
for _, job := range jobs {
212-
if _, ok := needs[job.JobID]; !ok {
210+
if !needs.Contains(job.JobID) {
213211
continue
214212
}
215213
if job.TaskID == 0 || !job.Status.IsDone() {

routers/web/user/home.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -567,12 +567,9 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
567567

568568
// Remove repositories that should not be shown,
569569
// which are repositories that have no issues and are not selected by the user.
570-
selectedReposMap := make(map[int64]struct{}, len(selectedRepoIDs))
571-
for _, repoID := range selectedRepoIDs {
572-
selectedReposMap[repoID] = struct{}{}
573-
}
570+
selectedRepos := container.SetOf(selectedRepoIDs...)
574571
for k, v := range issueCountByRepo {
575-
if _, ok := selectedReposMap[k]; !ok && v == 0 {
572+
if v == 0 && !selectedRepos.Contains(k) {
576573
delete(issueCountByRepo, k)
577574
}
578575
}

services/auth/source/ldap/source_sync.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"code.gitea.io/gitea/models/organization"
1414
user_model "code.gitea.io/gitea/models/user"
1515
auth_module "code.gitea.io/gitea/modules/auth"
16+
"code.gitea.io/gitea/modules/container"
1617
"code.gitea.io/gitea/modules/log"
1718
"code.gitea.io/gitea/modules/util"
1819
source_service "code.gitea.io/gitea/services/auth/source"
@@ -41,7 +42,7 @@ func (source *Source) Sync(ctx context.Context, updateExisting bool) error {
4142

4243
usernameUsers := make(map[string]*user_model.User, len(users))
4344
mailUsers := make(map[string]*user_model.User, len(users))
44-
keepActiveUsers := make(map[int64]struct{})
45+
keepActiveUsers := make(container.Set[int64])
4546

4647
for _, u := range users {
4748
usernameUsers[u.LowerName] = u
@@ -97,7 +98,7 @@ func (source *Source) Sync(ctx context.Context, updateExisting bool) error {
9798
}
9899

99100
if usr != nil {
100-
keepActiveUsers[usr.ID] = struct{}{}
101+
keepActiveUsers.Add(usr.ID)
101102
} else if len(su.Username) == 0 {
102103
// we cannot create the user if su.Username is empty
103104
continue
@@ -208,7 +209,7 @@ func (source *Source) Sync(ctx context.Context, updateExisting bool) error {
208209
// Deactivate users not present in LDAP
209210
if updateExisting {
210211
for _, usr := range users {
211-
if _, ok := keepActiveUsers[usr.ID]; ok {
212+
if keepActiveUsers.Contains(usr.ID) {
212213
continue
213214
}
214215

services/migrations/gitlab.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strings"
1515
"time"
1616

17+
"code.gitea.io/gitea/modules/container"
1718
"code.gitea.io/gitea/modules/log"
1819
base "code.gitea.io/gitea/modules/migration"
1920
"code.gitea.io/gitea/modules/structs"
@@ -673,16 +674,15 @@ func (g *GitlabDownloader) GetReviews(reviewable base.Reviewable) ([]*base.Revie
673674

674675
func (g *GitlabDownloader) awardsToReactions(awards []*gitlab.AwardEmoji) []*base.Reaction {
675676
result := make([]*base.Reaction, 0, len(awards))
676-
uniqCheck := make(map[string]struct{})
677+
uniqCheck := make(container.Set[string])
677678
for _, award := range awards {
678679
uid := fmt.Sprintf("%s%d", award.Name, award.User.ID)
679-
if _, ok := uniqCheck[uid]; !ok {
680+
if uniqCheck.Add(uid) {
680681
result = append(result, &base.Reaction{
681682
UserID: int64(award.User.ID),
682683
UserName: award.User.Username,
683684
Content: award.Name,
684685
})
685-
uniqCheck[uid] = struct{}{}
686686
}
687687
}
688688
return result

0 commit comments

Comments
 (0)