Skip to content

Commit 37f4ba2

Browse files
authored
Functionality to make ignore work (#464)
**Issue** This PR is related to fix the issue generating after executing PR #462 **Summary** To disregard fields marked with `set: ignore` for nested fields. This PR also enhances the functionality of `set:ignore` to support the exclusion of fields from `set_sdk`. **Description** The PR fixes the issue coming after a new custom field is created and has `set.ignore` set to `true` for it. Even after setting the field to ignore, the field is getting used to set the SDK fields. In this case particularly, the new field was defined as shown below: ``` Code.S3SHA256: type: string set: - method: Create ignore: true ``` And the issue it is causing is under `sdk.go/newCreateRequestPayload` function, where it is being set, as shown below: ``` if r.ko.Spec.Code.S3SHA256 != nil { f1.SetS3SHA256(*r.ko.Spec.Code.S3SHA256) } ``` The goal is to ignore this particular line of code. And also to improve the basic functionality of `set.ignore`. The `set.ignore` functionality was till now supporting exclusion of fields only when the field was used to set for the resource. This PR checks the `set.ignore` for field and ignores the following code that creates request payload if it is set to `true`. **Limitations** Currently this PR checks the `set.ignore` field only for members of the field whose type is `structure`. **Acknowledgment** By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent e8df4d5 commit 37f4ba2

File tree

9 files changed

+291
-9
lines changed

9 files changed

+291
-9
lines changed

pkg/config/field.go

+61-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package config
1515

1616
import (
17+
"encoding/json"
1718
"strings"
1819
)
1920

@@ -226,7 +227,38 @@ type SetFieldConfig struct {
226227
// - method: Update
227228
// ignore: true
228229
// ```
229-
Ignore bool `json:"ignore,omitempty"`
230+
Ignore BoolOrString `json:"ignore,omitempty"`
231+
}
232+
233+
// IgnoreResourceSetter returns true if the field should be ignored when setting
234+
// the resource's field value from the SDK output shape.
235+
func (s *SetFieldConfig) IgnoreResourceSetter() bool {
236+
if s.Ignore.Bool != nil {
237+
return *s.Ignore.Bool
238+
}
239+
if s.Ignore.String != nil {
240+
return strings.EqualFold(*s.Ignore.String, "from") || strings.EqualFold(*s.Ignore.String, "all")
241+
}
242+
return false
243+
}
244+
245+
// IgnoreSDKSetter returns true if the field should be ignored when setting the
246+
// SDK field value from the resource's field.
247+
func (s *SetFieldConfig) IgnoreSDKSetter() bool {
248+
if s.Ignore.Bool != nil {
249+
return false
250+
}
251+
if s.Ignore.String != nil {
252+
return strings.EqualFold(*s.Ignore.String, "to") || strings.EqualFold(*s.Ignore.String, "all")
253+
}
254+
return false
255+
}
256+
257+
// IsAllIgnored returns true if the field should be ignored when setting the
258+
// resource's field value from the SDK output shape and when setting the SDK
259+
// field value from the resource's field.
260+
func (s *SetFieldConfig) IsAllIgnored() bool {
261+
return s.IgnoreResourceSetter() && s.IgnoreSDKSetter()
230262
}
231263

232264
// CompareFieldConfig informs the code generator how to compare two values of a
@@ -486,3 +518,31 @@ func (c *Config) GetLateInitConfigByPath(resourceName string, fieldPath string)
486518
}
487519
return nil
488520
}
521+
522+
// BoolOrString is a type that can be unmarshalled from either a boolean or a
523+
// string.
524+
type BoolOrString struct {
525+
// Bool is the boolean value of the field. This field is non-nil if the
526+
// field was unmarshalled from a boolean value.
527+
Bool *bool
528+
// String is the string value of the field. This field is non-nil if the
529+
// field was unmarshalled from a string value.
530+
String *string
531+
}
532+
533+
// UnmarshalJSON unmarshals a BoolOrString from a YAML/JSON byte slice.
534+
func (a *BoolOrString) UnmarshalJSON(b []byte) error {
535+
var str string
536+
err := json.Unmarshal(b, &str)
537+
if err != nil {
538+
var boolean bool
539+
err := json.Unmarshal(b, &boolean)
540+
if err != nil {
541+
return err
542+
}
543+
a.Bool = &boolean
544+
} else {
545+
a.String = &str
546+
}
547+
return nil
548+
}

pkg/generate/ack/controller.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ var (
122122
return code.SetSDK(r.Config(), r, ackmodel.OpTypeDelete, sourceVarName, targetVarName, indentLevel)
123123
},
124124
"GoCodeSetSDKForStruct": func(r *ackmodel.CRD, targetFieldName string, targetVarName string, targetShapeRef *awssdkmodel.ShapeRef, sourceFieldPath string, sourceVarName string, indentLevel int) string {
125-
return code.SetSDKForStruct(r.Config(), r, targetFieldName, targetVarName, targetShapeRef, sourceFieldPath, sourceVarName, indentLevel)
125+
return code.SetSDKForStruct(r.Config(), r, targetFieldName, targetVarName, targetShapeRef, sourceFieldPath, sourceVarName, model.OpTypeList, indentLevel)
126126
},
127127
"GoCodeSetResourceForStruct": func(r *ackmodel.CRD, targetFieldName string, targetVarName string, targetShapeRef *awssdkmodel.ShapeRef, sourceVarName string, sourceShapeRef *awssdkmodel.ShapeRef, indentLevel int) string {
128128
var setCfg *ackgenconfig.SetFieldConfig = nil
@@ -135,7 +135,7 @@ var (
135135
if ok {
136136
setCfg = f.GetSetterConfig(ackmodel.OpTypeList)
137137
}
138-
if setCfg != nil && setCfg.Ignore {
138+
if setCfg != nil && setCfg.IgnoreResourceSetter() {
139139
return ""
140140
}
141141
return code.SetResourceForStruct(r.Config(), r, targetVarName, targetShapeRef, setCfg, sourceVarName, sourceShapeRef, "", model.OpTypeList, indentLevel)

pkg/generate/code/set_resource.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ func SetResource(
231231
// field value...
232232
setCfg := f.GetSetterConfig(opType)
233233

234-
if setCfg != nil && setCfg.Ignore {
234+
if setCfg != nil && setCfg.IgnoreResourceSetter() {
235235
continue
236236
}
237237

@@ -588,7 +588,7 @@ func setResourceReadMany(
588588
// field value...
589589
setCfg := f.GetSetterConfig(model.OpTypeList)
590590

591-
if setCfg != nil && setCfg.Ignore {
591+
if setCfg != nil && setCfg.IgnoreResourceSetter() {
592592
continue
593593
}
594594

pkg/generate/code/set_sdk.go

+34
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ func SetSDK(
345345
sourceFieldPath,
346346
sourceAdaptedVarName,
347347
memberShapeRef,
348+
opType,
348349
indentLevel+1,
349350
)
350351
out += setSDKForScalar(
@@ -928,6 +929,7 @@ func setSDKForContainer(
928929
sourceVarName string,
929930
// ShapeRef of the target struct field
930931
targetShapeRef *awssdkmodel.ShapeRef,
932+
op model.OpType,
931933
indentLevel int,
932934
) string {
933935
switch targetShapeRef.Shape.Type {
@@ -939,6 +941,7 @@ func setSDKForContainer(
939941
targetShapeRef,
940942
sourceFieldPath,
941943
sourceVarName,
944+
op,
942945
indentLevel,
943946
)
944947
case "list":
@@ -949,6 +952,7 @@ func setSDKForContainer(
949952
targetShapeRef,
950953
sourceFieldPath,
951954
sourceVarName,
955+
op,
952956
indentLevel,
953957
)
954958
case "map":
@@ -959,6 +963,7 @@ func setSDKForContainer(
959963
targetShapeRef,
960964
sourceFieldPath,
961965
sourceVarName,
966+
op,
962967
indentLevel,
963968
)
964969
default:
@@ -1082,6 +1087,7 @@ func SetSDKForStruct(
10821087
sourceFieldPath string,
10831088
// The struct or struct field that we access our source value from
10841089
sourceVarName string,
1090+
op model.OpType,
10851091
indentLevel int,
10861092
) string {
10871093
out := ""
@@ -1096,6 +1102,29 @@ func SetSDKForStruct(
10961102
sourceAdaptedVarName := sourceVarName + "." + cleanMemberName
10971103
memberFieldPath := sourceFieldPath + "." + cleanMemberName
10981104

1105+
// todo: To make `ignore` functionality work for all fields that has `ignore` set to `true`,
1106+
// we need to add the below logic inside `SetSDK` function.
1107+
1108+
// To check if the field member has `ignore` set to `true`.
1109+
// This condition currently applies only for members of a field whose shape is `structure`
1110+
var setCfg *ackgenconfig.SetFieldConfig
1111+
f, ok := r.Fields[targetFieldName]
1112+
if ok {
1113+
mf, ok := f.MemberFields[memberName]
1114+
if ok {
1115+
setCfg = mf.GetSetterConfig(op)
1116+
if setCfg != nil && setCfg.IgnoreSDKSetter() {
1117+
continue
1118+
}
1119+
}
1120+
1121+
}
1122+
1123+
fallBackName := r.GetMatchingInputShapeFieldName(op, targetFieldName)
1124+
if fallBackName == memberName {
1125+
// TODO: implement @AmineHilaly
1126+
}
1127+
10991128
out += fmt.Sprintf(
11001129
"%sif %s != nil {\n", indent, sourceAdaptedVarName,
11011130
)
@@ -1119,6 +1148,7 @@ func SetSDKForStruct(
11191148
memberFieldPath,
11201149
sourceAdaptedVarName,
11211150
memberShapeRef,
1151+
op,
11221152
indentLevel+1,
11231153
)
11241154
out += setSDKForScalar(
@@ -1176,6 +1206,7 @@ func setSDKForSlice(
11761206
sourceFieldPath string,
11771207
// The struct or struct field that we access our source value from
11781208
sourceVarName string,
1209+
op model.OpType,
11791210
indentLevel int,
11801211
) string {
11811212
out := ""
@@ -1209,6 +1240,7 @@ func setSDKForSlice(
12091240
sourceFieldPath,
12101241
iterVarName,
12111242
&targetShape.MemberRef,
1243+
op,
12121244
indentLevel+1,
12131245
)
12141246
addressOfVar := ""
@@ -1239,6 +1271,7 @@ func setSDKForMap(
12391271
sourceFieldPath string,
12401272
// The struct or struct field that we access our source value from
12411273
sourceVarName string,
1274+
op model.OpType,
12421275
indentLevel int,
12431276
) string {
12441277
out := ""
@@ -1273,6 +1306,7 @@ func setSDKForMap(
12731306
sourceFieldPath,
12741307
valIterVarName,
12751308
&targetShape.ValueRef,
1309+
op,
12761310
indentLevel+1,
12771311
)
12781312
addressOfVar := ""

0 commit comments

Comments
 (0)