Skip to content

Commit 6a00a13

Browse files
committed
all: use more maps and slices APIs from Go 1.23
These were found by looking for common suspects such as make calls using the length of another slice or map, or calls to slices.Sort after a simple loop. Done as a separate commit, as none of these were already marked as TODOs so they require slightly more scrutiny to check for mistakes. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I1da3c8dcbb58efa16b1d616a244b0a4c6262de84 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1208912 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Roger Peppe <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent 15a702e commit 6a00a13

File tree

8 files changed

+17
-53
lines changed

8 files changed

+17
-53
lines changed

cue/build/import.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package build
1616

1717
import (
18+
"maps"
1819
"slices"
1920
"strconv"
2021

@@ -143,11 +144,7 @@ func (inst *Instance) complete() errors.Error {
143144
deps[path] = p1
144145
}
145146
}
146-
inst.Deps = make([]string, 0, len(deps))
147-
for dep := range deps {
148-
inst.Deps = append(inst.Deps, dep)
149-
}
150-
slices.Sort(inst.Deps)
147+
inst.Deps = slices.Sorted(maps.Keys(deps))
151148

152149
for _, dep := range inst.Deps {
153150
p1 := deps[dep]

cue/load/loader_common.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package load
1616

1717
import (
1818
"cmp"
19+
"maps"
1920
pathpkg "path"
2021
"path/filepath"
2122
"slices"
@@ -146,7 +147,7 @@ func (fp *fileProcessor) finalize(p *build.Instance) errors.Error {
146147
return fp.err
147148
}
148149

149-
p.ImportPaths, _ = cleanImports(fp.imported)
150+
p.ImportPaths = slices.Sorted(maps.Keys(fp.imported))
150151

151152
return nil
152153
}
@@ -344,15 +345,6 @@ func (fp *fileProcessor) add(root string, file *build.File, mode importMode) {
344345
}
345346
}
346347

347-
func cleanImports(m map[string][]token.Pos) ([]string, map[string][]token.Pos) {
348-
all := make([]string, 0, len(m))
349-
for path := range m {
350-
all = append(all, path)
351-
}
352-
slices.Sort(all)
353-
return all, m
354-
}
355-
356348
// isLocalImport reports whether the import path is
357349
// a local import path, like ".", "..", "./foo", or "../foo".
358350
func isLocalImport(path string) bool {

encoding/jsonschema/external_test.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
stdjson "encoding/json"
1919
"fmt"
2020
"io"
21+
"maps"
2122
"os"
2223
"path"
2324
"slices"
@@ -55,7 +56,7 @@ func TestExternal(t *testing.T) {
5556
// by the end.
5657
cuetdtest.SmallMatrix.Run(t, "tests", func(t *testing.T, m *cuetdtest.M) {
5758
// Run tests in deterministic order so we get some consistency between runs.
58-
for _, filename := range sortedKeys(tests) {
59+
for _, filename := range slices.Sorted(maps.Keys(tests)) {
5960
schemas := tests[filename]
6061
t.Run(testName(filename), func(t *testing.T) {
6162
for _, s := range schemas {
@@ -255,15 +256,6 @@ var extVersionToVersion = map[string]jsonschema.Version{
255256
"draft-next": jsonschema.VersionUnknown,
256257
}
257258

258-
func sortedKeys[T any](m map[string]T) []string {
259-
ks := make([]string, 0, len(m))
260-
for k := range m {
261-
ks = append(ks, k)
262-
}
263-
slices.Sort(ks)
264-
return ks
265-
}
266-
267259
func writeExternalTestStats(testDir string, tests map[string][]*externaltest.Schema) error {
268260
outf, err := os.Create("external_teststats.txt")
269261
if err != nil {

encoding/jsonschema/teststats.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"io"
2424
"log"
25+
"maps"
2526
"os"
2627
"path"
2728
"slices"
@@ -50,7 +51,7 @@ func main() {
5051
}
5152

5253
func listFailures(outw io.Writer, version string, tests map[string][]*externaltest.Schema) {
53-
for _, filename := range sortedKeys(tests) {
54+
for _, filename := range slices.Sorted(maps.Keys(tests)) {
5455
schemas := tests[filename]
5556
for _, schema := range schemas {
5657
if schema.Skip[version] != "" {
@@ -79,12 +80,3 @@ func testdataPos(p positioner) token.Position {
7980
pp.Filename = path.Join(testDir, pp.Filename)
8081
return pp
8182
}
82-
83-
func sortedKeys[T any](m map[string]T) []string {
84-
ks := make([]string, 0, len(m))
85-
for k := range m {
86-
ks = append(ks, k)
87-
}
88-
slices.Sort(ks)
89-
return ks
90-
}

encoding/openapi/build.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package openapi
1717
import (
1818
"cmp"
1919
"fmt"
20+
"maps"
2021
"math"
2122
"path"
2223
"regexp"
@@ -149,13 +150,7 @@ func schemas(g *Generator, inst cue.InstanceOrValue) (schemas *ast.StructLit, er
149150
done = len(c.externalRefs)
150151

151152
// From now on, all references need to be expanded
152-
external := []string{}
153-
for k := range c.externalRefs {
154-
external = append(external, k)
155-
}
156-
slices.Sort(external)
157-
158-
for _, k := range external {
153+
for _, k := range slices.Sorted(maps.Keys(c.externalRefs)) {
159154
ext := c.externalRefs[k]
160155
c.instExt = ext.inst
161156
sels := ext.path.Selectors()

internal/core/export/self_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package export_test
1616

1717
import (
1818
"bytes"
19+
"slices"
1920
"strings"
2021
"testing"
2122

@@ -106,8 +107,7 @@ func buildFile(t *testing.T, r *cue.Context, b *build.Instance) cue.Value {
106107
// file.
107108
func patch(t *testing.T, r *cue.Context, orig *txtar.Archive, f *ast.File) cue.Value {
108109
a := *orig
109-
a.Files = make([]txtar.File, len(a.Files))
110-
copy(a.Files, orig.Files)
110+
a.Files = slices.Clone(orig.Files)
111111

112112
k := 0
113113
for _, f := range a.Files {

internal/mod/modpkgload/pkgload.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"io/fs"
7+
"maps"
78
"runtime"
89
"slices"
910
"strings"
@@ -311,11 +312,8 @@ func (pkgs *Packages) load(ctx context.Context, pkg *Package) {
311312
}
312313
return
313314
}
314-
imports := make([]string, 0, len(importsMap))
315-
for imp := range importsMap {
316-
imports = append(imports, imp)
317-
}
318-
slices.Sort(imports) // Make the algorithm deterministic for tests.
315+
// Make the algorithm deterministic for tests.
316+
imports := slices.Sorted(maps.Keys(importsMap))
319317

320318
pkg.imports = make([]*Package, 0, len(imports))
321319
var importFlags Flags

internal/mod/mvs/mvs_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package mvs
66

77
import (
88
"fmt"
9+
"maps"
910
"reflect"
1011
"strings"
1112
"testing"
@@ -527,10 +528,7 @@ func Test(t *testing.T) {
527528
if err == nil {
528529
// Copy the reqs map, but substitute the upgraded requirements in
529530
// place of the target's original requirements.
530-
upReqs := make(reqsMap, len(reqs))
531-
for m, r := range reqs {
532-
upReqs[m] = r
533-
}
531+
upReqs := maps.Clone(reqs)
534532
upReqs[m(kf[1])] = list
535533

536534
list, err = Req(m(kf[1]), nil, upReqs)

0 commit comments

Comments
 (0)