mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
refactor: replace sort.Strings with slices.Sort (#23457)
The slices package provides type-safe generic replacements for the old typed sort convenience functions. The codebase already uses slices.Sort in 43 call sites; this finishes the migration for the remaining 29. - sort.Strings(x) -> slices.Sort(x) - sort.Float64s(x) -> slices.Sort(x) - sort.StringsAreSorted(x) -> slices.IsSorted(x)
This commit is contained in:
@@ -5,7 +5,7 @@ import (
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sort"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -376,8 +376,8 @@ func Test_sshConfigOptions_addOption(t *testing.T) {
|
||||
return
|
||||
}
|
||||
require.NoError(t, err)
|
||||
sort.Strings(tt.Expect)
|
||||
sort.Strings(o.sshOptions)
|
||||
slices.Sort(tt.Expect)
|
||||
slices.Sort(o.sshOptions)
|
||||
require.Equal(t, tt.Expect, o.sshOptions)
|
||||
})
|
||||
}
|
||||
|
||||
+2
-2
@@ -24,7 +24,7 @@ import (
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -2825,7 +2825,7 @@ func ReadExternalAuthProvidersFromEnv(environ []string) ([]codersdk.ExternalAuth
|
||||
// parsing of `GITAUTH` environment variables.
|
||||
func parseExternalAuthProvidersFromEnv(prefix string, environ []string) ([]codersdk.ExternalAuthConfig, error) {
|
||||
// The index numbers must be in-order.
|
||||
sort.Strings(environ)
|
||||
slices.Sort(environ)
|
||||
|
||||
var providers []codersdk.ExternalAuthConfig
|
||||
for _, v := range serpent.ParseEnviron(environ, prefix) {
|
||||
|
||||
+3
-3
@@ -7,7 +7,7 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"slices"
|
||||
|
||||
"golang.org/x/exp/maps"
|
||||
"golang.org/x/xerrors"
|
||||
@@ -31,7 +31,7 @@ func (*RootCmd) templateInit() *serpent.Command {
|
||||
for _, ex := range exampleList {
|
||||
templateIDs = append(templateIDs, ex.ID)
|
||||
}
|
||||
sort.Strings(templateIDs)
|
||||
slices.Sort(templateIDs)
|
||||
cmd := &serpent.Command{
|
||||
Use: "init [directory]",
|
||||
Short: "Get started with a templated template.",
|
||||
@@ -50,7 +50,7 @@ func (*RootCmd) templateInit() *serpent.Command {
|
||||
optsToID[name] = example.ID
|
||||
}
|
||||
opts := maps.Keys(optsToID)
|
||||
sort.Strings(opts)
|
||||
slices.Sort(opts)
|
||||
_, _ = fmt.Fprintln(
|
||||
inv.Stdout,
|
||||
pretty.Sprint(
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"sort"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -47,7 +47,7 @@ func TestTemplateList(t *testing.T) {
|
||||
|
||||
// expect that templates are listed alphabetically
|
||||
templatesList := []string{firstTemplate.Name, secondTemplate.Name}
|
||||
sort.Strings(templatesList)
|
||||
slices.Sort(templatesList)
|
||||
|
||||
require.NoError(t, <-errC)
|
||||
|
||||
|
||||
+2
-3
@@ -4,7 +4,6 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -194,7 +193,7 @@ func joinScopes(scopes []codersdk.APIKeyScope) string {
|
||||
return ""
|
||||
}
|
||||
vals := slice.ToStrings(scopes)
|
||||
sort.Strings(vals)
|
||||
slices.Sort(vals)
|
||||
return strings.Join(vals, ", ")
|
||||
}
|
||||
|
||||
@@ -206,7 +205,7 @@ func joinAllowList(entries []codersdk.APIAllowListTarget) string {
|
||||
for i, entry := range entries {
|
||||
vals[i] = entry.String()
|
||||
}
|
||||
sort.Strings(vals)
|
||||
slices.Sort(vals)
|
||||
return strings.Join(vals, ", ")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user