Skip to content

Commit 7413850

Browse files
authored
Use slices.Sorted(maps.Keys(...)) to replace collect-keys-then-sort pattern (#4954)
## Summary - Replace the 3-4 line "make slice, range map, append, sort" idiom with the Go 1.23 one-liner `slices.Sorted(maps.Keys(...))` - Remove `utils.SortedKeys` and two package-local duplicates (`sortedKeys`, `sortKeys`), inlining all call sites - Also replace `slices.Collect(maps.Values(...))` + `slices.Sort(...)` with `slices.Sorted(maps.Values(...))` in `dynloc/locations.go` ## Test plan - [x] `go build ./...` passes - [x] Unit tests pass for all affected packages This pull request was AI-assisted by Isaac.
1 parent 7d4a13a commit 7413850

33 files changed

Lines changed: 86 additions & 155 deletions

File tree

acceptance/acceptance_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"flag"
1111
"fmt"
1212
"io"
13+
"maps"
1314
"net/http"
1415
"os"
1516
"os/exec"
@@ -31,7 +32,6 @@ import (
3132
"github.com/databricks/cli/libs/auth"
3233
"github.com/databricks/cli/libs/testdiff"
3334
"github.com/databricks/cli/libs/testserver"
34-
"github.com/databricks/cli/libs/utils"
3535
"github.com/stretchr/testify/require"
3636
)
3737

@@ -817,7 +817,7 @@ func buildTestEnv(configEnv map[string]string, customEnv []string) []string {
817817
env := make([]string, 0, len(configEnv)+len(customEnv))
818818

819819
// Add config.Env first (but skip keys that exist in customEnv)
820-
for _, key := range utils.SortedKeys(configEnv) {
820+
for _, key := range slices.Sorted(maps.Keys(configEnv)) {
821821
if hasKey(customEnv, key) {
822822
continue
823823
}

acceptance/internal/config.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package internal
22

33
import (
44
"hash/fnv"
5+
"maps"
56
"os"
67
"path/filepath"
78
"reflect"
@@ -345,11 +346,7 @@ func ExpandEnvMatrix(matrix, exclude map[string][]string, extraVars []string) []
345346
return result
346347
}
347348

348-
keys := make([]string, 0, len(filteredMatrix))
349-
for key := range filteredMatrix {
350-
keys = append(keys, key)
351-
}
352-
slices.Sort(keys)
349+
keys := slices.Sorted(maps.Keys(filteredMatrix))
353350

354351
// Build an expansion of all combinations.
355352
// At each step we look at a given key and append each possible value to each

bundle/artifacts/build.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package artifacts
33
import (
44
"context"
55
"fmt"
6+
"maps"
67
"os"
78
"path/filepath"
9+
"slices"
810

911
"github.com/databricks/cli/bundle"
1012
"github.com/databricks/cli/bundle/config"
@@ -15,7 +17,6 @@ import (
1517
"github.com/databricks/cli/libs/log"
1618
"github.com/databricks/cli/libs/logdiag"
1719
"github.com/databricks/cli/libs/patchwheel"
18-
"github.com/databricks/cli/libs/utils"
1920
)
2021

2122
func Build() bundle.Mutator {
@@ -37,7 +38,7 @@ func (m *build) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
3738
})
3839
}
3940

40-
for _, artifactName := range utils.SortedKeys(b.Config.Artifacts) {
41+
for _, artifactName := range slices.Sorted(maps.Keys(b.Config.Artifacts)) {
4142
a := b.Config.Artifacts[artifactName]
4243

4344
if a.BuildCommand != "" {

bundle/artifacts/prepare.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package artifacts
33
import (
44
"context"
55
"errors"
6+
"maps"
67
"os"
78
"path/filepath"
9+
"slices"
810

911
"github.com/databricks/cli/bundle"
1012
"github.com/databricks/cli/bundle/config"
@@ -15,7 +17,6 @@ import (
1517
"github.com/databricks/cli/libs/log"
1618
"github.com/databricks/cli/libs/logdiag"
1719
"github.com/databricks/cli/libs/python"
18-
"github.com/databricks/cli/libs/utils"
1920
)
2021

2122
func Prepare() bundle.Mutator {
@@ -34,7 +35,7 @@ func (m *prepare) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics
3435
return diag.FromErr(err)
3536
}
3637

37-
for _, artifactName := range utils.SortedKeys(b.Config.Artifacts) {
38+
for _, artifactName := range slices.Sorted(maps.Keys(b.Config.Artifacts)) {
3839
artifact := b.Config.Artifacts[artifactName]
3940
if artifact == nil {
4041
l := b.Config.GetLocation("artifacts." + artifactName)

bundle/config/validate/scripts.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package validate
33
import (
44
"context"
55
"fmt"
6+
"maps"
67
"regexp"
8+
"slices"
79

810
"github.com/databricks/cli/bundle"
911
"github.com/databricks/cli/libs/diag"
1012
"github.com/databricks/cli/libs/dyn"
11-
"github.com/databricks/cli/libs/utils"
1213
)
1314

1415
type validateScripts struct{}
@@ -28,7 +29,7 @@ func (f *validateScripts) Apply(ctx context.Context, b *bundle.Bundle) diag.Diag
2829

2930
// Sort the scripts to have a deterministic order for the
3031
// generated diagnostics.
31-
scriptKeys := utils.SortedKeys(b.Config.Scripts)
32+
scriptKeys := slices.Sorted(maps.Keys(b.Config.Scripts))
3233

3334
for _, k := range scriptKeys {
3435
script := b.Config.Scripts[k]

bundle/configsync/format.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package configsync
22

33
import (
44
"fmt"
5+
"maps"
56
"slices"
67
"strings"
78
)
@@ -17,21 +18,13 @@ func FormatTextOutput(changes Changes) string {
1718

1819
output.WriteString(fmt.Sprintf("Detected changes in %d resource(s):\n\n", len(changes)))
1920

20-
resourceKeys := make([]string, 0, len(changes))
21-
for key := range changes {
22-
resourceKeys = append(resourceKeys, key)
23-
}
24-
slices.Sort(resourceKeys)
21+
resourceKeys := slices.Sorted(maps.Keys(changes))
2522

2623
for _, resourceKey := range resourceKeys {
2724
resourceChanges := changes[resourceKey]
2825
output.WriteString(fmt.Sprintf("Resource: %s\n", resourceKey))
2926

30-
paths := make([]string, 0, len(resourceChanges))
31-
for path := range resourceChanges {
32-
paths = append(paths, path)
33-
}
34-
slices.Sort(paths)
27+
paths := slices.Sorted(maps.Keys(resourceChanges))
3528

3629
for _, path := range paths {
3730
configChange := resourceChanges[path]

bundle/configsync/resolve.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"fmt"
77
"io/fs"
8+
"maps"
89
"path/filepath"
910
"slices"
1011
"strings"
@@ -171,11 +172,7 @@ func ResolveChanges(ctx context.Context, b *bundle.Bundle, configChanges Changes
171172
var result []FieldChange
172173
targetName := b.Config.Bundle.Target
173174

174-
resourceKeys := make([]string, 0, len(configChanges))
175-
for resourceKey := range configChanges {
176-
resourceKeys = append(resourceKeys, resourceKey)
177-
}
178-
slices.Sort(resourceKeys)
175+
resourceKeys := slices.Sorted(maps.Keys(configChanges))
179176

180177
for _, resourceKey := range resourceKeys {
181178
resourceChanges := configChanges[resourceKey]

bundle/direct/bundle_plan.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/databricks/cli/libs/structs/structdiff"
2424
"github.com/databricks/cli/libs/structs/structpath"
2525
"github.com/databricks/cli/libs/structs/structvar"
26-
"github.com/databricks/cli/libs/utils"
2726
"github.com/databricks/databricks-sdk-go"
2827
)
2928

@@ -968,7 +967,7 @@ func (b *DeploymentBundle) getAdapterForKey(resourceKey string) (*dresources.Ada
968967

969968
adapter, ok := b.Adapters[group]
970969
if !ok {
971-
return nil, fmt.Errorf("resource type %q not supported, available: %s", group, strings.Join(utils.SortedKeys(b.Adapters), ", "))
970+
return nil, fmt.Errorf("resource type %q not supported, available: %s", group, strings.Join(slices.Sorted(maps.Keys(b.Adapters)), ", "))
972971
}
973972

974973
return adapter, nil

bundle/direct/graph.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ package direct
22

33
import (
44
"fmt"
5+
"maps"
6+
"slices"
57

68
"github.com/databricks/cli/bundle/deployplan"
79
"github.com/databricks/cli/libs/dagrun"
8-
"github.com/databricks/cli/libs/utils"
910
)
1011

1112
func makeGraph(plan *deployplan.Plan) (*dagrun.Graph, error) {
1213
g := dagrun.NewGraph()
1314

1415
// Add all nodes first
15-
for _, resourceKey := range utils.SortedKeys(plan.Plan) {
16+
for _, resourceKey := range slices.Sorted(maps.Keys(plan.Plan)) {
1617
g.AddNode(resourceKey)
1718
}
1819

bundle/internal/schema/annotations.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"bytes"
55
"fmt"
6+
"maps"
67
"os"
78
"reflect"
89
"regexp"
@@ -183,12 +184,7 @@ func saveYamlWithStyle(outputPath string, annotations annotation.File) error {
183184
}
184185

185186
func getAlphabeticalOrder[T any](mapping map[string]T) *yamlsaver.Order {
186-
var order []string
187-
for k := range mapping {
188-
order = append(order, k)
189-
}
190-
slices.Sort(order)
191-
return yamlsaver.NewOrder(order)
187+
return yamlsaver.NewOrder(slices.Sorted(maps.Keys(mapping)))
192188
}
193189

194190
func convertLinksToAbsoluteUrl(s string) string {

0 commit comments

Comments
 (0)