Skip to content

Commit 959eaa5

Browse files
authored
Support custom nested fields that have path that contains types other than struct (#538)
Issue #, if available: Description of changes: Say we want to overwrite a nested field type e.g. for `WAFv2` controller (this is needed due to a cyclic dependency): ``` resources: RuleGroup: Rules.Statement.AndStatement: type: string ``` Currently, the [overwrite code logic](https://github.com/aws-controllers-k8s/code-generator/blob/62466746500d0c01fcfa8e8e3ca6abd9386e4687/pkg/model/crd.go#L660-L687) does not work for the above scenario and it returns the following error: ``` Expected parent field to be of type structure, but found list ``` This happens because `Rules` is not of type `struct`, but `[]struct`, and the referenced code logic requires that all fields within the path (i.e. `Rules` and `Statement` to be of type `struct`). This PR relaxes the logic to allow fields within the path to also be of types `[]struct` and `map[]struct`. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent bdf3f26 commit 959eaa5

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

pkg/model/crd.go

+25-12
Original file line numberDiff line numberDiff line change
@@ -656,17 +656,23 @@ func (crd *CRD) addCustomNestedFields(customNestedFields map[string]*ackgenconfi
656656
topLevelField := fieldParts[0]
657657

658658
f, ok := crd.checkSpecOrStatus(topLevelField)
659-
660-
if ok && f.ShapeRef.Shape.Type != "structure" {
661-
// We need to panic here because the user is providing wrong configuration.
662-
msg := fmt.Sprintf("Expected parent field to be of type structure, but found %s", f.ShapeRef.Shape.Type)
659+
if !ok {
660+
msg := fmt.Sprintf("Expected top level field %s to be present in Spec or Status", topLevelField)
663661
panic(msg)
664662
}
665663

666-
// If the provided top level field is not in the crd.SpecFields or crd.StatusFields...
667-
if !ok {
668-
// We need to panic here because the user is providing wrong configuration.
669-
msg := fmt.Sprintf("Expected top level field %s to be present in Spec or Status", topLevelField)
664+
shape := f.ShapeRef.Shape
665+
isValidShapeType := func(shape *awssdkmodel.Shape) bool {
666+
if shape.Type == "structure" ||
667+
(shape.Type == "list" && shape.MemberRef.Shape.Type == "structure") ||
668+
(shape.Type == "map" && shape.ValueRef.Shape.Type == "structure") {
669+
return true
670+
}
671+
return false
672+
}
673+
674+
if !isValidShapeType(shape) {
675+
msg := fmt.Sprintf("Expected top level field %s to be of type structure, []structure or map[]structure, but found %s", topLevelField, shape.Type)
670676
panic(msg)
671677
}
672678

@@ -676,13 +682,20 @@ func (crd *CRD) addCustomNestedFields(customNestedFields map[string]*ackgenconfi
676682

677683
// loop over the all left fieldParts except the last one
678684
for _, currentFieldName := range fieldParts[1 : len(fieldParts)-1] {
679-
// Check if parentField contains current field
680685
currentField, ok := parentField.MemberFields[currentFieldName]
681-
if !ok || currentField.ShapeRef.Shape.Type != "structure" {
682-
// Check if the field exists AND is of type structure
683-
msg := fmt.Sprintf("Cannot inject field, %s member doesn't exist or isn't a structure", currentFieldName)
686+
shape := currentField.ShapeRef.Shape
687+
688+
// if the parentField doesn't contain the current field
689+
if !ok {
690+
msg := fmt.Sprintf("Cannot inject field, nested field %s doesn't exist", currentFieldName)
684691
panic(msg)
685692
}
693+
694+
if !isValidShapeType(shape) {
695+
msg := fmt.Sprintf("Expected nested field %s to be of type structure, []structure or map[]structure, but found %s", currentFieldName, shape.Type)
696+
panic(msg)
697+
}
698+
686699
parentField = currentField
687700
}
688701

0 commit comments

Comments
 (0)