Skip to content

Commit 1fb0a1b

Browse files
committed
chore: update golang.org/x/exp/slices
This is necessary to bring us up to date with tailscale.
1 parent 98b6ecb commit 1fb0a1b

File tree

14 files changed

+92
-77
lines changed

14 files changed

+92
-77
lines changed

cli/clibase/cmd.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"golang.org/x/exp/slices"
1515
"golang.org/x/xerrors"
1616
"gopkg.in/yaml.v3"
17+
18+
"github.com/coder/coder/coderd/util/slice"
1719
)
1820

1921
// Cmd describes an executable command.
@@ -102,11 +104,11 @@ func (c *Cmd) PrepareAll() error {
102104
}
103105
}
104106

105-
slices.SortFunc(c.Options, func(a, b Option) bool {
106-
return a.Name < b.Name
107+
slices.SortFunc(c.Options, func(a, b Option) int {
108+
return slice.Ascending(a.Name, b.Name)
107109
})
108-
slices.SortFunc(c.Children, func(a, b *Cmd) bool {
109-
return a.Name() < b.Name()
110+
slices.SortFunc(c.Children, func(a, b *Cmd) int {
111+
return slice.Ascending(a.Name(), b.Name())
110112
})
111113
for _, child := range c.Children {
112114
child.Parent = c

cli/configssh.go

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

2525
"github.com/coder/coder/cli/clibase"
2626
"github.com/coder/coder/cli/cliui"
27+
"github.com/coder/coder/coderd/util/slice"
2728
"github.com/coder/coder/codersdk"
2829
)
2930

@@ -367,8 +368,8 @@ func (r *RootCmd) configSSH() *clibase.Cmd {
367368
}
368369

369370
// Ensure stable sorting of output.
370-
slices.SortFunc(workspaceConfigs, func(a, b sshWorkspaceConfig) bool {
371-
return a.Name < b.Name
371+
slices.SortFunc(workspaceConfigs, func(a, b sshWorkspaceConfig) int {
372+
return slice.Ascending(a.Name, b.Name)
372373
})
373374
for _, wc := range workspaceConfigs {
374375
sort.Strings(wc.Hosts)

cli/create.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/coder/coder/cli/clibase"
1414
"github.com/coder/coder/cli/cliui"
1515
"github.com/coder/coder/coderd/util/ptr"
16+
"github.com/coder/coder/coderd/util/slice"
1617
"github.com/coder/coder/codersdk"
1718
)
1819

@@ -80,8 +81,8 @@ func (r *RootCmd) create() *clibase.Cmd {
8081
return err
8182
}
8283

83-
slices.SortFunc(templates, func(a, b codersdk.Template) bool {
84-
return a.ActiveUserCount > b.ActiveUserCount
84+
slices.SortFunc(templates, func(a, b codersdk.Template) int {
85+
return slice.Descending(a.ActiveUserCount, b.ActiveUserCount)
8586
})
8687

8788
templateNames := make([]string, 0, len(templates))

coderd/database/dbfake/dbfake.go

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
"github.com/google/uuid"
1717
"github.com/lib/pq"
18-
"golang.org/x/exp/constraints"
1918
"golang.org/x/exp/maps"
2019
"golang.org/x/exp/slices"
2120
"golang.org/x/xerrors"
@@ -607,7 +606,7 @@ func uniqueSortedUUIDs(uuids []uuid.UUID) []uuid.UUID {
607606
unique = append(unique, id)
608607
}
609608
slices.SortFunc(unique, func(a, b uuid.UUID) int {
610-
return orderingAscend(a, b)
609+
return slice.Ascending(a.String(), b.String())
611610
})
612611
return unique
613612
}
@@ -2061,8 +2060,8 @@ func (q *FakeQuerier) GetTemplateDailyInsights(_ context.Context, arg database.G
20612060
for templateID := range ds.templateIDSet {
20622061
templateIDs = append(templateIDs, templateID)
20632062
}
2064-
slices.SortFunc(templateIDs, func(a, b uuid.UUID) bool {
2065-
return a.String() < b.String()
2063+
slices.SortFunc(templateIDs, func(a, b uuid.UUID) int {
2064+
return slice.Ascending(a.String(), b.String())
20662065
})
20672066
result = append(result, database.GetTemplateDailyInsightsRow{
20682067
StartTime: ds.startTime,
@@ -2120,8 +2119,8 @@ func (q *FakeQuerier) GetTemplateInsights(_ context.Context, arg database.GetTem
21202119
for templateID := range templateIDSet {
21212120
templateIDs = append(templateIDs, templateID)
21222121
}
2123-
slices.SortFunc(templateIDs, func(a, b uuid.UUID) bool {
2124-
return a.String() < b.String()
2122+
slices.SortFunc(templateIDs, func(a, b uuid.UUID) int {
2123+
return slice.Ascending(a.String(), b.String())
21252124
})
21262125
result := database.GetTemplateInsightsRow{
21272126
TemplateIDs: templateIDs,
@@ -2342,13 +2341,17 @@ func (q *FakeQuerier) GetTemplateVersionsByTemplateID(_ context.Context, arg dat
23422341
}
23432342

23442343
// Database orders by created_at
2345-
slices.SortFunc(version, func(a, b database.TemplateVersion) bool {
2344+
slices.SortFunc(version, func(a, b database.TemplateVersion) int {
23462345
if a.CreatedAt.Equal(b.CreatedAt) {
23472346
// Technically the postgres database also orders by uuid. So match
23482347
// that behavior
2349-
return a.ID.String() < b.ID.String()
2348+
return slice.Ascending(a.ID.String(), b.ID.String())
2349+
}
2350+
if a.CreatedAt.Before(b.CreatedAt) {
2351+
return -1
2352+
} else {
2353+
return 1
23502354
}
2351-
return a.CreatedAt.Before(b.CreatedAt)
23522355
})
23532356

23542357
if arg.AfterID != uuid.Nil {
@@ -2407,11 +2410,11 @@ func (q *FakeQuerier) GetTemplates(_ context.Context) ([]database.Template, erro
24072410
defer q.mutex.RUnlock()
24082411

24092412
templates := slices.Clone(q.templates)
2410-
slices.SortFunc(templates, func(i, j database.TemplateTable) bool {
2411-
if i.Name != j.Name {
2412-
return i.Name < j.Name
2413+
slices.SortFunc(templates, func(a, b database.TemplateTable) int {
2414+
if a.Name != b.Name {
2415+
return slice.Ascending(a.Name, b.Name)
24132416
}
2414-
return i.ID.String() < j.ID.String()
2417+
return slice.Ascending(a.ID.String(), b.ID.String())
24152418
})
24162419

24172420
return q.templatesWithUserNoLock(templates), nil
@@ -2524,8 +2527,8 @@ func (q *FakeQuerier) GetUserLatencyInsights(_ context.Context, arg database.Get
25242527
for templateID := range templateIDSet {
25252528
templateIDs = append(templateIDs, templateID)
25262529
}
2527-
slices.SortFunc(templateIDs, func(a, b uuid.UUID) bool {
2528-
return a.String() < b.String()
2530+
slices.SortFunc(templateIDs, func(a, b uuid.UUID) int {
2531+
return slice.Ascending(a.String(), b.String())
25292532
})
25302533
user, err := q.getUserByIDNoLock(userID)
25312534
if err != nil {
@@ -2541,8 +2544,8 @@ func (q *FakeQuerier) GetUserLatencyInsights(_ context.Context, arg database.Get
25412544
}
25422545
rows = append(rows, row)
25432546
}
2544-
slices.SortFunc(rows, func(a, b database.GetUserLatencyInsightsRow) bool {
2545-
return a.UserID.String() < b.UserID.String()
2547+
slices.SortFunc(rows, func(a, b database.GetUserLatencyInsightsRow) int {
2548+
return slice.Ascending(a.UserID.String(), b.UserID.String())
25462549
})
25472550

25482551
return rows, nil
@@ -2589,8 +2592,8 @@ func (q *FakeQuerier) GetUsers(_ context.Context, params database.GetUsersParams
25892592
copy(users, q.users)
25902593

25912594
// Database orders by username
2592-
slices.SortFunc(users, func(a, b database.User) bool {
2593-
return strings.ToLower(a.Username) < strings.ToLower(b.Username)
2595+
slices.SortFunc(users, func(a, b database.User) int {
2596+
return slice.Ascending(a.Username, b.Username)
25942597
})
25952598

25962599
// Filter out deleted since they should never be returned..
@@ -3128,7 +3131,8 @@ func (q *FakeQuerier) GetWorkspaceBuildsByWorkspaceID(_ context.Context,
31283131

31293132
// Order by build_number
31303133
slices.SortFunc(history, func(a, b database.WorkspaceBuild) int {
3131-
return orderingDescend(a.BuildNumber, b.BuildNumber)
3134+
return slice.Descending(a.BuildNumber, b.BuildNumber)
3135+
31323136
})
31333137

31343138
if params.AfterID != uuid.Nil {
@@ -5488,11 +5492,11 @@ func (q *FakeQuerier) GetAuthorizedTemplates(ctx context.Context, arg database.G
54885492
templates = append(templates, template)
54895493
}
54905494
if len(templates) > 0 {
5491-
slices.SortFunc(templates, func(i, j database.Template) bool {
5492-
if i.Name != j.Name {
5493-
return i.Name < j.Name
5495+
slices.SortFunc(templates, func(a, b database.Template) int {
5496+
if a.Name != b.Name {
5497+
return slice.Ascending(a.Name, b.Name)
54945498
}
5495-
return i.ID.String() < j.ID.String()
5499+
return slice.Ascending(a.ID.String(), b.ID.String())
54965500
})
54975501
return templates, nil
54985502
}
@@ -5865,17 +5869,3 @@ func (q *FakeQuerier) GetAuthorizedUsers(ctx context.Context, arg database.GetUs
58655869
}
58665870
return filteredUsers, nil
58675871
}
5868-
5869-
func orderingAscend[E constraints.Ordered](a, b E) int {
5870-
if a < b {
5871-
return -1
5872-
} else if a == b {
5873-
return 0
5874-
} else {
5875-
return 1
5876-
}
5877-
}
5878-
5879-
func orderingDescend[E constraints.Ordered](a, b E) int {
5880-
return -orderingAscend[E](a, b)
5881-
}

coderd/devtunnel/servers.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"golang.org/x/sync/errgroup"
1111
"golang.org/x/xerrors"
1212

13+
"github.com/coder/coder/coderd/util/slice"
1314
"github.com/coder/coder/cryptorand"
1415
)
1516

@@ -115,8 +116,8 @@ func FindClosestNode(nodes []Node) (Node, error) {
115116
return Node{}, err
116117
}
117118

118-
slices.SortFunc(nodes, func(i, j Node) bool {
119-
return i.AvgLatency < j.AvgLatency
119+
slices.SortFunc(nodes, func(a, b Node) int {
120+
return slice.Ascending(a.AvgLatency, b.AvgLatency)
120121
})
121122
return nodes[0], nil
122123
}

coderd/insights.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/coder/coder/coderd/database"
1616
"github.com/coder/coder/coderd/httpapi"
1717
"github.com/coder/coder/coderd/rbac"
18+
"github.com/coder/coder/coderd/util/slice"
1819
"github.com/coder/coder/codersdk"
1920
)
2021

@@ -132,8 +133,8 @@ func (api *API) insightsUserLatency(rw http.ResponseWriter, r *http.Request) {
132133
for templateID := range templateIDSet {
133134
seenTemplateIDs = append(seenTemplateIDs, templateID)
134135
}
135-
slices.SortFunc(seenTemplateIDs, func(a, b uuid.UUID) bool {
136-
return a.String() < b.String()
136+
slices.SortFunc(seenTemplateIDs, func(a, b uuid.UUID) int {
137+
return slice.Ascending(a.String(), b.String())
137138
})
138139

139140
resp := codersdk.UserLatencyInsightsResponse{

coderd/metricscache/metricscache.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,14 @@ func convertDAUResponse[T dauRow](rows []T, tzOffset int) codersdk.DAUsResponse
146146
}
147147

148148
dates := maps.Keys(respMap)
149-
slices.SortFunc(dates, func(a, b time.Time) bool {
150-
return a.Before(b)
149+
slices.SortFunc(dates, func(a, b time.Time) int {
150+
if a.Before(b) {
151+
return -1
152+
} else if a.Equal(b) {
153+
return 0
154+
} else {
155+
return 1
156+
}
151157
})
152158

153159
var resp codersdk.DAUsResponse

coderd/util/slice/slice.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package slice
22

3+
import "golang.org/x/exp/constraints"
4+
35
// SameElements returns true if the 2 lists have the same elements in any
46
// order.
57
func SameElements[T comparable](a []T, b []T) bool {
@@ -67,3 +69,17 @@ func OverlapCompare[T any](a []T, b []T, equal func(a, b T) bool) bool {
6769
func New[T any](items ...T) []T {
6870
return items
6971
}
72+
73+
func Ascending[T constraints.Ordered](a, b T) int {
74+
if a < b {
75+
return -1
76+
} else if a == b {
77+
return 0
78+
} else {
79+
return 1
80+
}
81+
}
82+
83+
func Descending[T constraints.Ordered](a, b T) int {
84+
return -Ascending[T](a, b)
85+
}

coderd/workspaceagents.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/coder/coder/coderd/httpmw"
3939
"github.com/coder/coder/coderd/rbac"
4040
"github.com/coder/coder/coderd/util/ptr"
41+
"github.com/coder/coder/coderd/util/slice"
4142
"github.com/coder/coder/codersdk"
4243
"github.com/coder/coder/codersdk/agentsdk"
4344
"github.com/coder/coder/tailnet"
@@ -1614,8 +1615,8 @@ func (api *API) watchWorkspaceAgentMetadata(rw http.ResponseWriter, r *http.Requ
16141615
})
16151616
return
16161617
}
1617-
slices.SortFunc(lastDBMeta, func(i, j database.WorkspaceAgentMetadatum) bool {
1618-
return i.Key < j.Key
1618+
slices.SortFunc(lastDBMeta, func(a, b database.WorkspaceAgentMetadatum) int {
1619+
return slice.Ascending(a.Key, b.Key)
16191620
})
16201621

16211622
// Avoid sending refresh if the client is about to get a

enterprise/tailnet/pgcoord.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/coder/coder/coderd/database/dbauthz"
2323
"github.com/coder/coder/coderd/database/pubsub"
2424
"github.com/coder/coder/coderd/rbac"
25+
"github.com/coder/coder/coderd/util/slice"
2526
agpl "github.com/coder/coder/tailnet"
2627
)
2728

@@ -1352,7 +1353,7 @@ func (c *pgCoord) htmlDebug(ctx context.Context) (agpl.HTMLDebug, error) {
13521353
})
13531354
}
13541355
slices.SortFunc(htmlAgent.Connections, func(a, b *agpl.HTMLClient) int {
1355-
return nameOrdering(a.Name, b.Name)
1356+
return slice.Ascending(a.Name, b.Name)
13561357
})
13571358

13581359
data.Agents = append(data.Agents, htmlAgent)
@@ -1363,7 +1364,7 @@ func (c *pgCoord) htmlDebug(ctx context.Context) (agpl.HTMLDebug, error) {
13631364
})
13641365
}
13651366
slices.SortFunc(data.Agents, func(a, b *agpl.HTMLAgent) int {
1366-
return nameOrdering(a.Name, b.Name)
1367+
return slice.Ascending(a.Name, b.Name)
13671368
})
13681369

13691370
for agentID, conns := range clients {
@@ -1390,13 +1391,13 @@ func (c *pgCoord) htmlDebug(ctx context.Context) (agpl.HTMLDebug, error) {
13901391
})
13911392
}
13921393
slices.SortFunc(agent.Connections, func(a, b *agpl.HTMLClient) int {
1393-
return nameOrdering(a.Name, b.Name)
1394+
return slice.Ascending(a.Name, b.Name)
13941395
})
13951396

13961397
data.MissingAgents = append(data.MissingAgents, agent)
13971398
}
13981399
slices.SortFunc(data.MissingAgents, func(a, b *agpl.HTMLAgent) int {
1399-
return nameOrdering(a.Name, b.Name)
1400+
return slice.Ascending(a.Name, b.Name)
14001401
})
14011402

14021403
return data, nil

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ require (
171171
go.uber.org/goleak v1.2.1
172172
go4.org/netipx v0.0.0-20230728180743-ad4cb58a6516
173173
golang.org/x/crypto v0.11.0
174-
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090
174+
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b
175175
golang.org/x/mod v0.12.0
176176
golang.org/x/net v0.12.0
177177
golang.org/x/oauth2 v0.10.0

go.sum

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -975,8 +975,9 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
975975
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
976976
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
977977
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
978-
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090 h1:Di6/M8l0O2lCLc6VVRWhgCiApHV8MnQurBnFSHsQtNY=
979-
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
978+
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b h1:r+vk0EmXNmekl0S0BascoeeoHk/L7wmaW2QF90K+kYI=
979+
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
980+
golang.org/x/exp/typeparams v0.0.0-20221208152030-732eee02a75a h1:Jw5wfR+h9mnIYH+OtGT2im5wV1YGGDora5vTv/aa5bE=
980981
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
981982
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
982983
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=

0 commit comments

Comments
 (0)