@@ -847,6 +847,9 @@ func identifierNameOrIDGuardConstructor(
847
847
// return ackerrors.MissingNameIdentifier
848
848
// }
849
849
func requiredFieldGuardContructor (
850
+ // requiredFieldVarName is the variable where the requiredField value
851
+ // will be stored
852
+ requiredFieldVarName string ,
850
853
// String representing the fields map that contains the required
851
854
// fields for adoption
852
855
sourceVarName string ,
@@ -856,7 +859,7 @@ func requiredFieldGuardContructor(
856
859
indentLevel int ,
857
860
) string {
858
861
indent := strings .Repeat ("\t " , indentLevel )
859
- out := fmt .Sprintf ("%stmp , ok := %s[\" %s\" ]\n " , indent , sourceVarName , requiredField )
862
+ out := fmt .Sprintf ("%s%s , ok := %s[\" %s\" ]\n " , indent , requiredFieldVarName , sourceVarName , requiredField )
860
863
out += fmt .Sprintf ("%sif !ok {\n " , indent )
861
864
out += fmt .Sprintf ("%s\t return ackerrors.NewTerminalError(fmt.Errorf(\" required field missing: %s\" ))\n " , indent , requiredField )
862
865
out += fmt .Sprintf ("%s}\n " , indent )
@@ -1258,30 +1261,30 @@ func SetResourceIdentifiers(
1258
1261
//
1259
1262
// ```
1260
1263
//
1261
- // tmp , ok := field["brokerID"]
1264
+ // primaryKey , ok := field["brokerID"]
1262
1265
// if !ok {
1263
- // return ackerrors.MissingNameIdentifier
1266
+ // return ackerrors.NewTerminalError(fmt.Errorf("required field missing: brokerID"))
1264
1267
// }
1265
- // r.ko.Status.BrokerID = &tmp
1268
+ // r.ko.Status.BrokerID = &primaryKey
1266
1269
//
1267
1270
// ```
1268
1271
//
1269
1272
// An example of code with additional keys:
1270
1273
//
1271
1274
// ```
1272
1275
//
1273
- // tmp , ok := field["resourceID"]
1276
+ // primaryKey , ok := field["resourceID"]
1274
1277
// if !ok {
1275
- // return ackerrors.MissingNameIdentifier
1278
+ // return ackerrors.NewTerminalError(fmt.Errorf("required field missing: resourceID"))
1276
1279
// }
1277
1280
//
1278
- // r.ko.Spec.ResourceID = &tmp
1279
- //
1280
- // f0, f0ok := fields["scalableDimension"]
1281
+ // r.ko.Spec.ResourceID = &primaryKey
1281
1282
//
1282
- // if f0ok {
1283
- // r.ko.Spec.ScalableDimension = &f0
1283
+ // f0, ok := fields["scalableDimension"]
1284
+ // if !ok {
1285
+ // return ackerrors.NewTerminalError(fmt.Errorf("required field missing: scalableDimension"))
1284
1286
// }
1287
+ // r.ko.Spec.ScalableDimension = &f0
1285
1288
//
1286
1289
// f1, f1ok := fields["serviceNamespace"]
1287
1290
//
@@ -1295,17 +1298,17 @@ func SetResourceIdentifiers(
1295
1298
// ```
1296
1299
//
1297
1300
// tmpArn, ok := field["arn"]
1298
- // if !ok {
1299
- // return ackerrors.MissingNameIdentifier
1301
+ // if !ok {
1302
+ // return ackerrors.NewTerminalError(fmt.Errorf("required field missing: arn"))
1300
1303
// }
1301
1304
// if r.ko.Status.ACKResourceMetadata == nil {
1302
1305
// r.ko.Status.ACKResourceMetadata = &ackv1alpha1.ResourceMetadata{}
1303
1306
// }
1304
1307
// arn := ackv1alpha1.AWSResourceName(tmp)
1305
1308
//
1306
- // r.ko.Status.ACKResourceMetadata.ARN = &arn
1309
+ // r.ko.Status.ACKResourceMetadata.ARN = &arn
1307
1310
//
1308
- // f0, f0ok := fields["modelPackageName"]
1311
+ // f0, f0ok := fields["modelPackageName"]
1309
1312
//
1310
1313
// if f0ok {
1311
1314
// r.ko.Spec.ModelPackageName = &f0
@@ -1355,10 +1358,10 @@ func PopulateResourceFromAnnotation(
1355
1358
out := "\n "
1356
1359
// Check if the CRD defines the primary keys
1357
1360
primaryKeyConditionalOut := "\n "
1358
- primaryKeyConditionalOut += requiredFieldGuardContructor (sourceVarName , "arn" , indentLevel )
1361
+ primaryKeyConditionalOut += requiredFieldGuardContructor ("resourceARN" , sourceVarName , "arn" , indentLevel )
1359
1362
arnOut += ackResourceMetadataGuardConstructor (fmt .Sprintf ("%s.Status" , targetVarName ), indentLevel )
1360
1363
arnOut += fmt .Sprintf (
1361
- "%sarn := ackv1alpha1.AWSResourceName(tmp )\n " ,
1364
+ "%sarn := ackv1alpha1.AWSResourceName(resourceARN )\n " ,
1362
1365
indent ,
1363
1366
)
1364
1367
arnOut += fmt .Sprintf (
@@ -1377,9 +1380,10 @@ func PopulateResourceFromAnnotation(
1377
1380
isPrimarySet := primaryField != nil
1378
1381
if isPrimarySet {
1379
1382
memberPath , _ := findFieldInCR (cfg , r , primaryField .Names .Original )
1380
- primaryKeyOut += requiredFieldGuardContructor (sourceVarName , primaryField .Names .CamelLower , indentLevel )
1383
+ primaryKeyOut += requiredFieldGuardContructor ("primaryKey" , sourceVarName , primaryField .Names .CamelLower , indentLevel )
1381
1384
targetVarPath := fmt .Sprintf ("%s%s" , targetVarName , memberPath )
1382
1385
primaryKeyOut += setResourceIdentifierPrimaryIdentifierAnn (cfg , r ,
1386
+ "&primaryKey" ,
1383
1387
primaryField ,
1384
1388
targetVarPath ,
1385
1389
sourceVarName ,
@@ -1451,18 +1455,18 @@ func PopulateResourceFromAnnotation(
1451
1455
switch targetField .ShapeRef .Shape .Type {
1452
1456
case "list" , "structure" , "map" :
1453
1457
panic ("primary identifier '" + targetField .Path + "' must be a scalar type since NameOrID is a string" )
1454
- default :
1455
- break
1456
1458
}
1457
1459
1458
1460
targetVarPath := fmt .Sprintf ("%s%s" , targetVarName , memberPath )
1459
- if isPrimaryIdentifier {
1460
- primaryKeyOut += requiredFieldGuardContructor (sourceVarName , targetField .Names .CamelLower , indentLevel )
1461
+ if inputShape . IsRequired ( memberName ) || isPrimaryIdentifier {
1462
+ primaryKeyOut += requiredFieldGuardContructor (fmt . Sprintf ( "f%d" , memberIndex ), sourceVarName , targetField .Names .CamelLower , indentLevel )
1461
1463
primaryKeyOut += setResourceIdentifierPrimaryIdentifierAnn (cfg , r ,
1464
+ fmt .Sprintf ("&f%d" , memberIndex ),
1462
1465
targetField ,
1463
1466
targetVarPath ,
1464
1467
sourceVarName ,
1465
- indentLevel )
1468
+ indentLevel ,
1469
+ )
1466
1470
} else {
1467
1471
additionalKeyOut += setResourceIdentifierAdditionalKeyAnn (
1468
1472
cfg , r ,
@@ -1471,7 +1475,8 @@ func PopulateResourceFromAnnotation(
1471
1475
targetVarPath ,
1472
1476
sourceVarName ,
1473
1477
names .New (fieldName ).CamelLower ,
1474
- indentLevel )
1478
+ indentLevel ,
1479
+ )
1475
1480
}
1476
1481
}
1477
1482
@@ -1539,6 +1544,8 @@ func setResourceIdentifierPrimaryIdentifier(
1539
1544
func setResourceIdentifierPrimaryIdentifierAnn (
1540
1545
cfg * ackgenconfig.Config ,
1541
1546
r * model.CRD ,
1547
+ // The variable used for primary key
1548
+ requiredFieldVarName string ,
1542
1549
// The field that will be set on the target variable
1543
1550
targetField * model.Field ,
1544
1551
// The variable name that we want to set a value to
@@ -1548,12 +1555,11 @@ func setResourceIdentifierPrimaryIdentifierAnn(
1548
1555
// Number of levels of indentation to use
1549
1556
indentLevel int ,
1550
1557
) string {
1551
- adaptedMemberPath := fmt .Sprintf ("&tmp" )
1552
1558
qualifiedTargetVar := fmt .Sprintf ("%s.%s" , targetVarName , targetField .Path )
1553
1559
1554
1560
return setResourceForScalar (
1555
1561
qualifiedTargetVar ,
1556
- adaptedMemberPath ,
1562
+ requiredFieldVarName ,
1557
1563
targetField .ShapeRef ,
1558
1564
indentLevel ,
1559
1565
false ,
0 commit comments