Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions pkg/generate/code/set_sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,6 @@ func SetSDK(
indentLevel+1,
)
out += setSDKForScalar(
cfg, r,
memberName,
targetVarName,
inputShape.Type,
Expand All @@ -391,7 +390,6 @@ func SetSDK(
)
} else {
out += setSDKForScalar(
cfg, r,
memberName,
targetVarName,
inputShape.Type,
Expand Down Expand Up @@ -577,7 +575,6 @@ func SetSDKGetAttributes(
indent, sourceVarPath,
)
out += setSDKForScalar(
cfg, r,
memberName,
targetVarName,
inputShape.Type,
Expand Down Expand Up @@ -788,7 +785,6 @@ func SetSDKSetAttributes(
indent, sourceVarPath,
)
out += setSDKForScalar(
cfg, r,
memberName,
targetVarName,
inputShape.Type,
Expand Down Expand Up @@ -918,7 +914,6 @@ func setSDKReadMany(

// res.SetIds(f0)
out += setSDKForScalar(
cfg, r,
memberName,
targetVarName,
inputShape.Type,
Expand All @@ -932,7 +927,6 @@ func setSDKReadMany(
// For ReadMany that have a singular identifier field.
// ex: DescribeReplicationGroups
out += setSDKForScalar(
cfg, r,
memberName,
targetVarName,
inputShape.Type,
Expand Down Expand Up @@ -1041,7 +1035,6 @@ func setSDKForContainer(
}

return setSDKForScalar(
cfg, r,
targetFieldName,
targetVarName,
targetShapeRef.Shape.Type,
Expand Down Expand Up @@ -1215,7 +1208,6 @@ func SetSDKForStruct(
indentLevel+1,
)
out += setSDKForScalar(
cfg, r,
memberName,
targetVarName,
targetShape.Type,
Expand All @@ -1238,7 +1230,6 @@ func SetSDKForStruct(
} else {

out += setSDKForScalar(
cfg, r,
memberName,
targetVarName,
targetShape.Type,
Expand Down Expand Up @@ -1558,8 +1549,6 @@ func varEmptyConstructorK8sType(
// the aws-sdk-go's common SetXXX() method. For everything else, we output
// normal assignment operations.
func setSDKForScalar(
cfg *ackgenconfig.Config,
r *model.CRD,
// The name of the Input SDK Shape member we're outputting for
targetFieldName string,
// The variable name that we want to set a value to
Expand Down Expand Up @@ -1606,7 +1595,23 @@ func setSDKForScalar(
dereferencedVal := ogShapeName.CamelLower + "Copy0"
out += fmt.Sprintf("%s%s := %s\n", indent, dereferencedVal, setTo)

out += fmt.Sprintf("%sif %s > math.Max%s32 || %s < math.%s32 {\n", indent, dereferencedVal, actualType[0], dereferencedVal, actualType[1])
if shape.Type == "float" {
out += fmt.Sprintf(
"%[1]sif %[2]s > math.Max%[3]s32 || %[2]s < -math.Max%[3]s32 || (%[2]s < math.%[4]s32 && !(%[2]s <= 0)) || (%[2]s > -math.%[4]s32 && !(%[2]s >= 0)) {\n",
indent,
dereferencedVal,
actualType[0],
actualType[1],
)
} else {
out += fmt.Sprintf(
"%[1]sif %[2]s > math.Max%[3]s32 || %[2]s < math.%[4]s32 {\n",
indent,
dereferencedVal,
actualType[0],
actualType[1],
)
}
out += fmt.Sprintf("%s\treturn nil, fmt.Errorf(\"error: field %s is of type %s32\")\n", indent, ogShapeName.Original, actualType[2])
out += fmt.Sprintf("%s}\n", indent)
tempVar := ogShapeName.CamelLower + "Copy"
Expand Down
132 changes: 132 additions & 0 deletions pkg/generate/code/set_sdk_whitebox_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to use a new file since the existing set_sdk_test.go has a circular dependency with the code package due to testutils.

//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.

package code

import (
"testing"

awssdkmodel "github.com/aws-controllers-k8s/code-generator/pkg/api"
"github.com/stretchr/testify/assert"
)

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

testCases := []struct {
name string
targetFieldName string
targetVarName string
targetVarType string
sourceFieldPath string
sourceVarName string
isListMember bool
shapeRef *awssdkmodel.ShapeRef
indentLevel int
expected string
}{
{
name: "string scalar",
targetFieldName: "BucketName",
targetVarName: "res",
targetVarType: "structure",
sourceFieldPath: "Name",
sourceVarName: "ko.Spec.Name",
isListMember: false,
shapeRef: &awssdkmodel.ShapeRef{
Shape: &awssdkmodel.Shape{
Type: "string",
},
},
indentLevel: 1,
expected: "\tres.BucketName = ko.Spec.Name\n",
},
{
name: "boolean scalar",
targetFieldName: "Enabled",
targetVarName: "res",
targetVarType: "structure",
sourceFieldPath: "Enabled",
sourceVarName: "ko.Spec.Enabled",
isListMember: false,
shapeRef: &awssdkmodel.ShapeRef{
Shape: &awssdkmodel.Shape{
Type: "boolean",
},
},
indentLevel: 1,
expected: "\tres.Enabled = ko.Spec.Enabled\n",
},
{
name: "integer scalar",
targetFieldName: "MaxKeys",
targetVarName: "res",
targetVarType: "structure",
sourceFieldPath: "MaxKeys",
sourceVarName: "ko.Spec.MaxKeys",
isListMember: false,
shapeRef: &awssdkmodel.ShapeRef{
Shape: &awssdkmodel.Shape{
Type: "integer",
},
},
indentLevel: 1,
expected: ` Copy0 := *ko.Spec.MaxKeys
if Copy0 > math.MaxInt32 || Copy0 < math.MinInt32 {
return nil, fmt.Errorf("error: field is of type int32")
}
Copy := int32(Copy0)
res.MaxKeys = &Copy
`,
},
{
name: "float scalar",
targetFieldName: "Temperature",
targetVarName: "res",
targetVarType: "structure",
sourceFieldPath: "Temperature",
sourceVarName: "ko.Spec.Temperature",
isListMember: false,
shapeRef: &awssdkmodel.ShapeRef{
Shape: &awssdkmodel.Shape{
Type: "float",
},
},
indentLevel: 1,
expected: ` Copy0 := *ko.Spec.Temperature
if Copy0 > math.MaxFloat32 || Copy0 < -math.MaxFloat32 || (Copy0 < math.SmallestNonzeroFloat32 && !(Copy0 <= 0)) || (Copy0 > -math.SmallestNonzeroFloat32 && !(Copy0 >= 0)) {
return nil, fmt.Errorf("error: field is of type float32")
}
Copy := float32(Copy0)
res.Temperature = &Copy
`,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := setSDKForScalar(
tc.targetFieldName,
tc.targetVarName,
tc.targetVarType,
tc.sourceFieldPath,
tc.sourceVarName,
tc.isListMember,
tc.shapeRef,
tc.indentLevel,
)

assert.Equal(tc.expected, result, "setSDKForScalar() did not return expected result for %s", tc.name)
})
}
}