Skip to content

Commit 6af8200

Browse files
committed
Use 'names.ToSnakeCase'.
1 parent d956827 commit 6af8200

File tree

7 files changed

+15
-68
lines changed

7 files changed

+15
-68
lines changed

skaff/convert/convert.go

-16
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,6 @@ import (
1111
"github.com/YakDriver/regexache"
1212
)
1313

14-
// ToSnakeCase converts a camel cased string to snake case
15-
//
16-
// If the override argument is a non-empty string, its value is returned
17-
// unchanged.
18-
func ToSnakeCase(upper string, override string) string {
19-
if override != "" {
20-
return override
21-
}
22-
23-
re := regexache.MustCompile(`([a-z])([A-Z]{2,})`)
24-
upper = re.ReplaceAllString(upper, `${1}_${2}`)
25-
26-
re2 := regexache.MustCompile(`([A-Z][a-z])`)
27-
return strings.TrimPrefix(strings.ToLower(re2.ReplaceAllString(upper, `_$1`)), "_")
28-
}
29-
3014
// ToHumanResName converts a camel cased string to a human readable name
3115
func ToHumanResName(upper string) string {
3216
re := regexache.MustCompile(`([a-z])([A-Z]{2,})`)

skaff/convert/convert_test.go

-49
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,6 @@ import (
77
"testing"
88
)
99

10-
func TestToSnakeName(t *testing.T) {
11-
testCases := []struct {
12-
TestName string
13-
Input string
14-
Expected string
15-
}{
16-
{
17-
TestName: "empty",
18-
Input: "",
19-
Expected: "",
20-
},
21-
{
22-
TestName: "simple",
23-
Input: "Cheese",
24-
Expected: "cheese",
25-
},
26-
{
27-
TestName: "two word",
28-
Input: "CityLights",
29-
Expected: "city_lights",
30-
},
31-
{
32-
TestName: "three word",
33-
Input: "OpenEndResource",
34-
Expected: "open_end_resource",
35-
},
36-
{
37-
TestName: "initialism",
38-
Input: "DBInstance",
39-
Expected: "db_instance",
40-
},
41-
{
42-
TestName: "initialisms",
43-
Input: "DBInstanceVPCEndpoint",
44-
Expected: "db_instance_vpc_endpoint",
45-
},
46-
}
47-
48-
for _, testCase := range testCases {
49-
t.Run(testCase.TestName, func(t *testing.T) {
50-
got := ToSnakeCase(testCase.Input, "")
51-
52-
if got != testCase.Expected {
53-
t.Errorf("got %s, expected %s", got, testCase.Expected)
54-
}
55-
})
56-
}
57-
}
58-
5910
func TestToHumanResName(t *testing.T) {
6011
testCases := []struct {
6112
TestName string

skaff/datasource/datasource.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strings"
1515
"text/template"
1616

17+
"github.com/hashicorp/terraform-provider-aws/names"
1718
"github.com/hashicorp/terraform-provider-aws/names/data"
1819
"github.com/hashicorp/terraform-provider-aws/skaff/convert"
1920
)
@@ -67,7 +68,9 @@ func Create(dsName, snakeName string, comments, force, pluginFramework, tags boo
6768
return fmt.Errorf("error checking: snake name should be all lower case with underscores, if needed (e.g., db_instance)")
6869
}
6970

70-
snakeName = convert.ToSnakeCase(dsName, snakeName)
71+
if snakeName != "" {
72+
snakeName = names.ToSnakeCase(dsName)
73+
}
7174

7275
service, err := data.LookupService(servicePackage)
7376
if err != nil {

skaff/function/function.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strings"
1515
"text/template"
1616

17+
"github.com/hashicorp/terraform-provider-aws/names"
1718
"github.com/hashicorp/terraform-provider-aws/skaff/convert"
1819
)
1920

@@ -43,7 +44,9 @@ func Create(name, snakeName, description string, comments, force bool) error {
4344
return fmt.Errorf("snake name should be all lower case with underscores, if needed (e.g., arn_build)")
4445
}
4546

46-
snakeName = convert.ToSnakeCase(name, snakeName)
47+
if snakeName != "" {
48+
snakeName = names.ToSnakeCase(name)
49+
}
4750

4851
templateData := TemplateData{
4952
Function: name,

skaff/go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
1414
github.com/google/go-cmp v0.6.0 // indirect
1515
github.com/google/uuid v1.6.0 // indirect
16+
github.com/hashicorp/aws-sdk-go-base/v2 v2.0.0-beta.59 // indirect
1617
github.com/hashicorp/hcl/v2 v2.22.0 // indirect
1718
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1819
github.com/mitchellh/go-wordwrap v1.0.1 // indirect

skaff/go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
1313
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
1414
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
1515
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
16+
github.com/hashicorp/aws-sdk-go-base/v2 v2.0.0-beta.59 h1:j5ZrYJbLfZLJ9X5Bnp43z+ygN7kf6rbLCGIBGCIWWEA=
17+
github.com/hashicorp/aws-sdk-go-base/v2 v2.0.0-beta.59/go.mod h1:jZEzCETkIzEinF749zPXsqpPmsA9P0fbMqRyoBh5UNo=
1618
github.com/hashicorp/hcl/v2 v2.22.0 h1:hkZ3nCtqeJsDhPRFz5EA9iwcG1hNWGePOTw6oyul12M=
1719
github.com/hashicorp/hcl/v2 v2.22.0/go.mod h1:62ZYHrXgPoX8xBnzl8QzbWq4dyDsDtfCRgIq1rbJEvA=
1820
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=

skaff/resource/resource.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strings"
1515
"text/template"
1616

17+
"github.com/hashicorp/terraform-provider-aws/names"
1718
"github.com/hashicorp/terraform-provider-aws/names/data"
1819
"github.com/hashicorp/terraform-provider-aws/skaff/convert"
1920
)
@@ -67,7 +68,9 @@ func Create(resName, snakeName string, comments, force, pluginFramework, tags bo
6768
return fmt.Errorf("error checking: snake name should be all lower case with underscores, if needed (e.g., db_instance)")
6869
}
6970

70-
snakeName = convert.ToSnakeCase(resName, snakeName)
71+
if snakeName != "" {
72+
snakeName = names.ToSnakeCase(resName)
73+
}
7174

7275
service, err := data.LookupService(servicePackage)
7376
if err != nil {

0 commit comments

Comments
 (0)