Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 3 additions & 7 deletions github/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ import (
"fmt"
"hash"
"io"
"maps"
"mime"
"net/http"
"net/url"
"reflect"
"sort"
"slices"
"strings"
)

Expand Down Expand Up @@ -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.
Expand Down
9 changes: 4 additions & 5 deletions tools/check-structfield-settings/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package main

import (
"cmp"
"errors"
"flag"
"fmt"
Expand All @@ -21,7 +22,6 @@ import (
"path/filepath"
"regexp"
"slices"
"sort"
"strings"

"github.com/golangci/plugin-module-register/register"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
30 changes: 10 additions & 20 deletions tools/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package main

import (
"bytes"
"cmp"
"context"
"errors"
"fmt"
Expand All @@ -15,12 +16,13 @@ import (
"go/parser"
"go/printer"
"go/token"
"maps"
"net/url"
"os"
"path"
"path/filepath"
"regexp"
"sort"
"slices"
"strings"
"sync"

Expand Down Expand Up @@ -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))
})
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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))
}
}
Expand Down
29 changes: 12 additions & 17 deletions tools/metadata/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
package main

import (
"cmp"
"context"
"fmt"
"io"
"regexp"
"sort"
"slices"
"strconv"

"github.com/getkin/kin-openapi/openapi3"
Expand Down Expand Up @@ -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(`^(?P<plan>api\.github\.com)(-(?P<major>\d+)\.(?P<minor>\d+))?$`),
regexp.MustCompile(`^(?P<plan>ghec)(-(?P<major>\d+)\.(?P<minor>\d+))?$`),
Expand Down Expand Up @@ -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 {
Expand Down
Loading