chore: teach set module about iter.Seq (#6676)

- Add a new `Seq` function to the `Set` type, this returns an iterator over the values.
- Convert some users of the `Values` method to allow for more optimal code.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6676
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-committed-by: Gusted <postmaster@gusted.xyz>
This commit is contained in:
Gusted 2025-01-24 16:45:46 +00:00 committed by Earl Warren
parent 46e60ce966
commit 443f7d59f9
9 changed files with 38 additions and 28 deletions

View file

@ -195,7 +195,7 @@ func serveInstalled(ctx *cli.Context) error {
publicFilesSet.Remove(".well-known") publicFilesSet.Remove(".well-known")
publicFilesSet.Remove("assets") publicFilesSet.Remove("assets")
publicFilesSet.Remove("robots.txt") publicFilesSet.Remove("robots.txt")
for _, fn := range publicFilesSet.Values() { for fn := range publicFilesSet.Seq() {
log.Error("Found legacy public asset %q in CustomPath. Please move it to %s/public/assets/%s", fn, setting.CustomPath, fn) log.Error("Found legacy public asset %q in CustomPath. Please move it to %s/public/assets/%s", fn, setting.CustomPath, fn)
} }
if _, err := os.Stat(filepath.Join(setting.CustomPath, "robots.txt")); err == nil { if _, err := os.Stat(filepath.Join(setting.CustomPath, "robots.txt")); err == nil {

View file

@ -715,7 +715,7 @@ func TestDisabledUserFeatures(t *testing.T) {
// no features should be disabled with a plain login type // no features should be disabled with a plain login type
assert.LessOrEqual(t, user.LoginType, auth.Plain) assert.LessOrEqual(t, user.LoginType, auth.Plain)
assert.Empty(t, user_model.DisabledFeaturesWithLoginType(user).Values()) assert.Empty(t, user_model.DisabledFeaturesWithLoginType(user).Values())
for _, f := range testValues.Values() { for f := range testValues.Seq() {
assert.False(t, user_model.IsFeatureDisabledWithLoginType(user, f)) assert.False(t, user_model.IsFeatureDisabledWithLoginType(user, f))
} }
@ -724,7 +724,7 @@ func TestDisabledUserFeatures(t *testing.T) {
// all features should be disabled // all features should be disabled
assert.NotEmpty(t, user_model.DisabledFeaturesWithLoginType(user).Values()) assert.NotEmpty(t, user_model.DisabledFeaturesWithLoginType(user).Values())
for _, f := range testValues.Values() { for f := range testValues.Seq() {
assert.True(t, user_model.IsFeatureDisabledWithLoginType(user, f)) assert.True(t, user_model.IsFeatureDisabledWithLoginType(user, f))
} }
} }

View file

@ -11,7 +11,7 @@ import (
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"sort" "slices"
"time" "time"
"code.gitea.io/gitea/modules/container" "code.gitea.io/gitea/modules/container"
@ -143,8 +143,7 @@ func (l *LayeredFS) ListFiles(name string, fileMode ...bool) ([]string, error) {
} }
} }
} }
files := fileSet.Values() files := slices.Sorted(fileSet.Seq())
sort.Strings(files)
return files, nil return files, nil
} }
@ -184,8 +183,7 @@ func listAllFiles(layers []*Layer, name string, fileMode ...bool) ([]string, err
if err := list(name); err != nil { if err := list(name); err != nil {
return nil, err return nil, err
} }
files := fileSet.Values() files := slices.Sorted(fileSet.Seq())
sort.Strings(files)
return files, nil return files, nil
} }

View file

@ -3,6 +3,11 @@
package container package container
import (
"iter"
"maps"
)
type Set[T comparable] map[T]struct{} type Set[T comparable] map[T]struct{}
// SetOf creates a set and adds the specified elements to it. // SetOf creates a set and adds the specified elements to it.
@ -63,3 +68,9 @@ func (s Set[T]) Values() []T {
} }
return keys return keys
} }
// Seq returns a iterator over the elements in the set.
// It returns a single-use iterator.
func (s Set[T]) Seq() iter.Seq[T] {
return maps.Keys(s)
}

View file

@ -4,6 +4,7 @@
package container package container
import ( import (
"slices"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -29,6 +30,14 @@ func TestSet(t *testing.T) {
assert.True(t, s.Contains("key4")) assert.True(t, s.Contains("key4"))
assert.True(t, s.Contains("key5")) assert.True(t, s.Contains("key5"))
values := s.Values()
called := 0
for value := range s.Seq() {
called++
assert.True(t, slices.Contains(values, value))
}
assert.EqualValues(t, len(values), called)
s = SetOf("key6", "key7") s = SetOf("key6", "key7")
assert.False(t, s.Contains("key1")) assert.False(t, s.Contains("key1"))
assert.True(t, s.Contains("key6")) assert.True(t, s.Contains("key6"))

View file

@ -4,7 +4,6 @@
package util package util
import ( import (
"cmp"
"slices" "slices"
"strings" "strings"
) )
@ -47,13 +46,6 @@ func SliceRemoveAll[T comparable](slice []T, target T) []T {
return slices.DeleteFunc(slice, func(t T) bool { return t == target }) return slices.DeleteFunc(slice, func(t T) bool { return t == target })
} }
// Sorted returns the sorted slice
// Note: The parameter is sorted inline.
func Sorted[S ~[]E, E cmp.Ordered](values S) S {
slices.Sort(values)
return values
}
// TODO: Replace with "maps.Values" once available, current it only in golang.org/x/exp/maps but not in standard library // TODO: Replace with "maps.Values" once available, current it only in golang.org/x/exp/maps but not in standard library
func ValuesOfMap[K comparable, V any](m map[K]V) []V { func ValuesOfMap[K comparable, V any](m map[K]V) []V {
values := make([]V, 0, len(m)) values := make([]V, 0, len(m))

View file

@ -6,6 +6,7 @@ package user
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"slices"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
org_model "code.gitea.io/gitea/models/organization" org_model "code.gitea.io/gitea/models/organization"
@ -23,7 +24,6 @@ import (
debian_module "code.gitea.io/gitea/modules/packages/debian" debian_module "code.gitea.io/gitea/modules/packages/debian"
rpm_module "code.gitea.io/gitea/modules/packages/rpm" rpm_module "code.gitea.io/gitea/modules/packages/rpm"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/modules/web"
packages_helper "code.gitea.io/gitea/routers/api/packages/helper" packages_helper "code.gitea.io/gitea/routers/api/packages/helper"
shared_user "code.gitea.io/gitea/routers/web/shared/user" shared_user "code.gitea.io/gitea/routers/web/shared/user"
@ -200,9 +200,9 @@ func ViewPackageVersion(ctx *context.Context) {
} }
} }
ctx.Data["Branches"] = util.Sorted(branches.Values()) ctx.Data["Branches"] = slices.Sorted(branches.Seq())
ctx.Data["Repositories"] = util.Sorted(repositories.Values()) ctx.Data["Repositories"] = slices.Sorted(repositories.Seq())
ctx.Data["Architectures"] = util.Sorted(architectures.Values()) ctx.Data["Architectures"] = slices.Sorted(architectures.Seq())
case packages_model.TypeArch: case packages_model.TypeArch:
ctx.Data["SignMail"] = fmt.Sprintf("%s@noreply.%s", ctx.Package.Owner.Name, setting.Packages.RegistryHost) ctx.Data["SignMail"] = fmt.Sprintf("%s@noreply.%s", ctx.Package.Owner.Name, setting.Packages.RegistryHost)
groups := make(container.Set[string]) groups := make(container.Set[string])
@ -213,7 +213,7 @@ func ViewPackageVersion(ctx *context.Context) {
} }
} }
} }
ctx.Data["Groups"] = util.Sorted(groups.Values()) ctx.Data["Groups"] = slices.Sorted(groups.Seq())
case packages_model.TypeDebian: case packages_model.TypeDebian:
distributions := make(container.Set[string]) distributions := make(container.Set[string])
components := make(container.Set[string]) components := make(container.Set[string])
@ -232,9 +232,9 @@ func ViewPackageVersion(ctx *context.Context) {
} }
} }
ctx.Data["Distributions"] = util.Sorted(distributions.Values()) ctx.Data["Distributions"] = slices.Sorted(distributions.Seq())
ctx.Data["Components"] = util.Sorted(components.Values()) ctx.Data["Components"] = slices.Sorted(components.Seq())
ctx.Data["Architectures"] = util.Sorted(architectures.Values()) ctx.Data["Architectures"] = slices.Sorted(architectures.Seq())
case packages_model.TypeRpm, packages_model.TypeAlt: case packages_model.TypeRpm, packages_model.TypeAlt:
groups := make(container.Set[string]) groups := make(container.Set[string])
architectures := make(container.Set[string]) architectures := make(container.Set[string])
@ -250,8 +250,8 @@ func ViewPackageVersion(ctx *context.Context) {
} }
} }
ctx.Data["Groups"] = util.Sorted(groups.Values()) ctx.Data["Groups"] = slices.Sorted(groups.Seq())
ctx.Data["Architectures"] = util.Sorted(architectures.Values()) ctx.Data["Architectures"] = slices.Sorted(architectures.Seq())
} }
var ( var (

View file

@ -711,7 +711,7 @@ func buildRelease(ctx context.Context, pv *packages_model.PackageVersion, pfs []
architectures.Add(pd.FileMetadata.Architecture) architectures.Add(pd.FileMetadata.Architecture)
} }
for architecture := range architectures { for architecture := range architectures.Seq() {
version := time.Now().Unix() version := time.Now().Unix()
label := setting.AppName label := setting.AppName
data := fmt.Sprintf(`Archive: Alt Linux Team data := fmt.Sprintf(`Archive: Alt Linux Team

View file

@ -372,7 +372,7 @@ func UpdateRelease(ctx context.Context, doer *user_model.User, gitRepo *git.Repo
return err return err
} }
for _, uuid := range delAttachmentUUIDs.Values() { for uuid := range delAttachmentUUIDs.Seq() {
if err := storage.Attachments.Delete(repo_model.AttachmentRelativePath(uuid)); err != nil { if err := storage.Attachments.Delete(repo_model.AttachmentRelativePath(uuid)); err != nil {
// Even delete files failed, but the attachments has been removed from database, so we // Even delete files failed, but the attachments has been removed from database, so we
// should not return error but only record the error on logs. // should not return error but only record the error on logs.