Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Set[Type] instead of map[Type]bool/struct{}. #26804

Merged
merged 3 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions build/backport-locales.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"path/filepath"
"strings"

"code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/setting"
)

Expand Down Expand Up @@ -58,7 +59,7 @@ func main() {

// use old en-US as the base, and copy the new translations to the old locales
enUsOld := inisOld["options/locale/locale_en-US.ini"]
brokenWarned := map[string]bool{}
brokenWarned := make(container.Set[string])
for path, iniOld := range inisOld {
if iniOld == enUsOld {
continue
Expand All @@ -77,7 +78,7 @@ func main() {
broken := oldStr != "" && strings.Count(oldStr, "%") != strings.Count(newStr, "%")
broken = broken || strings.Contains(oldStr, "\n") || strings.Contains(oldStr, "\n")
if broken {
brokenWarned[secOld.Name()+"."+keyEnUs.Name()] = true
brokenWarned.Add(secOld.Name() + "." + keyEnUs.Name())
fmt.Println("----")
fmt.Printf("WARNING: skip broken locale: %s , [%s] %s\n", path, secEnUS.Name(), keyEnUs.Name())
fmt.Printf("\told: %s\n", strings.ReplaceAll(oldStr, "\n", "\\n"))
Expand All @@ -103,7 +104,7 @@ func main() {
broken = broken || strings.HasPrefix(str, "`\"")
broken = broken || strings.Count(str, `"`)%2 == 1
broken = broken || strings.Count(str, "`")%2 == 1
if broken && !brokenWarned[sec.Name()+"."+key.Name()] {
if broken && !brokenWarned.Contains(sec.Name()+"."+key.Name()) {
fmt.Printf("WARNING: found broken locale: %s , [%s] %s\n", path, sec.Name(), key.Name())
fmt.Printf("\tstr: %s\n", strings.ReplaceAll(str, "\n", "\\n"))
fmt.Println("----")
Expand Down
14 changes: 5 additions & 9 deletions build/generate-go-licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"regexp"
"sort"
"strings"

"code.gitea.io/gitea/modules/container"
)

// regexp is based on go-license, excluding README and NOTICE
Expand Down Expand Up @@ -55,20 +57,14 @@ func main() {
// yml
//
// It could be removed once we have a better regex.
excludedExt := map[string]bool{
".gitignore": true,
".go": true,
".mod": true,
".sum": true,
".toml": true,
".yml": true,
}
excludedExt := container.SetOf(".gitignore", ".go", ".mod", ".sum", ".toml", ".yml")

var paths []string
err := filepath.WalkDir(base, func(path string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
if entry.IsDir() || !licenseRe.MatchString(entry.Name()) || excludedExt[filepath.Ext(entry.Name())] {
if entry.IsDir() || !licenseRe.MatchString(entry.Name()) || excludedExt.Contains(filepath.Ext(entry.Name())) {
return nil
}
paths = append(paths, path)
Expand Down
6 changes: 3 additions & 3 deletions models/unit/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"code.gitea.io/gitea/models/perm"
"code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
)
Expand Down Expand Up @@ -318,14 +319,13 @@ var (

// FindUnitTypes give the unit key names and return valid unique units and invalid keys
func FindUnitTypes(nameKeys ...string) (res []Type, invalidKeys []string) {
m := map[Type]struct{}{}
m := make(container.Set[Type])
for _, key := range nameKeys {
t := TypeFromKey(key)
if t == TypeInvalid {
invalidKeys = append(invalidKeys, key)
} else if _, ok := m[t]; !ok {
} else if m.Add(t) {
res = append(res, t)
m[t] = struct{}{}
}
}
return res, invalidKeys
Expand Down
19 changes: 7 additions & 12 deletions modules/assetfs/layered.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"sort"
"time"

"code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/process"
"code.gitea.io/gitea/modules/util"
Expand Down Expand Up @@ -130,22 +131,19 @@ func readDir(layer *Layer, name string) ([]fs.FileInfo, error) {
// * false: only directories will be returned.
// The returned files are sorted by name.
func (l *LayeredFS) ListFiles(name string, fileMode ...bool) ([]string, error) {
fileMap := map[string]bool{}
fileSet := make(container.Set[string])
for _, layer := range l.layers {
infos, err := readDir(layer, name)
if err != nil {
return nil, err
}
for _, info := range infos {
if shouldInclude(info, fileMode...) {
fileMap[info.Name()] = true
fileSet.Add(info.Name())
}
}
}
files := make([]string, 0, len(fileMap))
for file := range fileMap {
files = append(files, file)
}
files := fileSet.Values()
sort.Strings(files)
return files, nil
}
Expand All @@ -161,7 +159,7 @@ func (l *LayeredFS) ListAllFiles(name string, fileMode ...bool) ([]string, error
}

func listAllFiles(layers []*Layer, name string, fileMode ...bool) ([]string, error) {
fileMap := map[string]bool{}
fileSet := make(container.Set[string])
var list func(dir string) error
list = func(dir string) error {
for _, layer := range layers {
Expand All @@ -172,7 +170,7 @@ func listAllFiles(layers []*Layer, name string, fileMode ...bool) ([]string, err
for _, info := range infos {
path := util.PathJoinRelX(dir, info.Name())
if shouldInclude(info, fileMode...) {
fileMap[path] = true
fileSet.Add(path)
}
if info.IsDir() {
if err = list(path); err != nil {
Expand All @@ -186,10 +184,7 @@ func listAllFiles(layers []*Layer, name string, fileMode ...bool) ([]string, err
if err := list(name); err != nil {
return nil, err
}
var files []string
for file := range fileMap {
files = append(files, file)
}
files := fileSet.Values()
sort.Strings(files)
return files, nil
}
Expand Down
10 changes: 5 additions & 5 deletions modules/templates/util_dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"html/template"
"reflect"

"code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/setting"
)
Expand Down Expand Up @@ -51,7 +52,7 @@ func dict(args ...any) (map[string]any, error) {
return m, nil
}

func dumpVarMarshalable(v any, dumped map[uintptr]bool) (ret any, ok bool) {
func dumpVarMarshalable(v any, dumped container.Set[uintptr]) (ret any, ok bool) {
if v == nil {
return nil, true
}
Expand All @@ -61,11 +62,10 @@ func dumpVarMarshalable(v any, dumped map[uintptr]bool) (ret any, ok bool) {
}
if e.CanAddr() {
addr := e.UnsafeAddr()
if dumped[addr] {
if !dumped.Add(addr) {
return "[dumped]", false
}
dumped[addr] = true
defer delete(dumped, addr)
defer dumped.Remove(addr)
}
switch e.Kind() {
case reflect.Bool, reflect.String,
Expand Down Expand Up @@ -107,7 +107,7 @@ func dumpVar(v any) template.HTML {
if setting.IsProd {
return "<pre>dumpVar: only available in dev mode</pre>"
}
m, ok := dumpVarMarshalable(v, map[uintptr]bool{})
m, ok := dumpVarMarshalable(v, make(container.Set[uintptr]))
var dumpStr string
jsonBytes, err := json.MarshalIndent(m, "", " ")
if err != nil {
Expand Down
8 changes: 3 additions & 5 deletions routers/api/actions/runner/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
actions_model "code.gitea.io/gitea/models/actions"
secret_model "code.gitea.io/gitea/models/secret"
actions_module "code.gitea.io/gitea/modules/actions"
"code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
Expand Down Expand Up @@ -200,10 +201,7 @@ func findTaskNeeds(ctx context.Context, task *actions_model.ActionTask) (map[str
if len(task.Job.Needs) == 0 {
return nil, nil
}
needs := map[string]struct{}{}
for _, v := range task.Job.Needs {
needs[v] = struct{}{}
}
needs := container.SetOf(task.Job.Needs...)

jobs, _, err := actions_model.FindRunJobs(ctx, actions_model.FindRunJobOptions{RunID: task.Job.RunID})
if err != nil {
Expand All @@ -212,7 +210,7 @@ func findTaskNeeds(ctx context.Context, task *actions_model.ActionTask) (map[str

ret := make(map[string]*runnerv1.TaskNeed, len(needs))
for _, job := range jobs {
if _, ok := needs[job.JobID]; !ok {
if !needs.Contains(job.JobID) {
continue
}
if job.TaskID == 0 || !job.Status.IsDone() {
Expand Down
7 changes: 2 additions & 5 deletions routers/web/user/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,12 +567,9 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {

// Remove repositories that should not be shown,
// which are repositories that have no issues and are not selected by the user.
selectedReposMap := make(map[int64]struct{}, len(selectedRepoIDs))
for _, repoID := range selectedRepoIDs {
selectedReposMap[repoID] = struct{}{}
}
selectedRepos := container.SetOf(selectedRepoIDs...)
for k, v := range issueCountByRepo {
if _, ok := selectedReposMap[k]; !ok && v == 0 {
if v == 0 && !selectedRepos.Contains(k) {
delete(issueCountByRepo, k)
}
}
Expand Down
7 changes: 4 additions & 3 deletions services/auth/source/ldap/source_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"code.gitea.io/gitea/models/organization"
user_model "code.gitea.io/gitea/models/user"
auth_module "code.gitea.io/gitea/modules/auth"
"code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util"
source_service "code.gitea.io/gitea/services/auth/source"
Expand Down Expand Up @@ -41,7 +42,7 @@ func (source *Source) Sync(ctx context.Context, updateExisting bool) error {

usernameUsers := make(map[string]*user_model.User, len(users))
mailUsers := make(map[string]*user_model.User, len(users))
keepActiveUsers := make(map[int64]struct{})
keepActiveUsers := make(container.Set[int64])

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

if usr != nil {
keepActiveUsers[usr.ID] = struct{}{}
keepActiveUsers.Add(usr.ID)
} else if len(su.Username) == 0 {
// we cannot create the user if su.Username is empty
continue
Expand Down Expand Up @@ -208,7 +209,7 @@ func (source *Source) Sync(ctx context.Context, updateExisting bool) error {
// Deactivate users not present in LDAP
if updateExisting {
for _, usr := range users {
if _, ok := keepActiveUsers[usr.ID]; ok {
if keepActiveUsers.Contains(usr.ID) {
continue
}

Expand Down
5 changes: 2 additions & 3 deletions services/migrations/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -673,16 +673,15 @@

func (g *GitlabDownloader) awardsToReactions(awards []*gitlab.AwardEmoji) []*base.Reaction {
result := make([]*base.Reaction, 0, len(awards))
uniqCheck := make(map[string]struct{})
uniqCheck := make(container.Set[string])

Check failure on line 676 in services/migrations/gitlab.go

View workflow job for this annotation

GitHub Actions / test-e2e

undefined: container

Check failure on line 676 in services/migrations/gitlab.go

View workflow job for this annotation

GitHub Actions / lint-backend

undefined: container) (typecheck)

Check failure on line 676 in services/migrations/gitlab.go

View workflow job for this annotation

GitHub Actions / lint-backend

undefined: container) (typecheck)

Check failure on line 676 in services/migrations/gitlab.go

View workflow job for this annotation

GitHub Actions / lint-backend

undefined: container) (typecheck)

Check failure on line 676 in services/migrations/gitlab.go

View workflow job for this annotation

GitHub Actions / lint-backend

undefined: container) (typecheck)

Check failure on line 676 in services/migrations/gitlab.go

View workflow job for this annotation

GitHub Actions / lint-backend

undefined: container) (typecheck)

Check failure on line 676 in services/migrations/gitlab.go

View workflow job for this annotation

GitHub Actions / backend

undefined: container

Check failure on line 676 in services/migrations/gitlab.go

View workflow job for this annotation

GitHub Actions / checks-backend

undefined: container

Check failure on line 676 in services/migrations/gitlab.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

undefined: container) (typecheck)

Check failure on line 676 in services/migrations/gitlab.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

undefined: container) (typecheck)

Check failure on line 676 in services/migrations/gitlab.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

undefined: container) (typecheck)

Check failure on line 676 in services/migrations/gitlab.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

undefined: container) (typecheck)

Check failure on line 676 in services/migrations/gitlab.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

undefined: container) (typecheck)

Check failure on line 676 in services/migrations/gitlab.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

undefined: container) (typecheck)

Check failure on line 676 in services/migrations/gitlab.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

undefined: container) (typecheck)

Check failure on line 676 in services/migrations/gitlab.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

undefined: container) (typecheck)

Check failure on line 676 in services/migrations/gitlab.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

undefined: container) (typecheck)

Check failure on line 676 in services/migrations/gitlab.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

undefined: container) (typecheck)

Check failure on line 676 in services/migrations/gitlab.go

View workflow job for this annotation

GitHub Actions / test-sqlite

undefined: container

Check failure on line 676 in services/migrations/gitlab.go

View workflow job for this annotation

GitHub Actions / test-mysql5

undefined: container

Check failure on line 676 in services/migrations/gitlab.go

View workflow job for this annotation

GitHub Actions / test-mssql

undefined: container

Check failure on line 676 in services/migrations/gitlab.go

View workflow job for this annotation

GitHub Actions / test-mysql8

undefined: container

Check failure on line 676 in services/migrations/gitlab.go

View workflow job for this annotation

GitHub Actions / test-unit

undefined: container

Check failure on line 676 in services/migrations/gitlab.go

View workflow job for this annotation

GitHub Actions / test-pgsql

undefined: container
for _, award := range awards {
uid := fmt.Sprintf("%s%d", award.Name, award.User.ID)
if _, ok := uniqCheck[uid]; !ok {
if uniqCheck.Add(uid) {
result = append(result, &base.Reaction{
UserID: int64(award.User.ID),
UserName: award.User.Username,
Content: award.Name,
})
uniqCheck[uid] = struct{}{}
}
}
return result
Expand Down
Loading