@@ -847,6 +847,7 @@ func identifierNameOrIDGuardConstructor(
847
847
// return ackerrors.MissingNameIdentifier
848
848
// }
849
849
func requiredFieldGuardContructor (
850
+ holdingVar string ,
850
851
// String representing the fields map that contains the required
851
852
// fields for adoption
852
853
sourceVarName string ,
@@ -856,7 +857,7 @@ func requiredFieldGuardContructor(
856
857
indentLevel int ,
857
858
) string {
858
859
indent := strings .Repeat ("\t " , indentLevel )
859
- out := fmt .Sprintf ("%stmp , ok := %s[\" %s\" ]\n " , indent , sourceVarName , requiredField )
860
+ out := fmt .Sprintf ("%s%s , ok := %s[\" %s\" ]\n " , indent , holdingVar , sourceVarName , requiredField )
860
861
out += fmt .Sprintf ("%sif !ok {\n " , indent )
861
862
out += fmt .Sprintf ("%s\t return ackerrors.NewTerminalError(fmt.Errorf(\" required field missing: %s\" ))\n " , indent , requiredField )
862
863
out += fmt .Sprintf ("%s}\n " , indent )
@@ -1355,10 +1356,10 @@ func PopulateResourceFromAnnotation(
1355
1356
out := "\n "
1356
1357
// Check if the CRD defines the primary keys
1357
1358
primaryKeyConditionalOut := "\n "
1358
- primaryKeyConditionalOut += requiredFieldGuardContructor (sourceVarName , "arn" , indentLevel )
1359
+ primaryKeyConditionalOut += requiredFieldGuardContructor ("resourceARN" , sourceVarName , "arn" , indentLevel )
1359
1360
arnOut += ackResourceMetadataGuardConstructor (fmt .Sprintf ("%s.Status" , targetVarName ), indentLevel )
1360
1361
arnOut += fmt .Sprintf (
1361
- "%sarn := ackv1alpha1.AWSResourceName(tmp )\n " ,
1362
+ "%sarn := ackv1alpha1.AWSResourceName(resourceARN )\n " ,
1362
1363
indent ,
1363
1364
)
1364
1365
arnOut += fmt .Sprintf (
@@ -1377,9 +1378,10 @@ func PopulateResourceFromAnnotation(
1377
1378
isPrimarySet := primaryField != nil
1378
1379
if isPrimarySet {
1379
1380
memberPath , _ := findFieldInCR (cfg , r , primaryField .Names .Original )
1380
- primaryKeyOut += requiredFieldGuardContructor (sourceVarName , primaryField .Names .CamelLower , indentLevel )
1381
+ primaryKeyOut += requiredFieldGuardContructor ("primaryKey" , sourceVarName , primaryField .Names .CamelLower , indentLevel )
1381
1382
targetVarPath := fmt .Sprintf ("%s%s" , targetVarName , memberPath )
1382
1383
primaryKeyOut += setResourceIdentifierPrimaryIdentifierAnn (cfg , r ,
1384
+ "&primaryKey" ,
1383
1385
primaryField ,
1384
1386
targetVarPath ,
1385
1387
sourceVarName ,
@@ -1451,18 +1453,18 @@ func PopulateResourceFromAnnotation(
1451
1453
switch targetField .ShapeRef .Shape .Type {
1452
1454
case "list" , "structure" , "map" :
1453
1455
panic ("primary identifier '" + targetField .Path + "' must be a scalar type since NameOrID is a string" )
1454
- default :
1455
- break
1456
1456
}
1457
1457
1458
1458
targetVarPath := fmt .Sprintf ("%s%s" , targetVarName , memberPath )
1459
- if isPrimaryIdentifier {
1460
- primaryKeyOut += requiredFieldGuardContructor (sourceVarName , targetField .Names .CamelLower , indentLevel )
1459
+ if inputShape . IsRequired ( memberName ) || isPrimaryIdentifier {
1460
+ primaryKeyOut += requiredFieldGuardContructor (fmt . Sprintf ( "f%d" , memberIndex ), sourceVarName , targetField .Names .CamelLower , indentLevel )
1461
1461
primaryKeyOut += setResourceIdentifierPrimaryIdentifierAnn (cfg , r ,
1462
+ fmt .Sprintf ("&f%d" , memberIndex ),
1462
1463
targetField ,
1463
1464
targetVarPath ,
1464
1465
sourceVarName ,
1465
- indentLevel )
1466
+ indentLevel ,
1467
+ )
1466
1468
} else {
1467
1469
additionalKeyOut += setResourceIdentifierAdditionalKeyAnn (
1468
1470
cfg , r ,
@@ -1471,7 +1473,8 @@ func PopulateResourceFromAnnotation(
1471
1473
targetVarPath ,
1472
1474
sourceVarName ,
1473
1475
names .New (fieldName ).CamelLower ,
1474
- indentLevel )
1476
+ indentLevel ,
1477
+ )
1475
1478
}
1476
1479
}
1477
1480
@@ -1539,6 +1542,8 @@ func setResourceIdentifierPrimaryIdentifier(
1539
1542
func setResourceIdentifierPrimaryIdentifierAnn (
1540
1543
cfg * ackgenconfig.Config ,
1541
1544
r * model.CRD ,
1545
+ // The variable used for primary key
1546
+ requiredFieldVarName string ,
1542
1547
// The field that will be set on the target variable
1543
1548
targetField * model.Field ,
1544
1549
// The variable name that we want to set a value to
@@ -1548,12 +1553,11 @@ func setResourceIdentifierPrimaryIdentifierAnn(
1548
1553
// Number of levels of indentation to use
1549
1554
indentLevel int ,
1550
1555
) string {
1551
- adaptedMemberPath := fmt .Sprintf ("&tmp" )
1552
1556
qualifiedTargetVar := fmt .Sprintf ("%s.%s" , targetVarName , targetField .Path )
1553
1557
1554
1558
return setResourceForScalar (
1555
1559
qualifiedTargetVar ,
1556
- adaptedMemberPath ,
1560
+ requiredFieldVarName ,
1557
1561
targetField .ShapeRef ,
1558
1562
indentLevel ,
1559
1563
false ,
0 commit comments