Skip to content

Commit 2bb9266

Browse files
authored
Extend getAttributeFromPath search to status fields as well. (#512)
Prior to this patch, `getAttributeFromPath` searched for paths in spec fields only. And acted as a no-op when it comes to status fields. This patch extends the search to status fields as well, allowing us to configure nested fields in the status as well. This is gonna be used to configure ECS Cluster `Status.Attachments.Type` go tags (set `type` instead of `type_`) Signed-off-by: Amine Hilaly <[email protected]> By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent f096213 commit 2bb9266

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

pkg/model/model.go

+31-14
Original file line numberDiff line numberDiff line change
@@ -547,38 +547,55 @@ func getAttributeFromPath(crd *CRD, fieldPath string, tdefs []*TypeDef) (parentT
547547
}
548548
// First part of nested reference fieldPath is the name of top level Spec
549549
// field. Ex: For 'ResourcesVpcConfig.SecurityGroupIds' fieldpath the
550-
// specFieldName is 'ResourcesVpcConfig'
551-
specFieldName := fp.Front()
552-
var specField *Field
550+
// topLevelFieldName is 'ResourcesVpcConfig'
551+
topLevelFieldName := fp.Front()
552+
var topLevelField *Field
553+
foundInSpec := false
554+
foundInStatus := false
553555
for fName, field := range crd.SpecFields {
554-
if strings.EqualFold(fName, specFieldName) {
555-
specField = field
556+
if strings.EqualFold(fName, topLevelFieldName) {
557+
topLevelField = field
558+
foundInSpec = true
556559
break
557560
}
558561
}
559-
if specField == nil {
560-
panic(fmt.Sprintf("Unable to find a spec field with name %s"+
561-
" to add reference for %s", specFieldName,
562+
if !foundInSpec {
563+
for fName, field := range crd.StatusFields {
564+
if strings.EqualFold(fName, topLevelFieldName) {
565+
topLevelField = field
566+
foundInStatus = true
567+
}
568+
}
569+
}
570+
if !foundInSpec && !foundInStatus {
571+
panic(fmt.Sprintf("Unable to find a spec or status field with name %s"+
572+
" to add get attribute from %s", topLevelFieldName,
562573
fieldPath))
563574
}
575+
if foundInSpec && foundInStatus {
576+
panic(fmt.Sprintf(
577+
"error getting attributes from %s: found a field with name %s in both spec and status",
578+
fieldPath, topLevelFieldName,
579+
))
580+
}
564581

565582
// Create a new fieldPath starting with ShapeName of Spec Field
566583
// to determine the shape of typedef which will contain the reference
567584
// attribute. We replace the spec-field Name with spec-field ShapeName in
568585
// the beginning of field path and leave rest of nested member names as is.
569586
// Ex: ResourcesVpcConfig.SecurityGroupIDs will become VPCConfigRequest.SecurityGroupIDs
570587
// for Cluster resource in eks-controller.
571-
specFieldShapeRef := specField.ShapeRef
588+
specFieldShapeRef := topLevelField.ShapeRef
572589
specFieldShapeName := specFieldShapeRef.ShapeName
573590
switch shapeType := specFieldShapeRef.Shape.Type; shapeType {
574591
case "list":
575-
specFieldShapeName = specField.ShapeRef.Shape.MemberRef.ShapeName
576-
specFieldShapeRef = &specField.ShapeRef.Shape.MemberRef
592+
specFieldShapeName = topLevelField.ShapeRef.Shape.MemberRef.ShapeName
593+
specFieldShapeRef = &topLevelField.ShapeRef.Shape.MemberRef
577594
case "map":
578-
specFieldShapeName = specField.ShapeRef.Shape.ValueRef.ShapeName
579-
specFieldShapeRef = &specField.ShapeRef.Shape.ValueRef
595+
specFieldShapeName = topLevelField.ShapeRef.Shape.ValueRef.ShapeName
596+
specFieldShapeRef = &topLevelField.ShapeRef.Shape.ValueRef
580597
}
581-
fieldShapePath := strings.Replace(fieldPath, specFieldName, specFieldShapeName, 1)
598+
fieldShapePath := strings.Replace(fieldPath, topLevelFieldName, specFieldShapeName, 1)
582599
fsp := ackfp.FromString(fieldShapePath)
583600

584601
// "fieldName" is the member name for which reference field will be created.

0 commit comments

Comments
 (0)