Skip to content

Commit 5ff3c4a

Browse files
authored
Remove loops from GetWrapperOutputShape (#145)
I believe two loops were left over from refactoring: * One loop over fieldPathParts have been replaced with recursion. The current loop will only execute the first element, so we can remove the loop. * Another loop over map keys, but we can simply use a map lookup instead for a bit better performance There is already a unit test for this code. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 2f11b23 commit 5ff3c4a

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

pkg/model/crd.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -458,27 +458,26 @@ func (r *CRD) GetWrapperOutputShape(
458458
return shape, nil
459459
}
460460
fieldPathParts := strings.Split(fieldPath, ".")
461-
for x, wrapperField := range fieldPathParts {
462-
for memberName, memberRef := range shape.MemberRefs {
463-
if memberName == wrapperField {
464-
if memberRef.Shape.Type != "structure" {
465-
// All the mentionned shapes must be structure
466-
return nil, fmt.Errorf(
467-
"Expected SetOutput.WrapperFieldPath to only contain fields of type 'structure'."+
468-
" Found %s of type '%s'",
469-
memberName, memberRef.Shape.Type,
470-
)
471-
}
472-
remainPath := strings.Join(fieldPathParts[x+1:], ".")
473-
return r.GetWrapperOutputShape(memberRef.Shape, remainPath)
474-
}
475-
}
461+
wrapperField := fieldPathParts[0]
462+
463+
memberRef, ok := shape.MemberRefs[wrapperField]
464+
if !ok {
476465
return nil, fmt.Errorf(
477466
"Incorrect SetOutput.WrapperFieldPath. Could not find %s in Shape %s",
478467
wrapperField, shape.ShapeName,
479468
)
480469
}
481-
return shape, nil
470+
471+
if memberRef.Shape.Type != "structure" {
472+
// All the mentioned shapes must be structure
473+
return nil, fmt.Errorf(
474+
"Expected SetOutput.WrapperFieldPath to only contain fields of type 'structure'."+
475+
" Found %s of type '%s'",
476+
wrapperField, memberRef.Shape.Type,
477+
)
478+
}
479+
remainPath := strings.Join(fieldPathParts[1:], ".")
480+
return r.GetWrapperOutputShape(memberRef.Shape, remainPath)
482481
}
483482

484483
// GetCustomImplementation returns custom implementation method name for the

0 commit comments

Comments
 (0)