Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ linters:
msg: Use env.UserHomeDir(ctx) from libs/env instead.
- pattern: 'os\.Getenv'
msg: Use env.Get(ctx) from the libs/env package instead of os.Getenv.
- pattern: 'sort\.Slice'
msg: Use slices.SortFunc from the standard library instead.
- pattern: 'sort\.SliceStable'
msg: Use slices.SortStableFunc from the standard library instead.
- pattern: 'sort\.Strings'
msg: Use slices.Sort from the standard library instead.
- pattern: 'sort\.Ints'
msg: Use slices.Sort from the standard library instead.
- pattern: 'sort\.Float64s'
msg: Use slices.Sort from the standard library instead.
analyze-types: true
copyloopvar:
check-alias: true
Expand Down
3 changes: 1 addition & 2 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"regexp"
"runtime"
"slices"
"sort"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -486,7 +485,7 @@ func getTests(t *testing.T) []string {
})
require.NoError(t, err)

sort.Strings(testDirs)
slices.Sort(testDirs)
return testDirs
}

Expand Down
3 changes: 1 addition & 2 deletions acceptance/internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"path/filepath"
"reflect"
"slices"
"sort"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -350,7 +349,7 @@ func ExpandEnvMatrix(matrix, exclude map[string][]string, extraVars []string) []
for key := range filteredMatrix {
keys = append(keys, key)
}
sort.Strings(keys)
slices.Sort(keys)

// Build an expansion of all combinations.
// At each step we look at a given key and append each possible value to each
Expand Down
12 changes: 6 additions & 6 deletions bundle/config/loader/process_include.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package loader

import (
"cmp"
"context"
"fmt"
"slices"
"sort"
"strings"

"github.com/databricks/cli/bundle"
Expand Down Expand Up @@ -98,7 +98,7 @@ func validateSingleResourceDefined(configRoot dyn.Value, ext, typ string) diag.D
lines = append(lines, fmt.Sprintf(" - %s (%s)\n", r.key, r.typ))
}
// Sort the lines to print to make the output deterministic.
sort.Strings(lines)
slices.Sort(lines)
// Compact the lines before writing them to the message to remove any duplicate lines.
// This is needed because we do not dedup earlier when gathering the resources
// and it's valid to define the same resource in both the resources and targets block.
Expand All @@ -114,11 +114,11 @@ func validateSingleResourceDefined(configRoot dyn.Value, ext, typ string) diag.D
paths = append(paths, rr.path)
}
// Sort the locations and paths to make the output deterministic.
sort.Slice(locations, func(i, j int) bool {
return locations[i].String() < locations[j].String()
slices.SortFunc(locations, func(a, b dyn.Location) int {
return cmp.Compare(a.String(), b.String())
})
sort.Slice(paths, func(i, j int) bool {
return paths[i].String() < paths[j].String()
slices.SortFunc(paths, func(a, b dyn.Path) int {
return cmp.Compare(a.String(), b.String())
})

return diag.Diagnostics{
Expand Down
6 changes: 3 additions & 3 deletions bundle/config/mutator/resourcemutator/apply_presets.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package resourcemutator

import (
"cmp"
"context"
"path"
"slices"
"sort"
"strings"

"github.com/databricks/cli/bundle"
Expand Down Expand Up @@ -315,8 +315,8 @@ func toTagArray(tags map[string]string) []Tag {
for key, value := range tags {
tagArray = append(tagArray, Tag{Key: key, Value: value})
}
sort.Slice(tagArray, func(i, j int) bool {
return tagArray[i].Key < tagArray[j].Key
slices.SortFunc(tagArray, func(a, b Tag) int {
return cmp.Compare(a.Key, b.Key)
})
return tagArray
}
Expand Down
12 changes: 5 additions & 7 deletions bundle/config/validate/enum.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package validate

import (
"cmp"
"context"
"fmt"
"slices"
"sort"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/internal/validation/generated"
Expand Down Expand Up @@ -86,16 +86,14 @@ func (f *enum) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
}

// Sort diagnostics to make them deterministic
sort.Slice(diags, func(i, j int) bool {
slices.SortFunc(diags, func(a, b diag.Diagnostic) int {
// First sort by summary
if diags[i].Summary != diags[j].Summary {
return diags[i].Summary < diags[j].Summary
if n := cmp.Compare(a.Summary, b.Summary); n != 0 {
return n
}

// Then sort by locations as a tie breaker if summaries are the same.
iLocs := fmt.Sprintf("%v", diags[i].Locations)
jLocs := fmt.Sprintf("%v", diags[j].Locations)
return iLocs < jLocs
return cmp.Compare(fmt.Sprintf("%v", a.Locations), fmt.Sprintf("%v", b.Locations))
})

return diags
Expand Down
12 changes: 5 additions & 7 deletions bundle/config/validate/required.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package validate

import (
"cmp"
"context"
"fmt"
"slices"
"sort"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/internal/validation/generated"
Expand Down Expand Up @@ -69,16 +69,14 @@ func warnForMissingFields(ctx context.Context, b *bundle.Bundle) diag.Diagnostic
}

// Sort diagnostics to make them deterministic
sort.Slice(diags, func(i, j int) bool {
slices.SortFunc(diags, func(a, b diag.Diagnostic) int {
// First sort by summary
if diags[i].Summary != diags[j].Summary {
return diags[i].Summary < diags[j].Summary
if n := cmp.Compare(a.Summary, b.Summary); n != 0 {
return n
}

// Finally sort by locations as a tie breaker if summaries are the same.
iLocs := fmt.Sprintf("%v", diags[i].Locations)
jLocs := fmt.Sprintf("%v", diags[j].Locations)
return iLocs < jLocs
return cmp.Compare(fmt.Sprintf("%v", a.Locations), fmt.Sprintf("%v", b.Locations))
})

return diags
Expand Down
22 changes: 10 additions & 12 deletions bundle/config/validate/unique_resource_keys.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package validate

import (
"cmp"
"context"
"sort"
"slices"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/libs/diag"
Expand Down Expand Up @@ -99,20 +100,17 @@ func (m *uniqueResourceKeys) Apply(ctx context.Context, b *bundle.Bundle) diag.D

// Sort the locations and paths for consistent error messages. This helps
// with unit testing.
sort.Slice(v.locations, func(i, j int) bool {
l1 := v.locations[i]
l2 := v.locations[j]

if l1.File != l2.File {
return l1.File < l2.File
slices.SortFunc(v.locations, func(a, b dyn.Location) int {
if n := cmp.Compare(a.File, b.File); n != 0 {
return n
}
if l1.Line != l2.Line {
return l1.Line < l2.Line
if n := cmp.Compare(a.Line, b.Line); n != 0 {
return n
}
return l1.Column < l2.Column
return cmp.Compare(a.Column, b.Column)
})
sort.Slice(v.paths, func(i, j int) bool {
return v.paths[i].String() < v.paths[j].String()
slices.SortFunc(v.paths, func(a, b dyn.Path) int {
return cmp.Compare(a.String(), b.String())
})

// If there are multiple resources with the same key, report an error.
Expand Down
6 changes: 3 additions & 3 deletions bundle/configsync/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package configsync

import (
"fmt"
"sort"
"slices"
"strings"
)

Expand All @@ -21,7 +21,7 @@ func FormatTextOutput(changes Changes) string {
for key := range changes {
resourceKeys = append(resourceKeys, key)
}
sort.Strings(resourceKeys)
slices.Sort(resourceKeys)

for _, resourceKey := range resourceKeys {
resourceChanges := changes[resourceKey]
Expand All @@ -31,7 +31,7 @@ func FormatTextOutput(changes Changes) string {
for path := range resourceChanges {
paths = append(paths, path)
}
sort.Strings(paths)
slices.Sort(paths)

for _, path := range paths {
configChange := resourceChanges[path]
Expand Down
7 changes: 4 additions & 3 deletions bundle/configsync/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package configsync

import (
"bytes"
"cmp"
"context"
"errors"
"fmt"
"os"
"regexp"
"sort"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -62,8 +63,8 @@ func ApplyChangesToYAML(ctx context.Context, b *bundle.Bundle, fieldChanges []Fi
})
}

sort.Slice(result, func(i, j int) bool {
return result[i].Path < result[j].Path
slices.SortFunc(result, func(a, b FileChange) int {
return cmp.Compare(a.Path, b.Path)
})

return result, nil
Expand Down
29 changes: 15 additions & 14 deletions bundle/configsync/resolve.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package configsync

import (
"cmp"
"context"
"fmt"
"io/fs"
"path/filepath"
"sort"
"slices"
"strings"

"github.com/databricks/cli/bundle"
Expand Down Expand Up @@ -174,7 +175,7 @@ func ResolveChanges(ctx context.Context, b *bundle.Bundle, configChanges Changes
for resourceKey := range configChanges {
resourceKeys = append(resourceKeys, resourceKey)
}
sort.Strings(resourceKeys)
slices.Sort(resourceKeys)

for _, resourceKey := range resourceKeys {
resourceChanges := configChanges[resourceKey]
Expand All @@ -187,25 +188,25 @@ func ResolveChanges(ctx context.Context, b *bundle.Bundle, configChanges Changes
}

// Sort field paths by depth (deeper first), then operation type (removals before adds), then alphabetically
sort.SliceStable(fieldPaths, func(i, j int) bool {
depthI := fieldPathsDepths[fieldPaths[i]]
depthJ := fieldPathsDepths[fieldPaths[j]]
slices.SortStableFunc(fieldPaths, func(a, b string) int {
depthA := fieldPathsDepths[a]
depthB := fieldPathsDepths[b]

if depthI != depthJ {
return depthI > depthJ
if depthA != depthB {
return cmp.Compare(depthB, depthA)
}

opI := resourceChanges[fieldPaths[i]].Operation
opJ := resourceChanges[fieldPaths[j]].Operation
opA := resourceChanges[a].Operation
opB := resourceChanges[b].Operation

if opI == OperationRemove && opJ != OperationRemove {
return true
if opA == OperationRemove && opB != OperationRemove {
return -1
}
if opI != OperationRemove && opJ == OperationRemove {
return false
if opA != OperationRemove && opB == OperationRemove {
return 1
}

return fieldPaths[i] < fieldPaths[j]
return cmp.Compare(a, b)
})

// Create indices map for this resource, path -> indices, that we could use to replace with added elements
Expand Down
23 changes: 13 additions & 10 deletions bundle/deploy/terraform/tfdyn/convert_job.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package tfdyn

import (
"cmp"
"context"
"fmt"
"slices"
"sort"
"strings"

"github.com/databricks/cli/bundle/internal/tf/schema"
Expand Down Expand Up @@ -101,7 +101,7 @@ func patchApplyPolicyDefaultValues(_ dyn.Path, v dyn.Value) (dyn.Value, error) {
}
}

sort.Strings(paths)
slices.Sort(paths)
valList := make([]dyn.Value, len(paths))
for i, s := range paths {
valList[i] = dyn.V(s)
Expand Down Expand Up @@ -132,19 +132,22 @@ func convertJobResource(ctx context.Context, vin dyn.Value) (dyn.Value, error) {
var err error
tasks, ok := vin.Get("tasks").AsSequence()
if ok {
sort.Slice(tasks, func(i, j int) bool {
slices.SortFunc(tasks, func(a, b dyn.Value) int {
// We sort the tasks by their task key. Tasks without task keys are ordered
// before tasks with task keys. We do not error for those tasks
// since presence of a task_key is validated for in the Jobs backend.
tk1, ok := tasks[i].Get("task_key").AsString()
if !ok {
return true
tk1, ok1 := a.Get("task_key").AsString()
tk2, ok2 := b.Get("task_key").AsString()
if !ok1 && ok2 {
return -1
}
tk2, ok := tasks[j].Get("task_key").AsString()
if !ok {
return false
if ok1 && !ok2 {
return 1
}
return tk1 < tk2
if !ok1 && !ok2 {
return 0
}
return cmp.Compare(tk1, tk2)
})
vout, err = dyn.Set(vin, "tasks", dyn.V(tasks))
if err != nil {
Expand Down
Loading
Loading