Skip to content

Commit 443f7d5

Browse files
GustedEarl Warren
authored andcommitted
chore: teach set module about iter.Seq (go-gitea#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 <[email protected]> Co-authored-by: Gusted <[email protected]> Co-committed-by: Gusted <[email protected]>
1 parent 46e60ce commit 443f7d5

File tree

9 files changed

+38
-28
lines changed

9 files changed

+38
-28
lines changed

cmd/web.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func serveInstalled(ctx *cli.Context) error {
195195
publicFilesSet.Remove(".well-known")
196196
publicFilesSet.Remove("assets")
197197
publicFilesSet.Remove("robots.txt")
198-
for _, fn := range publicFilesSet.Values() {
198+
for fn := range publicFilesSet.Seq() {
199199
log.Error("Found legacy public asset %q in CustomPath. Please move it to %s/public/assets/%s", fn, setting.CustomPath, fn)
200200
}
201201
if _, err := os.Stat(filepath.Join(setting.CustomPath, "robots.txt")); err == nil {

models/user/user_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ func TestDisabledUserFeatures(t *testing.T) {
715715
// no features should be disabled with a plain login type
716716
assert.LessOrEqual(t, user.LoginType, auth.Plain)
717717
assert.Empty(t, user_model.DisabledFeaturesWithLoginType(user).Values())
718-
for _, f := range testValues.Values() {
718+
for f := range testValues.Seq() {
719719
assert.False(t, user_model.IsFeatureDisabledWithLoginType(user, f))
720720
}
721721

@@ -724,7 +724,7 @@ func TestDisabledUserFeatures(t *testing.T) {
724724

725725
// all features should be disabled
726726
assert.NotEmpty(t, user_model.DisabledFeaturesWithLoginType(user).Values())
727-
for _, f := range testValues.Values() {
727+
for f := range testValues.Seq() {
728728
assert.True(t, user_model.IsFeatureDisabledWithLoginType(user, f))
729729
}
730730
}

modules/assetfs/layered.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"net/http"
1212
"os"
1313
"path/filepath"
14-
"sort"
14+
"slices"
1515
"time"
1616

1717
"code.gitea.io/gitea/modules/container"
@@ -143,8 +143,7 @@ func (l *LayeredFS) ListFiles(name string, fileMode ...bool) ([]string, error) {
143143
}
144144
}
145145
}
146-
files := fileSet.Values()
147-
sort.Strings(files)
146+
files := slices.Sorted(fileSet.Seq())
148147
return files, nil
149148
}
150149

@@ -184,8 +183,7 @@ func listAllFiles(layers []*Layer, name string, fileMode ...bool) ([]string, err
184183
if err := list(name); err != nil {
185184
return nil, err
186185
}
187-
files := fileSet.Values()
188-
sort.Strings(files)
186+
files := slices.Sorted(fileSet.Seq())
189187
return files, nil
190188
}
191189

modules/container/set.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
package container
55

6+
import (
7+
"iter"
8+
"maps"
9+
)
10+
611
type Set[T comparable] map[T]struct{}
712

813
// SetOf creates a set and adds the specified elements to it.
@@ -63,3 +68,9 @@ func (s Set[T]) Values() []T {
6368
}
6469
return keys
6570
}
71+
72+
// Seq returns a iterator over the elements in the set.
73+
// It returns a single-use iterator.
74+
func (s Set[T]) Seq() iter.Seq[T] {
75+
return maps.Keys(s)
76+
}

modules/container/set_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package container
55

66
import (
7+
"slices"
78
"testing"
89

910
"github.com/stretchr/testify/assert"
@@ -29,6 +30,14 @@ func TestSet(t *testing.T) {
2930
assert.True(t, s.Contains("key4"))
3031
assert.True(t, s.Contains("key5"))
3132

33+
values := s.Values()
34+
called := 0
35+
for value := range s.Seq() {
36+
called++
37+
assert.True(t, slices.Contains(values, value))
38+
}
39+
assert.EqualValues(t, len(values), called)
40+
3241
s = SetOf("key6", "key7")
3342
assert.False(t, s.Contains("key1"))
3443
assert.True(t, s.Contains("key6"))

modules/util/slice.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package util
55

66
import (
7-
"cmp"
87
"slices"
98
"strings"
109
)
@@ -47,13 +46,6 @@ func SliceRemoveAll[T comparable](slice []T, target T) []T {
4746
return slices.DeleteFunc(slice, func(t T) bool { return t == target })
4847
}
4948

50-
// Sorted returns the sorted slice
51-
// Note: The parameter is sorted inline.
52-
func Sorted[S ~[]E, E cmp.Ordered](values S) S {
53-
slices.Sort(values)
54-
return values
55-
}
56-
5749
// TODO: Replace with "maps.Values" once available, current it only in golang.org/x/exp/maps but not in standard library
5850
func ValuesOfMap[K comparable, V any](m map[K]V) []V {
5951
values := make([]V, 0, len(m))

routers/web/user/package.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package user
66
import (
77
"fmt"
88
"net/http"
9+
"slices"
910

1011
"code.gitea.io/gitea/models/db"
1112
org_model "code.gitea.io/gitea/models/organization"
@@ -23,7 +24,6 @@ import (
2324
debian_module "code.gitea.io/gitea/modules/packages/debian"
2425
rpm_module "code.gitea.io/gitea/modules/packages/rpm"
2526
"code.gitea.io/gitea/modules/setting"
26-
"code.gitea.io/gitea/modules/util"
2727
"code.gitea.io/gitea/modules/web"
2828
packages_helper "code.gitea.io/gitea/routers/api/packages/helper"
2929
shared_user "code.gitea.io/gitea/routers/web/shared/user"
@@ -200,9 +200,9 @@ func ViewPackageVersion(ctx *context.Context) {
200200
}
201201
}
202202

203-
ctx.Data["Branches"] = util.Sorted(branches.Values())
204-
ctx.Data["Repositories"] = util.Sorted(repositories.Values())
205-
ctx.Data["Architectures"] = util.Sorted(architectures.Values())
203+
ctx.Data["Branches"] = slices.Sorted(branches.Seq())
204+
ctx.Data["Repositories"] = slices.Sorted(repositories.Seq())
205+
ctx.Data["Architectures"] = slices.Sorted(architectures.Seq())
206206
case packages_model.TypeArch:
207207
ctx.Data["SignMail"] = fmt.Sprintf("%s@noreply.%s", ctx.Package.Owner.Name, setting.Packages.RegistryHost)
208208
groups := make(container.Set[string])
@@ -213,7 +213,7 @@ func ViewPackageVersion(ctx *context.Context) {
213213
}
214214
}
215215
}
216-
ctx.Data["Groups"] = util.Sorted(groups.Values())
216+
ctx.Data["Groups"] = slices.Sorted(groups.Seq())
217217
case packages_model.TypeDebian:
218218
distributions := make(container.Set[string])
219219
components := make(container.Set[string])
@@ -232,9 +232,9 @@ func ViewPackageVersion(ctx *context.Context) {
232232
}
233233
}
234234

235-
ctx.Data["Distributions"] = util.Sorted(distributions.Values())
236-
ctx.Data["Components"] = util.Sorted(components.Values())
237-
ctx.Data["Architectures"] = util.Sorted(architectures.Values())
235+
ctx.Data["Distributions"] = slices.Sorted(distributions.Seq())
236+
ctx.Data["Components"] = slices.Sorted(components.Seq())
237+
ctx.Data["Architectures"] = slices.Sorted(architectures.Seq())
238238
case packages_model.TypeRpm, packages_model.TypeAlt:
239239
groups := make(container.Set[string])
240240
architectures := make(container.Set[string])
@@ -250,8 +250,8 @@ func ViewPackageVersion(ctx *context.Context) {
250250
}
251251
}
252252

253-
ctx.Data["Groups"] = util.Sorted(groups.Values())
254-
ctx.Data["Architectures"] = util.Sorted(architectures.Values())
253+
ctx.Data["Groups"] = slices.Sorted(groups.Seq())
254+
ctx.Data["Architectures"] = slices.Sorted(architectures.Seq())
255255
}
256256

257257
var (

services/packages/alt/repository.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ func buildRelease(ctx context.Context, pv *packages_model.PackageVersion, pfs []
711711
architectures.Add(pd.FileMetadata.Architecture)
712712
}
713713

714-
for architecture := range architectures {
714+
for architecture := range architectures.Seq() {
715715
version := time.Now().Unix()
716716
label := setting.AppName
717717
data := fmt.Sprintf(`Archive: Alt Linux Team

services/release/release.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ func UpdateRelease(ctx context.Context, doer *user_model.User, gitRepo *git.Repo
372372
return err
373373
}
374374

375-
for _, uuid := range delAttachmentUUIDs.Values() {
375+
for uuid := range delAttachmentUUIDs.Seq() {
376376
if err := storage.Attachments.Delete(repo_model.AttachmentRelativePath(uuid)); err != nil {
377377
// Even delete files failed, but the attachments has been removed from database, so we
378378
// should not return error but only record the error on logs.

0 commit comments

Comments
 (0)