Skip to content

Commit 05971ba

Browse files
committed
fixed list_of issues
1 parent 252cd20 commit 05971ba

File tree

6 files changed

+29
-120
lines changed

6 files changed

+29
-120
lines changed

go.mod

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ require (
2727
require (
2828
github.com/kro-run/kro v0.2.2
2929
k8s.io/apiextensions-apiserver v0.31.0
30-
)
31-
32-
require (
3330
dario.cat/mergo v1.0.0 // indirect
3431
github.com/Microsoft/go-winio v0.6.1 // indirect
3532
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect

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: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -412,14 +412,12 @@ func (m *Model) GetTypeDefs() ([]*TypeDef, error) {
412412
if m.typeDefs != nil {
413413
return m.typeDefs, nil
414414
}
415-
416415
tdefs := []*TypeDef{}
417416
// Map, keyed by original Shape GoTypeElem(), with the values being a
418417
// renamed type name (due to conflicting names)
419418
trenames := map[string]string{}
420419

421420
payloads := m.SDKAPI.GetPayloads()
422-
423421
for shapeName, shape := range m.SDKAPI.API.Shapes {
424422
if util.InStrings(shapeName, payloads) && !m.IsShapeUsedInCRDs(shapeName) {
425423
// Payloads are not type defs, unless explicitly used
@@ -462,14 +460,6 @@ func (m *Model) GetTypeDefs() ([]*TypeDef, error) {
462460
return tdefs[i].Names.Camel < tdefs[j].Names.Camel
463461
})
464462
m.processNestedFieldTypeDefs(tdefs)
465-
466-
// Add custom type definitions for custom shapes
467-
var err error
468-
tdefs, err = m.AddCustomTypeDefinitions(tdefs)
469-
if err != nil {
470-
return nil, fmt.Errorf("error adding custom type definitions: %w", err)
471-
}
472-
473463
m.typeDefs = tdefs
474464
m.typeRenames = trenames
475465
return tdefs, nil
@@ -1046,50 +1036,4 @@ func New(
10461036
}
10471037
m.ApplyShapeIgnoreRules()
10481038
return m, nil
1049-
}
1050-
1051-
// 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.
1054-
func (m *Model) AddCustomTypeDefinitions(tdefs []*TypeDef) ([]*TypeDef, error) {
1055-
for _, customShape := range m.SDKAPI.CustomShapes {
1056-
shape := customShape.Shape
1057-
if shape.Type != "structure" {
1058-
continue
1059-
}
1060-
1061-
// Skip if we already have a type def for this shape
1062-
shapeExists := false
1063-
for _, tdef := range tdefs {
1064-
if tdef.Shape.ShapeName == shape.ShapeName {
1065-
shapeExists = true
1066-
break
1067-
}
1068-
}
1069-
if shapeExists {
1070-
continue
1071-
}
1072-
1073-
// Create a new TypeDef for this custom shape
1074-
tdefNames := names.New(shape.ShapeName)
1075-
attrs := map[string]*Attr{}
1076-
for memberName, memberRef := range shape.MemberRefs {
1077-
memberNames := names.New(memberName)
1078-
memberShape := memberRef.Shape
1079-
if memberShape == nil {
1080-
return nil, fmt.Errorf("nil member shape for %s.%s", shape.ShapeName, memberName)
1081-
}
1082-
1083-
gt := m.getShapeCleanGoType(memberShape)
1084-
attrs[memberName] = NewAttr(memberNames, gt, memberShape)
1085-
}
1086-
if len(attrs) > 0 {
1087-
tdefs = append(tdefs, &TypeDef{
1088-
Shape: shape,
1089-
Names: tdefNames,
1090-
Attrs: attrs,
1091-
})
1092-
}
1093-
}
1094-
return tdefs, nil
10951039
}

pkg/sdk/custom_shapes.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ type customShapeInjector struct {
4141
// into the list of shapes in the API and update the list of custom shapes in
4242
// the SDKAPI object.
4343
func (h *Helper) InjectCustomShapes(sdkapi *ackmodel.SDKAPI) error {
44+
err := h.InjectSimpleSchemaShapes(sdkapi)
45+
if err != nil {
46+
return err
47+
}
4448
injector := customShapeInjector{sdkapi}
4549

4650
for _, memberShape := range h.cfg.GetCustomMapFieldMembers() {
@@ -52,7 +56,6 @@ func (h *Helper) InjectCustomShapes(sdkapi *ackmodel.SDKAPI) error {
5256
sdkapi.API.Shapes[customShape.Shape.ShapeName] = customShape.Shape
5357
sdkapi.CustomShapes = append(sdkapi.CustomShapes, customShape)
5458
}
55-
5659
for _, memberShape := range h.cfg.GetCustomListFieldMembers() {
5760
customShape, err := injector.newList(memberShape)
5861
if err != nil {
@@ -62,7 +65,6 @@ func (h *Helper) InjectCustomShapes(sdkapi *ackmodel.SDKAPI) error {
6265
sdkapi.API.Shapes[customShape.Shape.ShapeName] = customShape.Shape
6366
sdkapi.CustomShapes = append(sdkapi.CustomShapes, customShape)
6467
}
65-
6668
return nil
6769
}
6870

pkg/sdk/helper.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,10 @@ func (h *Helper) API(serviceModelName string) (*model.SDKAPI, error) {
110110
_ = api.ServicePackageDoc()
111111
sdkapi := model.NewSDKAPI(api, h.APIGroupSuffix)
112112

113-
h.InjectCustomShapes(sdkapi)
114-
h.InjectSimpleSchemaShapes(sdkapi)
115-
113+
err := h.InjectCustomShapes(sdkapi)
114+
if err != nil {
115+
return nil, err
116+
}
116117
return sdkapi, nil
117118
}
118119
return nil, ErrServiceNotFound

pkg/sdk/simpleschema.go

Lines changed: 16 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,15 @@ func (h *Helper) InjectSimpleSchemaShapes(sdkapi *ackmodel.SDKAPI) error {
3030
return nil
3131
}
3232

33+
apiShapeNames := sdkapi.API.ShapeNames()
3334
for shapeName, fieldsMap := range h.cfg.CustomShapes {
34-
schemaObj := convertFieldsMapToSchemaObj(fieldsMap)
35-
35+
// check dublicates
36+
for _, as := range apiShapeNames {
37+
if as == shapeName {
38+
return fmt.Errorf("shapeName %s already exists in the API", shapeName)
39+
}
40+
}
41+
schemaObj := convertMapValues(fieldsMap)
3642
openAPISchema, err := simpleschema.ToOpenAPISpec(schemaObj)
3743
if err != nil {
3844
return err
@@ -47,16 +53,13 @@ func (h *Helper) InjectSimpleSchemaShapes(sdkapi *ackmodel.SDKAPI) error {
4753
MemberShapeName: nil,
4854
ValueShapeName: nil,
4955
})
50-
51-
// Create and register the list shape for this custom shape
52-
h.createAndRegisterListShape(sdkapi, shapeName, shapeRef)
5356
}
5457

5558
return nil
5659
}
5760

5861
// convertFieldsMapToSchemaObj converts a fields map to a schema object for OpenAPI spec generation
59-
func convertFieldsMapToSchemaObj(fieldsMap map[string]string) map[string]interface{} {
62+
func convertMapValues(fieldsMap map[string]string) map[string]interface{} {
6063
schemaObj := make(map[string]interface{})
6164
for fieldName, fieldType := range fieldsMap {
6265
schemaObj[fieldName] = fieldType
@@ -70,6 +73,7 @@ func (h *Helper) createBaseShape(
7073
shapeName string,
7174
openAPISchema *apiextv1.JSONSchemaProps,
7275
) (*awssdkmodel.Shape, *awssdkmodel.ShapeRef) {
76+
injector := customShapeInjector{sdkapi}
7377
shape := &awssdkmodel.Shape{
7478
API: sdkapi.API,
7579
ShapeName: shapeName,
@@ -85,66 +89,27 @@ func (h *Helper) createBaseShape(
8589
panic(fmt.Sprintf("Field %s in shape %s has non-string type '%s'",
8690
fieldName, shapeName, propType))
8791
}
88-
addFieldToShape(sdkapi, shape, fieldName, shapeName)
89-
}
90-
91-
shapeRef := &awssdkmodel.ShapeRef{
92-
API: sdkapi.API,
93-
Shape: shape,
94-
ShapeName: shape.ShapeName,
95-
Documentation: fmt.Sprintf("// Reference to %s", shapeName),
92+
addStringFieldToShape(sdkapi, shape, fieldName, shapeName)
9693
}
9794

95+
shapeRef := injector.createShapeRefForMember(shape)
9896
return shape, shapeRef
9997
}
10098

101-
// addFieldToShape adds a field to the parent shape
102-
func addFieldToShape(
99+
// addStringFieldToShape adds a string field to the parent shape
100+
func addStringFieldToShape(
103101
sdkapi *ackmodel.SDKAPI,
104102
parentShape *awssdkmodel.Shape,
105103
fieldName string,
106104
shapeName string,
107105
) {
106+
injector := customShapeInjector{sdkapi}
108107
fieldShape := &awssdkmodel.Shape{
109108
API: sdkapi.API,
110109
ShapeName: fieldName,
111110
Type: "string",
112111
}
113112

114113
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-
)
114+
parentShape.MemberRefs[fieldName] = injector.createShapeRefForMember(fieldShape)
150115
}

0 commit comments

Comments
 (0)