Skip to content

Commit 208f277

Browse files
iphmrgrain
andauthored
fix: allow translation of doubles from strings (#751)
* fix: allow translation of doubles from strings There were certain templates that errored out and said "not a valid integer". When looking at the code, it turns out that if a string thinks there is a number -- a number in JSON can be either integer or doable. Originally, the code parsed only ints, so any float/double fails outright. This update now will try both. I'm sure in the future we will need to try to parse one, fail, parse the other -- but for now, just look for a ".". * Fix unit test --------- Co-authored-by: Momo Kornher <[email protected]>
1 parent 40d084a commit 208f277

File tree

16 files changed

+65
-36
lines changed

16 files changed

+65
-36
lines changed

src/ir/resources/mod.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,20 @@ impl<'a, 'b> ResourceTranslator<'a, 'b> {
8585
}
8686
})?)),
8787
Primitive::Number => {
88-
Ok(ResourceIr::Number(s.parse().map_err(|cause| {
89-
Error::ResourceTranslationError {
90-
message: format!("{cause}"),
91-
}
92-
})?))
88+
let ir = match s.contains(".") {
89+
true => ResourceIr::Double(WrapperF64::new(s.parse().map_err(
90+
|cause| Error::ResourceTranslationError {
91+
message: format!("{cause}"),
92+
},
93+
)?)),
94+
false => ResourceIr::Number(s.parse().map_err(|cause| {
95+
Error::ResourceTranslationError {
96+
message: format!("{cause}"),
97+
}
98+
})?),
99+
};
100+
101+
Ok(ir)
93102
}
94103
_ => Ok(ResourceIr::String(s)),
95104
};

src/ir/resources/tests.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::ir::reference::{Origin, Reference};
88
use crate::ir::resources::{order, ResourceInstruction, ResourceIr, ResourceType};
99
use crate::ir::ReferenceOrigins;
1010
use crate::parser::resource::{IntrinsicFunction, ResourceValue};
11+
use crate::primitives::WrapperF64;
1112
use crate::Hasher;
1213

1314
use super::{Primitive, ResourceTranslator, Schema, TypeReference};
@@ -198,7 +199,7 @@ fn test_boolean_parse_error() {
198199
}
199200

200201
#[test]
201-
fn test_number_parse_error() {
202+
fn test_number_parse_float() {
202203
let origins = ReferenceOrigins {
203204
origins: HashMap::default(),
204205
};
@@ -208,6 +209,21 @@ fn test_number_parse_error() {
208209
value_type: Some(TypeReference::Primitive(Primitive::Number)),
209210
};
210211
let resource_value = ResourceValue::String("1.5".into());
212+
let result = translator.translate(resource_value).unwrap();
213+
assert_eq!(ResourceIr::Double(WrapperF64::new(1.5)), result);
214+
}
215+
216+
#[test]
217+
fn test_number_parse_error() {
218+
let origins = ReferenceOrigins {
219+
origins: HashMap::default(),
220+
};
221+
let translator = ResourceTranslator {
222+
schema: &Schema::builtin(),
223+
origins: &origins,
224+
value_type: Some(TypeReference::Primitive(Primitive::Number)),
225+
};
226+
let resource_value = ResourceValue::String("15abc".into());
211227
let result = translator.translate(resource_value).unwrap_err();
212228
assert_eq!("invalid digit found in string", result.to_string());
213229
}

tests/end-to-end/cloudwatch/csharp/Stack.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public CloudwatchStack(Construct scope, string id, CloudwatchStackProps props =
3939
MetricName = "5XXError",
4040
ComparisonOperator = "GreaterThanThreshold",
4141
Statistic = "Average",
42-
Threshold = 0,
42+
Threshold = 0.005,
4343
Period = 900,
4444
EvaluationPeriods = 1,
4545
TreatMissingData = "notBreaching",

tests/end-to-end/cloudwatch/csharp/Stack.diff

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/./tests/end-to-end/cloudwatch/template.json b/tests/end-to-end/cloudwatch-csharp-working-dir/cdk.out/Stack.template.json
2-
index 8d26c7b..aad44e9 100644
2+
index 30a1dd6..fd7e22a 100644
33
--- a/./tests/end-to-end/cloudwatch/template.json
44
+++ b/tests/end-to-end/cloudwatch-csharp-working-dir/cdk.out/Stack.template.json
55
@@ -1,50 +1,28 @@
@@ -38,11 +38,10 @@ index 8d26c7b..aad44e9 100644
3838
+ "EvaluationPeriods": 1,
3939
"MetricName": "5XXError",
4040
- "ComparisonOperator": "GreaterThanThreshold",
41+
- "Statistic": "Average",
42+
- "Threshold": "0.005",
4143
+ "Namespace": "AWS/ApiGateway",
42-
+ "Period": 900,
43-
"Statistic": "Average",
44-
"Threshold": 0,
45-
- "Period": 900,
44+
"Period": 900,
4645
- "EvaluationPeriods": 1,
4746
- "TreatMissingData": "notBreaching",
4847
- "AlarmActions": [
@@ -59,6 +58,8 @@ index 8d26c7b..aad44e9 100644
5958
- }
6059
- }
6160
- ]
61+
+ "Statistic": "Average",
62+
+ "Threshold": 0.005,
6263
+ "TreatMissingData": "notBreaching"
6364
}
6465
}

tests/end-to-end/cloudwatch/csharp/Stack.template.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"Namespace": "AWS/ApiGateway",
2222
"Period": 900,
2323
"Statistic": "Average",
24-
"Threshold": 0,
24+
"Threshold": 0.005,
2525
"TreatMissingData": "notBreaching"
2626
}
2727
}

tests/end-to-end/cloudwatch/golang/stack.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func NewCloudwatchStack(scope constructs.Construct, id string, props *Cloudwatch
4141
MetricName: jsii.String("5XXError"),
4242
ComparisonOperator: jsii.String("GreaterThanThreshold"),
4343
Statistic: jsii.String("Average"),
44-
Threshold: jsii.Number(0),
44+
Threshold: jsii.Number(0.005),
4545
Period: jsii.Number(900),
4646
EvaluationPeriods: jsii.Number(1),
4747
TreatMissingData: jsii.String("notBreaching"),

tests/end-to-end/cloudwatch/java/Stack.diff

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/./tests/end-to-end/cloudwatch/template.json b/tests/end-to-end/cloudwatch-java-working-dir/cdk.out/Stack.template.json
2-
index 8d26c7b..aad44e9 100644
2+
index 30a1dd6..fd7e22a 100644
33
--- a/./tests/end-to-end/cloudwatch/template.json
44
+++ b/tests/end-to-end/cloudwatch-java-working-dir/cdk.out/Stack.template.json
55
@@ -1,50 +1,28 @@
@@ -38,11 +38,10 @@ index 8d26c7b..aad44e9 100644
3838
+ "EvaluationPeriods": 1,
3939
"MetricName": "5XXError",
4040
- "ComparisonOperator": "GreaterThanThreshold",
41+
- "Statistic": "Average",
42+
- "Threshold": "0.005",
4143
+ "Namespace": "AWS/ApiGateway",
42-
+ "Period": 900,
43-
"Statistic": "Average",
44-
"Threshold": 0,
45-
- "Period": 900,
44+
"Period": 900,
4645
- "EvaluationPeriods": 1,
4746
- "TreatMissingData": "notBreaching",
4847
- "AlarmActions": [
@@ -59,6 +58,8 @@ index 8d26c7b..aad44e9 100644
5958
- }
6059
- }
6160
- ]
61+
+ "Statistic": "Average",
62+
+ "Threshold": 0.005,
6263
+ "TreatMissingData": "notBreaching"
6364
}
6465
}

tests/end-to-end/cloudwatch/java/Stack.template.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"Namespace": "AWS/ApiGateway",
2222
"Period": 900,
2323
"Statistic": "Average",
24-
"Threshold": 0,
24+
"Threshold": 0.005,
2525
"TreatMissingData": "notBreaching"
2626
}
2727
}

tests/end-to-end/cloudwatch/java/src/main/java/com/myorg/Stack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public CloudwatchStack(final Construct scope, final String id, final StackProps
3838
.metricName("5XXError")
3939
.comparisonOperator("GreaterThanThreshold")
4040
.statistic("Average")
41-
.threshold(0)
41+
.threshold(0.005)
4242
.period(900)
4343
.evaluationPeriods(1)
4444
.treatMissingData("notBreaching")

tests/end-to-end/cloudwatch/python/Stack.diff

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/./tests/end-to-end/cloudwatch/template.json b/tests/end-to-end/cloudwatch-python-working-dir/cdk.out/Stack.template.json
2-
index 8d26c7b..aad44e9 100644
2+
index 30a1dd6..fd7e22a 100644
33
--- a/./tests/end-to-end/cloudwatch/template.json
44
+++ b/tests/end-to-end/cloudwatch-python-working-dir/cdk.out/Stack.template.json
55
@@ -1,50 +1,28 @@
@@ -38,11 +38,10 @@ index 8d26c7b..aad44e9 100644
3838
+ "EvaluationPeriods": 1,
3939
"MetricName": "5XXError",
4040
- "ComparisonOperator": "GreaterThanThreshold",
41+
- "Statistic": "Average",
42+
- "Threshold": "0.005",
4143
+ "Namespace": "AWS/ApiGateway",
42-
+ "Period": 900,
43-
"Statistic": "Average",
44-
"Threshold": 0,
45-
- "Period": 900,
44+
"Period": 900,
4645
- "EvaluationPeriods": 1,
4746
- "TreatMissingData": "notBreaching",
4847
- "AlarmActions": [
@@ -59,6 +58,8 @@ index 8d26c7b..aad44e9 100644
5958
- }
6059
- }
6160
- ]
61+
+ "Statistic": "Average",
62+
+ "Threshold": 0.005,
6263
+ "TreatMissingData": "notBreaching"
6364
}
6465
}

tests/end-to-end/cloudwatch/python/Stack.template.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"Namespace": "AWS/ApiGateway",
2222
"Period": 900,
2323
"Statistic": "Average",
24-
"Threshold": 0,
24+
"Threshold": 0.005,
2525
"TreatMissingData": "notBreaching"
2626
}
2727
}

tests/end-to-end/cloudwatch/python/stack.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
2525
metric_name = '5XXError',
2626
comparison_operator = 'GreaterThanThreshold',
2727
statistic = 'Average',
28-
threshold = 0,
28+
threshold = 0.005,
2929
period = 900,
3030
evaluation_periods = 1,
3131
treat_missing_data = 'notBreaching',

tests/end-to-end/cloudwatch/template.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"MetricName": "5XXError",
2828
"ComparisonOperator": "GreaterThanThreshold",
2929
"Statistic": "Average",
30-
"Threshold": 0,
30+
"Threshold": "0.005",
3131
"Period": 900,
3232
"EvaluationPeriods": 1,
3333
"TreatMissingData": "notBreaching",

tests/end-to-end/cloudwatch/typescript/Stack.diff

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/./tests/end-to-end/cloudwatch/template.json b/tests/end-to-end/cloudwatch-typescript-working-dir/cdk.out/Stack.template.json
2-
index 8d26c7b..aad44e9 100644
2+
index 30a1dd6..fd7e22a 100644
33
--- a/./tests/end-to-end/cloudwatch/template.json
44
+++ b/tests/end-to-end/cloudwatch-typescript-working-dir/cdk.out/Stack.template.json
55
@@ -1,50 +1,28 @@
@@ -38,11 +38,10 @@ index 8d26c7b..aad44e9 100644
3838
+ "EvaluationPeriods": 1,
3939
"MetricName": "5XXError",
4040
- "ComparisonOperator": "GreaterThanThreshold",
41+
- "Statistic": "Average",
42+
- "Threshold": "0.005",
4143
+ "Namespace": "AWS/ApiGateway",
42-
+ "Period": 900,
43-
"Statistic": "Average",
44-
"Threshold": 0,
45-
- "Period": 900,
44+
"Period": 900,
4645
- "EvaluationPeriods": 1,
4746
- "TreatMissingData": "notBreaching",
4847
- "AlarmActions": [
@@ -59,6 +58,8 @@ index 8d26c7b..aad44e9 100644
5958
- }
6059
- }
6160
- ]
61+
+ "Statistic": "Average",
62+
+ "Threshold": 0.005,
6263
+ "TreatMissingData": "notBreaching"
6364
}
6465
}

tests/end-to-end/cloudwatch/typescript/Stack.template.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"Namespace": "AWS/ApiGateway",
2222
"Period": 900,
2323
"Statistic": "Average",
24-
"Threshold": 0,
24+
"Threshold": 0.005,
2525
"TreatMissingData": "notBreaching"
2626
}
2727
}

tests/end-to-end/cloudwatch/typescript/stack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class CloudwatchStack extends cdk.Stack {
3232
metricName: '5XXError',
3333
comparisonOperator: 'GreaterThanThreshold',
3434
statistic: 'Average',
35-
threshold: 0,
35+
threshold: 0.005,
3636
period: 900,
3737
evaluationPeriods: 1,
3838
treatMissingData: 'notBreaching',

0 commit comments

Comments
 (0)