Skip to content

Commit 3a50ed4

Browse files
authored
Resources synced variables renames (#276)
Issue #, if available: Description of changes Addressing recent comments in #247: - rename `condition` to `condCfg` - rename `c` to `fp` By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 6da05cd commit 3a50ed4

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

pkg/generate/code/synced.go

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import (
5151
func ResourceIsSynced(
5252
cfg *ackgenconfig.Config,
5353
r *model.CRD,
54-
// String
54+
// resource variable name
5555
resVarName string,
5656
// Number of levels of indentation to use
5757
indentLevel int,
@@ -62,24 +62,24 @@ func ResourceIsSynced(
6262
return out
6363
}
6464

65-
for _, condition := range resConfig.Synced.When {
66-
if condition.Path == nil || *condition.Path == "" {
65+
for _, condCfg := range resConfig.Synced.When {
66+
if condCfg.Path == nil || *condCfg.Path == "" {
6767
panic("Received an empty sync condition path. 'SyncCondition.Path' must be provided.")
6868
}
69-
if len(condition.In) == 0 {
69+
if len(condCfg.In) == 0 {
7070
panic("'SyncCondition.In' must be provided.")
7171
}
72-
fp := fieldpath.FromString(*condition.Path)
73-
field, err := getTopLevelField(r, *condition.Path)
72+
fp := fieldpath.FromString(*condCfg.Path)
73+
field, err := getTopLevelField(r, *condCfg.Path)
7474
if err != nil {
75-
msg := fmt.Sprintf("cannot find top level field of path '%s': %v", *condition.Path, err)
75+
msg := fmt.Sprintf("cannot find top level field of path '%s': %v", *condCfg.Path, err)
7676
panic(msg)
7777
}
7878
candidatesVarName := fmt.Sprintf("%sCandidates", field.Names.CamelLower)
7979
if fp.Size() == 2 {
80-
out += scalarFieldEqual(resVarName, candidatesVarName, field.ShapeRef.GoTypeElem(), condition)
80+
out += scalarFieldEqual(resVarName, candidatesVarName, field.ShapeRef.GoTypeElem(), condCfg)
8181
} else {
82-
out += fieldPathSafeEqual(resVarName, candidatesVarName, field, condition)
82+
out += fieldPathSafeEqual(resVarName, candidatesVarName, field, condCfg)
8383
}
8484
}
8585

@@ -113,21 +113,26 @@ func getTopLevelField(r *model.CRD, fieldPath string) (*model.Field, error) {
113113
}
114114

115115
// scalarFieldEqual returns Go code that compares a scalar field to a given set of values.
116-
func scalarFieldEqual(resVarName string, candidatesVarName string, goType string, condition ackgenconfig.SyncedCondition) string {
116+
func scalarFieldEqual(
117+
resVarName string,
118+
candidatesVarName string,
119+
goType string,
120+
condCfg ackgenconfig.SyncedCondition,
121+
) string {
117122
out := ""
118-
fieldPath := fmt.Sprintf("%s.%s", resVarName, *condition.Path)
123+
fieldPath := fmt.Sprintf("%s.%s", resVarName, *condCfg.Path)
119124

120125
valuesSlice := ""
121126
switch goType {
122127
case "string":
123128
// []string{"AVAILABLE", "ACTIVE"}
124-
valuesSlice = fmt.Sprintf("[]string{\"%s\"}", strings.Join(condition.In, "\", \""))
129+
valuesSlice = fmt.Sprintf("[]string{\"%s\"}", strings.Join(condCfg.In, "\", \""))
125130
case "int64", "PositiveLongObject", "Long":
126131
// []int64{1, 2}
127-
valuesSlice = fmt.Sprintf("[]int{%s}", strings.Join(condition.In, ", "))
132+
valuesSlice = fmt.Sprintf("[]int{%s}", strings.Join(condCfg.In, ", "))
128133
case "bool":
129134
// []bool{false}
130-
valuesSlice = fmt.Sprintf("[]bool{%s}", condition.In)
135+
valuesSlice = fmt.Sprintf("[]bool{%s}", condCfg.In)
131136
default:
132137
panic("not supported type " + goType)
133138
}
@@ -157,11 +162,11 @@ func fieldPathSafeEqual(
157162
resVarName string,
158163
candidatesVarName string,
159164
field *model.Field,
160-
condition ackgenconfig.SyncedCondition,
165+
condCfg ackgenconfig.SyncedCondition,
161166
) string {
162167
out := ""
163-
rootPath := fmt.Sprintf("%s.%s", resVarName, strings.Split(*condition.Path, ".")[0])
164-
knownShapesPath := strings.Join(strings.Split(*condition.Path, ".")[1:], ".")
168+
rootPath := fmt.Sprintf("%s.%s", resVarName, strings.Split(*condCfg.Path, ".")[0])
169+
knownShapesPath := strings.Join(strings.Split(*condCfg.Path, ".")[1:], ".")
165170

166171
fp := fieldpath.FromString(knownShapesPath)
167172
shapes := fp.IterShapeRefs(field.ShapeRef)
@@ -171,7 +176,7 @@ func fieldPathSafeEqual(
171176
if index == len(shapes)-1 {
172177
// Some aws-sdk-go scalar shapes don't contain the real name of a shape
173178
// In this case we use the full path given in condition.Path
174-
subFieldPath = fmt.Sprintf("%s.%s", resVarName, *condition.Path)
179+
subFieldPath = fmt.Sprintf("%s.%s", resVarName, *condCfg.Path)
175180
} else {
176181
subFieldPath += "." + shape.Shape.ShapeName
177182
}
@@ -182,13 +187,13 @@ func fieldPathSafeEqual(
182187
// }
183188
out += "\t}\n"
184189
}
185-
out += scalarFieldEqual(resVarName, candidatesVarName, shapes[len(shapes)-1].GoTypeElem(), condition)
190+
out += scalarFieldEqual(resVarName, candidatesVarName, shapes[len(shapes)-1].GoTypeElem(), condCfg)
186191
return out
187192
}
188193

189194
func fieldPathContainsMapOrArray(fieldPath string, shapeRef *awssdkmodel.ShapeRef) bool {
190-
c := fieldpath.FromString(fieldPath)
191-
sr := c.ShapeRef(shapeRef)
195+
fp := fieldpath.FromString(fieldPath)
196+
sr := fp.ShapeRef(shapeRef)
192197

193198
if sr == nil {
194199
return false
@@ -197,8 +202,8 @@ func fieldPathContainsMapOrArray(fieldPath string, shapeRef *awssdkmodel.ShapeRe
197202
return true
198203
}
199204
if sr.ShapeName == "structure" {
200-
fieldName := c.PopFront()
201-
return fieldPathContainsMapOrArray(c.Copy().At(1), sr.Shape.MemberRefs[fieldName])
205+
fieldName := fp.PopFront()
206+
return fieldPathContainsMapOrArray(fp.Copy().At(1), sr.Shape.MemberRefs[fieldName])
202207
}
203208
return false
204209
}

0 commit comments

Comments
 (0)