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