Skip to content

Commit

Permalink
Extend getAttributeFromPath search to status fields as well. (#512)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
a-hilaly authored Feb 19, 2024
1 parent f096213 commit 2bb9266
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions pkg/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,38 +547,55 @@ func getAttributeFromPath(crd *CRD, fieldPath string, tdefs []*TypeDef) (parentT
}
// First part of nested reference fieldPath is the name of top level Spec
// field. Ex: For 'ResourcesVpcConfig.SecurityGroupIds' fieldpath the
// specFieldName is 'ResourcesVpcConfig'
specFieldName := fp.Front()
var specField *Field
// topLevelFieldName is 'ResourcesVpcConfig'
topLevelFieldName := fp.Front()
var topLevelField *Field
foundInSpec := false
foundInStatus := false
for fName, field := range crd.SpecFields {
if strings.EqualFold(fName, specFieldName) {
specField = field
if strings.EqualFold(fName, topLevelFieldName) {
topLevelField = field
foundInSpec = true
break
}
}
if specField == nil {
panic(fmt.Sprintf("Unable to find a spec field with name %s"+
" to add reference for %s", specFieldName,
if !foundInSpec {
for fName, field := range crd.StatusFields {
if strings.EqualFold(fName, topLevelFieldName) {
topLevelField = field
foundInStatus = true
}
}
}
if !foundInSpec && !foundInStatus {
panic(fmt.Sprintf("Unable to find a spec or status field with name %s"+
" to add get attribute from %s", topLevelFieldName,
fieldPath))
}
if foundInSpec && foundInStatus {
panic(fmt.Sprintf(
"error getting attributes from %s: found a field with name %s in both spec and status",
fieldPath, topLevelFieldName,
))
}

// Create a new fieldPath starting with ShapeName of Spec Field
// to determine the shape of typedef which will contain the reference
// attribute. We replace the spec-field Name with spec-field ShapeName in
// the beginning of field path and leave rest of nested member names as is.
// Ex: ResourcesVpcConfig.SecurityGroupIDs will become VPCConfigRequest.SecurityGroupIDs
// for Cluster resource in eks-controller.
specFieldShapeRef := specField.ShapeRef
specFieldShapeRef := topLevelField.ShapeRef
specFieldShapeName := specFieldShapeRef.ShapeName
switch shapeType := specFieldShapeRef.Shape.Type; shapeType {
case "list":
specFieldShapeName = specField.ShapeRef.Shape.MemberRef.ShapeName
specFieldShapeRef = &specField.ShapeRef.Shape.MemberRef
specFieldShapeName = topLevelField.ShapeRef.Shape.MemberRef.ShapeName
specFieldShapeRef = &topLevelField.ShapeRef.Shape.MemberRef
case "map":
specFieldShapeName = specField.ShapeRef.Shape.ValueRef.ShapeName
specFieldShapeRef = &specField.ShapeRef.Shape.ValueRef
specFieldShapeName = topLevelField.ShapeRef.Shape.ValueRef.ShapeName
specFieldShapeRef = &topLevelField.ShapeRef.Shape.ValueRef
}
fieldShapePath := strings.Replace(fieldPath, specFieldName, specFieldShapeName, 1)
fieldShapePath := strings.Replace(fieldPath, topLevelFieldName, specFieldShapeName, 1)
fsp := ackfp.FromString(fieldShapePath)

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

0 comments on commit 2bb9266

Please sign in to comment.