Skip to content

Commit 75c5229

Browse files
dmitshurgopherbot
authored andcommitted
all: migrate x/exp/{maps,slices} uses to maps and slices in std lib
Also migrate to their updated API. This makes it possible to update to the latest version of the golang.org/x/exp module without running into errors. Change-Id: Ie23cff86130ac767d0a5972293784cc70affab79 Reviewed-on: https://go-review.googlesource.com/c/pkgsite-metrics/+/697175 Reviewed-by: Dmitri Shuralyov <[email protected]> Auto-Submit: Dmitri Shuralyov <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Sean Liao <[email protected]> Reviewed-by: Neal Patel <[email protected]>
1 parent 04204cc commit 75c5229

File tree

6 files changed

+33
-20
lines changed

6 files changed

+33
-20
lines changed

cmd/govulncheck_sandbox/govulncheck_sandbox_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88
"bytes"
99
"path/filepath"
1010
"runtime"
11+
"slices"
1112
"strings"
1213
"testing"
1314

14-
"golang.org/x/exp/slices"
1515
"golang.org/x/pkgsite-metrics/internal/buildtest"
1616
"golang.org/x/pkgsite-metrics/internal/govulncheck"
1717
"golang.org/x/pkgsite-metrics/internal/govulncheckapi"

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ require (
2222
go.opencensus.io v0.24.0
2323
go.opentelemetry.io/otel v1.11.2
2424
go.opentelemetry.io/otel/sdk v1.4.0
25-
golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2
2625
golang.org/x/exp/event v0.0.0-20220218215828-6cf2b201936e
2726
golang.org/x/mod v0.26.0
2827
golang.org/x/net v0.42.0
@@ -72,6 +71,7 @@ require (
7271
go.opentelemetry.io/otel/sdk/metric v0.26.0 // indirect
7372
go.opentelemetry.io/otel/trace v1.11.2 // indirect
7473
golang.org/x/crypto v0.40.0 // indirect
74+
golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2 // indirect
7575
golang.org/x/exp/typeparams v0.0.0-20250711185948-6ae5c78190dc // indirect
7676
golang.org/x/sys v0.34.0 // indirect
7777
golang.org/x/telemetry v0.0.0-20250710130107-8d8967aff50b // indirect

internal/analysis/analysis.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import (
1010
"context"
1111
"encoding/json"
1212
"fmt"
13+
"maps"
1314
"net/http"
14-
"sort"
15+
"slices"
1516
"strings"
1617
"time"
1718

1819
bq "cloud.google.com/go/bigquery"
19-
"golang.org/x/exp/maps"
2020
"golang.org/x/pkgsite-metrics/internal/bigquery"
2121
"golang.org/x/pkgsite-metrics/internal/derrors"
2222
"golang.org/x/pkgsite-metrics/internal/queue"
@@ -247,12 +247,10 @@ func ReadWorkVersion(ctx context.Context, c *bigquery.Client, module_path, versi
247247
func JSONTreeToDiagnostics(jsonTree JSONTree) []*Diagnostic {
248248
var diags []*Diagnostic
249249
// Sort for determinism.
250-
pkgIDs := maps.Keys(jsonTree)
251-
sort.Strings(pkgIDs)
250+
pkgIDs := slices.Sorted(maps.Keys(jsonTree))
252251
for _, pkgID := range pkgIDs {
253252
amap := jsonTree[pkgID]
254-
aNames := maps.Keys(amap)
255-
sort.Strings(aNames)
253+
aNames := slices.Sorted(maps.Keys(amap))
256254
for _, aName := range aNames {
257255
diagsOrErr := amap[aName]
258256
if diagsOrErr.Error != nil {

internal/vulndbreqs/bq_test.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ package vulndbreqs
66

77
import (
88
"context"
9+
"slices"
910
"testing"
1011
"time"
1112

1213
"cloud.google.com/go/civil"
1314
"github.com/google/go-cmp/cmp"
1415
"github.com/google/go-cmp/cmp/cmpopts"
15-
"golang.org/x/exp/slices"
1616
"golang.org/x/pkgsite-metrics/internal/bigquery"
1717
test "golang.org/x/pkgsite-metrics/internal/testing"
1818
)
@@ -58,8 +58,23 @@ func TestBigQuery(t *testing.T) {
5858
if err != nil {
5959
t.Fatal(err)
6060
}
61-
slices.SortFunc(want, func(c1, c2 *RequestCount) bool { return c1.Date.After(c2.Date) })
61+
slices.SortFunc(want, func(c1, c2 *RequestCount) int { return -1 * compareDate(c1.Date, c2.Date) })
6262
if diff := cmp.Diff(want, got, cmpopts.IgnoreFields(RequestCount{}, "CreatedAt")); diff != "" {
6363
t.Errorf("mismatch (-want, +got):\n%s", diff)
6464
}
6565
}
66+
67+
// compareDate compares d1 and d2. If d1 is before d2, it returns -1;
68+
// if d1 is after d2, it returns +1; otherwise it returns 0.
69+
//
70+
// TODO(go.dev/issue/74596): Delete and replace with
71+
// https://pkg.go.dev/cloud.google.com/go/civil#Date.Compare
72+
// after updating that module to v0.114.0 or higher.
73+
func compareDate(d1, d2 civil.Date) int {
74+
if d1.Before(d2) {
75+
return -1
76+
} else if d1.After(d2) {
77+
return +1
78+
}
79+
return 0
80+
}

internal/vulndbreqs/compute_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
package vulndbreqs
66

77
import (
8+
cmppkg "cmp"
89
"context"
10+
"maps"
911
"os"
1012
"path/filepath"
13+
"slices"
1114
"strings"
1215
"testing"
1316
"time"
@@ -16,8 +19,6 @@ import (
1619
"cloud.google.com/go/storage"
1720
"github.com/google/go-cmp/cmp"
1821
"github.com/google/go-cmp/cmp/cmpopts"
19-
"golang.org/x/exp/maps"
20-
"golang.org/x/exp/slices"
2122
test "golang.org/x/pkgsite-metrics/internal/testing"
2223
)
2324

@@ -56,8 +57,8 @@ func TestComputeFromStorage(t *testing.T) {
5657
t.Fatal(err)
5758
}
5859
// The returned slice comes from a map, so sort for determinism.
59-
slices.SortFunc(got, func(r1, r2 *IPRequestCount) bool {
60-
return r1.Count < r2.Count
60+
slices.SortFunc(got, func(r1, r2 *IPRequestCount) int {
61+
return cmppkg.Compare(r1.Count, r2.Count)
6162
})
6263
want := []*IPRequestCount{
6364
{Date: testDate, Count: 30},

internal/worker/jobs_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import (
99
"context"
1010
"encoding/json"
1111
"fmt"
12+
"maps"
13+
"slices"
1214
"strings"
1315
"testing"
1416
"time"
1517

1618
"github.com/google/go-cmp/cmp"
17-
"golang.org/x/exp/maps"
18-
"golang.org/x/exp/slices"
1919
"golang.org/x/pkgsite-metrics/internal/derrors"
2020
"golang.org/x/pkgsite-metrics/internal/jobs"
2121
)
@@ -107,12 +107,11 @@ func (d *testJobDB) UpdateJob(ctx context.Context, id string, f func(*jobs.Job)
107107
}
108108

109109
func (d *testJobDB) ListJobs(ctx context.Context, f func(*jobs.Job, time.Time) error) error {
110-
jobslice := maps.Values(d.jobs)
111110
// Sort by StartedAt descending.
112-
slices.SortFunc(jobslice, func(j1, j2 *jobs.Job) bool {
113-
return j1.StartedAt.After(j2.StartedAt)
111+
sortedJobs := slices.SortedFunc(maps.Values(d.jobs), func(j1, j2 *jobs.Job) int {
112+
return -1 * j1.StartedAt.Compare(j2.StartedAt)
114113
})
115-
for _, j := range jobslice {
114+
for _, j := range sortedJobs {
116115
if err := f(j, time.Time{}); err != nil {
117116
return err
118117
}

0 commit comments

Comments
 (0)