Skip to content

Commit eaf2e7b

Browse files
authored
Add utils module with SortedKeys helper (#2689)
## Changes - Move helper sortedKeys from artifacts package to libs/utils - Use it where it's useful. ## Tests Existing tests.
1 parent 6083062 commit eaf2e7b

6 files changed

Lines changed: 25 additions & 30 deletions

File tree

bundle/artifacts/build.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/databricks/cli/libs/log"
1616
"github.com/databricks/cli/libs/patchwheel"
1717
"github.com/databricks/cli/libs/python"
18+
"github.com/databricks/cli/libs/utils"
1819
)
1920

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

41-
for _, artifactName := range sortedKeys(b.Config.Artifacts) {
42+
for _, artifactName := range utils.SortedKeys(b.Config.Artifacts) {
4243
a := b.Config.Artifacts[artifactName]
4344

4445
if a.BuildCommand != "" {

bundle/artifacts/prepare.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"os"
66
"path/filepath"
7-
"sort"
87

98
"github.com/databricks/cli/bundle"
109
"github.com/databricks/cli/bundle/config"
@@ -13,6 +12,7 @@ import (
1312
"github.com/databricks/cli/libs/diag"
1413
"github.com/databricks/cli/libs/log"
1514
"github.com/databricks/cli/libs/python"
15+
"github.com/databricks/cli/libs/utils"
1616
)
1717

1818
func Prepare() bundle.Mutator {
@@ -36,7 +36,7 @@ func (m *prepare) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics
3636
removeFolders := make(map[string]bool, len(b.Config.Artifacts))
3737
cleanupWheelFolders := make(map[string]bool, len(b.Config.Artifacts))
3838

39-
for _, artifactName := range sortedKeys(b.Config.Artifacts) {
39+
for _, artifactName := range utils.SortedKeys(b.Config.Artifacts) {
4040
artifact := b.Config.Artifacts[artifactName]
4141
b.Metrics.AddBoolValue(metrics.ArtifactBuildCommandIsSet, artifact.BuildCommand != "")
4242
b.Metrics.AddBoolValue(metrics.ArtifactFilesIsSet, len(artifact.Files) != 0)
@@ -89,14 +89,14 @@ func (m *prepare) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics
8989
return diags
9090
}
9191

92-
for _, dir := range sortedKeys(removeFolders) {
92+
for _, dir := range utils.SortedKeys(removeFolders) {
9393
err := os.RemoveAll(dir)
9494
if err != nil {
9595
log.Infof(ctx, "Failed to remove %s: %s", dir, err)
9696
}
9797
}
9898

99-
for _, dir := range sortedKeys(cleanupWheelFolders) {
99+
for _, dir := range utils.SortedKeys(cleanupWheelFolders) {
100100
log.Infof(ctx, "Cleaning up Python build artifacts in %s", dir)
101101
python.CleanupWheelFolder(dir)
102102
}
@@ -140,12 +140,3 @@ func InsertPythonArtifact(ctx context.Context, b *bundle.Bundle) error {
140140

141141
return nil
142142
}
143-
144-
func sortedKeys[T any](m map[string]T) []string {
145-
keys := make([]string, 0, len(m))
146-
for k := range m {
147-
keys = append(keys, k)
148-
}
149-
sort.Strings(keys)
150-
return keys
151-
}

bundle/libraries/upload.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"os"
88
"path"
99
"path/filepath"
10-
"sort"
1110
"strings"
1211

1312
"github.com/databricks/cli/bundle"
@@ -16,6 +15,7 @@ import (
1615
"github.com/databricks/cli/libs/dyn"
1716
"github.com/databricks/cli/libs/filer"
1817
"github.com/databricks/cli/libs/log"
18+
"github.com/databricks/cli/libs/utils"
1919

2020
"golang.org/x/sync/errgroup"
2121
)
@@ -150,11 +150,7 @@ func (u *upload) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
150150
return diag.FromErr(err)
151151
}
152152

153-
sources := make([]string, 0, len(libs))
154-
for source := range libs {
155-
sources = append(sources, source)
156-
}
157-
sort.Strings(sources)
153+
sources := utils.SortedKeys(libs)
158154

159155
errs, errCtx := errgroup.WithContext(ctx)
160156
errs.SetLimit(maxFilesRequestsInFlight)

bundle/tests/include_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ package config_tests
33
import (
44
"context"
55
"path/filepath"
6-
"sort"
76
"testing"
87

98
"github.com/databricks/cli/bundle"
109
"github.com/databricks/cli/bundle/phases"
10+
"github.com/databricks/cli/libs/utils"
1111
"github.com/stretchr/testify/assert"
1212
"github.com/stretchr/testify/require"
13-
"golang.org/x/exp/maps"
1413
)
1514

1615
func TestIncludeInvalid(t *testing.T) {
@@ -25,8 +24,7 @@ func TestIncludeInvalid(t *testing.T) {
2524
func TestIncludeWithGlob(t *testing.T) {
2625
b := load(t, "./include_with_glob")
2726

28-
keys := maps.Keys(b.Config.Resources.Jobs)
29-
sort.Strings(keys)
27+
keys := utils.SortedKeys(b.Config.Resources.Jobs)
3028
assert.Equal(t, []string{"my_job"}, keys)
3129

3230
job := b.Config.Resources.Jobs["my_job"]
@@ -46,8 +44,7 @@ func TestIncludeForMultipleMatches(t *testing.T) {
4644
b := load(t, "./include_multiple")
4745

4846
// Test that both jobs were loaded.
49-
keys := maps.Keys(b.Config.Resources.Jobs)
50-
sort.Strings(keys)
47+
keys := utils.SortedKeys(b.Config.Resources.Jobs)
5148
assert.Equal(t, []string{"my_first_job", "my_second_job"}, keys)
5249

5350
first := b.Config.Resources.Jobs["my_first_job"]

libs/dyn/dynvar/resolve.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import (
44
"errors"
55
"fmt"
66
"slices"
7-
"sort"
87
"strings"
98

109
"github.com/databricks/cli/libs/dyn"
11-
"golang.org/x/exp/maps"
10+
"github.com/databricks/cli/libs/utils"
1211
)
1312

1413
// Resolve resolves variable references in the given input value using the provided lookup function.
@@ -102,8 +101,7 @@ func (r *resolver) resolveVariableReferences() (err error) {
102101
// We sort the keys here to ensure that we always resolve the same variable reference first.
103102
// This is done such that the cycle detection error is deterministic. If we did not do this,
104103
// we could enter the cycle at any point in the cycle and return varying errors.
105-
keys := maps.Keys(r.refs)
106-
sort.Strings(keys)
104+
keys := utils.SortedKeys(r.refs)
107105
for _, key := range keys {
108106
v, err := r.resolveRef(r.refs[key], []string{key})
109107
if err != nil {

libs/utils/utils.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package utils
2+
3+
import "sort"
4+
5+
func SortedKeys[T any](m map[string]T) []string {
6+
keys := make([]string, 0, len(m))
7+
for k := range m {
8+
keys = append(keys, k)
9+
}
10+
sort.Strings(keys)
11+
return keys
12+
}

0 commit comments

Comments
 (0)