@@ -37,38 +37,22 @@ func FindLateInitializedFieldNames(
3737) string {
3838 out := ""
3939 indent := strings .Repeat ("\t " , indentLevel )
40- sortedFieldNames , _ := getSortedLateInitFieldsAndConfig (cfg , r )
41- if len (sortedFieldNames ) > 0 {
42- out += fmt .Sprintf ("%svar %s = []string{" , indent , resVarName )
43- for _ , fName := range sortedFieldNames {
44- out += fmt .Sprintf ("%q," , fName )
45- }
46- out += "}\n "
47- } else {
48- out += fmt .Sprintf ("%svar %s = []string{}\n " , indent , resVarName )
40+ var lateInitFieldNames []string
41+ lateInitConfigs := cfg .GetLateInitConfigs (r .Names .Original )
42+ for fieldName := range lateInitConfigs {
43+ lateInitFieldNames = append (lateInitFieldNames , fieldName )
4944 }
50- return out
51- }
52-
53- // getSortedLateInitFieldsAndConfig returns the field names in alphabetically sorted order which have LateInitialization
54- // configuration inside generator config and also a map from fieldName to LateInitializationConfig.
55- func getSortedLateInitFieldsAndConfig (
56- cfg * ackgenconfig.Config ,
57- r * model.CRD ,
58- ) ([]string , map [string ]* ackgenconfig.LateInitializeConfig ) {
59- fieldNameToConfig := cfg .GetResourceFields (r .Names .Original )
60- fieldNameToLateInitConfig := make (map [string ]* ackgenconfig.LateInitializeConfig )
61- sortedLateInitFieldNames := make ([]string , 0 )
62- if len (fieldNameToConfig ) > 0 {
63- for fName , fConfig := range fieldNameToConfig {
64- if fConfig != nil && fConfig .LateInitialize != nil {
65- fieldNameToLateInitConfig [fName ] = fConfig .LateInitialize
66- sortedLateInitFieldNames = append (sortedLateInitFieldNames , fName )
67- }
68- }
69- sort .Strings (sortedLateInitFieldNames )
45+ if len (lateInitFieldNames ) == 0 {
46+ return fmt .Sprintf ("%svar %s = []string{}\n " , indent , resVarName )
47+ }
48+ // sort the slice to help with short circuiting AWSResourceManager.LateInitialize()
49+ sort .Strings (lateInitFieldNames )
50+ out += fmt .Sprintf ("%svar %s = []string{" , indent , resVarName )
51+ for _ , fName := range lateInitFieldNames {
52+ out += fmt .Sprintf ("%q," , fName )
7053 }
71- return sortedLateInitFieldNames , fieldNameToLateInitConfig
54+ out += "}\n "
55+ return out
7256}
7357
7458// LateInitializeFromReadOne returns the gocode to set LateInitialization fields from the ReadOne output
@@ -147,14 +131,19 @@ func LateInitializeFromReadOne(
147131) string {
148132 out := ""
149133 indent := strings .Repeat ("\t " , indentLevel )
150- lateInitializedFieldNames , _ := getSortedLateInitFieldsAndConfig (cfg , r )
151- if len (lateInitializedFieldNames ) == 0 {
134+ var lateInitFieldNames []string
135+ lateInitConfigs := cfg .GetLateInitConfigs (r .Names .Original )
136+ for fieldName := range lateInitConfigs {
137+ lateInitFieldNames = append (lateInitFieldNames , fieldName )
138+ }
139+ if len (lateInitFieldNames ) == 0 {
152140 return fmt .Sprintf ("%sreturn %s" , indent , targetResVarName )
153141 }
142+ sort .Strings (lateInitFieldNames )
154143 out += fmt .Sprintf ("%sobservedKo := rm.concreteResource(%s).ko.DeepCopy()\n " , indent , sourceResVarName )
155144 out += fmt .Sprintf ("%slatestKo := rm.concreteResource(%s).ko.DeepCopy()\n " , indent , targetResVarName )
156145 // TODO(vijat@): Add validation for correct field path in lateInitializedFieldNames
157- for _ , fName := range lateInitializedFieldNames {
146+ for _ , fName := range lateInitFieldNames {
158147 // split the field name by period
159148 // each substring represents a field.
160149 fNameParts := strings .Split (fName , "." )
@@ -283,13 +272,17 @@ func IncompleteLateInitialization(
283272) string {
284273 out := ""
285274 indent := strings .Repeat ("\t " , indentLevel )
286- sortedLateInitFieldNames , _ := getSortedLateInitFieldsAndConfig (cfg , r )
287- if len (sortedLateInitFieldNames ) == 0 {
288- out += fmt .Sprintf ("%sreturn false" , indent )
289- return out
275+ var lateInitFieldNames []string
276+ lateInitConfigs := cfg .GetLateInitConfigs (r .Names .Original )
277+ for fieldName := range lateInitConfigs {
278+ lateInitFieldNames = append (lateInitFieldNames , fieldName )
279+ }
280+ if len (lateInitFieldNames ) == 0 {
281+ return fmt .Sprintf ("%sreturn false" , indent )
290282 }
283+ sort .Strings (lateInitFieldNames )
291284 out += fmt .Sprintf ("%sko := rm.concreteResource(%s).ko.DeepCopy()\n " , indent , resVarName )
292- for _ , fName := range sortedLateInitFieldNames {
285+ for _ , fName := range lateInitFieldNames {
293286 // split the field name by period
294287 // each substring represents a field.
295288 fNameParts := strings .Split (fName , "." )
0 commit comments