Skip to content

Commit d20cb4f

Browse files
authored
Refactor setSDKReadMany and setResourceForScalar (#177)
Issue #, if available: [#922](aws-controllers-k8s/community#922) Description of changes: * Refactor `setSDKReadMany`: * use *FindPluralizedIdentifiersInShape* helpers to simplify logic * Refactor `setResourceForScalar`: * Remove unused params * Add `targetVar` param to be the variable's fully-qualified name (i.e. fieldPath+varName r.ko.Spec.CacheClusterID) By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 709a1e1 commit d20cb4f

File tree

2 files changed

+40
-63
lines changed

2 files changed

+40
-63
lines changed

pkg/generate/code/set_resource.go

+31-41
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ func SetResource(
258258
out += fmt.Sprintf(
259259
"%sif %s != nil {\n", indent, sourceAdaptedVarName,
260260
)
261+
qualifiedTargetVar := fmt.Sprintf(
262+
"%s.%s", targetAdaptedVarName, f.Names.Camel,
263+
)
261264

262265
switch sourceMemberShape.Type {
263266
case "list", "structure", "map":
@@ -279,19 +282,15 @@ func SetResource(
279282
indentLevel+1,
280283
)
281284
out += setResourceForScalar(
282-
cfg, r,
283-
f.Names.Camel,
284-
targetAdaptedVarName,
285+
qualifiedTargetVar,
285286
memberVarName,
286287
sourceMemberShapeRef,
287288
indentLevel+1,
288289
)
289290
}
290291
default:
291292
out += setResourceForScalar(
292-
cfg, r,
293-
f.Names.Camel,
294-
targetAdaptedVarName,
293+
qualifiedTargetVar,
295294
sourceAdaptedVarName,
296295
sourceMemberShapeRef,
297296
indentLevel+1,
@@ -514,6 +513,11 @@ func setResourceReadMany(
514513
out += fmt.Sprintf(
515514
"%s\tif %s != nil {\n", indent, sourceAdaptedVarName,
516515
)
516+
517+
//ex: r.ko.Spec.CacheClusterID
518+
qualifiedTargetVar := fmt.Sprintf(
519+
"%s.%s", targetAdaptedVarName, f.Names.Camel,
520+
)
517521
switch sourceMemberShape.Type {
518522
case "list", "structure", "map":
519523
{
@@ -534,9 +538,7 @@ func setResourceReadMany(
534538
indentLevel+2,
535539
)
536540
out += setResourceForScalar(
537-
cfg, r,
538-
f.Names.Camel,
539-
targetAdaptedVarName,
541+
qualifiedTargetVar,
540542
memberVarName,
541543
sourceMemberShapeRef,
542544
indentLevel+2,
@@ -574,9 +576,7 @@ func setResourceReadMany(
574576
}
575577
// r.ko.Spec.CacheClusterID = elem.CacheClusterId
576578
out += setResourceForScalar(
577-
cfg, r,
578-
f.Names.Camel,
579-
targetAdaptedVarName,
579+
qualifiedTargetVar,
580580
sourceAdaptedVarName,
581581
sourceMemberShapeRef,
582582
indentLevel+2,
@@ -1008,10 +1008,10 @@ func setResourceIdentifierPrimaryIdentifier(
10081008
indentLevel int,
10091009
) string {
10101010
adaptedMemberPath := fmt.Sprintf("&%s.NameOrID", sourceVarName)
1011+
qualifiedTargetVar := fmt.Sprintf("%s.%s", targetVarName, targetField.Path)
1012+
10111013
return setResourceForScalar(
1012-
cfg, r,
1013-
targetField.Path,
1014-
targetVarName,
1014+
qualifiedTargetVar,
10151015
adaptedMemberPath,
10161016
targetField.ShapeRef,
10171017
indentLevel,
@@ -1052,10 +1052,9 @@ func setResourceIdentifierAdditionalKey(
10521052
// throwing an error accessible to the user
10531053
additionalKeyOut += fmt.Sprintf("%s%s, %sok := %s\n", indent, fieldIndexName, fieldIndexName, sourceAdaptedVarName)
10541054
additionalKeyOut += fmt.Sprintf("%sif %sok {\n", indent, fieldIndexName)
1055+
qualifiedTargetVar := fmt.Sprintf("%s.%s", targetVarName, targetField.Path)
10551056
additionalKeyOut += setResourceForScalar(
1056-
cfg, r,
1057-
targetField.Path,
1058-
targetVarName,
1057+
qualifiedTargetVar,
10591058
fmt.Sprintf("&%s", fieldIndexName),
10601059
targetField.ShapeRef,
10611060
indentLevel+1,
@@ -1117,9 +1116,7 @@ func setResourceForContainer(
11171116
)
11181117
default:
11191118
return setResourceForScalar(
1120-
cfg, r,
1121-
targetFieldName,
1122-
targetVarName,
1119+
fmt.Sprintf("%s.%s", targetFieldName, targetVarName),
11231120
sourceVarName,
11241121
sourceShapeRef,
11251122
indentLevel,
@@ -1162,6 +1159,9 @@ func SetResourceForStruct(
11621159
out += fmt.Sprintf(
11631160
"%sif %s != nil {\n", indent, sourceAdaptedVarName,
11641161
)
1162+
qualifiedTargetVar := fmt.Sprintf(
1163+
"%s.%s", targetVarName, cleanNames.Camel,
1164+
)
11651165
switch memberShape.Type {
11661166
case "list", "structure", "map":
11671167
{
@@ -1181,19 +1181,15 @@ func SetResourceForStruct(
11811181
indentLevel+1,
11821182
)
11831183
out += setResourceForScalar(
1184-
cfg, r,
1185-
cleanNames.Camel,
1186-
targetVarName,
1184+
qualifiedTargetVar,
11871185
memberVarName,
11881186
memberShapeRef,
11891187
indentLevel+1,
11901188
)
11911189
}
11921190
default:
11931191
out += setResourceForScalar(
1194-
cfg, r,
1195-
cleanNames.Camel,
1196-
targetVarName,
1192+
qualifiedTargetVar,
11971193
sourceAdaptedVarName,
11981194
memberShapeRef,
11991195
indentLevel+1,
@@ -1334,30 +1330,24 @@ func setResourceForMap(
13341330
// value to a source variable when the type of the source variable is a scalar
13351331
// type (not a map, slice or struct).
13361332
func setResourceForScalar(
1337-
cfg *ackgenconfig.Config,
1338-
r *model.CRD,
1339-
// The name of the Input SDK Shape member we're outputting for
1340-
targetFieldName string,
1341-
// The variable name that we want to set a value to
1342-
targetVarName string,
1333+
// The fully-qualified variable that will be set to sourceVar
1334+
targetVar string,
13431335
// The struct or struct field that we access our source value from
1344-
sourceVarName string,
1336+
sourceVar string,
13451337
shapeRef *awssdkmodel.ShapeRef,
13461338
indentLevel int,
13471339
) string {
13481340
out := ""
13491341
indent := strings.Repeat("\t", indentLevel)
1350-
setTo := sourceVarName
1342+
setTo := sourceVar
13511343
shape := shapeRef.Shape
13521344
if shape.Type == "timestamp" {
1353-
setTo = "&metav1.Time{*" + sourceVarName + "}"
1345+
setTo = "&metav1.Time{*" + sourceVar + "}"
13541346
}
1355-
targetVarPath := targetVarName
1356-
if targetFieldName != "" {
1357-
targetVarPath += "." + targetFieldName
1358-
} else {
1347+
if strings.HasPrefix(targetVar, ".") {
1348+
targetVar = targetVar[1:]
13591349
setTo = "*" + setTo
13601350
}
1361-
out += fmt.Sprintf("%s%s = %s\n", indent, targetVarPath, setTo)
1351+
out += fmt.Sprintf("%s%s = %s\n", indent, targetVar, setTo)
13621352
return out
13631353
}

pkg/generate/code/set_sdk.go

+9-22
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ import (
1919
"strings"
2020

2121
awssdkmodel "github.com/aws/aws-sdk-go/private/model/api"
22-
"github.com/gertd/go-pluralize"
2322

2423
ackgenconfig "github.com/aws-controllers-k8s/code-generator/pkg/generate/config"
2524
"github.com/aws-controllers-k8s/code-generator/pkg/model"
2625
"github.com/aws-controllers-k8s/code-generator/pkg/names"
27-
"github.com/aws-controllers-k8s/code-generator/pkg/util"
2826
)
2927

3028
// SetSDK returns the Go code that sets an SDK input shape's member fields from
@@ -760,9 +758,7 @@ func setSDKReadMany(
760758
indent := strings.Repeat("\t", indentLevel)
761759

762760
resVarPath := ""
763-
pluralize := pluralize.NewClient()
764761
opConfig, override := cfg.OverrideValues(op.Name)
765-
shapeIdentifiers := FindIdentifiersInShape(r, inputShape)
766762
var err error
767763
for memberIndex, memberName := range inputShape.MemberNames() {
768764
if override {
@@ -787,24 +783,15 @@ func setSDKReadMany(
787783
// Field renames are handled in GetSanitizedMemberPath
788784
resVarPath, err = r.GetSanitizedMemberPath(memberName, op, sourceVarName)
789785
if err != nil {
790-
// if it's an identifier field check for singular/plural
791-
if util.InStrings(memberName, shapeIdentifiers) {
792-
var flipped string
793-
if pluralize.IsPlural(memberName) {
794-
flipped = pluralize.Singular(memberName)
795-
} else {
796-
flipped = pluralize.Plural(memberName)
797-
}
798-
// If there are multiple identifiers, then prioritize the
799-
// 'Id' identifier.
800-
if resVarPath == "" || (!strings.HasSuffix(resVarPath, "Id") ||
801-
!strings.HasSuffix(resVarPath, "Ids")) {
802-
resVarPath, err = r.GetSanitizedMemberPath(flipped, op, sourceVarName)
803-
if err != nil {
804-
panic(fmt.Sprintf(
805-
"Unable to locate identifier field %s in "+
806-
"%s Spec/Status in generate.code.setSDKReadMany", flipped, r.Kind))
807-
}
786+
// if memberName is an identifier field, then check for
787+
// corresponding model identifier
788+
crIdentifier, shapeIdentifier := FindPluralizedIdentifiersInShape(r, inputShape)
789+
if strings.EqualFold(memberName, shapeIdentifier) {
790+
resVarPath, err = r.GetSanitizedMemberPath(crIdentifier, op, sourceVarName)
791+
if err != nil {
792+
panic(fmt.Sprintf(
793+
"Unable to locate identifier field %s in "+
794+
"%s Spec/Status in generate.code.setSDKReadMany", crIdentifier, r.Kind))
808795
}
809796
} else {
810797
// TODO(jaypipes): check generator config for exceptions?

0 commit comments

Comments
 (0)