Skip to content

Commit

Permalink
With functionality for ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
Vandita2020 committed Apr 7, 2024
1 parent 974b987 commit c12a3d4
Show file tree
Hide file tree
Showing 8 changed files with 259 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
}
2 changes: 1 addition & 1 deletion pkg/generate/ack/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 6 additions & 1 deletion pkg/generate/code/set_sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -1113,13 +1113,18 @@ func SetSDKForStruct(
mf, ok := f.MemberFields[memberName]
if ok {
setCfg = mf.GetSetterConfig(op)
if setCfg != nil && setCfg.Ignore {
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 Down
177 changes: 177 additions & 0 deletions pkg/generate/code/set_sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2190,3 +2190,180 @@ func TestSetSDK_IAM_User_NewPath(t *testing.T) {
code.SetSDK(crd.Config(), crd, model.OpTypeUpdate, "r.ko", "res", 1),
)
}

func TestSetSDK_Lambda_Ignore_Code_SHA256(t *testing.T) {
assert := assert.New(t)
require := require.New(t)

g := testutil.NewModelForServiceWithOptions(t, "lambda", &testutil.TestingModelOptions{
GeneratorConfigFile: "generator-lambda-ignore-code-sha256.yaml",
})

crd := testutil.GetCRDByName(t, g, "Function")
require.NotNil(crd)

expected := `
if r.ko.Spec.Code != nil {
f0 := &svcsdk.FunctionCode{}
if r.ko.Spec.Code.ImageURI != nil {
f0.SetImageUri(*r.ko.Spec.Code.ImageURI)
}
if r.ko.Spec.Code.S3Bucket != nil {
f0.SetS3Bucket(*r.ko.Spec.Code.S3Bucket)
}
if r.ko.Spec.Code.S3Key != nil {
f0.SetS3Key(*r.ko.Spec.Code.S3Key)
}
if r.ko.Spec.Code.S3ObjectVersion != nil {
f0.SetS3ObjectVersion(*r.ko.Spec.Code.S3ObjectVersion)
}
if r.ko.Spec.Code.ZipFile != nil {
f0.SetZipFile(r.ko.Spec.Code.ZipFile)
}
res.SetCode(f0)
}
if r.ko.Spec.CodeSigningConfigARN != nil {
res.SetCodeSigningConfigArn(*r.ko.Spec.CodeSigningConfigARN)
}
if r.ko.Spec.DeadLetterConfig != nil {
f2 := &svcsdk.DeadLetterConfig{}
if r.ko.Spec.DeadLetterConfig.TargetARN != nil {
f2.SetTargetArn(*r.ko.Spec.DeadLetterConfig.TargetARN)
}
res.SetDeadLetterConfig(f2)
}
if r.ko.Spec.Description != nil {
res.SetDescription(*r.ko.Spec.Description)
}
if r.ko.Spec.Environment != nil {
f4 := &svcsdk.Environment{}
if r.ko.Spec.Environment.Variables != nil {
f4f0 := map[string]*string{}
for f4f0key, f4f0valiter := range r.ko.Spec.Environment.Variables {
var f4f0val string
f4f0val = *f4f0valiter
f4f0[f4f0key] = &f4f0val
}
f4.SetVariables(f4f0)
}
res.SetEnvironment(f4)
}
if r.ko.Spec.FileSystemConfigs != nil {
f5 := []*svcsdk.FileSystemConfig{}
for _, f5iter := range r.ko.Spec.FileSystemConfigs {
f5elem := &svcsdk.FileSystemConfig{}
if f5iter.ARN != nil {
f5elem.SetArn(*f5iter.ARN)
}
if f5iter.LocalMountPath != nil {
f5elem.SetLocalMountPath(*f5iter.LocalMountPath)
}
f5 = append(f5, f5elem)
}
res.SetFileSystemConfigs(f5)
}
if r.ko.Spec.FunctionName != nil {
res.SetFunctionName(*r.ko.Spec.FunctionName)
}
if r.ko.Spec.Handler != nil {
res.SetHandler(*r.ko.Spec.Handler)
}
if r.ko.Spec.ImageConfig != nil {
f8 := &svcsdk.ImageConfig{}
if r.ko.Spec.ImageConfig.Command != nil {
f8f0 := []*string{}
for _, f8f0iter := range r.ko.Spec.ImageConfig.Command {
var f8f0elem string
f8f0elem = *f8f0iter
f8f0 = append(f8f0, &f8f0elem)
}
f8.SetCommand(f8f0)
}
if r.ko.Spec.ImageConfig.EntryPoint != nil {
f8f1 := []*string{}
for _, f8f1iter := range r.ko.Spec.ImageConfig.EntryPoint {
var f8f1elem string
f8f1elem = *f8f1iter
f8f1 = append(f8f1, &f8f1elem)
}
f8.SetEntryPoint(f8f1)
}
if r.ko.Spec.ImageConfig.WorkingDirectory != nil {
f8.SetWorkingDirectory(*r.ko.Spec.ImageConfig.WorkingDirectory)
}
res.SetImageConfig(f8)
}
if r.ko.Spec.KMSKeyARN != nil {
res.SetKMSKeyArn(*r.ko.Spec.KMSKeyARN)
}
if r.ko.Spec.Layers != nil {
f10 := []*string{}
for _, f10iter := range r.ko.Spec.Layers {
var f10elem string
f10elem = *f10iter
f10 = append(f10, &f10elem)
}
res.SetLayers(f10)
}
if r.ko.Spec.MemorySize != nil {
res.SetMemorySize(*r.ko.Spec.MemorySize)
}
if r.ko.Spec.PackageType != nil {
res.SetPackageType(*r.ko.Spec.PackageType)
}
if r.ko.Spec.Publish != nil {
res.SetPublish(*r.ko.Spec.Publish)
}
if r.ko.Spec.Role != nil {
res.SetRole(*r.ko.Spec.Role)
}
if r.ko.Spec.Runtime != nil {
res.SetRuntime(*r.ko.Spec.Runtime)
}
if r.ko.Spec.Tags != nil {
f16 := map[string]*string{}
for f16key, f16valiter := range r.ko.Spec.Tags {
var f16val string
f16val = *f16valiter
f16[f16key] = &f16val
}
res.SetTags(f16)
}
if r.ko.Spec.Timeout != nil {
res.SetTimeout(*r.ko.Spec.Timeout)
}
if r.ko.Spec.TracingConfig != nil {
f18 := &svcsdk.TracingConfig{}
if r.ko.Spec.TracingConfig.Mode != nil {
f18.SetMode(*r.ko.Spec.TracingConfig.Mode)
}
res.SetTracingConfig(f18)
}
if r.ko.Spec.VPCConfig != nil {
f19 := &svcsdk.VpcConfig{}
if r.ko.Spec.VPCConfig.SecurityGroupIDs != nil {
f19f0 := []*string{}
for _, f19f0iter := range r.ko.Spec.VPCConfig.SecurityGroupIDs {
var f19f0elem string
f19f0elem = *f19f0iter
f19f0 = append(f19f0, &f19f0elem)
}
f19.SetSecurityGroupIds(f19f0)
}
if r.ko.Spec.VPCConfig.SubnetIDs != nil {
f19f1 := []*string{}
for _, f19f1iter := range r.ko.Spec.VPCConfig.SubnetIDs {
var f19f1elem string
f19f1elem = *f19f1iter
f19f1 = append(f19f1, &f19f1elem)
}
f19.SetSubnetIds(f19f1)
}
res.SetVpcConfig(f19)
}
`
assert.Equal(
expected,
code.SetSDK(crd.Config(), crd, model.OpTypeCreate, "r.ko", "res", 1),
)
}
5 changes: 1 addition & 4 deletions pkg/model/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,10 +428,7 @@ func (r *CRD) GetMatchingInputShapeFieldName(opType OpType, sdkField string) str
}
rmMethod := ResourceManagerMethodFromOpType(opType)
for _, setCfg := range f.FieldConfig.Set {
if setCfg == nil {
continue
}
if setCfg.Ignore == true || setCfg.To == nil {
if setCfg == nil || setCfg.IgnoreSDKSetter() {
continue
}
// If the Method attribute is nil, that means the setter config applies to
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
resources:
Function:
fields:
Code.S3SHA256:
type: string
set:
- ignore: "to"
method: Create
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ resources:
type: string
compare:
is_ignored: true
set:
- ignore: true
method: Create
ImageConfigResponse.Error.New:
is_read_only: true
type: string
Expand Down

0 comments on commit c12a3d4

Please sign in to comment.