Skip to content

Commit

Permalink
Functionality to make ignore work (#464)
Browse files Browse the repository at this point in the history
**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.
  • Loading branch information
Vandita2020 authored Apr 8, 2024
1 parent e8df4d5 commit 37f4ba2
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 9 deletions.
62 changes: 61 additions & 1 deletion pkg/config/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package config

import (
"encoding/json"
"strings"
)

Expand Down Expand Up @@ -226,7 +227,38 @@ type SetFieldConfig struct {
// - method: Update
// ignore: true
// ```
Ignore bool `json:"ignore,omitempty"`
Ignore BoolOrString `json:"ignore,omitempty"`
}

// IgnoreResourceSetter returns true if the field should be ignored when setting
// the resource's field value from the SDK output shape.
func (s *SetFieldConfig) IgnoreResourceSetter() bool {
if s.Ignore.Bool != nil {
return *s.Ignore.Bool
}
if s.Ignore.String != nil {
return strings.EqualFold(*s.Ignore.String, "from") || strings.EqualFold(*s.Ignore.String, "all")
}
return false
}

// IgnoreSDKSetter returns true if the field should be ignored when setting the
// SDK field value from the resource's field.
func (s *SetFieldConfig) IgnoreSDKSetter() bool {
if s.Ignore.Bool != nil {
return false
}
if s.Ignore.String != nil {
return strings.EqualFold(*s.Ignore.String, "to") || strings.EqualFold(*s.Ignore.String, "all")
}
return false
}

// IsAllIgnored returns true if the field should be ignored when setting the
// resource's field value from the SDK output shape and when setting the SDK
// field value from the resource's field.
func (s *SetFieldConfig) IsAllIgnored() bool {
return s.IgnoreResourceSetter() && s.IgnoreSDKSetter()
}

// CompareFieldConfig informs the code generator how to compare two values of a
Expand Down Expand Up @@ -486,3 +518,31 @@ func (c *Config) GetLateInitConfigByPath(resourceName string, fieldPath string)
}
return nil
}

// BoolOrString is a type that can be unmarshalled from either a boolean or a
// string.
type BoolOrString struct {
// Bool is the boolean value of the field. This field is non-nil if the
// field was unmarshalled from a boolean value.
Bool *bool
// String is the string value of the field. This field is non-nil if the
// field was unmarshalled from a string value.
String *string
}

// UnmarshalJSON unmarshals a BoolOrString from a YAML/JSON byte slice.
func (a *BoolOrString) UnmarshalJSON(b []byte) error {
var str string
err := json.Unmarshal(b, &str)
if err != nil {
var boolean bool
err := json.Unmarshal(b, &boolean)
if err != nil {
return err
}
a.Bool = &boolean
} else {
a.String = &str
}
return nil
}
4 changes: 2 additions & 2 deletions pkg/generate/ack/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ var (
return code.SetSDK(r.Config(), r, ackmodel.OpTypeDelete, sourceVarName, targetVarName, indentLevel)
},
"GoCodeSetSDKForStruct": func(r *ackmodel.CRD, targetFieldName string, targetVarName string, targetShapeRef *awssdkmodel.ShapeRef, sourceFieldPath string, sourceVarName string, indentLevel int) string {
return code.SetSDKForStruct(r.Config(), r, targetFieldName, targetVarName, targetShapeRef, sourceFieldPath, sourceVarName, indentLevel)
return code.SetSDKForStruct(r.Config(), r, targetFieldName, targetVarName, targetShapeRef, sourceFieldPath, sourceVarName, model.OpTypeList, indentLevel)
},
"GoCodeSetResourceForStruct": func(r *ackmodel.CRD, targetFieldName string, targetVarName string, targetShapeRef *awssdkmodel.ShapeRef, sourceVarName string, sourceShapeRef *awssdkmodel.ShapeRef, indentLevel int) string {
var setCfg *ackgenconfig.SetFieldConfig = nil
Expand All @@ -135,7 +135,7 @@ var (
if ok {
setCfg = f.GetSetterConfig(ackmodel.OpTypeList)
}
if setCfg != nil && setCfg.Ignore {
if setCfg != nil && setCfg.IgnoreResourceSetter() {
return ""
}
return code.SetResourceForStruct(r.Config(), r, targetVarName, targetShapeRef, setCfg, sourceVarName, sourceShapeRef, "", model.OpTypeList, indentLevel)
Expand Down
4 changes: 2 additions & 2 deletions pkg/generate/code/set_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func SetResource(
// field value...
setCfg := f.GetSetterConfig(opType)

if setCfg != nil && setCfg.Ignore {
if setCfg != nil && setCfg.IgnoreResourceSetter() {
continue
}

Expand Down Expand Up @@ -588,7 +588,7 @@ func setResourceReadMany(
// field value...
setCfg := f.GetSetterConfig(model.OpTypeList)

if setCfg != nil && setCfg.Ignore {
if setCfg != nil && setCfg.IgnoreResourceSetter() {
continue
}

Expand Down
34 changes: 34 additions & 0 deletions pkg/generate/code/set_sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ func SetSDK(
sourceFieldPath,
sourceAdaptedVarName,
memberShapeRef,
opType,
indentLevel+1,
)
out += setSDKForScalar(
Expand Down Expand Up @@ -928,6 +929,7 @@ func setSDKForContainer(
sourceVarName string,
// ShapeRef of the target struct field
targetShapeRef *awssdkmodel.ShapeRef,
op model.OpType,
indentLevel int,
) string {
switch targetShapeRef.Shape.Type {
Expand All @@ -939,6 +941,7 @@ func setSDKForContainer(
targetShapeRef,
sourceFieldPath,
sourceVarName,
op,
indentLevel,
)
case "list":
Expand All @@ -949,6 +952,7 @@ func setSDKForContainer(
targetShapeRef,
sourceFieldPath,
sourceVarName,
op,
indentLevel,
)
case "map":
Expand All @@ -959,6 +963,7 @@ func setSDKForContainer(
targetShapeRef,
sourceFieldPath,
sourceVarName,
op,
indentLevel,
)
default:
Expand Down Expand Up @@ -1082,6 +1087,7 @@ func SetSDKForStruct(
sourceFieldPath string,
// The struct or struct field that we access our source value from
sourceVarName string,
op model.OpType,
indentLevel int,
) string {
out := ""
Expand All @@ -1096,6 +1102,29 @@ func SetSDKForStruct(
sourceAdaptedVarName := sourceVarName + "." + cleanMemberName
memberFieldPath := sourceFieldPath + "." + cleanMemberName

// todo: To make `ignore` functionality work for all fields that has `ignore` set to `true`,
// we need to add the below logic inside `SetSDK` function.

// To check if the field member has `ignore` set to `true`.
// This condition currently applies only for members of a field whose shape is `structure`
var setCfg *ackgenconfig.SetFieldConfig
f, ok := r.Fields[targetFieldName]
if ok {
mf, ok := f.MemberFields[memberName]
if ok {
setCfg = mf.GetSetterConfig(op)
if setCfg != nil && setCfg.IgnoreSDKSetter() {
continue
}
}

}

fallBackName := r.GetMatchingInputShapeFieldName(op, targetFieldName)
if fallBackName == memberName {
// TODO: implement @AmineHilaly
}

out += fmt.Sprintf(
"%sif %s != nil {\n", indent, sourceAdaptedVarName,
)
Expand All @@ -1119,6 +1148,7 @@ func SetSDKForStruct(
memberFieldPath,
sourceAdaptedVarName,
memberShapeRef,
op,
indentLevel+1,
)
out += setSDKForScalar(
Expand Down Expand Up @@ -1176,6 +1206,7 @@ func setSDKForSlice(
sourceFieldPath string,
// The struct or struct field that we access our source value from
sourceVarName string,
op model.OpType,
indentLevel int,
) string {
out := ""
Expand Down Expand Up @@ -1209,6 +1240,7 @@ func setSDKForSlice(
sourceFieldPath,
iterVarName,
&targetShape.MemberRef,
op,
indentLevel+1,
)
addressOfVar := ""
Expand Down Expand Up @@ -1239,6 +1271,7 @@ func setSDKForMap(
sourceFieldPath string,
// The struct or struct field that we access our source value from
sourceVarName string,
op model.OpType,
indentLevel int,
) string {
out := ""
Expand Down Expand Up @@ -1273,6 +1306,7 @@ func setSDKForMap(
sourceFieldPath,
valIterVarName,
&targetShape.ValueRef,
op,
indentLevel+1,
)
addressOfVar := ""
Expand Down
Loading

0 comments on commit 37f4ba2

Please sign in to comment.