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:
@@ -6,7 +6,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -97,7 +97,7 @@ func (s *MethodTestSuite) TearDownSuite() {
|
||||
notCalled = append(notCalled, m)
|
||||
}
|
||||
}
|
||||
sort.Strings(notCalled)
|
||||
slices.Sort(notCalled)
|
||||
for _, m := range notCalled {
|
||||
t.Errorf("Method never called: %q", m)
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package dynamicparameters
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sort"
|
||||
"slices"
|
||||
|
||||
"github.com/hashicorp/hcl/v2"
|
||||
|
||||
@@ -94,7 +94,7 @@ func (e *DiagnosticError) Response() (int, codersdk.Response) {
|
||||
for name := range e.KeyedDiagnostics {
|
||||
sortedNames = append(sortedNames, name)
|
||||
}
|
||||
sort.Strings(sortedNames)
|
||||
slices.Sort(sortedNames)
|
||||
|
||||
for _, name := range sortedNames {
|
||||
diag := e.KeyedDiagnostics[name]
|
||||
|
||||
@@ -18,7 +18,6 @@ import (
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -549,8 +548,8 @@ func TestExpiredLeaseIsRequeued(t *testing.T) {
|
||||
leasedIDs = append(leasedIDs, msg.ID.String())
|
||||
}
|
||||
|
||||
sort.Strings(msgs)
|
||||
sort.Strings(leasedIDs)
|
||||
slices.Sort(msgs)
|
||||
slices.Sort(leasedIDs)
|
||||
require.EqualValues(t, msgs, leasedIDs)
|
||||
|
||||
// Wait out the lease period; all messages should be eligible to be re-acquired.
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package prometheusmetrics_test
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
@@ -134,7 +135,7 @@ func collectAndSortMetrics(t *testing.T, collector prometheus.Collector, count i
|
||||
|
||||
// Ensure always the same order of metrics
|
||||
sort.Slice(metrics, func(i, j int) bool {
|
||||
return sort.StringsAreSorted([]string{metrics[i].Label[0].GetValue(), metrics[j].Label[1].GetValue()})
|
||||
return slices.IsSorted([]string{metrics[i].Label[0].GetValue(), metrics[j].Label[1].GetValue()})
|
||||
})
|
||||
return metrics
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package rbac
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -176,7 +175,7 @@ func CompositeScopeNames() []string {
|
||||
for k := range compositePerms {
|
||||
out = append(out, string(k))
|
||||
}
|
||||
sort.Strings(out)
|
||||
slices.Sort(out)
|
||||
return out
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package rbac
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -16,7 +16,7 @@ func TestExternalScopeNames(t *testing.T) {
|
||||
|
||||
// Ensure sorted ascending
|
||||
sorted := append([]string(nil), names...)
|
||||
sort.Strings(sorted)
|
||||
slices.Sort(sorted)
|
||||
require.Equal(t, sorted, names)
|
||||
|
||||
// Ensure each entry expands to site-only
|
||||
|
||||
+3
-3
@@ -7,7 +7,7 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/mail"
|
||||
"sort"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -1589,7 +1589,7 @@ func claimFields(claims map[string]interface{}) []string {
|
||||
for field := range claims {
|
||||
fields = append(fields, field)
|
||||
}
|
||||
sort.Strings(fields)
|
||||
slices.Sort(fields)
|
||||
return fields
|
||||
}
|
||||
|
||||
@@ -1602,7 +1602,7 @@ func blankFields(claims map[string]interface{}) []string {
|
||||
fields = append(fields, field)
|
||||
}
|
||||
}
|
||||
sort.Strings(fields)
|
||||
slices.Sort(fields)
|
||||
return fields
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user