Skip to content

Commit a78c2ea

Browse files
authored
Replace util.SliceXxx with slices.Xxx (#26958)
1 parent e97e883 commit a78c2ea

File tree

16 files changed

+46
-110
lines changed

16 files changed

+46
-110
lines changed

models/actions/run.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package actions
66
import (
77
"context"
88
"fmt"
9+
"slices"
910
"strings"
1011
"time"
1112

@@ -350,7 +351,7 @@ func UpdateRun(ctx context.Context, run *ActionRun, cols ...string) error {
350351
// It's impossible that the run is not found, since Gitea never deletes runs.
351352
}
352353

353-
if run.Status != 0 || util.SliceContains(cols, "status") {
354+
if run.Status != 0 || slices.Contains(cols, "status") {
354355
if run.RepoID == 0 {
355356
run, err = GetRunByID(ctx, run.ID)
356357
if err != nil {

models/actions/run_job.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package actions
66
import (
77
"context"
88
"fmt"
9+
"slices"
910
"time"
1011

1112
"code.gitea.io/gitea/models/db"
@@ -107,11 +108,11 @@ func UpdateRunJob(ctx context.Context, job *ActionRunJob, cond builder.Cond, col
107108
return 0, err
108109
}
109110

110-
if affected == 0 || (!util.SliceContains(cols, "status") && job.Status == 0) {
111+
if affected == 0 || (!slices.Contains(cols, "status") && job.Status == 0) {
111112
return affected, nil
112113
}
113114

114-
if affected != 0 && util.SliceContains(cols, "status") && job.Status.IsWaiting() {
115+
if affected != 0 && slices.Contains(cols, "status") && job.Status.IsWaiting() {
115116
// if the status of job changes to waiting again, increase tasks version.
116117
if err := IncreaseTaskVersion(ctx, job.OwnerID, job.RepoID); err != nil {
117118
return 0, err

models/git/protected_branch.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"errors"
99
"fmt"
10+
"slices"
1011
"strings"
1112

1213
"code.gitea.io/gitea/models/db"
@@ -435,7 +436,7 @@ func updateTeamWhitelist(ctx context.Context, repo *repo_model.Repository, curre
435436

436437
whitelist = make([]int64, 0, len(teams))
437438
for i := range teams {
438-
if util.SliceContains(newWhitelist, teams[i].ID) {
439+
if slices.Contains(newWhitelist, teams[i].ID) {
439440
whitelist = append(whitelist, teams[i].ID)
440441
}
441442
}

models/issues/issue.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"fmt"
1010
"regexp"
11+
"slices"
1112

1213
"code.gitea.io/gitea/models/db"
1314
project_model "code.gitea.io/gitea/models/project"
@@ -605,7 +606,7 @@ func IsUserParticipantsOfIssue(user *user_model.User, issue *Issue) bool {
605606
log.Error(err.Error())
606607
return false
607608
}
608-
return util.SliceContains(userIDs, user.ID)
609+
return slices.Contains(userIDs, user.ID)
609610
}
610611

611612
// DependencyInfo represents high level information about an issue which is a dependency of another issue.
@@ -630,7 +631,7 @@ func (issue *Issue) GetParticipantIDsByIssue(ctx context.Context) ([]int64, erro
630631
Find(&userIDs); err != nil {
631632
return nil, fmt.Errorf("get poster IDs: %w", err)
632633
}
633-
if !util.SliceContains(userIDs, issue.PosterID) {
634+
if !slices.Contains(userIDs, issue.PosterID) {
634635
return append(userIDs, issue.PosterID), nil
635636
}
636637
return userIDs, nil

models/repo/repo_unit.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package repo
66
import (
77
"context"
88
"fmt"
9+
"slices"
910
"strings"
1011

1112
"code.gitea.io/gitea/models/db"
@@ -176,7 +177,7 @@ func (cfg *ActionsConfig) ToString() string {
176177
}
177178

178179
func (cfg *ActionsConfig) IsWorkflowDisabled(file string) bool {
179-
return util.SliceContains(cfg.DisabledWorkflows, file)
180+
return slices.Contains(cfg.DisabledWorkflows, file)
180181
}
181182

182183
func (cfg *ActionsConfig) DisableWorkflow(file string) {

modules/indexer/code/indexer.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"os"
99
"runtime/pprof"
10+
"slices"
1011
"sync/atomic"
1112
"time"
1213

@@ -20,7 +21,6 @@ import (
2021
"code.gitea.io/gitea/modules/process"
2122
"code.gitea.io/gitea/modules/queue"
2223
"code.gitea.io/gitea/modules/setting"
23-
"code.gitea.io/gitea/modules/util"
2424
)
2525

2626
var (
@@ -54,22 +54,22 @@ func index(ctx context.Context, indexer internal.Indexer, repoID int64) error {
5454
}
5555

5656
// skip forks from being indexed if unit is not present
57-
if !util.SliceContains(repoTypes, "forks") && repo.IsFork {
57+
if !slices.Contains(repoTypes, "forks") && repo.IsFork {
5858
return nil
5959
}
6060

6161
// skip mirrors from being indexed if unit is not present
62-
if !util.SliceContains(repoTypes, "mirrors") && repo.IsMirror {
62+
if !slices.Contains(repoTypes, "mirrors") && repo.IsMirror {
6363
return nil
6464
}
6565

6666
// skip templates from being indexed if unit is not present
67-
if !util.SliceContains(repoTypes, "templates") && repo.IsTemplate {
67+
if !slices.Contains(repoTypes, "templates") && repo.IsTemplate {
6868
return nil
6969
}
7070

7171
// skip regular repos from being indexed if unit is not present
72-
if !util.SliceContains(repoTypes, "sources") && !repo.IsFork && !repo.IsMirror && !repo.IsTemplate {
72+
if !slices.Contains(repoTypes, "sources") && !repo.IsFork && !repo.IsMirror && !repo.IsTemplate {
7373
return nil
7474
}
7575

modules/indexer/issues/internal/tests/tests.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package tests
1010
import (
1111
"context"
1212
"fmt"
13+
"slices"
1314
"testing"
1415
"time"
1516

@@ -457,7 +458,7 @@ var cases = []*testIndexerCase{
457458
assert.Contains(t, data[v.ID].MentionIDs, int64(1))
458459
}
459460
assert.Equal(t, countIndexerData(data, func(v *internal.IndexerData) bool {
460-
return util.SliceContains(v.MentionIDs, 1)
461+
return slices.Contains(v.MentionIDs, 1)
461462
}), result.Total)
462463
},
463464
},
@@ -478,7 +479,7 @@ var cases = []*testIndexerCase{
478479
assert.Contains(t, data[v.ID].ReviewedIDs, int64(1))
479480
}
480481
assert.Equal(t, countIndexerData(data, func(v *internal.IndexerData) bool {
481-
return util.SliceContains(v.ReviewedIDs, 1)
482+
return slices.Contains(v.ReviewedIDs, 1)
482483
}), result.Total)
483484
},
484485
},
@@ -499,7 +500,7 @@ var cases = []*testIndexerCase{
499500
assert.Contains(t, data[v.ID].ReviewRequestedIDs, int64(1))
500501
}
501502
assert.Equal(t, countIndexerData(data, func(v *internal.IndexerData) bool {
502-
return util.SliceContains(v.ReviewRequestedIDs, 1)
503+
return slices.Contains(v.ReviewRequestedIDs, 1)
503504
}), result.Total)
504505
},
505506
},
@@ -520,7 +521,7 @@ var cases = []*testIndexerCase{
520521
assert.Contains(t, data[v.ID].SubscriberIDs, int64(1))
521522
}
522523
assert.Equal(t, countIndexerData(data, func(v *internal.IndexerData) bool {
523-
return util.SliceContains(v.SubscriberIDs, 1)
524+
return slices.Contains(v.SubscriberIDs, 1)
524525
}), result.Total)
525526
},
526527
},

modules/templates/base.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
package templates
55

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

910
"code.gitea.io/gitea/modules/assetfs"
1011
"code.gitea.io/gitea/modules/setting"
11-
"code.gitea.io/gitea/modules/util"
1212
)
1313

1414
func AssetFS() *assetfs.LayeredFS {
@@ -24,7 +24,7 @@ func ListWebTemplateAssetNames(assets *assetfs.LayeredFS) ([]string, error) {
2424
if err != nil {
2525
return nil, err
2626
}
27-
return util.SliceRemoveAllFunc(files, func(file string) bool {
27+
return slices.DeleteFunc(files, func(file string) bool {
2828
return strings.HasPrefix(file, "mail/") || !strings.HasSuffix(file, ".tmpl")
2929
}), nil
3030
}
@@ -34,7 +34,7 @@ func ListMailTemplateAssetNames(assets *assetfs.LayeredFS) ([]string, error) {
3434
if err != nil {
3535
return nil, err
3636
}
37-
return util.SliceRemoveAllFunc(files, func(file string) bool {
37+
return slices.DeleteFunc(files, func(file string) bool {
3838
return !strings.HasPrefix(file, "mail/") || !strings.HasSuffix(file, ".tmpl")
3939
}), nil
4040
}

modules/util/slice.go

Lines changed: 7 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,21 @@
11
// Copyright 2022 The Gitea Authors. All rights reserved.
22
// SPDX-License-Identifier: MIT
33

4-
// Most of the functions in this file can have better implementations with "golang.org/x/exp/slices".
5-
// However, "golang.org/x/exp" is experimental and unreliable, we shouldn't use it.
6-
// So lets waiting for the "slices" has be promoted to the main repository one day.
7-
84
package util
95

10-
import "strings"
11-
12-
// SliceContains returns true if the target exists in the slice.
13-
func SliceContains[T comparable](slice []T, target T) bool {
14-
return SliceContainsFunc(slice, func(t T) bool { return t == target })
15-
}
16-
17-
// SliceContainsFunc returns true if any element in the slice satisfies the targetFunc.
18-
func SliceContainsFunc[T any](slice []T, targetFunc func(T) bool) bool {
19-
for _, v := range slice {
20-
if targetFunc(v) {
21-
return true
22-
}
23-
}
24-
return false
25-
}
6+
import (
7+
"slices"
8+
"strings"
9+
)
2610

2711
// SliceContainsString sequential searches if string exists in slice.
2812
func SliceContainsString(slice []string, target string, insensitive ...bool) bool {
2913
if len(insensitive) != 0 && insensitive[0] {
3014
target = strings.ToLower(target)
31-
return SliceContainsFunc(slice, func(t string) bool { return strings.ToLower(t) == target })
15+
return slices.ContainsFunc(slice, func(t string) bool { return strings.ToLower(t) == target })
3216
}
3317

34-
return SliceContains(slice, target)
18+
return slices.Contains(slice, target)
3519
}
3620

3721
// SliceSortedEqual returns true if the two slices will be equal when they get sorted.
@@ -57,34 +41,7 @@ func SliceSortedEqual[T comparable](s1, s2 []T) bool {
5741
return true
5842
}
5943

60-
// SliceEqual returns true if the two slices are equal.
61-
func SliceEqual[T comparable](s1, s2 []T) bool {
62-
if len(s1) != len(s2) {
63-
return false
64-
}
65-
66-
for i, v := range s1 {
67-
if s2[i] != v {
68-
return false
69-
}
70-
}
71-
return true
72-
}
73-
7444
// SliceRemoveAll removes all the target elements from the slice.
7545
func SliceRemoveAll[T comparable](slice []T, target T) []T {
76-
return SliceRemoveAllFunc(slice, func(t T) bool { return t == target })
77-
}
78-
79-
// SliceRemoveAllFunc removes all elements which satisfy the targetFunc from the slice.
80-
func SliceRemoveAllFunc[T comparable](slice []T, targetFunc func(T) bool) []T {
81-
idx := 0
82-
for _, v := range slice {
83-
if targetFunc(v) {
84-
continue
85-
}
86-
slice[idx] = v
87-
idx++
88-
}
89-
return slice[:idx]
46+
return slices.DeleteFunc(slice, func(t T) bool { return t == target })
9047
}

modules/util/slice_test.go

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,6 @@ import (
99
"github.com/stretchr/testify/assert"
1010
)
1111

12-
func TestSliceContains(t *testing.T) {
13-
assert.True(t, SliceContains([]int{2, 0, 2, 3}, 2))
14-
assert.True(t, SliceContains([]int{2, 0, 2, 3}, 0))
15-
assert.True(t, SliceContains([]int{2, 0, 2, 3}, 3))
16-
17-
assert.True(t, SliceContains([]string{"2", "0", "2", "3"}, "0"))
18-
assert.True(t, SliceContains([]float64{2, 0, 2, 3}, 0))
19-
assert.True(t, SliceContains([]bool{false, true, false}, true))
20-
21-
assert.False(t, SliceContains([]int{2, 0, 2, 3}, 4))
22-
assert.False(t, SliceContains([]int{}, 4))
23-
assert.False(t, SliceContains(nil, 4))
24-
}
25-
2612
func TestSliceContainsString(t *testing.T) {
2713
assert.True(t, SliceContainsString([]string{"c", "b", "a", "b"}, "a"))
2814
assert.True(t, SliceContainsString([]string{"c", "b", "a", "b"}, "b"))
@@ -54,25 +40,6 @@ func TestSliceSortedEqual(t *testing.T) {
5440
assert.False(t, SliceSortedEqual([]int{2, 0, 0, 3}, []int{2, 0, 2, 3}))
5541
}
5642

57-
func TestSliceEqual(t *testing.T) {
58-
assert.True(t, SliceEqual([]int{2, 0, 2, 3}, []int{2, 0, 2, 3}))
59-
assert.True(t, SliceEqual([]int{}, []int{}))
60-
assert.True(t, SliceEqual([]int(nil), nil))
61-
assert.True(t, SliceEqual([]int(nil), []int{}))
62-
assert.True(t, SliceEqual([]int{}, []int{}))
63-
64-
assert.True(t, SliceEqual([]string{"2", "0", "2", "3"}, []string{"2", "0", "2", "3"}))
65-
assert.True(t, SliceEqual([]float64{2, 0, 2, 3}, []float64{2, 0, 2, 3}))
66-
assert.True(t, SliceEqual([]bool{false, true, false}, []bool{false, true, false}))
67-
68-
assert.False(t, SliceEqual([]int{3, 0, 2, 2}, []int{2, 0, 2, 3}))
69-
assert.False(t, SliceEqual([]int{2, 0, 2}, []int{2, 0, 2, 3}))
70-
assert.False(t, SliceEqual([]int{}, []int{2, 0, 2, 3}))
71-
assert.False(t, SliceEqual(nil, []int{2, 0, 2, 3}))
72-
assert.False(t, SliceEqual([]int{2, 0, 2, 4}, []int{2, 0, 2, 3}))
73-
assert.False(t, SliceEqual([]int{2, 0, 0, 3}, []int{2, 0, 2, 3}))
74-
}
75-
7643
func TestSliceRemoveAll(t *testing.T) {
7744
assert.ElementsMatch(t, []int{2, 2, 3}, SliceRemoveAll([]int{2, 0, 2, 3}, 0))
7845
assert.ElementsMatch(t, []int{0, 3}, SliceRemoveAll([]int{2, 0, 2, 3}, 2))

routers/api/packages/chef/auth.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"net/http"
1717
"path"
1818
"regexp"
19+
"slices"
1920
"strconv"
2021
"strings"
2122
"time"
@@ -265,7 +266,7 @@ func verifyDataOld(signature, data []byte, pub *rsa.PublicKey) error {
265266
}
266267
}
267268

268-
if !util.SliceEqual(out[skip:], data) {
269+
if !slices.Equal(out[skip:], data) {
269270
return fmt.Errorf("could not verify signature")
270271
}
271272

routers/api/v1/repo/repo.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package repo
77
import (
88
"fmt"
99
"net/http"
10+
"slices"
1011
"strings"
1112
"time"
1213

@@ -235,7 +236,7 @@ func CreateUserRepo(ctx *context.APIContext, owner *user_model.User, opt api.Cre
235236
}
236237

237238
// If the readme template does not exist, a 400 will be returned.
238-
if opt.AutoInit && len(opt.Readme) > 0 && !util.SliceContains(repo_module.Readmes, opt.Readme) {
239+
if opt.AutoInit && len(opt.Readme) > 0 && !slices.Contains(repo_module.Readmes, opt.Readme) {
239240
ctx.Error(http.StatusBadRequest, "", fmt.Errorf("readme template does not exist, available templates: %v", repo_module.Readmes))
240241
return
241242
}

routers/web/repo/issue.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"math/big"
1313
"net/http"
1414
"net/url"
15+
"slices"
1516
"sort"
1617
"strconv"
1718
"strings"
@@ -3628,7 +3629,7 @@ func issuePosters(ctx *context.Context, isPullList bool) {
36283629
if search == "" && ctx.Doer != nil {
36293630
// the returned posters slice only contains limited number of users,
36303631
// to make the current user (doer) can quickly filter their own issues, always add doer to the posters slice
3631-
if !util.SliceContainsFunc(posters, func(user *user_model.User) bool { return user.ID == ctx.Doer.ID }) {
3632+
if !slices.ContainsFunc(posters, func(user *user_model.User) bool { return user.ID == ctx.Doer.ID }) {
36323633
posters = append(posters, ctx.Doer)
36333634
}
36343635
}

0 commit comments

Comments
 (0)