Skip to content

Commit a0f3e72

Browse files
committed
change sort to slices
Signed-off-by: dongjiang1989 <dongjiang1989@126.com>
1 parent 82ab3ee commit a0f3e72

File tree

16 files changed

+133
-79
lines changed

16 files changed

+133
-79
lines changed

.golangci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ linters:
77
- asciicheck
88
- bidichk
99
- copyloopvar
10+
- depguard
1011
- dupl
1112
- errcheck
1213
- ginkgolinter
@@ -29,6 +30,12 @@ linters:
2930
- wrapcheck
3031
- whitespace
3132
settings:
33+
depguard:
34+
rules:
35+
forbid-pkg-errors:
36+
deny:
37+
- pkg: sort
38+
desc: Should be replaced with slices package
3239
ginkgolinter:
3340
forbid-focus-container: true
3441
forbid-spec-pollution: true

docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"context"
2727
"fmt"
2828
"maps"
29-
"sort"
29+
"slices"
3030
"time"
3131

3232
"github.com/robfig/cron"
@@ -373,11 +373,19 @@ func (r *CronJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
373373
// NB: deleting these are "best effort" -- if we fail on a particular one,
374374
// we won't requeue just to finish the deleting.
375375
if cronJob.Spec.FailedJobsHistoryLimit != nil {
376-
sort.Slice(failedJobs, func(i, j int) bool {
377-
if failedJobs[i].Status.StartTime == nil {
378-
return failedJobs[j].Status.StartTime != nil
376+
slices.SortStableFunc(failedJobs, func(a, b *kbatch.Job) int {
377+
aStartTime := a.Status.StartTime
378+
bStartTime := b.Status.StartTime
379+
if aStartTime == nil && bStartTime != nil {
380+
return 1
379381
}
380-
return failedJobs[i].Status.StartTime.Before(failedJobs[j].Status.StartTime)
382+
383+
if aStartTime.Before(bStartTime) {
384+
return -1
385+
} else if bStartTime.Before(aStartTime) {
386+
return 1
387+
}
388+
return 0
381389
})
382390
for i, job := range failedJobs {
383391
if int32(i) >= int32(len(failedJobs))-*cronJob.Spec.FailedJobsHistoryLimit {
@@ -392,11 +400,19 @@ func (r *CronJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
392400
}
393401

394402
if cronJob.Spec.SuccessfulJobsHistoryLimit != nil {
395-
sort.Slice(successfulJobs, func(i, j int) bool {
396-
if successfulJobs[i].Status.StartTime == nil {
397-
return successfulJobs[j].Status.StartTime != nil
403+
slices.SortStableFunc(successfulJobs, func(a, b *kbatch.Job) int {
404+
aStartTime := a.Status.StartTime
405+
bStartTime := b.Status.StartTime
406+
if aStartTime == nil && bStartTime != nil {
407+
return 1
408+
}
409+
410+
if aStartTime.Before(bStartTime) {
411+
return -1
412+
} else if bStartTime.Before(aStartTime) {
413+
return 1
398414
}
399-
return successfulJobs[i].Status.StartTime.Before(successfulJobs[j].Status.StartTime)
415+
return 0
400416
})
401417
for i, job := range successfulJobs {
402418
if int32(i) >= int32(len(successfulJobs))-*cronJob.Spec.SuccessfulJobsHistoryLimit {

docs/book/src/multiversion-tutorial/testdata/project/internal/controller/cronjob_controller.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"context"
2727
"fmt"
2828
"maps"
29-
"sort"
29+
"slices"
3030
"time"
3131

3232
"github.com/robfig/cron"
@@ -373,11 +373,19 @@ func (r *CronJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
373373
// NB: deleting these are "best effort" -- if we fail on a particular one,
374374
// we won't requeue just to finish the deleting.
375375
if cronJob.Spec.FailedJobsHistoryLimit != nil {
376-
sort.Slice(failedJobs, func(i, j int) bool {
377-
if failedJobs[i].Status.StartTime == nil {
378-
return failedJobs[j].Status.StartTime != nil
376+
slices.SortStableFunc(failedJobs, func(a, b *kbatch.Job) int {
377+
aStartTime := a.Status.StartTime
378+
bStartTime := b.Status.StartTime
379+
if aStartTime == nil && bStartTime != nil {
380+
return 1
379381
}
380-
return failedJobs[i].Status.StartTime.Before(failedJobs[j].Status.StartTime)
382+
383+
if aStartTime.Before(bStartTime) {
384+
return -1
385+
} else if bStartTime.Before(aStartTime) {
386+
return 1
387+
}
388+
return 0
381389
})
382390
for i, job := range failedJobs {
383391
if int32(i) >= int32(len(failedJobs))-*cronJob.Spec.FailedJobsHistoryLimit {
@@ -392,11 +400,19 @@ func (r *CronJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
392400
}
393401

394402
if cronJob.Spec.SuccessfulJobsHistoryLimit != nil {
395-
sort.Slice(successfulJobs, func(i, j int) bool {
396-
if successfulJobs[i].Status.StartTime == nil {
397-
return successfulJobs[j].Status.StartTime != nil
403+
slices.SortStableFunc(successfulJobs, func(a, b *kbatch.Job) int {
404+
aStartTime := a.Status.StartTime
405+
bStartTime := b.Status.StartTime
406+
if aStartTime == nil && bStartTime != nil {
407+
return 1
408+
}
409+
410+
if aStartTime.Before(bStartTime) {
411+
return -1
412+
} else if bStartTime.Before(aStartTime) {
413+
return 1
398414
}
399-
return successfulJobs[i].Status.StartTime.Before(successfulJobs[j].Status.StartTime)
415+
return 0
400416
})
401417
for i, job := range successfulJobs {
402418
if int32(i) >= int32(len(successfulJobs))-*cronJob.Spec.SuccessfulJobsHistoryLimit {

hack/docs/internal/cronjob-tutorial/controller_implementation.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const controllerImport = `import (
2828
"context"
2929
"fmt"
3030
"maps"
31-
"sort"
31+
"slices"
3232
"time"
3333
3434
"github.com/robfig/cron"
@@ -362,11 +362,19 @@ const controllerReconcileLogic = `log := logf.FromContext(ctx)
362362
// NB: deleting these are "best effort" -- if we fail on a particular one,
363363
// we won't requeue just to finish the deleting.
364364
if cronJob.Spec.FailedJobsHistoryLimit != nil {
365-
sort.Slice(failedJobs, func(i, j int) bool {
366-
if failedJobs[i].Status.StartTime == nil {
367-
return failedJobs[j].Status.StartTime != nil
365+
slices.SortStableFunc(failedJobs, func(a, b *kbatch.Job) int {
366+
aStartTime := a.Status.StartTime
367+
bStartTime := b.Status.StartTime
368+
if aStartTime == nil && bStartTime != nil {
369+
return 1
368370
}
369-
return failedJobs[i].Status.StartTime.Before(failedJobs[j].Status.StartTime)
371+
372+
if aStartTime.Before(bStartTime) {
373+
return -1
374+
} else if bStartTime.Before(aStartTime) {
375+
return 1
376+
}
377+
return 0
370378
})
371379
for i, job := range failedJobs {
372380
if int32(i) >= int32(len(failedJobs))-*cronJob.Spec.FailedJobsHistoryLimit {
@@ -381,11 +389,19 @@ const controllerReconcileLogic = `log := logf.FromContext(ctx)
381389
}
382390
383391
if cronJob.Spec.SuccessfulJobsHistoryLimit != nil {
384-
sort.Slice(successfulJobs, func(i, j int) bool {
385-
if successfulJobs[i].Status.StartTime == nil {
386-
return successfulJobs[j].Status.StartTime != nil
392+
slices.SortStableFunc(successfulJobs, func(a, b *kbatch.Job) int {
393+
aStartTime := a.Status.StartTime
394+
bStartTime := b.Status.StartTime
395+
if aStartTime == nil && bStartTime != nil {
396+
return 1
397+
}
398+
399+
if aStartTime.Before(bStartTime) {
400+
return -1
401+
} else if bStartTime.Before(aStartTime) {
402+
return 1
387403
}
388-
return successfulJobs[i].Status.StartTime.Before(successfulJobs[j].Status.StartTime)
404+
return 0
389405
})
390406
for i, job := range successfulJobs {
391407
if int32(i) >= int32(len(successfulJobs))-*cronJob.Spec.SuccessfulJobsHistoryLimit {

pkg/cli/alpha/internal/update/helpers/conflict.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"os"
2525
"os/exec"
2626
"path/filepath"
27-
"sort"
27+
"slices"
2828
"strings"
2929
)
3030

@@ -83,8 +83,8 @@ func FindConflictFiles() ConflictResult {
8383
}
8484
}
8585

86-
sort.Strings(result.SourceFiles)
87-
sort.Strings(result.GeneratedFiles)
86+
slices.Sort(result.SourceFiles)
87+
slices.Sort(result.GeneratedFiles)
8888

8989
// Build summary for existing conflict.go usage
9090
result.Summary = ConflictSummary{

pkg/cli/alpha/internal/update/helpers/open_gh_issue.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"fmt"
2323
"os/exec"
2424
"regexp"
25-
"sort"
25+
"slices"
2626
"strings"
2727
)
2828

@@ -225,8 +225,8 @@ func listChangedFiles(base, head string) (src []string, gen []string) {
225225
src = append(src, p)
226226
}
227227
}
228-
sort.Strings(src)
229-
sort.Strings(gen)
228+
slices.Sort(src)
229+
slices.Sort(gen)
230230
return src, gen
231231
}
232232

@@ -500,12 +500,12 @@ func concatSelectedDiffs(base, head string, files []string, perFileLineCap, tota
500500
candidates = append(candidates, p)
501501
}
502502
}
503-
sort.Slice(candidates, func(i, j int) bool {
504-
pi, pj := filePriority(candidates[i]), filePriority(candidates[j])
503+
slices.SortStableFunc(candidates, func(a, b string) int {
504+
pi, pj := filePriority(a), filePriority(b)
505505
if pi != pj {
506-
return pi < pj
506+
return pi - pj
507507
}
508-
return candidates[i] < candidates[j] // stable alphabetical within same priority
508+
return strings.Compare(a, b) // stable alphabetical within same priority
509509
})
510510

511511
// Emit diffs until the global budget is hit

pkg/cli/init.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package cli
1818

1919
import (
2020
"fmt"
21-
"sort"
21+
"slices"
2222
"strconv"
2323
"strings"
2424

@@ -101,6 +101,6 @@ func (c CLI) getAvailableProjectVersions() (projectVersions []string) {
101101
for version := range versionSet {
102102
projectVersions = append(projectVersions, strconv.Quote(version.String()))
103103
}
104-
sort.Strings(projectVersions)
104+
slices.Sort(projectVersions)
105105
return projectVersions
106106
}

pkg/cli/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package cli
1818

1919
import (
2020
"fmt"
21-
"sort"
21+
"slices"
2222
"strings"
2323

2424
"github.com/spf13/cobra"
@@ -115,7 +115,7 @@ func (c CLI) getPluginTable() string {
115115
lines = append(lines, strings.Repeat("-", maxPluginKeyLength+2)+"+"+
116116
strings.Repeat("-", maxProjectVersionLength+2))
117117

118-
sort.Strings(pluginKeys)
118+
slices.Sort(pluginKeys)
119119
for _, pluginKey := range pluginKeys {
120120
supportedProjectVersions := projectVersions[pluginKey]
121121
lines = append(lines, fmt.Sprintf(" %[1]*[2]s | %[3]*[4]s",

pkg/config/v3/config_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
package v3
1818

1919
import (
20-
"sort"
20+
"slices"
2121
"testing"
2222

2323
. "github.com/onsi/ginkgo/v2"
@@ -314,7 +314,7 @@ var _ = Describe("Cfg", func() {
314314
},
315315
)
316316
versions := c.ListCRDVersions()
317-
sort.Strings(versions) // ListCRDVersions has no order guarantee so sorting for reproducibility
317+
slices.Sort(versions) // ListCRDVersions has no order guarantee so sorting for reproducibility
318318
Expect(versions).To(Equal([]string{"v1", "v1beta1"}))
319319
})
320320

@@ -342,7 +342,7 @@ var _ = Describe("Cfg", func() {
342342
},
343343
)
344344
versions := c.ListWebhookVersions()
345-
sort.Strings(versions) // ListWebhookVersions has no order guarantee so sorting for reproducibility
345+
slices.Sort(versions) // ListWebhookVersions has no order guarantee so sorting for reproducibility
346346
Expect(versions).To(Equal([]string{"v1", "v1beta1"}))
347347
})
348348
})

pkg/config/version_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
package config
1818

1919
import (
20-
"sort"
20+
"slices"
2121

2222
. "github.com/onsi/ginkgo/v2"
2323
. "github.com/onsi/gomega"
@@ -64,8 +64,8 @@ var _ = Describe("Version", func() {
6464
})
6565

6666
It("sorts a valid list of versions correctly", func() {
67-
sort.Slice(versions, func(i int, j int) bool {
68-
return versions[i].Compare(versions[j]) == -1
67+
slices.SortStableFunc(versions, func(a, b Version) int {
68+
return a.Compare(b)
6969
})
7070
Expect(versions).To(Equal(sortedVersions))
7171
})

0 commit comments

Comments
 (0)