Skip to content

Commit 238c905

Browse files
committed
final fixes?
1 parent 61dff7f commit 238c905

File tree

8 files changed

+104
-128
lines changed

8 files changed

+104
-128
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ require (
2525
)
2626

2727
require (
28-
github.com/kro-run/kro v0.2.2
29-
k8s.io/apiextensions-apiserver v0.31.0
3028
dario.cat/mergo v1.0.0 // indirect
3129
github.com/Microsoft/go-winio v0.6.1 // indirect
3230
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
@@ -74,6 +72,7 @@ require (
7472
github.com/josharian/intern v1.0.0 // indirect
7573
github.com/json-iterator/go v1.1.12 // indirect
7674
github.com/kevinburke/ssh_config v1.2.0 // indirect
75+
github.com/kro-run/kro v0.2.2
7776
github.com/mailru/easyjson v0.7.7 // indirect
7877
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
7978
github.com/modern-go/reflect2 v1.0.2 // indirect
@@ -109,6 +108,7 @@ require (
109108
gopkg.in/yaml.v2 v2.4.0 // indirect
110109
gopkg.in/yaml.v3 v3.0.1 // indirect
111110
k8s.io/api v0.31.0 // indirect
111+
k8s.io/apiextensions-apiserver v0.31.0
112112
k8s.io/client-go v0.31.0 // indirect
113113
k8s.io/klog/v2 v2.130.1 // indirect
114114
k8s.io/kube-openapi v0.0.0-20240816214639-573285566f34 // indirect

pkg/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ type Config struct {
6969
// list_of: ShapeName # For arrays of the shape
7070
// map_of: ShapeName # For maps with the shape as values
7171
//
72-
CustomShapes map[string]map[string]string `json:"custom_shapes,omitempty"`
72+
CustomShapes map[string]map[string]interface{} `json:"custom_shapes,omitempty"`
7373
}
7474

7575
// SDKNames contains information on the SDK Client package. More precisely

pkg/model/crd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ func (r *CRD) ReferencedServiceNames() (serviceNames []string) {
834834
}
835835
}
836836

837-
for serviceName, _ := range serviceNamesMap {
837+
for serviceName := range serviceNamesMap {
838838
serviceNames = append(serviceNames, serviceName)
839839
}
840840
sort.Strings(serviceNames)

pkg/model/model.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ func (m *Model) GetTypeDefs() ([]*TypeDef, error) {
412412
if m.typeDefs != nil {
413413
return m.typeDefs, nil
414414
}
415-
415+
416416
tdefs := []*TypeDef{}
417417
// Map, keyed by original Shape GoTypeElem(), with the values being a
418418
// renamed type name (due to conflicting names)

pkg/model/model_dynamodb_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,11 @@ func TestDynamoDB_CustomShape_ReplicasState(t *testing.T) {
122122
crd := getCRDByName("Table", crds)
123123
require.NotNil(crd)
124124

125-
// Verify the ReplicaStates field exists
125+
// Verify the ReplicaStates field exists
126126
assert.Contains(crd.StatusFields, "ReplicaStates")
127127
replicasDescField := crd.StatusFields["ReplicaStates"]
128128
require.NotNil(replicasDescField)
129129

130-
131130
replicasStateShape := replicasDescField.ShapeRef.Shape.MemberRef.Shape
132131
require.NotNil(replicasStateShape)
133132

pkg/model/sdk_api.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,14 @@ func (a *SDKAPI) GetShapeRefFromType(
134134
// TODO(jaypipes): Only handling maps with string keys at the moment...
135135
isMap := strings.HasPrefix(typeOverride, "map[string]")
136136
if isMap {
137-
elemType = typeOverride[11:len(typeOverride)]
137+
elemType = typeOverride[11:]
138138
}
139139
if isSlice {
140-
elemType = typeOverride[2:len(typeOverride)]
140+
elemType = typeOverride[2:]
141141
}
142142
isPtrElem := strings.HasPrefix(elemType, "*")
143143
if isPtrElem {
144-
elemType = elemType[1:len(elemType)]
144+
elemType = elemType[1:]
145145
}
146146
// first check to see if the element type is a scalar and if it is, just
147147
// create a ShapeRef to represent the type.

pkg/sdk/custom_shapes.go

Lines changed: 95 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ import (
1818
"fmt"
1919

2020
awssdkmodel "github.com/aws-controllers-k8s/code-generator/pkg/api"
21+
simpleschema "github.com/kro-run/kro/pkg/simpleschema"
2122

2223
ackmodel "github.com/aws-controllers-k8s/code-generator/pkg/model"
24+
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2325
)
2426

2527
var (
@@ -41,11 +43,11 @@ type customShapeInjector struct {
4143
// into the list of shapes in the API and update the list of custom shapes in
4244
// the SDKAPI object.
4345
func (h *Helper) InjectCustomShapes(sdkapi *ackmodel.SDKAPI) error {
44-
err := h.InjectSimpleSchemaShapes(sdkapi)
45-
if err != nil {
46+
injector := customShapeInjector{sdkapi}
47+
48+
if err := injector.injectSimpleSchemaShapes(h.cfg.CustomShapes); err != nil {
4649
return err
4750
}
48-
injector := customShapeInjector{sdkapi}
4951

5052
for _, memberShape := range h.cfg.GetCustomMapFieldMembers() {
5153
customShape, err := injector.newMap(memberShape)
@@ -68,6 +70,96 @@ func (h *Helper) InjectCustomShapes(sdkapi *ackmodel.SDKAPI) error {
6870
return nil
6971
}
7072

73+
// injectSimpleSchemaShapes processes custom shapes from the top-level custom_shapes
74+
// section and injects them into the SDK API model.
75+
// Only string types are supported - all other types will cause a panic.
76+
func (i *customShapeInjector) injectSimpleSchemaShapes(customShapes map[string]map[string]interface{}) error {
77+
if len(customShapes) == 0 {
78+
return nil
79+
}
80+
81+
apiShapeNames := i.sdkAPI.API.ShapeNames()
82+
for shapeName, fieldsMap := range customShapes {
83+
// check for duplicates
84+
for _, as := range apiShapeNames {
85+
if as == shapeName {
86+
return fmt.Errorf("CustomType name %s already exists in the API", shapeName)
87+
}
88+
}
89+
schemaObj := convertMapValues(fieldsMap)
90+
openAPISchema, err := simpleschema.ToOpenAPISpec(schemaObj)
91+
if err != nil {
92+
return err
93+
}
94+
95+
// Create and register the base structure shape
96+
shape, shapeRef := i.newStructureShape(shapeName, openAPISchema)
97+
i.sdkAPI.API.Shapes[shape.ShapeName] = shape
98+
i.sdkAPI.CustomShapes = append(i.sdkAPI.CustomShapes, &ackmodel.CustomShape{
99+
Shape: shape,
100+
ShapeRef: shapeRef,
101+
MemberShapeName: nil,
102+
ValueShapeName: nil,
103+
})
104+
}
105+
106+
return nil
107+
}
108+
109+
// convertMapValues converts a fields map to a schema object for OpenAPI spec generation
110+
func convertMapValues(fieldsMap map[string]interface{}) map[string]interface{} {
111+
schemaObj := make(map[string]interface{})
112+
for fieldName, fieldType := range fieldsMap {
113+
schemaObj[fieldName] = fieldType
114+
}
115+
return schemaObj
116+
}
117+
118+
// newStructureShape creates a base shape with its member fields
119+
func (i *customShapeInjector) newStructureShape(
120+
shapeName string,
121+
openAPISchema *apiextv1.JSONSchemaProps,
122+
) (*awssdkmodel.Shape, *awssdkmodel.ShapeRef) {
123+
shape := &awssdkmodel.Shape{
124+
API: i.sdkAPI.API,
125+
ShapeName: shapeName,
126+
Type: "structure",
127+
Documentation: "// Custom ACK type for " + shapeName,
128+
MemberRefs: make(map[string]*awssdkmodel.ShapeRef),
129+
}
130+
131+
properties := openAPISchema.Properties
132+
for fieldName, propObj := range properties {
133+
propType := propObj.Type
134+
if propType != "string" {
135+
panic(fmt.Sprintf("Field %s in shape %s has non-string type '%s'",
136+
fieldName, shapeName, propType))
137+
}
138+
addStringFieldToShape(i.sdkAPI, shape, fieldName, shapeName)
139+
}
140+
141+
shapeRef := i.createShapeRefForMember(shape)
142+
return shape, shapeRef
143+
}
144+
145+
// addStringFieldToShape adds a string field to the parent shape
146+
func addStringFieldToShape(
147+
sdkapi *ackmodel.SDKAPI,
148+
parentShape *awssdkmodel.Shape,
149+
fieldName string,
150+
shapeName string,
151+
) {
152+
injector := customShapeInjector{sdkapi}
153+
fieldShape := &awssdkmodel.Shape{
154+
API: sdkapi.API,
155+
ShapeName: fieldName,
156+
Type: "string",
157+
}
158+
159+
sdkapi.API.Shapes[fieldShape.ShapeName] = fieldShape
160+
parentShape.MemberRefs[fieldName] = injector.createShapeRefForMember(fieldShape)
161+
}
162+
71163
// createShapeRefForMember creates a minimal ShapeRef type to encapsulate a
72164
// shape.
73165
func (i *customShapeInjector) createShapeRefForMember(shape *awssdkmodel.Shape) *awssdkmodel.ShapeRef {

pkg/sdk/simpleschema.go

Lines changed: 0 additions & 115 deletions
This file was deleted.

0 commit comments

Comments
 (0)