Skip to content

Commit 2074485

Browse files
authored
chore: apply fixes from Go modernize command (#1312)
The modernize command replaces old constructs with simpler, updated ones. Command is: go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...
1 parent d834c7a commit 2074485

File tree

21 files changed

+60
-89
lines changed

21 files changed

+60
-89
lines changed

cmd/purge_binding.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"fmt"
55
"log"
6+
"slices"
67

78
"github.com/spf13/cobra"
89

@@ -72,14 +73,12 @@ func purgeServiceBinding(serviceInstanceGUID, serviceBindingGUID string) {
7273
if err != nil {
7374
log.Fatalf("error listing bindings: %s", err)
7475
}
75-
for _, bindingGUID := range bindings {
76-
if bindingGUID == serviceBindingGUID {
77-
if err := deleteServiceBindingFromStore(store, serviceInstanceGUID, serviceBindingGUID); err != nil {
78-
log.Fatalf("error deleting binding %q for service instance %q: %s", serviceBindingGUID, serviceInstanceGUID, err)
79-
}
80-
log.Printf("deleted binding %q for service instance %q from the Cloud Service Broker database", serviceBindingGUID, serviceInstanceGUID)
81-
return
76+
if slices.Contains(bindings, serviceBindingGUID) {
77+
if err := deleteServiceBindingFromStore(store, serviceInstanceGUID, serviceBindingGUID); err != nil {
78+
log.Fatalf("error deleting binding %q for service instance %q: %s", serviceBindingGUID, serviceInstanceGUID, err)
8279
}
80+
log.Printf("deleted binding %q for service instance %q from the Cloud Service Broker database", serviceBindingGUID, serviceInstanceGUID)
81+
return
8382
}
8483

8584
log.Fatalf("could not find service binding %q for service instance %q", serviceBindingGUID, serviceInstanceGUID)

internal/brokerpak/fetcher/fetch.go

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

55
import (
66
"fmt"
7+
"maps"
78
"os"
89
"path/filepath"
910

@@ -40,9 +41,7 @@ func newFileGetterClient(src, dest string) *getter.Client {
4041

4142
func defaultGetters() map[string]getter.Getter {
4243
getters := map[string]getter.Getter{}
43-
for k, g := range getter.Getters {
44-
getters[k] = g
45-
}
44+
maps.Copy(getters, getter.Getters)
4645

4746
return getters
4847
}

internal/local/local.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func nameToID(name string) string {
1717
name = name + "."
1818
}
1919

20-
h := []byte(fmt.Sprintf("%x", name))
20+
h := fmt.Appendf(nil, "%x", name)
2121
return fmt.Sprintf("%s-%s-%s-%s-%s", h[0:8], h[8:12], h[12:16], h[16:20], h[20:32])
2222
}
2323

internal/storage/terraform_deployment_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,11 @@ func addFakeTerraformDeployments() {
280280
func fakeWorkspace(name, ver string) []byte {
281281
state := "null"
282282
if ver != "" {
283-
state = fmt.Sprintf("%q", base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(`{"terraform_version":"%s"}`, ver))))
283+
state = fmt.Sprintf("%q", base64.StdEncoding.EncodeToString(fmt.Appendf(nil, `{"terraform_version":"%s"}`, ver)))
284284
}
285-
return []byte(fmt.Sprintf(`{"modules":[{"Name":"%s","Definition":"","Definitions":null}],"instances":null,"tfstate":%s,"transform":{"parameter_mappings":null,"parameters_to_remove":null,"parameters_to_add":null}}`, name, state))
285+
return fmt.Appendf(nil, `{"modules":[{"Name":"%s","Definition":"","Definitions":null}],"instances":null,"tfstate":%s,"transform":{"parameter_mappings":null,"parameters_to_remove":null,"parameters_to_add":null}}`, name, state)
286286
}
287287

288288
func fakeEncryptedWorkspace(name, ver string) []byte {
289-
return []byte(fmt.Sprintf(`{"encrypted":{"decrypted":%s}}`, fakeWorkspace(name, ver)))
289+
return fmt.Appendf(nil, `{"encrypted":{"decrypted":%s}}`, fakeWorkspace(name, ver))
290290
}

internal/zippy/extract.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"archive/zip"
55
"fmt"
66
"path/filepath"
7+
"slices"
78
"strings"
89

910
"github.com/cloudfoundry/cloud-service-broker/v2/utils/stream"
@@ -82,12 +83,7 @@ func containsDotDot(v string) bool {
8283
if !strings.Contains(v, "..") {
8384
return false
8485
}
85-
for _, ent := range strings.FieldsFunc(v, isSlashRune) {
86-
if ent == ".." {
87-
return true
88-
}
89-
}
90-
return false
86+
return slices.Contains(strings.FieldsFunc(v, isSlashRune), "..")
9187
}
9288

9389
func isSlashRune(r rune) bool { return r == '/' || r == '\\' }

pkg/broker/service_definition.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package broker
1717
import (
1818
"encoding/json"
1919
"fmt"
20+
"maps"
2021
"os"
2122
"strings"
2223

@@ -196,7 +197,7 @@ func unmarshalViperMap(key string) (map[string]any, error) {
196197
if err := json.Unmarshal([]byte(v), &vals); err != nil {
197198
return nil, fmt.Errorf("failed unmarshaling config value %s", key)
198199
}
199-
case map[string]interface{}:
200+
case map[string]any:
200201
vals = v
201202
}
202203
}
@@ -530,15 +531,11 @@ func (svc *ServiceDefinition) BindVariables(instance storage.ServiceInstanceDeta
530531
func (svc *ServiceDefinition) combineLabels(defaultLabels map[string]string) map[string]string {
531532
ll := map[string]string{}
532533

533-
for key, value := range svc.GlobalLabels {
534-
ll[key] = value
535-
}
534+
maps.Copy(ll, svc.GlobalLabels)
536535

537536
// default labels override global labels
538537
// in other words, "pcf-organization-guid", "pcf-space-guid", and "pcf-instance-id" have preference over global labels
539-
for key, value := range defaultLabels {
540-
ll[key] = value
541-
}
538+
maps.Copy(ll, defaultLabels)
542539

543540
return ll
544541
}

pkg/broker/service_definition_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ var _ = Describe("ServiceDefinition", func() {
414414
When("a plan is provided in configuration as an object", func() {
415415
BeforeEach(func() {
416416
// fakePlanName, fakePlanID, fakePlanDescription, fakePlanProperty
417-
fakeServicePlanConfigObject := []map[string]interface{}{
417+
fakeServicePlanConfigObject := []map[string]any{
418418
{
419419
"name": fakePlanName,
420420
"id": fakePlanID,
@@ -428,7 +428,7 @@ var _ = Describe("ServiceDefinition", func() {
428428
"additional_property": fmt.Sprintf("second-%s", fakePlanProperty),
429429
},
430430
}
431-
viper.Set("service.fake-service.provision.defaults", map[string]interface{}{
431+
viper.Set("service.fake-service.provision.defaults", map[string]any{
432432
"test": "value",
433433
})
434434
viper.Set("service.fake-service.plans", fakeServicePlanConfigObject)
@@ -439,13 +439,13 @@ var _ = Describe("ServiceDefinition", func() {
439439
Expect(plan).To(Not(HaveLen(0)))
440440
provisionOverrides, err := service.ProvisionDefaultOverrides()
441441
Expect(err).To(Not(HaveOccurred()))
442-
Expect(provisionOverrides).To(Equal(map[string]interface{}{"test": `value`}))
442+
Expect(provisionOverrides).To(Equal(map[string]any{"test": `value`}))
443443
})
444444
})
445445

446446
When("a plan with provision defaults is provided in configuration as a string", func() {
447447
BeforeEach(func() {
448-
fakeServicePlanConfigObject := []map[string]interface{}{
448+
fakeServicePlanConfigObject := []map[string]any{
449449
{
450450
"name": fakePlanName,
451451
"id": fakePlanID,

pkg/broker/variables.go

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

1717
import (
1818
"fmt"
19+
"maps"
1920
"sort"
2021
"strings"
2122
"unicode"
@@ -99,9 +100,7 @@ func (bv *BrokerVariable) ToSchema() map[string]any {
99100
schema[validation.KeyTitle] = fieldNameToLabel(bv.FieldName)
100101
}
101102

102-
for k, v := range bv.Constraints {
103-
schema[k] = v
104-
}
103+
maps.Copy(schema, bv.Constraints)
105104

106105
if len(bv.Enum) > 0 {
107106
enumeration := []any{}

pkg/client/example_runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func runExamples(workers int, client *Client, examples []CompleteServiceExample)
7373
queue := make(chan work)
7474
var wg sync.WaitGroup
7575
wg.Add(workers)
76-
for i := 0; i < workers; i++ {
76+
for range workers {
7777
go func() {
7878
for w := range queue {
7979
start := time.Now()

pkg/providers/tf/executor/executor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (defaultExecutor) Execute(ctx context.Context, c *exec.Cmd) (ExecutionOutpu
9090

9191
func flatten(input []byte) string {
9292
var lines []string
93-
for _, l := range strings.Split(string(input), "\n") {
93+
for l := range strings.SplitSeq(string(input), "\n") {
9494
if line := strings.TrimSpace(l); len(line) > 0 {
9595
lines = append(lines, line)
9696
}

0 commit comments

Comments
 (0)