Skip to content

Commit 5bf1e45

Browse files
authored
fix populateResourceFromAnnotation field setting (#624)
Description of changes: Since we know the adoptionFields are always going to be `map[string]string`, we don't need to use `setResourceForScalar`, which handles all types of scalar assignments. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 6b42111 commit 5bf1e45

File tree

4 files changed

+5530
-20
lines changed

4 files changed

+5530
-20
lines changed

pkg/generate/code/set_resource.go

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,11 +1383,10 @@ func PopulateResourceFromAnnotation(
13831383
memberPath, _ := findFieldInCR(cfg, r, primaryField.Names.Original)
13841384
primaryKeyOut += requiredFieldGuardContructor("primaryKey", sourceVarName, primaryField.Names.CamelLower, indentLevel)
13851385
targetVarPath := fmt.Sprintf("%s%s", targetVarName, memberPath)
1386-
primaryKeyOut += setResourceIdentifierPrimaryIdentifierAnn(cfg, r,
1386+
primaryKeyOut += setResourceIdentifierPrimaryIdentifierAnn(
13871387
"&primaryKey",
13881388
primaryField,
13891389
targetVarPath,
1390-
sourceVarName,
13911390
indentLevel,
13921391
)
13931392
} else {
@@ -1458,23 +1457,22 @@ func PopulateResourceFromAnnotation(
14581457
panic("primary identifier '" + targetField.Path + "' must be a scalar type since NameOrID is a string")
14591458
}
14601459

1461-
targetVarPath := fmt.Sprintf("%s%s", targetVarName, memberPath)
1460+
sourceVarPath := fmt.Sprintf("%s%s", targetVarName, memberPath)
14621461
if inputShape.IsRequired(memberName) || isPrimaryIdentifier {
14631462
requiredFieldVarName := fmt.Sprintf("f%d", memberIndex)
14641463
primaryKeyOut += requiredFieldGuardContructor(requiredFieldVarName, sourceVarName, targetField.Names.CamelLower, indentLevel)
1465-
primaryKeyOut += setResourceIdentifierPrimaryIdentifierAnn(cfg, r,
1464+
primaryKeyOut += setResourceIdentifierPrimaryIdentifierAnn(
14661465
fmt.Sprintf("&%s", requiredFieldVarName),
14671466
targetField,
1468-
targetVarPath,
1469-
sourceVarName,
1467+
sourceVarPath,
14701468
indentLevel,
14711469
)
14721470
} else {
14731471
additionalKeyOut += setResourceIdentifierAdditionalKeyAnn(
14741472
cfg, r,
14751473
memberIndex,
14761474
targetField,
1477-
targetVarPath,
1475+
sourceVarPath,
14781476
sourceVarName,
14791477
names.New(fieldName).CamelLower,
14801478
indentLevel,
@@ -1544,29 +1542,18 @@ func setResourceIdentifierPrimaryIdentifier(
15441542
//
15451543
// r.ko.Status.BrokerID = &identifier.NameOrID
15461544
func setResourceIdentifierPrimaryIdentifierAnn(
1547-
cfg *ackgenconfig.Config,
1548-
r *model.CRD,
15491545
// The variable used for required key
15501546
requiredFieldVarName string,
15511547
// The field that will be set on the target variable
15521548
targetField *model.Field,
15531549
// The variable name that we want to set a value to
15541550
targetVarName string,
1555-
// The struct or struct field that we access our source value from
1556-
sourceVarName string,
15571551
// Number of levels of indentation to use
15581552
indentLevel int,
15591553
) string {
15601554
qualifiedTargetVar := fmt.Sprintf("%s.%s", targetVarName, targetField.Path)
1561-
1562-
return setResourceForScalar(
1563-
qualifiedTargetVar,
1564-
requiredFieldVarName,
1565-
targetField.ShapeRef,
1566-
indentLevel,
1567-
false,
1568-
false,
1569-
)
1555+
indent := strings.Repeat("\t", indentLevel)
1556+
return fmt.Sprintf("%s%s = %s\n", indent, qualifiedTargetVar, requiredFieldVarName)
15701557
}
15711558

15721559
// setResourceIdentifierAdditionalKey returns a string of Go code that sets a

pkg/generate/code/set_resource_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,6 +1812,34 @@ func TestSetResource_EKS_Cluster_PopulateResourceFromAnnotation(t *testing.T) {
18121812
)
18131813
}
18141814

1815+
func TestSetResource_OpensearchServerless_SecurityPolicy_PopulateResourceFromAnnotation(t *testing.T) {
1816+
assert := assert.New(t)
1817+
require := require.New(t)
1818+
1819+
g := testutil.NewModelForService(t, "opensearchserverless")
1820+
1821+
crd := testutil.GetCRDByName(t, g, "SecurityPolicy")
1822+
require.NotNil(crd)
1823+
1824+
expected := `
1825+
f0, ok := fields["name"]
1826+
if !ok {
1827+
return ackerrors.NewTerminalError(fmt.Errorf("required field missing: name"))
1828+
}
1829+
r.ko.Spec.Name = &f0
1830+
f1, ok := fields["type_"]
1831+
if !ok {
1832+
return ackerrors.NewTerminalError(fmt.Errorf("required field missing: type_"))
1833+
}
1834+
r.ko.Spec.Type = &f1
1835+
1836+
`
1837+
assert.Equal(
1838+
expected,
1839+
code.PopulateResourceFromAnnotation(crd.Config(), crd, "fields", "r.ko", 1),
1840+
)
1841+
}
1842+
18151843
func TestSetResource_SageMaker_ModelPackage_PopulateResourceFromAnnotation(t *testing.T) {
18161844
assert := assert.New(t)
18171845
require := require.New(t)

0 commit comments

Comments
 (0)