diff --git a/.golangci.yml b/.golangci.yml index a8422998da8..2d728266541 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -47,6 +47,8 @@ linters: msg: "Use cmp.Equal instead of reflect.DeepEqual" - pattern: "^http\\.Method[A-Z][a-z]*$" msg: "Use string literals instead of http.Method constants (https://pkg.go.dev/net/http#pkg-constants)" + - pattern: ^sort\..*$ + msg: "Use sorting functions from the slices package instead." gocritic: disable-all: true enabled-checks: diff --git a/github/messages.go b/github/messages.go index 21eb7c70c37..c3d14acdc64 100644 --- a/github/messages.go +++ b/github/messages.go @@ -19,11 +19,12 @@ import ( "fmt" "hash" "io" + "maps" "mime" "net/http" "net/url" "reflect" - "sort" + "slices" "strings" ) @@ -334,12 +335,7 @@ func ParseWebHook(messageType string, payload []byte) (any, error) { // MessageTypes returns a sorted list of all the known GitHub event type strings // supported by go-github. func MessageTypes() []string { - types := make([]string, 0, len(eventTypeMapping)) - for t := range eventTypeMapping { - types = append(types, t) - } - sort.Strings(types) - return types + return slices.Sorted(maps.Keys(eventTypeMapping)) } // EventForType returns an empty struct matching the specified GitHub event type. diff --git a/tools/check-structfield-settings/main.go b/tools/check-structfield-settings/main.go index 0454ac42245..ae6d0448218 100644 --- a/tools/check-structfield-settings/main.go +++ b/tools/check-structfield-settings/main.go @@ -11,6 +11,7 @@ package main import ( + "cmp" "errors" "flag" "fmt" @@ -21,7 +22,6 @@ import ( "path/filepath" "regexp" "slices" - "sort" "strings" "github.com/golangci/plugin-module-register/register" @@ -278,8 +278,7 @@ func diffKeys(all, used map[string]bool) []string { obsolete = append(obsolete, key) } } - slices.Sort(obsolete) - return obsolete + return slices.Sorted(slices.Values(obsolete)) } func findDuplicates(values []string) map[string]int { @@ -401,8 +400,8 @@ type listItem struct { } func appendSortedItems(lines []string, items []*listItem) []string { - sort.Slice(items, func(i, j int) bool { - return items[i].value < items[j].value + slices.SortFunc(items, func(a, b *listItem) int { + return cmp.Compare(a.value, b.value) }) for _, item := range items { lines = append(lines, item.line) diff --git a/tools/metadata/metadata.go b/tools/metadata/metadata.go index 60408280110..08da2a5150e 100644 --- a/tools/metadata/metadata.go +++ b/tools/metadata/metadata.go @@ -7,6 +7,7 @@ package main import ( "bytes" + "cmp" "context" "errors" "fmt" @@ -15,12 +16,13 @@ import ( "go/parser" "go/printer" "go/token" + "maps" "net/url" "os" "path" "path/filepath" "regexp" - "sort" + "slices" "strings" "sync" @@ -70,13 +72,10 @@ func operationsEqual(a, b []*operation) bool { } func sortOperations(ops []*operation) { - sort.Slice(ops, func(i, j int) bool { - leftVerb, leftURL := parseOpName(ops[i].Name) - rightVerb, rightURL := parseOpName(ops[j].Name) - if leftURL != rightURL { - return leftURL < rightURL - } - return leftVerb < rightVerb + slices.SortFunc(ops, func(a, b *operation) int { + leftVerb, leftURL := parseOpName(a.Name) + rightVerb, rightURL := parseOpName(b.Name) + return cmp.Or(cmp.Compare(leftURL, rightURL), cmp.Compare(leftVerb, rightVerb)) }) } @@ -291,11 +290,7 @@ func updateDocsVisitor(opsFile *operationsFile) nodeVisitor { } linksMap[op.DocumentationURL] = struct{}{} } - var undocumentedOps []string - for op := range undocMap { - undocumentedOps = append(undocumentedOps, op) - } - sort.Strings(undocumentedOps) + undocumentedOps := slices.Sorted(maps.Keys(undocMap)) // Find the group that comes before the function var group *ast.CommentGroup @@ -327,12 +322,7 @@ func updateDocsVisitor(opsFile *operationsFile) nodeVisitor { // add an empty line before adding doc links group.List = append(group.List, &ast.Comment{Text: "//"}) - var docLinks []string - for link := range linksMap { - docLinks = append(docLinks, link) - } - sort.Strings(docLinks) - + docLinks := slices.Sorted(maps.Keys(linksMap)) for i, dl := range docLinks { group.List = append( group.List, @@ -479,7 +469,7 @@ func methodOps(opsFile *operationsFile, cmap ast.CommentMap, fn *ast.FuncDecl) ( for _, op := range found { foundNames = append(foundNames, op.Name) } - sort.Strings(foundNames) + slices.Sort(foundNames) err = errors.Join(err, fmt.Errorf("ambiguous operation %q could match any of: %v", opName, foundNames)) } } diff --git a/tools/metadata/openapi.go b/tools/metadata/openapi.go index 61d92f1a4c6..a14cd045e54 100644 --- a/tools/metadata/openapi.go +++ b/tools/metadata/openapi.go @@ -6,11 +6,12 @@ package main import ( + "cmp" "context" "fmt" "io" "regexp" - "sort" + "slices" "strconv" "github.com/getkin/kin-openapi/openapi3" @@ -80,20 +81,6 @@ func (o *openapiFile) loadDescription(ctx context.Context, client *github.Client return err } -// less sorts by the following rules: -// - planIdx ascending -// - releaseMajor descending -// - releaseMinor descending -func (o *openapiFile) less(other *openapiFile) bool { - if o.planIdx != other.planIdx { - return o.planIdx < other.planIdx - } - if o.releaseMajor != other.releaseMajor { - return o.releaseMajor > other.releaseMajor - } - return o.releaseMinor > other.releaseMinor -} - var dirPatterns = []*regexp.Regexp{ regexp.MustCompile(`^(?Papi\.github\.com)(-(?P\d+)\.(?P\d+))?$`), regexp.MustCompile(`^(?Pghec)(-(?P\d+)\.(?P\d+))?$`), @@ -154,8 +141,16 @@ func getDescriptions(ctx context.Context, client *github.Client, gitRef string) break } } - sort.Slice(files, func(i, j int) bool { - return files[i].less(files[j]) + slices.SortFunc(files, func(a, b *openapiFile) int { + // sort by the following rules: + // - planIdx ascending + // - releaseMajor descending + // - releaseMinor descending + return cmp.Or( + cmp.Compare(a.planIdx, b.planIdx), + cmp.Compare(b.releaseMajor, a.releaseMajor), + cmp.Compare(b.releaseMinor, a.releaseMinor), + ) }) g, ctx := errgroup.WithContext(ctx) for _, file := range files {