mirror of
https://github.com/go-gitea/gitea
synced 2025-07-25 19:58:36 +00:00
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>
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
||||
"code.gitea.io/gitea/models/db"
|
||||
packages_model "code.gitea.io/gitea/models/packages"
|
||||
conan_model "code.gitea.io/gitea/models/packages/conan"
|
||||
"code.gitea.io/gitea/modules/container"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
@@ -33,20 +34,18 @@ const (
|
||||
packageReferenceKey = "PackageReference"
|
||||
)
|
||||
|
||||
type stringSet map[string]struct{}
|
||||
|
||||
var (
|
||||
recipeFileList = stringSet{
|
||||
conanfileFile: struct{}{},
|
||||
"conanmanifest.txt": struct{}{},
|
||||
"conan_sources.tgz": struct{}{},
|
||||
"conan_export.tgz": struct{}{},
|
||||
}
|
||||
packageFileList = stringSet{
|
||||
conaninfoFile: struct{}{},
|
||||
"conanmanifest.txt": struct{}{},
|
||||
"conan_package.tgz": struct{}{},
|
||||
}
|
||||
recipeFileList = container.SetOf(
|
||||
conanfileFile,
|
||||
"conanmanifest.txt",
|
||||
"conan_sources.tgz",
|
||||
"conan_export.tgz",
|
||||
)
|
||||
packageFileList = container.SetOf(
|
||||
conaninfoFile,
|
||||
"conanmanifest.txt",
|
||||
"conan_package.tgz",
|
||||
)
|
||||
)
|
||||
|
||||
func jsonResponse(ctx *context.Context, status int, obj interface{}) {
|
||||
@@ -268,7 +267,7 @@ func PackageUploadURLs(ctx *context.Context) {
|
||||
)
|
||||
}
|
||||
|
||||
func serveUploadURLs(ctx *context.Context, fileFilter stringSet, uploadURL string) {
|
||||
func serveUploadURLs(ctx *context.Context, fileFilter container.Set[string], uploadURL string) {
|
||||
defer ctx.Req.Body.Close()
|
||||
|
||||
var files map[string]int64
|
||||
@@ -279,7 +278,7 @@ func serveUploadURLs(ctx *context.Context, fileFilter stringSet, uploadURL strin
|
||||
|
||||
urls := make(map[string]string)
|
||||
for file := range files {
|
||||
if _, ok := fileFilter[file]; ok {
|
||||
if fileFilter.Contains(file) {
|
||||
urls[file] = fmt.Sprintf("%s/%s", uploadURL, file)
|
||||
}
|
||||
}
|
||||
@@ -301,12 +300,12 @@ func UploadPackageFile(ctx *context.Context) {
|
||||
uploadFile(ctx, packageFileList, pref.AsKey())
|
||||
}
|
||||
|
||||
func uploadFile(ctx *context.Context, fileFilter stringSet, fileKey string) {
|
||||
func uploadFile(ctx *context.Context, fileFilter container.Set[string], fileKey string) {
|
||||
rref := ctx.Data[recipeReferenceKey].(*conan_module.RecipeReference)
|
||||
pref := ctx.Data[packageReferenceKey].(*conan_module.PackageReference)
|
||||
|
||||
filename := ctx.Params("filename")
|
||||
if _, ok := fileFilter[filename]; !ok {
|
||||
if !fileFilter.Contains(filename) {
|
||||
apiError(ctx, http.StatusBadRequest, nil)
|
||||
return
|
||||
}
|
||||
@@ -442,11 +441,11 @@ func DownloadPackageFile(ctx *context.Context) {
|
||||
downloadFile(ctx, packageFileList, pref.AsKey())
|
||||
}
|
||||
|
||||
func downloadFile(ctx *context.Context, fileFilter stringSet, fileKey string) {
|
||||
func downloadFile(ctx *context.Context, fileFilter container.Set[string], fileKey string) {
|
||||
rref := ctx.Data[recipeReferenceKey].(*conan_module.RecipeReference)
|
||||
|
||||
filename := ctx.Params("filename")
|
||||
if _, ok := fileFilter[filename]; !ok {
|
||||
if !fileFilter.Contains(filename) {
|
||||
apiError(ctx, http.StatusBadRequest, nil)
|
||||
return
|
||||
}
|
||||
|
@@ -30,6 +30,7 @@ import (
|
||||
"code.gitea.io/gitea/models/unit"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/base"
|
||||
"code.gitea.io/gitea/modules/container"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/convert"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
@@ -947,10 +948,11 @@ func ValidateRepoMetas(ctx *context.Context, form forms.CreateIssueForm, isPull
|
||||
if err != nil {
|
||||
return nil, nil, 0, 0
|
||||
}
|
||||
labelIDMark := base.Int64sToMap(labelIDs)
|
||||
labelIDMark := make(container.Set[int64])
|
||||
labelIDMark.AddMultiple(labelIDs...)
|
||||
|
||||
for i := range labels {
|
||||
if labelIDMark[labels[i].ID] {
|
||||
if labelIDMark.Contains(labels[i].ID) {
|
||||
labels[i].IsChecked = true
|
||||
hasSelected = true
|
||||
}
|
||||
@@ -1293,9 +1295,9 @@ func ViewIssue(ctx *context.Context) {
|
||||
|
||||
// Metas.
|
||||
// Check labels.
|
||||
labelIDMark := make(map[int64]bool)
|
||||
for i := range issue.Labels {
|
||||
labelIDMark[issue.Labels[i].ID] = true
|
||||
labelIDMark := make(container.Set[int64])
|
||||
for _, label := range issue.Labels {
|
||||
labelIDMark.Add(label.ID)
|
||||
}
|
||||
labels, err := issues_model.GetLabelsByRepoID(ctx, repo.ID, "", db.ListOptions{})
|
||||
if err != nil {
|
||||
@@ -1317,7 +1319,7 @@ func ViewIssue(ctx *context.Context) {
|
||||
|
||||
hasSelected := false
|
||||
for i := range labels {
|
||||
if labelIDMark[labels[i].ID] {
|
||||
if labelIDMark.Contains(labels[i].ID) {
|
||||
labels[i].IsChecked = true
|
||||
hasSelected = true
|
||||
}
|
||||
|
@@ -18,6 +18,7 @@ import (
|
||||
git_model "code.gitea.io/gitea/models/git"
|
||||
"code.gitea.io/gitea/modules/base"
|
||||
"code.gitea.io/gitea/modules/charset"
|
||||
"code.gitea.io/gitea/modules/container"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/git/pipeline"
|
||||
@@ -176,14 +177,12 @@ func LFSLocks(ctx *context.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
filemap := make(map[string]bool, len(filelist))
|
||||
for _, name := range filelist {
|
||||
filemap[name] = true
|
||||
}
|
||||
fileset := make(container.Set[string], len(filelist))
|
||||
fileset.AddMultiple(filelist...)
|
||||
|
||||
linkable := make([]bool, len(lfsLocks))
|
||||
for i, lock := range lfsLocks {
|
||||
linkable[i] = filemap[lock.Path]
|
||||
linkable[i] = fileset.Contains(lock.Path)
|
||||
}
|
||||
ctx.Data["Linkable"] = linkable
|
||||
|
||||
|
@@ -28,6 +28,7 @@ import (
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/base"
|
||||
"code.gitea.io/gitea/modules/charset"
|
||||
"code.gitea.io/gitea/modules/container"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/highlight"
|
||||
@@ -811,16 +812,14 @@ func renderDirectoryFiles(ctx *context.Context, timeout time.Duration) git.Entri
|
||||
defer cancel()
|
||||
}
|
||||
|
||||
selected := map[string]bool{}
|
||||
for _, pth := range ctx.FormStrings("f[]") {
|
||||
selected[pth] = true
|
||||
}
|
||||
selected := make(container.Set[string])
|
||||
selected.AddMultiple(ctx.FormStrings("f[]")...)
|
||||
|
||||
entries := allEntries
|
||||
if len(selected) > 0 {
|
||||
entries = make(git.Entries, 0, len(selected))
|
||||
for _, entry := range allEntries {
|
||||
if selected[entry.Name()] {
|
||||
if selected.Contains(entry.Name()) {
|
||||
entries = append(entries, entry)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user