Skip to content

Commit

Permalink
Merge pull request #39899 from hashicorp/td-sort-pkg-functions
Browse files Browse the repository at this point in the history
Replaces `sort.Slice` and `sort.SliceStable` with `slices.SortFunc` and `slices.SortStableFunc`
  • Loading branch information
gdavison authored Oct 28, 2024
2 parents cda3199 + 9543595 commit 0446658
Show file tree
Hide file tree
Showing 37 changed files with 220 additions and 253 deletions.
12 changes: 12 additions & 0 deletions .ci/semgrep/stdlib/sort.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
rules:
- id: prefer-slices-sortfunc
languages: [go]
message: Prefer slices.SortFunc to sort.Slice
pattern: sort.Slice(...)
severity: WARNING

- id: prefer-slices-sortstablefunc
languages: [go]
message: Prefer slices.SortStableFunc to sort.SliceStable
pattern: sort.SliceStable(...)
severity: WARNING
17 changes: 9 additions & 8 deletions internal/generate/acctestconsts/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
package main

import (
"cmp"
_ "embed"
"sort"
"slices"
"strings"

"github.com/hashicorp/terraform-provider-aws/internal/generate/common"
Expand All @@ -23,14 +24,14 @@ var constOrQuoteTmpl string
//go:embed semgrep.gtpl
var semgrepTmpl string

type ConstantDatum struct {
type constantDatum struct {
Constant string
Literal string
AltLiteral string
}

type TemplateData struct {
Constants []ConstantDatum
Constants []constantDatum
}

func main() {
Expand Down Expand Up @@ -91,21 +92,21 @@ func main() {
}
}

func readConstants(filename string) ([]ConstantDatum, error) {
func readConstants(filename string) ([]constantDatum, error) {
constants, err := common.ReadAllCSVData(filename)

if err != nil {
return nil, err
}

var constantList []ConstantDatum
var constantList []constantDatum

for _, row := range constants {
if row[0] == "" {
continue
}

cd := ConstantDatum{
cd := constantDatum{
Literal: row[0],
Constant: row[1],
}
Expand All @@ -128,8 +129,8 @@ func readConstants(filename string) ([]ConstantDatum, error) {
constantList = append(constantList, cd)
}

sort.SliceStable(constantList, func(i, j int) bool {
return constantList[j].Constant > constantList[i].Constant
slices.SortStableFunc(constantList, func(a, b constantDatum) int {
return cmp.Compare(a.Constant, b.Constant)
})

return constantList, nil
Expand Down
7 changes: 4 additions & 3 deletions internal/generate/allowsubcats/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
package main

import (
"cmp"
_ "embed"
"sort"
"slices"
"strings"

"github.com/hashicorp/terraform-provider-aws/internal/generate/common"
Expand Down Expand Up @@ -55,8 +56,8 @@ func main() {
td.Services = append(td.Services, sd)
}

sort.SliceStable(td.Services, func(i, j int) bool {
return td.Services[i].HumanFriendly < td.Services[j].HumanFriendly
slices.SortStableFunc(td.Services, func(a, b ServiceDatum) int {
return cmp.Compare(a.HumanFriendly, b.HumanFriendly)
})

d := g.NewUnformattedFileDestination(filename)
Expand Down
17 changes: 9 additions & 8 deletions internal/generate/attrconsts/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
package main

import (
"cmp"
_ "embed"
"sort"
"slices"
"strings"

"github.com/hashicorp/terraform-provider-aws/internal/generate/common"
Expand All @@ -27,13 +28,13 @@ var constOrQuoteTmpl string
//go:embed semgrep.gtpl
var semgrepTmpl string

type ConstantDatum struct {
type constantDatum struct {
Constant string
Literal string
}

type TemplateData struct {
Constants []ConstantDatum
Constants []constantDatum
}

func main() {
Expand Down Expand Up @@ -94,28 +95,28 @@ func main() {
}
}

func readConstants(filename string) ([]ConstantDatum, error) {
func readConstants(filename string) ([]constantDatum, error) {
constants, err := common.ReadAllCSVData(filename)

if err != nil {
return nil, err
}

var constantList []ConstantDatum
var constantList []constantDatum

for _, row := range constants {
if row[0] == "" {
continue
}

constantList = append(constantList, ConstantDatum{
constantList = append(constantList, constantDatum{
Literal: row[0],
Constant: row[1],
})
}

sort.SliceStable(constantList, func(i, j int) bool {
return constantList[j].Constant > constantList[i].Constant
slices.SortStableFunc(constantList, func(a, b constantDatum) int {
return cmp.Compare(a.Constant, b.Constant)
})

return constantList, nil
Expand Down
7 changes: 4 additions & 3 deletions internal/generate/awsclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
package main

import (
"cmp"
_ "embed"
"sort"
"slices"

"github.com/hashicorp/terraform-provider-aws/internal/generate/common"
"github.com/hashicorp/terraform-provider-aws/names/data"
Expand Down Expand Up @@ -59,8 +60,8 @@ func main() {
td.Services = append(td.Services, s)
}

sort.SliceStable(td.Services, func(i, j int) bool {
return td.Services[i].ProviderNameUpper < td.Services[j].ProviderNameUpper
slices.SortStableFunc(td.Services, func(a, b ServiceDatum) int {
return cmp.Compare(a.ProviderNameUpper, b.ProviderNameUpper)
})

d := g.NewGoFileDestination(filename)
Expand Down
7 changes: 4 additions & 3 deletions internal/generate/customends/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
package main

import (
"cmp"
_ "embed"
"sort"
"slices"
"strings"

"github.com/hashicorp/terraform-provider-aws/internal/generate/common"
Expand Down Expand Up @@ -72,8 +73,8 @@ func main() {
td.Services = append(td.Services, sd)
}

sort.Slice(td.Services, func(i, j int) bool {
return td.Services[i].ProviderPackage < td.Services[j].ProviderPackage
slices.SortFunc(td.Services, func(a, b serviceDatum) int {
return cmp.Compare(a.ProviderPackage, b.ProviderPackage)
})

d := g.NewUnformattedFileDestination(filename)
Expand Down
7 changes: 4 additions & 3 deletions internal/generate/customendsfwschema/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
package main

import (
"cmp"
_ "embed"
"sort"
"slices"
"strings"

"github.com/hashicorp/terraform-provider-aws/internal/generate/common"
Expand Down Expand Up @@ -56,8 +57,8 @@ func main() {
td.Services = append(td.Services, sd)
}

sort.Slice(td.Services, func(i, j int) bool {
return td.Services[i].ProviderPackage < td.Services[j].ProviderPackage
slices.SortFunc(td.Services, func(a, b serviceDatum) int {
return cmp.Compare(a.ProviderPackage, b.ProviderPackage)
})

d := g.NewGoFileDestination(filename)
Expand Down
7 changes: 4 additions & 3 deletions internal/generate/customendsschema/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
package main

import (
"cmp"
_ "embed"
"sort"
"slices"
"strings"

"github.com/hashicorp/terraform-provider-aws/internal/generate/common"
Expand Down Expand Up @@ -62,8 +63,8 @@ func main() {
td.Services = append(td.Services, sd)
}

sort.Slice(td.Services, func(i, j int) bool {
return td.Services[i].ProviderPackage < td.Services[j].ProviderPackage
slices.SortFunc(td.Services, func(a, b serviceDatum) int {
return cmp.Compare(a.ProviderPackage, b.ProviderPackage)
})

d := g.NewGoFileDestination(filename)
Expand Down
7 changes: 4 additions & 3 deletions internal/generate/issuelabels/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
package main

import (
"cmp"
_ "embed"
"sort"
"slices"
"strings"

"github.com/hashicorp/terraform-provider-aws/internal/generate/common"
Expand Down Expand Up @@ -57,8 +58,8 @@ func main() {
td.Services = append(td.Services, s)
}

sort.SliceStable(td.Services, func(i, j int) bool {
return td.Services[i].ProviderPackage < td.Services[j].ProviderPackage
slices.SortStableFunc(td.Services, func(a, b ServiceDatum) int {
return cmp.Compare(a.ProviderPackage, b.ProviderPackage)
})

d := g.NewUnformattedFileDestination(filename)
Expand Down
24 changes: 13 additions & 11 deletions internal/generate/namescapslist/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
package main

import (
"cmp"
_ "embed"
"fmt"
"sort"
"slices"
"strings"

"github.com/hashicorp/terraform-provider-aws/internal/generate/common"
Expand Down Expand Up @@ -91,11 +92,12 @@ func readBadCaps(filename string) ([]CapsDatum, error) {
})
}

sort.SliceStable(capsList, func(i, j int) bool {
if len(capsList[i].Wrong) == len(capsList[j].Wrong) {
return capsList[i].Wrong < capsList[j].Wrong
}
return len(capsList[j].Wrong) < len(capsList[i].Wrong)
slices.SortStableFunc(capsList, func(a, b CapsDatum) int {
return cmp.Or(
// Reverse length order
cmp.Compare(len(b.Wrong), len(a.Wrong)),
cmp.Compare(a.Wrong, b.Wrong),
)
})

onChunk := -1
Expand All @@ -108,11 +110,11 @@ func readBadCaps(filename string) ([]CapsDatum, error) {
capsList[i].Test = fmt.Sprintf(`%s%d`, "caps", onChunk)
}

sort.SliceStable(capsList, func(i, j int) bool {
if strings.EqualFold(capsList[i].Wrong, capsList[j].Wrong) {
return capsList[i].Wrong < capsList[j].Wrong
}
return strings.ToLower(capsList[i].Wrong) < strings.ToLower(capsList[j].Wrong)
slices.SortStableFunc(capsList, func(a, b CapsDatum) int {
return cmp.Or(
cmp.Compare(strings.ToLower(a.Wrong), strings.ToLower(b.Wrong)),
cmp.Compare(a.Wrong, b.Wrong),
)
})

return capsList, nil
Expand Down
13 changes: 7 additions & 6 deletions internal/generate/namesconsts/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@
package main

import (
"cmp"
_ "embed"
"sort"
"slices"

"github.com/hashicorp/terraform-provider-aws/internal/generate/common"
"github.com/hashicorp/terraform-provider-aws/names/data"
)

type TemplateData struct {
Services []ServiceDatum
Services []serviceDatum
}

type ServiceDatum struct {
type serviceDatum struct {
ProviderPackage string
ProviderNameUpper string
SDKID string
Expand Down Expand Up @@ -49,7 +50,7 @@ func main() {
continue
}

sd := ServiceDatum{
sd := serviceDatum{
ProviderPackage: l.ProviderPackage(),
ProviderNameUpper: l.ProviderNameUpper(),
SDKID: l.SDKID(),
Expand All @@ -58,8 +59,8 @@ func main() {
td.Services = append(td.Services, sd)
}

sort.Slice(td.Services, func(i, j int) bool {
return td.Services[i].ProviderNameUpper < td.Services[j].ProviderNameUpper
slices.SortFunc(td.Services, func(a, b serviceDatum) int {
return cmp.Compare(a.ProviderNameUpper, b.ProviderNameUpper)
})

d := g.NewGoFileDestination(filename)
Expand Down
7 changes: 4 additions & 3 deletions internal/generate/prlabels/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
package main

import (
"cmp"
_ "embed"
"sort"
"slices"
"strings"

"github.com/hashicorp/terraform-provider-aws/internal/generate/common"
Expand Down Expand Up @@ -65,8 +66,8 @@ func main() {
td.Services = append(td.Services, s)
}

sort.SliceStable(td.Services, func(i, j int) bool {
return td.Services[i].ProviderPackage < td.Services[j].ProviderPackage
slices.SortStableFunc(td.Services, func(a, b ServiceDatum) int {
return cmp.Compare(a.ProviderPackage, b.ProviderPackage)
})

d := g.NewUnformattedFileDestination(filename)
Expand Down
Loading

0 comments on commit 0446658

Please sign in to comment.