Skip to content

Commit f582110

Browse files
committed
final fixes?
1 parent 61dff7f commit f582110

File tree

8 files changed

+97
-129
lines changed

8 files changed

+97
-129
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: 3 additions & 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

@@ -145,4 +144,6 @@ func TestDynamoDB_CustomShape_ReplicasState(t *testing.T) {
145144
field := replicasStateShape.MemberRefs[fieldName]
146145
assert.Equal("string", field.Shape.Type, "Field "+fieldName+" should be of type string")
147146
}
147+
148+
assert.Equal(len(expectedFields), len(replicasStateShape.MemberRefs), "ReplicasState shape has unexpected number of fields")
148149
}

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: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ import (
1717
"errors"
1818
"fmt"
1919

20-
awssdkmodel "github.com/aws-controllers-k8s/code-generator/pkg/api"
20+
simpleschema "github.com/kro-run/kro/pkg/simpleschema"
21+
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2122

23+
awssdkmodel "github.com/aws-controllers-k8s/code-generator/pkg/api"
2224
ackmodel "github.com/aws-controllers-k8s/code-generator/pkg/model"
2325
)
2426

@@ -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,86 @@ 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+
openAPISchema, err := simpleschema.ToOpenAPISpec(fieldsMap)
90+
if err != nil {
91+
return err
92+
}
93+
94+
// Create and register the base structure shape
95+
shape, shapeRef := i.newStructureShape(shapeName, openAPISchema)
96+
i.sdkAPI.API.Shapes[shape.ShapeName] = shape
97+
i.sdkAPI.CustomShapes = append(i.sdkAPI.CustomShapes, &ackmodel.CustomShape{
98+
Shape: shape,
99+
ShapeRef: shapeRef,
100+
MemberShapeName: nil,
101+
ValueShapeName: nil,
102+
})
103+
}
104+
105+
return nil
106+
}
107+
108+
// newStructureShape creates a base shape with its member fields
109+
func (i *customShapeInjector) newStructureShape(
110+
shapeName string,
111+
openAPISchema *apiextv1.JSONSchemaProps,
112+
) (*awssdkmodel.Shape, *awssdkmodel.ShapeRef) {
113+
shape := &awssdkmodel.Shape{
114+
API: i.sdkAPI.API,
115+
ShapeName: shapeName,
116+
Type: "structure",
117+
Documentation: "// Custom ACK type for " + shapeName,
118+
MemberRefs: make(map[string]*awssdkmodel.ShapeRef),
119+
}
120+
121+
properties := openAPISchema.Properties
122+
for fieldName, propObj := range properties {
123+
propType := propObj.Type
124+
if propType != "string" {
125+
panic(fmt.Sprintf("Field %s in shape %s has non-string type '%s'",
126+
fieldName, shapeName, propType))
127+
}
128+
addStringFieldToShape(i.sdkAPI, shape, fieldName, shapeName)
129+
}
130+
131+
shapeRef := i.createShapeRefForMember(shape)
132+
return shape, shapeRef
133+
}
134+
135+
// addStringFieldToShape adds a string field to the parent shape
136+
func addStringFieldToShape(
137+
sdkapi *ackmodel.SDKAPI,
138+
parentShape *awssdkmodel.Shape,
139+
fieldName string,
140+
shapeName string,
141+
) {
142+
injector := customShapeInjector{sdkapi}
143+
fieldShape := &awssdkmodel.Shape{
144+
API: sdkapi.API,
145+
ShapeName: fieldName,
146+
Type: "string",
147+
}
148+
149+
sdkapi.API.Shapes[fieldShape.ShapeName] = fieldShape
150+
parentShape.MemberRefs[fieldName] = injector.createShapeRefForMember(fieldShape)
151+
}
152+
71153
// createShapeRefForMember creates a minimal ShapeRef type to encapsulate a
72154
// shape.
73155
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)