Skip to content

Commit 0e45f8d

Browse files
authored
use cleaned name for pluralized resources in cfg (#263)
Cleans up the `pkg/model:GetOpTypeAndResourceNameFromOpID()` function so that resources with pluralized singular names can be referred to with the "cleaned" Camel-cased name in the generator.yaml file. In addition, corrects an error in said function where the operation returning a singular pluralized resource -- e.g. DescribeDhcpOptions -- was incorrectly being returned as OpTypeList instead of OpTypeGet. Signed-off-by: Jay Pipes <[email protected]> By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 3e0ecb9 commit 0e45f8d

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

pkg/model/op.go

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ const (
4343

4444
type OperationMap map[OpType]map[string]*awssdkmodel.Operation
4545

46+
// resourceExistsInConfig returns true if the supplied resource name exists in
47+
// the supplied Config object's Resources collection, case-insensitive
48+
// matching.
49+
func resourceExistsInConfig(
50+
subject string,
51+
cfg *ackgenconfig.Config,
52+
) bool {
53+
for resName := range cfg.Resources {
54+
if strings.EqualFold(resName, subject) {
55+
return true
56+
}
57+
}
58+
return false
59+
}
60+
4661
// GetOpTypeAndResourceNameFromOpID guesses the resource name and type of
4762
// operation from the OperationID
4863
func GetOpTypeAndResourceNameFromOpID(
@@ -58,17 +73,19 @@ func GetOpTypeAndResourceNameFromOpID(
5873
// Do not singularize "pluralized singular" resources
5974
// like EC2's DhcpOptions, if defined in generator config's list of
6075
// resources.
61-
if _, exists := cfg.Resources[resName]; !exists {
62-
return OpTypeCreateBatch, pluralize.Singular(resName)
76+
if resourceExistsInConfig(resName, cfg) {
77+
return OpTypeCreateBatch, resName
6378
}
79+
return OpTypeCreateBatch, pluralize.Singular(resName)
6480
}
6581
return OpTypeCreateBatch, resName
6682
} else if strings.HasPrefix(opID, "CreateBatch") {
6783
resName := strings.TrimPrefix(opID, "CreateBatch")
6884
if pluralize.IsPlural(resName) {
69-
if _, exists := cfg.Resources[resName]; !exists {
70-
return OpTypeCreateBatch, pluralize.Singular(resName)
85+
if resourceExistsInConfig(resName, cfg) {
86+
return OpTypeCreateBatch, resName
7187
}
88+
return OpTypeCreateBatch, pluralize.Singular(resName)
7289
}
7390
return OpTypeCreateBatch, resName
7491
} else if strings.HasPrefix(opID, "Create") {
@@ -78,9 +95,10 @@ func GetOpTypeAndResourceNameFromOpID(
7895
// resources, then just return OpTypeCreate and the resource name.
7996
// This handles "pluralized singular" resource names like EC2's
8097
// DhcpOptions.
81-
if _, exists := cfg.Resources[resName]; !exists {
82-
return OpTypeCreateBatch, pluralize.Singular(resName)
98+
if resourceExistsInConfig(resName, cfg) {
99+
return OpTypeCreate, resName
83100
}
101+
return OpTypeCreateBatch, pluralize.Singular(resName)
84102
}
85103
return OpTypeCreate, resName
86104
} else if strings.HasPrefix(opID, "Modify") {
@@ -92,10 +110,10 @@ func GetOpTypeAndResourceNameFromOpID(
92110
} else if strings.HasPrefix(opID, "Describe") {
93111
resName := strings.TrimPrefix(opID, "Describe")
94112
if pluralize.IsPlural(resName) {
95-
if _, exists := cfg.Resources[resName]; !exists {
96-
return OpTypeList, pluralize.Singular(resName)
113+
if resourceExistsInConfig(resName, cfg) {
114+
return OpTypeGet, resName
97115
}
98-
return OpTypeList, resName
116+
return OpTypeList, pluralize.Singular(resName)
99117
}
100118
return OpTypeGet, resName
101119
} else if strings.HasPrefix(opID, "Get") {
@@ -106,18 +124,19 @@ func GetOpTypeAndResourceNameFromOpID(
106124
}
107125
resName := strings.TrimPrefix(opID, "Get")
108126
if pluralize.IsPlural(resName) {
109-
if _, exists := cfg.Resources[resName]; !exists {
110-
return OpTypeList, pluralize.Singular(resName)
127+
if resourceExistsInConfig(resName, cfg) {
128+
return OpTypeGet, resName
111129
}
112-
return OpTypeList, resName
130+
return OpTypeList, pluralize.Singular(resName)
113131
}
114132
return OpTypeGet, resName
115133
} else if strings.HasPrefix(opID, "List") {
116134
resName := strings.TrimPrefix(opID, "List")
117135
if pluralize.IsPlural(resName) {
118-
if _, exists := cfg.Resources[resName]; !exists {
119-
return OpTypeList, pluralize.Singular(resName)
136+
if resourceExistsInConfig(resName, cfg) {
137+
return OpTypeList, resName
120138
}
139+
return OpTypeList, pluralize.Singular(resName)
121140
}
122141
return OpTypeList, resName
123142
} else if strings.HasPrefix(opID, "Set") {

pkg/model/op_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func TestGetOpTypeAndResourceNameFromOpID_PluralSingular(t *testing.T) {
144144
},
145145
{
146146
"DescribeDhcpOptions",
147-
model.OpTypeList,
147+
model.OpTypeGet,
148148
"DhcpOptions",
149149
},
150150
{

pkg/testdata/models/apis/ec2/0000-00-00/generator.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ operations:
6868
output_wrapper_field_path: VpcEndpoint
6969

7070
resources:
71-
DhcpOptions:
71+
DHCPOptions:
7272

7373
SecurityGroup:
7474
renames:
@@ -85,4 +85,4 @@ resources:
8585
DescribeSecurityGroups:
8686
input_fields:
8787
GroupIds: Ids
88-
GroupNames: Names
88+
GroupNames: Names

0 commit comments

Comments
 (0)