Skip to content

Commit 82298d8

Browse files
committed
fixed list_of issues
1 parent 252cd20 commit 82298d8

File tree

5 files changed

+20
-65
lines changed

5 files changed

+20
-65
lines changed

pkg/config/config.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,17 @@ type Config struct {
5858
// The outer map is keyed by shape name (e.g., "ReplicaStatus"), and the inner map
5959
// is keyed by field name with string values representing field types.
6060
//
61-
// These shapes can be referenced in a resource's fields section using:
62-
// custom_field:
63-
// list_of: ShapeName # For arrays of the shape
64-
// map_of: ShapeName # For maps with the shape as values
65-
//
6661
// Example:
6762
// custom_shapes:
6863
// ReplicaStatus:
6964
// Status: string
7065
// Region: string
7166
// Description: string
67+
// These shapes can be referenced in a resource's fields section using:
68+
// custom_field:
69+
// list_of: ShapeName # For arrays of the shape
70+
// map_of: ShapeName # For maps with the shape as values
71+
//
7272
CustomShapes map[string]map[string]string `json:"custom_shapes,omitempty"`
7373
}
7474

pkg/model/model.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -464,11 +464,11 @@ func (m *Model) GetTypeDefs() ([]*TypeDef, error) {
464464
m.processNestedFieldTypeDefs(tdefs)
465465

466466
// Add custom type definitions for custom shapes
467-
var err error
468-
tdefs, err = m.AddCustomTypeDefinitions(tdefs)
467+
customTypes, err := m.AddCustomTypeDefinitions(tdefs)
469468
if err != nil {
470469
return nil, fmt.Errorf("error adding custom type definitions: %w", err)
471470
}
471+
tdefs = append(tdefs, customTypes...)
472472

473473
m.typeDefs = tdefs
474474
m.typeRenames = trenames
@@ -1049,16 +1049,14 @@ func New(
10491049
}
10501050

10511051
// AddCustomTypeDefinitions adds type definitions for custom shapes to the typeDefs slice
1052-
// It checks if a type def for the shape already exists and skips adding it if so.
1053-
// It also checks if the shape is a structure and only adds type defs for structure types.
10541052
func (m *Model) AddCustomTypeDefinitions(tdefs []*TypeDef) ([]*TypeDef, error) {
1053+
var customTypes []*TypeDef
10551054
for _, customShape := range m.SDKAPI.CustomShapes {
10561055
shape := customShape.Shape
10571056
if shape.Type != "structure" {
10581057
continue
10591058
}
10601059

1061-
// Skip if we already have a type def for this shape
10621060
shapeExists := false
10631061
for _, tdef := range tdefs {
10641062
if tdef.Shape.ShapeName == shape.ShapeName {
@@ -1070,7 +1068,6 @@ func (m *Model) AddCustomTypeDefinitions(tdefs []*TypeDef) ([]*TypeDef, error) {
10701068
continue
10711069
}
10721070

1073-
// Create a new TypeDef for this custom shape
10741071
tdefNames := names.New(shape.ShapeName)
10751072
attrs := map[string]*Attr{}
10761073
for memberName, memberRef := range shape.MemberRefs {
@@ -1084,12 +1081,13 @@ func (m *Model) AddCustomTypeDefinitions(tdefs []*TypeDef) ([]*TypeDef, error) {
10841081
attrs[memberName] = NewAttr(memberNames, gt, memberShape)
10851082
}
10861083
if len(attrs) > 0 {
1087-
tdefs = append(tdefs, &TypeDef{
1084+
customTypes = append(customTypes, &TypeDef{
10881085
Shape: shape,
10891086
Names: tdefNames,
10901087
Attrs: attrs,
10911088
})
10921089
}
10931090
}
1094-
return tdefs, nil
1095-
}
1091+
1092+
return customTypes, nil
1093+
}

pkg/sdk/custom_shapes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type customShapeInjector struct {
4242
// the SDKAPI object.
4343
func (h *Helper) InjectCustomShapes(sdkapi *ackmodel.SDKAPI) error {
4444
injector := customShapeInjector{sdkapi}
45+
h.InjectSimpleSchemaShapes(sdkapi)
4546

4647
for _, memberShape := range h.cfg.GetCustomMapFieldMembers() {
4748
customShape, err := injector.newMap(memberShape)

pkg/sdk/helper.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,7 @@ func (h *Helper) API(serviceModelName string) (*model.SDKAPI, error) {
109109
// unexported map variable...
110110
_ = api.ServicePackageDoc()
111111
sdkapi := model.NewSDKAPI(api, h.APIGroupSuffix)
112-
113112
h.InjectCustomShapes(sdkapi)
114-
h.InjectSimpleSchemaShapes(sdkapi)
115-
116113
return sdkapi, nil
117114
}
118115
return nil, ErrServiceNotFound

pkg/sdk/simpleschema.go

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (h *Helper) InjectSimpleSchemaShapes(sdkapi *ackmodel.SDKAPI) error {
3131
}
3232

3333
for shapeName, fieldsMap := range h.cfg.CustomShapes {
34-
schemaObj := convertFieldsMapToSchemaObj(fieldsMap)
34+
schemaObj := convertMapKeys(fieldsMap)
3535

3636
openAPISchema, err := simpleschema.ToOpenAPISpec(schemaObj)
3737
if err != nil {
@@ -47,16 +47,13 @@ func (h *Helper) InjectSimpleSchemaShapes(sdkapi *ackmodel.SDKAPI) error {
4747
MemberShapeName: nil,
4848
ValueShapeName: nil,
4949
})
50-
51-
// Create and register the list shape for this custom shape
52-
h.createAndRegisterListShape(sdkapi, shapeName, shapeRef)
5350
}
5451

5552
return nil
5653
}
5754

5855
// convertFieldsMapToSchemaObj converts a fields map to a schema object for OpenAPI spec generation
59-
func convertFieldsMapToSchemaObj(fieldsMap map[string]string) map[string]interface{} {
56+
func convertMapKeys(fieldsMap map[string]string) map[string]interface{} {
6057
schemaObj := make(map[string]interface{})
6158
for fieldName, fieldType := range fieldsMap {
6259
schemaObj[fieldName] = fieldType
@@ -70,6 +67,7 @@ func (h *Helper) createBaseShape(
7067
shapeName string,
7168
openAPISchema *apiextv1.JSONSchemaProps,
7269
) (*awssdkmodel.Shape, *awssdkmodel.ShapeRef) {
70+
injector := customShapeInjector{sdkapi}
7371
shape := &awssdkmodel.Shape{
7472
API: sdkapi.API,
7573
ShapeName: shapeName,
@@ -88,13 +86,7 @@ func (h *Helper) createBaseShape(
8886
addFieldToShape(sdkapi, shape, fieldName, shapeName)
8987
}
9088

91-
shapeRef := &awssdkmodel.ShapeRef{
92-
API: sdkapi.API,
93-
Shape: shape,
94-
ShapeName: shape.ShapeName,
95-
Documentation: fmt.Sprintf("// Reference to %s", shapeName),
96-
}
97-
89+
shapeRef := injector.createShapeRefForMember(shape)
9890
return shape, shapeRef
9991
}
10092

@@ -105,46 +97,13 @@ func addFieldToShape(
10597
fieldName string,
10698
shapeName string,
10799
) {
100+
injector := customShapeInjector{sdkapi}
108101
fieldShape := &awssdkmodel.Shape{
109102
API: sdkapi.API,
110103
ShapeName: fieldName,
111104
Type: "string",
112105
}
113106

114107
sdkapi.API.Shapes[fieldShape.ShapeName] = fieldShape
115-
parentShape.MemberRefs[fieldName] = &awssdkmodel.ShapeRef{
116-
API: sdkapi.API,
117-
Shape: fieldShape,
118-
ShapeName: fieldShape.ShapeName,
119-
Documentation: fmt.Sprintf("// %s field for %s", fieldName, shapeName),
120-
}
121-
}
122-
123-
// createAndRegisterListShape creates a list shape for a custom shape and registers it
124-
func (h *Helper) createAndRegisterListShape(
125-
sdkapi *ackmodel.SDKAPI,
126-
shapeName string,
127-
shapeRef *awssdkmodel.ShapeRef,
128-
) {
129-
listShapeName := fmt.Sprintf(ShapeNameTemplateList, shapeName)
130-
listShape := &awssdkmodel.Shape{
131-
API: sdkapi.API,
132-
ShapeName: listShapeName,
133-
Type: "list",
134-
Documentation: fmt.Sprintf("// List of %s", shapeName),
135-
MemberRef: *shapeRef,
136-
}
137-
138-
listShapeRef := &awssdkmodel.ShapeRef{
139-
API: sdkapi.API,
140-
Shape: listShape,
141-
ShapeName: listShapeName,
142-
Documentation: fmt.Sprintf("// List of %s", shapeName),
143-
}
144-
145-
sdkapi.API.Shapes[listShape.ShapeName] = listShape
146-
sdkapi.CustomShapes = append(
147-
sdkapi.CustomShapes,
148-
ackmodel.NewCustomListShape(listShape, listShapeRef, shapeName),
149-
)
150-
}
108+
parentShape.MemberRefs[fieldName] = injector.createShapeRefForMember(fieldShape)
109+
}

0 commit comments

Comments
 (0)