@@ -801,6 +801,19 @@ func SetResourceGetAttributes(
801
801
// r.ko.Spec.ServiceNamespace = f1
802
802
// }
803
803
// ```
804
+ // An example of code that uses the ARN:
805
+ //
806
+ // ```
807
+ // if r.ko.Status.ACKResourceMetadata == nil {
808
+ // r.ko.Status.ACKResourceMetadata = &ackv1alpha1.ResourceMetadata{}
809
+ // }
810
+ // r.ko.Status.ACKResourceMetadata.ARN = identifier.ARN
811
+ //
812
+ // f0, f0ok := identifier.AdditionalKeys["modelPackageName"]
813
+ // if f0ok {
814
+ // r.ko.Spec.ModelPackageName = &f0
815
+ // }
816
+ // ```
804
817
func SetResourceIdentifiers (
805
818
cfg * ackgenconfig.Config ,
806
819
r * model.CRD ,
@@ -832,15 +845,29 @@ func SetResourceIdentifiers(
832
845
return ""
833
846
}
834
847
835
- primaryKeyOut := "\n "
836
- arnOut := ""
848
+ primaryKeyOut := ""
849
+ primaryKeyConditionalOut := "\n "
837
850
additionalKeyOut := "\n "
838
851
839
852
indent := strings .Repeat ("\t " , indentLevel )
840
853
841
- primaryKeyOut += fmt .Sprintf ("%sif %s.NameOrID == \" \" {\n " , indent , sourceVarName )
842
- primaryKeyOut += fmt .Sprintf ("%s\t return ackerrors.MissingNameIdentifier\n " , indent )
843
- primaryKeyOut += fmt .Sprintf ("%s}\n " , indent )
854
+ // if identifier.NameOrID == "" {
855
+ // return ackerrors.MissingNameIdentifier
856
+ // }
857
+ primaryKeyConditionalOut += fmt .Sprintf ("%sif %s.NameOrID == \" \" {\n " , indent , sourceVarName )
858
+ primaryKeyConditionalOut += fmt .Sprintf ("%s\t return ackerrors.MissingNameIdentifier\n " , indent )
859
+ primaryKeyConditionalOut += fmt .Sprintf ("%s}\n " , indent )
860
+
861
+ arnOut := "\n "
862
+ // if r.ko.Status.ACKResourceMetadata == nil {
863
+ // r.ko.Status.ACKResourceMetadata = &ackv1alpha1.ResourceMetadata{}
864
+ // }
865
+ // r.ko.Status.ACKResourceMetadata.ARN = identifier.ARN
866
+ arnOut += ackResourceMetadataGuardConstructor (fmt .Sprintf ("%s.Status" , targetVarName ), indentLevel )
867
+ arnOut += fmt .Sprintf (
868
+ "%s%s.Status.ACKResourceMetadata.ARN = %s.ARN\n " ,
869
+ indent , targetVarName , sourceVarName ,
870
+ )
844
871
845
872
primaryIdentifier := ""
846
873
@@ -852,33 +879,20 @@ func SetResourceIdentifiers(
852
879
853
880
// Determine the "primary identifier" based on the names of each field
854
881
if primaryIdentifier == "" {
855
- primaryIdentifierLookup := []string {
856
- "Name" ,
857
- "Names" ,
858
- r .Names .Original + "Name" ,
859
- r .Names .Original + "Names" ,
860
- r .Names .Original + "Id" ,
861
- r .Names .Original + "Ids" ,
862
- }
863
-
864
- for _ , memberName := range inputShape .MemberNames () {
865
- if util .InStrings (memberName , primaryIdentifierLookup ) {
866
- if primaryIdentifier == "" {
867
- primaryIdentifier = memberName
868
- } else {
869
- panic ("Found multiple possible primary identifiers for " +
870
- r .Names .Original + ". Set " +
871
- "`primary_identifier_field_name` for the " + op .Name +
872
- " operation in the generator config." )
873
- }
874
- }
875
- }
882
+ identifiers := FindIdentifiersInShape (r , inputShape )
876
883
877
- // Still haven't determined the identifier? Panic
878
- if primaryIdentifier == "" {
884
+ switch len ( identifiers ) {
885
+ case 0 :
879
886
panic ("Could not find primary identifier for " + r .Names .Original +
880
887
". Set `primary_identifier_field_name` for the " + op .Name +
881
888
" operation in the generator config." )
889
+ case 1 :
890
+ primaryIdentifier = identifiers [0 ]
891
+ default :
892
+ panic ("Found multiple possible primary identifiers for " +
893
+ r .Names .Original + ". Set " +
894
+ "`primary_identifier_field_name` for the " + op .Name +
895
+ " operation in the generator config." )
882
896
}
883
897
}
884
898
@@ -894,11 +908,11 @@ func SetResourceIdentifiers(
894
908
}
895
909
896
910
memberShapeRef , _ := inputShape .MemberRefs [memberName ]
897
- memberShape := memberShapeRef .Shape
911
+ sourceMemberShape := memberShapeRef .Shape
898
912
899
913
// Only strings are currently accepted as valid inputs for
900
914
// additional key fields
901
- if memberShape .Type != "string" {
915
+ if sourceMemberShape .Type != "string" {
902
916
continue
903
917
}
904
918
@@ -908,40 +922,54 @@ func SetResourceIdentifiers(
908
922
}
909
923
910
924
if r .IsPrimaryARNField (memberName ) {
911
- // r.ko.Status.ACKResourceMetadata.ARN = identifier.ARN
912
- arnOut += fmt .Sprintf (
913
- "\n %s%s.Status.ACKResourceMetadata.ARN = %s.ARN\n " ,
914
- indent , targetVarName , sourceVarName ,
915
- )
916
925
continue
917
-
918
926
}
927
+
919
928
// Check that the field has potentially been renamed
920
929
renamedName , _ := r .InputFieldRename (
921
930
op .Name , memberName ,
922
931
)
923
932
924
933
isPrimaryIdentifier := memberName == primaryIdentifier
925
934
cleanMemberNames := names .New (renamedName )
926
- cleanMemberName := cleanMemberNames .Camel
927
935
928
936
memberPath := ""
929
- _ , inSpec := r .SpecFields [renamedName ]
930
- _ , inStatus := r .StatusFields [renamedName ]
937
+ var targetField * model.Field
938
+
939
+ specField , inSpec := r .SpecFields [renamedName ]
940
+ statusField , inStatus := r .StatusFields [renamedName ]
931
941
switch {
932
942
case inSpec :
933
943
memberPath = cfg .PrefixConfig .SpecField
944
+ targetField = specField
934
945
case inStatus :
935
946
memberPath = cfg .PrefixConfig .StatusField
947
+ targetField = statusField
936
948
case isPrimaryIdentifier :
937
949
panic ("Primary identifier field '" + memberName + "' in operation '" + op .Name + "' cannot be found in either spec or status." )
938
950
default :
939
951
continue
940
952
}
941
953
954
+ targetVarPath := fmt .Sprintf ("%s%s" , targetVarName , memberPath )
942
955
if isPrimaryIdentifier {
943
- // r.ko.Status.BrokerID = identifier.NameOrID
944
- primaryKeyOut += fmt .Sprintf ("%s%s%s.%s = &%s.NameOrID\n " , indent , targetVarName , memberPath , cleanMemberName , sourceVarName )
956
+ // r.ko.Status.BrokerID = &identifier.NameOrID
957
+ adaptedMemberPath := fmt .Sprintf ("&%s.NameOrID" , sourceVarName )
958
+ switch sourceMemberShape .Type {
959
+ case "list" , "structure" , "map" :
960
+ // TODO(RedbackThomson): Add support for slices and maps
961
+ // in ReadMany operations
962
+ break
963
+ default :
964
+ primaryKeyOut += setResourceForScalar (
965
+ cfg , r ,
966
+ targetField .Path ,
967
+ targetVarPath ,
968
+ adaptedMemberPath ,
969
+ memberShapeRef ,
970
+ indentLevel ,
971
+ )
972
+ }
945
973
} else {
946
974
// f0, f0ok := identifier.AdditionalKeys["scalableDimension"]
947
975
// if f0ok {
@@ -955,18 +983,33 @@ func SetResourceIdentifiers(
955
983
// throwing an error accessible to the user
956
984
additionalKeyOut += fmt .Sprintf ("%s%s, %sok := %s\n " , indent , fieldIndexName , fieldIndexName , sourceAdaptedVarName )
957
985
additionalKeyOut += fmt .Sprintf ("%sif %sok {\n " , indent , fieldIndexName )
958
- additionalKeyOut += fmt .Sprintf ("%s\t %s%s.%s = &%s\n " , indent , targetVarName , memberPath , cleanMemberName , fieldIndexName )
986
+
987
+ switch sourceMemberShape .Type {
988
+ case "list" , "structure" , "map" :
989
+ // TODO(RedbackThomson): Add support for slices and maps
990
+ // in ReadMany operations
991
+ break
992
+ default :
993
+ additionalKeyOut += setResourceForScalar (
994
+ cfg , r ,
995
+ targetField .Path ,
996
+ targetVarPath ,
997
+ fmt .Sprintf ("&%s" , fieldIndexName ),
998
+ memberShapeRef ,
999
+ indentLevel + 1 ,
1000
+ )
1001
+ }
959
1002
additionalKeyOut += fmt .Sprintf ("%s}\n " , indent )
960
1003
961
1004
additionalKeyCount ++
962
1005
}
963
1006
}
964
1007
965
1008
// Only use at most one of ARN or nameOrID as primary identifier outputs
966
- if arnOut ! = "" {
1009
+ if primaryIdentifier == "ARN" || primaryKeyOut = = "" {
967
1010
return arnOut + additionalKeyOut
968
1011
}
969
- return primaryKeyOut + additionalKeyOut
1012
+ return primaryKeyConditionalOut + primaryKeyOut + additionalKeyOut
970
1013
}
971
1014
972
1015
// setResourceForContainer returns a string of Go code that sets the value of a
0 commit comments