Skip to content

Commit d17d6ac

Browse files
authoredDec 7, 2017
private/model/api: Fix SDK service client name gen for lowercase names (aws#1684)
* private/model/api: Fix SDK service client name gen for lowercase names Fixes the SDK's code generation for services with lowercase names in the abbreviation field of the model. This also fixes Title casing bugs in the StructName method. * only strip off the first occurrence of a prefix
1 parent 0dd2bf5 commit d17d6ac

File tree

3 files changed

+98
-50
lines changed

3 files changed

+98
-50
lines changed
 

‎models/customizations/service-aliases.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"costandusagereportservice": "CostandUsageReportService",
23
"elasticloadbalancing": "ELB",
34
"elasticloadbalancingv2": "ELBV2",
45
"config": "ConfigService"

‎private/model/api/api.go

+41-11
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import (
1010
"io/ioutil"
1111
"path"
1212
"path/filepath"
13-
"regexp"
1413
"sort"
1514
"strings"
1615
"text/template"
16+
"unicode"
1717
)
1818

1919
// An API defines a service API's definition. and logic to serialize the definition.
@@ -94,23 +94,53 @@ func (a *API) InterfacePackageName() string {
9494
return a.PackageName() + "iface"
9595
}
9696

97-
var nameRegex = regexp.MustCompile(`^Amazon|AWS\s*|\(.*|\s+|\W+`)
97+
var stripServiceNamePrefixes = []string{
98+
"Amazon",
99+
"AWS",
100+
}
98101

99102
// StructName returns the struct name for a given API.
100103
func (a *API) StructName() string {
101-
if a.name == "" {
102-
name := a.Metadata.ServiceAbbreviation
103-
if name == "" {
104-
name = a.Metadata.ServiceFullName
105-
}
104+
if len(a.name) != 0 {
105+
return a.name
106+
}
106107

107-
name = nameRegex.ReplaceAllString(name, "")
108+
name := a.Metadata.ServiceAbbreviation
109+
if len(name) == 0 {
110+
name = a.Metadata.ServiceFullName
111+
}
112+
113+
name = strings.TrimSpace(name)
114+
115+
// Strip out prefix names not reflected in service client symbol names.
116+
for _, prefix := range stripServiceNamePrefixes {
117+
if strings.HasPrefix(name, prefix) {
118+
name = name[len(prefix):]
119+
break
120+
}
121+
}
108122

109-
a.name = name
110-
if name, ok := serviceAliases[strings.ToLower(name)]; ok {
111-
a.name = name
123+
// Replace all Non-letter/number values with space
124+
runes := []rune(name)
125+
for i := 0; i < len(runes); i++ {
126+
if r := runes[i]; !(unicode.IsNumber(r) || unicode.IsLetter(r)) {
127+
runes[i] = ' '
112128
}
113129
}
130+
name = string(runes)
131+
132+
// Title case name so its readable as a symbol.
133+
name = strings.Title(name)
134+
135+
// Strip out spaces.
136+
name = strings.Replace(name, " ", "", -1)
137+
138+
// Swap out for alias name if one is defined.
139+
if alias, ok := serviceAliases[strings.ToLower(name)]; ok {
140+
name = alias
141+
}
142+
143+
a.name = name
114144
return a.name
115145
}
116146

‎private/model/api/api_test.go

+56-39
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,71 @@
1-
// +build 1.6,codegen
1+
// +build go1.8,codegen
22

33
package api
44

55
import (
66
"testing"
77
)
88

9-
func TestStructNameWithFullName(t *testing.T) {
10-
a := API{
11-
Metadata: Metadata{
12-
ServiceFullName: "Amazon Service Name-100",
13-
},
14-
}
15-
if a.StructName() != "ServiceName100" {
16-
t.Errorf("API struct name should have been %s, but received %s", "ServiceName100", a.StructName())
17-
}
18-
}
9+
func TestAPI_StructName(t *testing.T) {
10+
origAliases := serviceAliases
11+
defer func() { serviceAliases = origAliases }()
1912

20-
func TestStructNameWithAbbreviation(t *testing.T) {
21-
a := API{
22-
Metadata: Metadata{
23-
ServiceFullName: "AWS Service Name-100",
24-
ServiceAbbreviation: "AWS SN100",
13+
cases := map[string]struct {
14+
Aliases map[string]string
15+
Metadata Metadata
16+
StructName string
17+
}{
18+
"FullName": {
19+
Metadata: Metadata{
20+
ServiceFullName: "Amazon Service Name-100",
21+
},
22+
StructName: "ServiceName100",
23+
},
24+
"Abbreviation": {
25+
Metadata: Metadata{
26+
ServiceFullName: "Amazon Service Name-100",
27+
ServiceAbbreviation: "AWS SN100",
28+
},
29+
StructName: "SN100",
30+
},
31+
"Lowercase Name": {
32+
Metadata: Metadata{
33+
EndpointPrefix: "other",
34+
ServiceFullName: "AWS Lowercase service",
35+
ServiceAbbreviation: "lowercase",
36+
},
37+
StructName: "Lowercase",
38+
},
39+
"Lowercase Name Mixed": {
40+
Metadata: Metadata{
41+
EndpointPrefix: "other",
42+
ServiceFullName: "AWS Lowercase service",
43+
ServiceAbbreviation: "lowercase name Goes heRe",
44+
},
45+
StructName: "LowercaseNameGoesHeRe",
46+
},
47+
"Alias": {
48+
Aliases: map[string]string{
49+
"elasticloadbalancing": "ELB",
50+
},
51+
Metadata: Metadata{
52+
ServiceFullName: "Elastic Load Balancing",
53+
},
54+
StructName: "ELB",
2555
},
2656
}
27-
if a.StructName() != "SN100" {
28-
t.Errorf("API struct name should have been %s, but received %s", "SN100", a.StructName())
29-
}
30-
}
3157

32-
func TestStructNameForExceptions(t *testing.T) {
33-
serviceAliases = map[string]string{}
34-
serviceAliases["elasticloadbalancing"] = "ELB"
35-
serviceAliases["config"] = "ConfigService"
58+
for k, c := range cases {
59+
t.Run(k, func(t *testing.T) {
60+
serviceAliases = c.Aliases
3661

37-
a := API{
38-
Metadata: Metadata{
39-
ServiceFullName: "Elastic Load Balancing",
40-
},
41-
}
42-
if a.StructName() != "ELB" {
43-
t.Errorf("API struct name should have been %s, but received %s", "ELB", a.StructName())
44-
}
62+
a := API{
63+
Metadata: c.Metadata,
64+
}
4565

46-
a = API{
47-
Metadata: Metadata{
48-
ServiceFullName: "AWS Config",
49-
},
50-
}
51-
if a.StructName() != "ConfigService" {
52-
t.Errorf("API struct name should have been %s, but received %s", "ConfigService", a.StructName())
66+
if e, o := c.StructName, a.StructName(); e != o {
67+
t.Errorf("expect %v structName, got %v", e, o)
68+
}
69+
})
5370
}
5471
}

0 commit comments

Comments
 (0)
Please sign in to comment.