Skip to content

Commit d49da30

Browse files
committed
Change sort to slices package
Signed-off-by: dongjiang <[email protected]>
1 parent bf7d6b7 commit d49da30

File tree

11 files changed

+45
-50
lines changed

11 files changed

+45
-50
lines changed

cmd/helpgen/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"go/format"
77
"io"
88
"os"
9-
"sort"
9+
"slices"
1010
"strings"
1111

1212
flag "github.com/spf13/pflag"
@@ -152,7 +152,7 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
152152
for typ := range byType {
153153
typeNames = append(typeNames, typ)
154154
}
155-
sort.Strings(typeNames)
155+
slices.Sort(typeNames)
156156

157157
outContent := new(bytes.Buffer)
158158
if headerText == "" {

pkg/crd/flatten.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package crd
1919
import (
2020
"fmt"
2121
"reflect"
22-
"sort"
22+
"slices"
2323
"strings"
2424
"sync"
2525

@@ -176,7 +176,7 @@ func flattenAllOfInto(dst *apiext.JSONSchemaProps, src apiext.JSONSchemaProps, e
176176
dst.Required = append(dst.Required, req)
177177
}
178178
// be deterministic
179-
sort.Strings(dst.Required)
179+
slices.Sort(dst.Required)
180180
}
181181
}
182182

pkg/crd/gen.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"fmt"
2121
"go/ast"
2222
"go/types"
23-
"sort"
23+
"slices"
2424
"strings"
2525

2626
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
@@ -328,8 +328,8 @@ func FindKubeKinds(parser *Parser, metav1Pkg *loader.Package) []schema.GroupKind
328328
for groupKind := range kubeKinds {
329329
groupKindList = append(groupKindList, groupKind)
330330
}
331-
sort.Slice(groupKindList, func(i, j int) bool {
332-
return groupKindList[i].String() < groupKindList[j].String()
331+
slices.SortStableFunc(groupKindList, func(a, b schema.GroupKind) int {
332+
return strings.Compare(a.String(), b.String())
333333
})
334334

335335
return groupKindList

pkg/crd/schema.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"go/ast"
2323
"go/token"
2424
"go/types"
25-
"sort"
25+
"slices"
2626
"strings"
2727

2828
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
@@ -173,10 +173,10 @@ func applyMarkers(ctx *schemaContext, markerSet markers.MarkerValues, props *api
173173
}
174174
}
175175

176-
cmpPriority := func(markers []schemaMarkerWithName, i, j int) bool {
176+
cmpPriority := func(i, j schemaMarkerWithName) int {
177177
var iPriority, jPriority crdmarkers.ApplyPriority
178178

179-
switch m := markers[i].SchemaMarker.(type) {
179+
switch m := i.SchemaMarker.(type) {
180180
case crdmarkers.ApplyPriorityMarker:
181181
iPriority = m.ApplyPriority()
182182
case applyFirstMarker:
@@ -185,7 +185,7 @@ func applyMarkers(ctx *schemaContext, markerSet markers.MarkerValues, props *api
185185
iPriority = crdmarkers.ApplyPriorityDefault
186186
}
187187

188-
switch m := markers[j].SchemaMarker.(type) {
188+
switch m := j.SchemaMarker.(type) {
189189
case crdmarkers.ApplyPriorityMarker:
190190
jPriority = m.ApplyPriority()
191191
case applyFirstMarker:
@@ -194,10 +194,10 @@ func applyMarkers(ctx *schemaContext, markerSet markers.MarkerValues, props *api
194194
jPriority = crdmarkers.ApplyPriorityDefault
195195
}
196196

197-
return iPriority < jPriority
197+
return int(iPriority - jPriority)
198198
}
199-
sort.Slice(markers, func(i, j int) bool { return cmpPriority(markers, i, j) })
200-
sort.Slice(itemsMarkers, func(i, j int) bool { return cmpPriority(itemsMarkers, i, j) })
199+
slices.SortStableFunc(markers, func(i, j schemaMarkerWithName) int { return cmpPriority(i, j) })
200+
slices.SortStableFunc(itemsMarkers, func(i, j schemaMarkerWithName) int { return cmpPriority(i, j) })
201201

202202
for _, schemaMarker := range markers {
203203
if err := schemaMarker.SchemaMarker.ApplyToSchema(props); err != nil {
@@ -519,7 +519,7 @@ func structToSchema(ctx *schemaContext, structType *ast.StructType) *apiext.JSON
519519
}
520520

521521
// Ensure the required fields are always listed alphabetically.
522-
sort.Strings(props.Required)
522+
slices.Sort(props.Required)
523523

524524
return props
525525
}

pkg/crd/spec.go

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

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

2424
"github.com/gobuffalo/flect"
@@ -139,7 +139,7 @@ func (p *Parser) NeedCRDFor(groupKind schema.GroupKind, maxDescLen *int) {
139139

140140
// it is necessary to make sure the order of CRD versions in crd.Spec.Versions is stable and explicitly set crd.Spec.Version.
141141
// Otherwise, crd.Spec.Version may point to different CRD versions across different runs.
142-
sort.Slice(crd.Spec.Versions, func(i, j int) bool { return crd.Spec.Versions[i].Name < crd.Spec.Versions[j].Name })
142+
slices.SortStableFunc(crd.Spec.Versions, func(a, b apiext.CustomResourceDefinitionVersion) int { return strings.Compare(a.Name, b.Name) })
143143

144144
// make sure we have *a* storage version
145145
// (default it if we only have one, otherwise, bail)

pkg/deepcopy/deepcopy_integration_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package deepcopy_test
1919
import (
2020
"io"
2121
"os"
22-
"sort"
22+
"slices"
2323

2424
"github.com/google/go-cmp/cmp"
2525
. "github.com/onsi/ginkgo"
@@ -46,7 +46,7 @@ func (m outputToMap) fileList() []string {
4646
for path := range m {
4747
ret = append(ret, path)
4848
}
49-
sort.Strings(ret)
49+
slices.Sort(ret)
5050
return ret
5151
}
5252

pkg/deepcopy/gen.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"go/ast"
2323
"go/format"
2424
"io"
25-
"sort"
25+
"slices"
2626
"strings"
2727

2828
"sigs.k8s.io/controller-tools/pkg/genall"
@@ -273,7 +273,7 @@ func writeMethods(pkg *loader.Package, out io.Writer, byType map[string][]byte)
273273
for name := range byType {
274274
sortedNames = append(sortedNames, name)
275275
}
276-
sort.Strings(sortedNames)
276+
slices.Sort(sortedNames)
277277

278278
for _, name := range sortedNames {
279279
_, err := out.Write(byType[name])

pkg/genall/help/sort.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import (
2424

2525
// SortGroup knows how to sort and group marker definitions.
2626
type SortGroup interface {
27-
// Less is equivalent to the Less function from sort, and is used to sort the markers.
28-
Less(*markers.Definition, *markers.Definition) bool
27+
// Compare is equivalent to the compare function from slices, and is used to sort the markers.
28+
Compare(*markers.Definition, *markers.Definition) int
2929
// Group returns the "group" that a given marker belongs to.
3030
Group(*markers.Definition, *markers.DefinitionHelp) string
3131
}
@@ -46,13 +46,13 @@ func (sortByCategory) Group(_ *markers.Definition, help *markers.DefinitionHelp)
4646
}
4747
return help.Category
4848
}
49-
func (sortByCategory) Less(i, j *markers.Definition) bool {
50-
return i.Name < j.Name
49+
func (sortByCategory) Compare(i, j *markers.Definition) int {
50+
return strings.Compare(j.Name, i.Name)
5151
}
5252

5353
type optionsSort struct{}
5454

55-
func (optionsSort) Less(i, j *markers.Definition) bool {
55+
func (optionsSort) Compare(i, j *markers.Definition) int {
5656
iParts := strings.Split(i.Name, ":")
5757
jParts := strings.Split(j.Name, ":")
5858

@@ -83,10 +83,10 @@ func (optionsSort) Less(i, j *markers.Definition) bool {
8383
}
8484

8585
if iGen != jGen {
86-
return iGen > jGen
86+
return strings.Compare(iGen, jGen)
8787
}
8888

89-
return iRule < jRule
89+
return strings.Compare(jRule, iRule)
9090
}
9191
func (optionsSort) Group(def *markers.Definition, _ *markers.DefinitionHelp) string {
9292
parts := strings.Split(def.Name, ":")

pkg/genall/help/types.go

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

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

2323
"sigs.k8s.io/controller-tools/pkg/markers"
@@ -166,7 +166,7 @@ func ForDefinition(defn *markers.Definition, maybeHelp *markers.DefinitionHelp)
166166
res.Fields = append(res.Fields, fieldHelp)
167167
}
168168

169-
sort.Slice(res.Fields, func(i, j int) bool { return res.Fields[i].Name < res.Fields[j].Name })
169+
slices.SortStableFunc(res.Fields, func(a, b FieldHelp) int { return strings.Compare(a.Name, b.Name) })
170170

171171
return res
172172
}
@@ -191,17 +191,17 @@ func ByCategory(reg *markers.Registry, sorter SortGroup) []CategoryDoc {
191191
allGroups = append(allGroups, groupName)
192192
}
193193

194-
sort.Strings(allGroups)
194+
slices.Sort(allGroups)
195195

196196
res := make([]CategoryDoc, len(allGroups))
197197
for i, groupName := range allGroups {
198-
markers := groupedMarkers[groupName]
199-
sort.Slice(markers, func(i, j int) bool {
200-
return sorter.Less(markers[i], markers[j])
198+
mks := groupedMarkers[groupName]
199+
slices.SortStableFunc(mks, func(a, b *markers.Definition) int {
200+
return sorter.Compare(a, b)
201201
})
202202

203-
markerDocs := make([]MarkerDoc, len(markers))
204-
for i, marker := range markers {
203+
markerDocs := make([]MarkerDoc, len(mks))
204+
for i, marker := range mks {
205205
markerDocs[i] = ForDefinition(marker, reg.HelpFor(marker))
206206
}
207207

pkg/rbac/parser.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ package rbac
2424

2525
import (
2626
"fmt"
27-
"sort"
27+
"slices"
2828
"strings"
2929

3030
rbacv1 "k8s.io/api/rbac/v1"
@@ -74,13 +74,6 @@ func (key ruleKey) String() string {
7474
return fmt.Sprintf("%s + %s + %s + %s", key.Groups, key.Resources, key.ResourceNames, key.URLs)
7575
}
7676

77-
// ruleKeys implements sort.Interface
78-
type ruleKeys []ruleKey
79-
80-
func (keys ruleKeys) Len() int { return len(keys) }
81-
func (keys ruleKeys) Swap(i, j int) { keys[i], keys[j] = keys[j], keys[i] }
82-
func (keys ruleKeys) Less(i, j int) bool { return keys[i].String() < keys[j].String() }
83-
8477
// key normalizes the Rule and returns a ruleKey object.
8578
func (r *Rule) key() ruleKey {
8679
r.normalize()
@@ -139,7 +132,7 @@ func removeDupAndSort(strs []string) []string {
139132
for str := range set {
140133
result = append(result, str)
141134
}
142-
sort.Strings(result)
135+
slices.Sort(result)
143136
return result
144137
}
145138

@@ -304,7 +297,9 @@ func GenerateRoles(ctx *genall.GenerationContext, roleName string) ([]interface{
304297
for key := range ruleMap {
305298
keys = append(keys, key)
306299
}
307-
sort.Sort(ruleKeys(keys))
300+
slices.SortStableFunc(keys, func(a, b ruleKey) int {
301+
return strings.Compare(a.String(), b.String())
302+
})
308303

309304
// Normalize rule verbs to "*" if any verb in the rule is an asterisk
310305
for _, rule := range ruleMap {
@@ -327,7 +322,7 @@ func GenerateRoles(ctx *genall.GenerationContext, roleName string) ([]interface{
327322
for ns := range rulesByNSResource {
328323
namespaces = append(namespaces, ns)
329324
}
330-
sort.Strings(namespaces)
325+
slices.Sort(namespaces)
331326

332327
// process the items in rulesByNS by the order specified in `namespaces` to make sure that the Role order is stable
333328
var objs []interface{}

0 commit comments

Comments
 (0)