Skip to content

Commit 7832e9a

Browse files
authored
Recursively build a clean GoType for nested maps and arrays (#127)
Issue N/A Description of changes: This patch fixes a bug where `ack-generate` used to prepend a `[]` prefix to any map of structs (example `Route_SDK.RequestParameters` in apigatewayv2) Also in the same PR, moving the GoType cleanup logic to a separate function `model.getShapeCleanGoType` to simplify `model.GetTypeDefs` code. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 9aa3fb4 commit 7832e9a

File tree

1 file changed

+33
-38
lines changed

1 file changed

+33
-38
lines changed

pkg/model/model.go

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/aws-controllers-k8s/code-generator/pkg/generate/templateset"
2424
"github.com/aws-controllers-k8s/code-generator/pkg/names"
2525
"github.com/aws-controllers-k8s/code-generator/pkg/util"
26+
awssdkmodel "github.com/aws/aws-sdk-go/private/model/api"
2627
)
2728

2829
var (
@@ -316,44 +317,7 @@ func (m *Model) GetTypeDefs() ([]*TypeDef, error) {
316317
if !m.IsShapeUsedInCRDs(memberShape.ShapeName) {
317318
continue
318319
}
319-
// There are shapes that are called things like DBProxyStatus that are
320-
// fields in a DBProxy CRD... we need to ensure the type names don't
321-
// conflict. Also, the name of the Go type in the generated code is
322-
// Camel-cased and normalized, so we use that as the Go type
323-
gt := memberShape.GoType()
324-
if memberShape.Type == "structure" {
325-
typeNames := names.New(memberShape.ShapeName)
326-
if m.SDKAPI.HasConflictingTypeName(memberShape.ShapeName, m.cfg) {
327-
typeNames.Camel += ConflictingNameSuffix
328-
}
329-
gt = "*" + typeNames.Camel
330-
} else if memberShape.Type == "list" {
331-
// If it's a list type, where the element is a structure, we need to
332-
// set the GoType to the cleaned-up Camel-cased name
333-
if memberShape.MemberRef.Shape.Type == "structure" {
334-
elemType := memberShape.MemberRef.Shape.GoTypeElem()
335-
typeNames := names.New(elemType)
336-
if m.SDKAPI.HasConflictingTypeName(elemType, m.cfg) {
337-
typeNames.Camel += ConflictingNameSuffix
338-
}
339-
gt = "[]*" + typeNames.Camel
340-
}
341-
} else if memberShape.Type == "map" {
342-
// If it's a map type, where the value element is a structure,
343-
// we need to set the GoType to the cleaned-up Camel-cased name
344-
if memberShape.ValueRef.Shape.Type == "structure" {
345-
valType := memberShape.ValueRef.Shape.GoTypeElem()
346-
typeNames := names.New(valType)
347-
if m.SDKAPI.HasConflictingTypeName(valType, m.cfg) {
348-
typeNames.Camel += ConflictingNameSuffix
349-
}
350-
gt = "[]map[string]*" + typeNames.Camel
351-
}
352-
} else if memberShape.Type == "timestamp" {
353-
// time.Time needs to be converted to apimachinery/metav1.Time
354-
// otherwise there is no DeepCopy support
355-
gt = "*metav1.Time"
356-
}
320+
gt := m.getShapeCleanGoType(memberShape)
357321
attrs[memberName] = NewAttr(memberNames, gt, memberShape)
358322
}
359323
if len(attrs) == 0 {
@@ -375,6 +339,37 @@ func (m *Model) GetTypeDefs() ([]*TypeDef, error) {
375339
return tdefs, nil
376340
}
377341

342+
// getShapeCleanGoType returns a cleaned-up and Camel-cased GoType name for a given shape.
343+
func (m *Model) getShapeCleanGoType(shape *awssdkmodel.Shape) string {
344+
switch shape.Type {
345+
case "map":
346+
// If it's a map type we need to set the GoType to the cleaned-up
347+
// Camel-cased name
348+
return "map[string]" + m.getShapeCleanGoType(shape.ValueRef.Shape)
349+
case "list", "array", "blob":
350+
// If it's a list type, we need to set the GoType to the cleaned-up
351+
// Camel-cased name
352+
return "[]" + m.getShapeCleanGoType(shape.MemberRef.Shape)
353+
case "timestamp":
354+
// time.Time needs to be converted to apimachinery/metav1.Time
355+
// otherwise there is no DeepCopy support
356+
return "*metav1.Time"
357+
case "structure":
358+
// There are shapes that are called things like DBProxyStatus that are
359+
// fields in a DBProxy CRD... we need to ensure the type names don't
360+
// conflict. Also, the name of the Go type in the generated code is
361+
// Camel-cased and normalized, so we use that as the Go type
362+
goType := shape.GoType()
363+
typeNames := names.New(goType)
364+
if m.SDKAPI.HasConflictingTypeName(goType, m.cfg) {
365+
typeNames.Camel += ConflictingNameSuffix
366+
}
367+
return "*" + typeNames.Camel
368+
default:
369+
return shape.GoType()
370+
}
371+
}
372+
378373
// processNestedFieldTypeDefs updates the supplied TypeDef structs' if a nested
379374
// field has been configured with a type overriding FieldConfig -- such as
380375
// FieldConfig.IsSecret.

0 commit comments

Comments
 (0)