Skip to content

Commit ed3e8af

Browse files
authored
Allow zero and negative values for generated float32 bounds check (#595)
Issue #, if available: Description of changes: - Update `set_sdk.go setSDKForScalar()` to allow zero and negative values for float32 in bounds check By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent f201cff commit ed3e8af

File tree

2 files changed

+153
-12
lines changed

2 files changed

+153
-12
lines changed

pkg/generate/code/set_sdk.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,6 @@ func SetSDK(
369369
indentLevel+1,
370370
)
371371
out += setSDKForScalar(
372-
cfg, r,
373372
memberName,
374373
targetVarName,
375374
inputShape.Type,
@@ -391,7 +390,6 @@ func SetSDK(
391390
)
392391
} else {
393392
out += setSDKForScalar(
394-
cfg, r,
395393
memberName,
396394
targetVarName,
397395
inputShape.Type,
@@ -577,7 +575,6 @@ func SetSDKGetAttributes(
577575
indent, sourceVarPath,
578576
)
579577
out += setSDKForScalar(
580-
cfg, r,
581578
memberName,
582579
targetVarName,
583580
inputShape.Type,
@@ -788,7 +785,6 @@ func SetSDKSetAttributes(
788785
indent, sourceVarPath,
789786
)
790787
out += setSDKForScalar(
791-
cfg, r,
792788
memberName,
793789
targetVarName,
794790
inputShape.Type,
@@ -918,7 +914,6 @@ func setSDKReadMany(
918914

919915
// res.SetIds(f0)
920916
out += setSDKForScalar(
921-
cfg, r,
922917
memberName,
923918
targetVarName,
924919
inputShape.Type,
@@ -932,7 +927,6 @@ func setSDKReadMany(
932927
// For ReadMany that have a singular identifier field.
933928
// ex: DescribeReplicationGroups
934929
out += setSDKForScalar(
935-
cfg, r,
936930
memberName,
937931
targetVarName,
938932
inputShape.Type,
@@ -1041,7 +1035,6 @@ func setSDKForContainer(
10411035
}
10421036

10431037
return setSDKForScalar(
1044-
cfg, r,
10451038
targetFieldName,
10461039
targetVarName,
10471040
targetShapeRef.Shape.Type,
@@ -1215,7 +1208,6 @@ func SetSDKForStruct(
12151208
indentLevel+1,
12161209
)
12171210
out += setSDKForScalar(
1218-
cfg, r,
12191211
memberName,
12201212
targetVarName,
12211213
targetShape.Type,
@@ -1238,7 +1230,6 @@ func SetSDKForStruct(
12381230
} else {
12391231

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

1609-
out += fmt.Sprintf("%sif %s > math.Max%s32 || %s < math.%s32 {\n", indent, dereferencedVal, actualType[0], dereferencedVal, actualType[1])
1598+
if shape.Type == "float" {
1599+
out += fmt.Sprintf(
1600+
"%[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",
1601+
indent,
1602+
dereferencedVal,
1603+
actualType[0],
1604+
actualType[1],
1605+
)
1606+
} else {
1607+
out += fmt.Sprintf(
1608+
"%[1]sif %[2]s > math.Max%[3]s32 || %[2]s < math.%[4]s32 {\n",
1609+
indent,
1610+
dereferencedVal,
1611+
actualType[0],
1612+
actualType[1],
1613+
)
1614+
}
16101615
out += fmt.Sprintf("%s\treturn nil, fmt.Errorf(\"error: field %s is of type %s32\")\n", indent, ogShapeName.Original, actualType[2])
16111616
out += fmt.Sprintf("%s}\n", indent)
16121617
tempVar := ogShapeName.CamelLower + "Copy"
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package code
15+
16+
import (
17+
"testing"
18+
19+
awssdkmodel "github.com/aws-controllers-k8s/code-generator/pkg/api"
20+
"github.com/stretchr/testify/assert"
21+
)
22+
23+
func TestSetSDKForScalar(t *testing.T) {
24+
assert := assert.New(t)
25+
26+
testCases := []struct {
27+
name string
28+
targetFieldName string
29+
targetVarName string
30+
targetVarType string
31+
sourceFieldPath string
32+
sourceVarName string
33+
isListMember bool
34+
shapeRef *awssdkmodel.ShapeRef
35+
indentLevel int
36+
expected string
37+
}{
38+
{
39+
name: "string scalar",
40+
targetFieldName: "BucketName",
41+
targetVarName: "res",
42+
targetVarType: "structure",
43+
sourceFieldPath: "Name",
44+
sourceVarName: "ko.Spec.Name",
45+
isListMember: false,
46+
shapeRef: &awssdkmodel.ShapeRef{
47+
Shape: &awssdkmodel.Shape{
48+
Type: "string",
49+
},
50+
OriginalMemberName: "BucketName",
51+
},
52+
indentLevel: 1,
53+
expected: "\tres.BucketName = ko.Spec.Name\n",
54+
},
55+
{
56+
name: "boolean scalar",
57+
targetFieldName: "Enabled",
58+
targetVarName: "res",
59+
targetVarType: "structure",
60+
sourceFieldPath: "Enabled",
61+
sourceVarName: "ko.Spec.Enabled",
62+
isListMember: false,
63+
shapeRef: &awssdkmodel.ShapeRef{
64+
Shape: &awssdkmodel.Shape{
65+
Type: "boolean",
66+
},
67+
OriginalMemberName: "Enabled",
68+
},
69+
indentLevel: 1,
70+
expected: "\tres.Enabled = ko.Spec.Enabled\n",
71+
},
72+
{
73+
name: "integer scalar",
74+
targetFieldName: "MaxKeys",
75+
targetVarName: "res",
76+
targetVarType: "structure",
77+
sourceFieldPath: "MaxKeys",
78+
sourceVarName: "ko.Spec.MaxKeys",
79+
isListMember: false,
80+
shapeRef: &awssdkmodel.ShapeRef{
81+
Shape: &awssdkmodel.Shape{
82+
Type: "integer",
83+
},
84+
OriginalMemberName: "MaxKeys",
85+
},
86+
indentLevel: 1,
87+
expected: ` maxKeysCopy0 := *ko.Spec.MaxKeys
88+
if maxKeysCopy0 > math.MaxInt32 || maxKeysCopy0 < math.MinInt32 {
89+
return nil, fmt.Errorf("error: field MaxKeys is of type int32")
90+
}
91+
maxKeysCopy := int32(maxKeysCopy0)
92+
res.MaxKeys = &maxKeysCopy
93+
`,
94+
},
95+
{
96+
name: "float scalar",
97+
targetFieldName: "Temperature",
98+
targetVarName: "res",
99+
targetVarType: "structure",
100+
sourceFieldPath: "Temperature",
101+
sourceVarName: "ko.Spec.Temperature",
102+
isListMember: false,
103+
shapeRef: &awssdkmodel.ShapeRef{
104+
Shape: &awssdkmodel.Shape{
105+
Type: "float",
106+
},
107+
OriginalMemberName: "Temperature",
108+
},
109+
indentLevel: 1,
110+
expected: ` temperatureCopy0 := *ko.Spec.Temperature
111+
if temperatureCopy0 > math.MaxFloat32 || temperatureCopy0 < -math.MaxFloat32 || (temperatureCopy0 < math.SmallestNonzeroFloat32 && !(temperatureCopy0 <= 0)) || (temperatureCopy0 > -math.SmallestNonzeroFloat32 && !(temperatureCopy0 >= 0)) {
112+
return nil, fmt.Errorf("error: field Temperature is of type float32")
113+
}
114+
temperatureCopy := float32(temperatureCopy0)
115+
res.Temperature = &temperatureCopy
116+
`,
117+
},
118+
}
119+
120+
for _, tc := range testCases {
121+
t.Run(tc.name, func(t *testing.T) {
122+
result := setSDKForScalar(
123+
tc.targetFieldName,
124+
tc.targetVarName,
125+
tc.targetVarType,
126+
tc.sourceFieldPath,
127+
tc.sourceVarName,
128+
tc.isListMember,
129+
tc.shapeRef,
130+
tc.indentLevel,
131+
)
132+
133+
assert.Equal(tc.expected, result, "setSDKForScalar() did not return expected result for %s", tc.name)
134+
})
135+
}
136+
}

0 commit comments

Comments
 (0)