Skip to content

Commit 7fa1166

Browse files
committed
implement SetSDK for Secret nested fields
Implements the `pkg/generate/code.SetSDK()` function for all nested fields that are SecretKeyReference types. This involved passing a field path down into the recursive `setSDKForXXX()` functions to keep track of where we were in the processing of nested fields. We look up whether a field is a SecretKeyReference by calling the `pkg/model.CRD:IsSecretField()` method, which accepts a field path, which is why we needed to pass this field path down into the recursive code generators. Issue aws-controllers-k8s/community#743
1 parent 22afd59 commit 7fa1166

File tree

2 files changed

+244
-2
lines changed

2 files changed

+244
-2
lines changed

pkg/generate/code/set_sdk.go

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,26 @@ func SetSDK(
226226
if found {
227227
sourceAdaptedVarName += cfg.PrefixConfig.SpecField
228228
} else {
229-
f, found = r.StatusFields[memberName]
229+
f, found = r.StatusFields[renamedName]
230230
if !found {
231231
// TODO(jaypipes): check generator config for exceptions?
232232
continue
233233
}
234234
sourceAdaptedVarName += cfg.PrefixConfig.StatusField
235235
}
236236
sourceAdaptedVarName += "." + f.Names.Camel
237+
sourceFieldPath := f.Names.Camel
238+
239+
if r.IsSecretField(memberName) {
240+
out += setSDKForSecret(
241+
cfg, r,
242+
memberName,
243+
targetVarName,
244+
sourceAdaptedVarName,
245+
indentLevel,
246+
)
247+
continue
248+
}
237249

238250
if r.IsSecretField(memberName) {
239251
out += setSDKForSecret(
@@ -321,6 +333,7 @@ func SetSDK(
321333
cfg, r,
322334
memberName,
323335
memberVarName,
336+
sourceFieldPath,
324337
sourceAdaptedVarName,
325338
memberShapeRef,
326339
indentLevel+1,
@@ -330,6 +343,7 @@ func SetSDK(
330343
memberName,
331344
targetVarName,
332345
inputShape.Type,
346+
sourceFieldPath,
333347
memberVarName,
334348
memberShapeRef,
335349
indentLevel+1,
@@ -341,6 +355,7 @@ func SetSDK(
341355
memberName,
342356
targetVarName,
343357
inputShape.Type,
358+
sourceFieldPath,
344359
sourceAdaptedVarName,
345360
memberShapeRef,
346361
indentLevel+1,
@@ -508,6 +523,7 @@ func SetSDKGetAttributes(
508523
memberName,
509524
targetVarName,
510525
inputShape.Type,
526+
cleanMemberName,
511527
sourceVarPath,
512528
field.ShapeRef,
513529
indentLevel+1,
@@ -702,6 +718,7 @@ func SetSDKSetAttributes(
702718
memberName,
703719
targetVarName,
704720
inputShape.Type,
721+
cleanMemberName,
705722
sourceVarPath,
706723
field.ShapeRef,
707724
indentLevel+1,
@@ -724,6 +741,8 @@ func setSDKForContainer(
724741
targetFieldName string,
725742
// The variable name that we want to set a value to
726743
targetVarName string,
744+
// The path to the field that we access our source value from
745+
sourceFieldPath string,
727746
// The struct or struct field that we access our source value from
728747
sourceVarName string,
729748
// ShapeRef of the target struct field
@@ -737,6 +756,7 @@ func setSDKForContainer(
737756
targetFieldName,
738757
targetVarName,
739758
targetShapeRef,
759+
sourceFieldPath,
740760
sourceVarName,
741761
indentLevel,
742762
)
@@ -746,6 +766,7 @@ func setSDKForContainer(
746766
targetFieldName,
747767
targetVarName,
748768
targetShapeRef,
769+
sourceFieldPath,
749770
sourceVarName,
750771
indentLevel,
751772
)
@@ -755,6 +776,7 @@ func setSDKForContainer(
755776
targetFieldName,
756777
targetVarName,
757778
targetShapeRef,
779+
sourceFieldPath,
758780
sourceVarName,
759781
indentLevel,
760782
)
@@ -764,6 +786,7 @@ func setSDKForContainer(
764786
targetFieldName,
765787
targetVarName,
766788
targetShapeRef.Shape.Type,
789+
sourceFieldPath,
767790
sourceVarName,
768791
targetShapeRef,
769792
indentLevel,
@@ -842,6 +865,8 @@ func setSDKForStruct(
842865
targetVarName string,
843866
// Shape Ref of the target struct field
844867
targetShapeRef *awssdkmodel.ShapeRef,
868+
// The path to the field that we access our source value from
869+
sourceFieldPath string,
845870
// The struct or struct field that we access our source value from
846871
sourceVarName string,
847872
indentLevel int,
@@ -855,14 +880,28 @@ func setSDKForStruct(
855880
memberShape := memberShapeRef.Shape
856881
cleanMemberNames := names.New(memberName)
857882
cleanMemberName := cleanMemberNames.Camel
858-
memberVarName := fmt.Sprintf("%sf%d", targetVarName, memberIndex)
859883
sourceAdaptedVarName := sourceVarName + "." + cleanMemberName
884+
memberFieldPath := sourceFieldPath + "." + cleanMemberName
885+
if r.IsSecretField(memberFieldPath) {
886+
out += setSDKForSecret(
887+
cfg, r,
888+
memberName,
889+
targetVarName,
890+
sourceAdaptedVarName,
891+
indentLevel,
892+
)
893+
continue
894+
}
860895
out += fmt.Sprintf(
861896
"%sif %s != nil {\n", indent, sourceAdaptedVarName,
862897
)
863898
switch memberShape.Type {
864899
case "list", "structure", "map":
865900
{
901+
memberVarName := fmt.Sprintf(
902+
"%sf%d",
903+
targetVarName, memberIndex,
904+
)
866905
out += varEmptyConstructorSDKType(
867906
cfg, r,
868907
memberVarName,
@@ -873,6 +912,7 @@ func setSDKForStruct(
873912
cfg, r,
874913
memberName,
875914
memberVarName,
915+
memberFieldPath,
876916
sourceAdaptedVarName,
877917
memberShapeRef,
878918
indentLevel+1,
@@ -882,6 +922,7 @@ func setSDKForStruct(
882922
memberName,
883923
targetVarName,
884924
targetShape.Type,
925+
memberFieldPath,
885926
memberVarName,
886927
memberShapeRef,
887928
indentLevel+1,
@@ -893,6 +934,7 @@ func setSDKForStruct(
893934
memberName,
894935
targetVarName,
895936
targetShape.Type,
937+
memberFieldPath,
896938
sourceAdaptedVarName,
897939
memberShapeRef,
898940
indentLevel+1,
@@ -916,6 +958,8 @@ func setSDKForSlice(
916958
targetVarName string,
917959
// Shape Ref of the target struct field
918960
targetShapeRef *awssdkmodel.ShapeRef,
961+
// The path to the field that we access our source value from
962+
sourceFieldPath string,
919963
// The struct or struct field that we access our source value from
920964
sourceVarName string,
921965
indentLevel int,
@@ -948,6 +992,7 @@ func setSDKForSlice(
948992
cfg, r,
949993
containerFieldName,
950994
elemVarName,
995+
sourceFieldPath+".",
951996
iterVarName,
952997
&targetShape.MemberRef,
953998
indentLevel+1,
@@ -976,6 +1021,8 @@ func setSDKForMap(
9761021
targetVarName string,
9771022
// Shape Ref of the target struct field
9781023
targetShapeRef *awssdkmodel.ShapeRef,
1024+
// The path to the field that we access our source value from
1025+
sourceFieldPath string,
9791026
// The struct or struct field that we access our source value from
9801027
sourceVarName string,
9811028
indentLevel int,
@@ -1009,6 +1056,7 @@ func setSDKForMap(
10091056
cfg, r,
10101057
containerFieldName,
10111058
valVarName,
1059+
sourceFieldPath+".",
10121060
valIterVarName,
10131061
&targetShape.ValueRef,
10141062
indentLevel+1,
@@ -1117,6 +1165,8 @@ func setSDKForScalar(
11171165
targetVarName string,
11181166
// The type of shape of the target variable
11191167
targetVarType string,
1168+
// The path to the field that we access our source value from
1169+
sourceFieldPath string,
11201170
// The struct or struct field that we access our source value from
11211171
sourceVarName string,
11221172
shapeRef *awssdkmodel.ShapeRef,

0 commit comments

Comments
 (0)