Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor converters to numeric types for aws_smithy_types::Number #1274

Merged
merged 16 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from 8 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
69 changes: 62 additions & 7 deletions CHANGELOG.next.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
message = "Add checksum calculation and validation wrappers for HTTP bodies."
references = ["smithy-rs#1263"]
meta = { "breaking" = false, "tada" = false, "bug" = false }
author = "Velf"
author = "Velfi"

[[smithy-rs]]
message = "`aws_smithy_http::header::append_merge_header_maps`, a function for merging two `HeaderMap`s, is now public."
references = ["smithy-rs#1263"]
meta = { "breaking" = false, "tada" = false, "bug" = false }
author = "Velf"
author = "Velfi"

[[aws-sdk-rust]]
message = """
Expand Down Expand Up @@ -71,21 +71,17 @@ Updated SDK Client retry behavior to allow for a configurable initial backoff. P
(named `r` in the code) was set to 2 seconds. This is not an ideal default for services like DynamoDB that expect
clients to quickly retry failed request attempts. Now, users can set quicker (or slower) backoffs according to their
needs.

```rust
#[tokio::main]
async fn main() -> Result<(), aws_sdk_dynamodb::Error> {
let retry_config = aws_smithy_types::retry::RetryConfigBuilder::new()
.max_attempts(4)
.initial_backoff(Duration::from_millis(20));

let shared_config = aws_config::from_env()
.retry_config(retry_config)
.load()
.await;

let client = aws_sdk_dynamodb::Client::new(&shared_config);

// Given the 20ms backoff multiplier, and assuming this request fails 3 times before succeeding,
// the first retry would take place between 0-20ms after the initial request,
// the second retry would take place between 0-40ms after the first retry,
Expand All @@ -96,11 +92,70 @@ async fn main() -> Result<(), aws_sdk_dynamodb::Error> {
.item("username", "Velfi")
.item("account_type", "Developer")
.send().await?;

Ok(())
}
```
"""
references = ["aws-sdk-rust#567"]
meta = { "breaking" = false, "tada" = true, "bug" = false }
author = "Velfi"

[[smithy-rs]]
jdisanti marked this conversation as resolved.
Show resolved Hide resolved
message = """
Lossy converters into integer types for `aws_smithy_types::Number` have been
removed. Lossy converters into floating point types for
`aws_smithy_types::Number` have been suffixed with `_lossy`. If you were
directly using the integer lossy converters, we recommend you use the safe
converters.
_Before:_
```rust
fn f1(n: aws_smithy_types::Number) {
let foo: f32 = n.to_f32(); // Lossy conversion!
let bar: u32 = n.to_u32(); // Lossy conversion!
}
```
_After:_
```rust
fn f1(n: aws_smithy_types::Number) {
use std::convert::TryInto; // Unnecessary import if you're using Rust 2021 edition.
let foo: f32 = n.try_into().expect("lossy conversion detected"); // Or handle the error instead of panicking.
// You can still do lossy conversions, but only into floating point types.
let foo: f32 = n.to_f32_lossy();
// To lossily convert into integer types, use an `as` cast directly.
let bar: u32 = n as u32; // Lossy conversion!
}
```
"""
references = ["smithy-rs#1274"]
meta = { "breaking" = true, "tada" = false, "bug" = true }
author = "david-perez"

[[aws-sdk-rust]]
message = """
Lossy converters into integer types for `aws_smithy_types::Number` have been
removed. Lossy converters into floating point types for
`aws_smithy_types::Number` have been suffixed with `_lossy`. If you were
directly using the integer lossy converters, we recommend you use the safe
converters.
_Before:_
```rust
fn f1(n: aws_smithy_types::Number) {
let foo: f32 = n.to_f32(); // Lossy conversion!
let bar: u32 = n.to_u32(); // Lossy conversion!
}
```
_After:_
```rust
fn f1(n: aws_smithy_types::Number) {
use std::convert::TryInto; // Unnecessary import if you're using Rust 2021 edition.
let foo: f32 = n.try_into().expect("lossy conversion detected"); // Or handle the error instead of panicking.
// You can still do lossy conversions, but only into floating point types.
let foo: f32 = n.to_f32_lossy();
// To lossily convert into integer types, use an `as` cast directly.
let bar: u32 = n as u32; // Lossy conversion!
}
```
"""
references = ["smithy-rs#1274"]
meta = { "breaking" = true, "tada" = false, "bug" = true }
author = "david-perez"
Original file line number Diff line number Diff line change
Expand Up @@ -660,40 +660,14 @@ class ServerProtocolTestGenerator(
FailingTest(RestJson, "RestJsonWithPayloadExpectsImpliedAccept", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyMalformedBlobInvalidBase64_case1", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyMalformedBlobInvalidBase64_case2", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyByteMalformedValueRejected_case2", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyByteMalformedValueRejected_case6", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyByteMalformedValueRejected_case8", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyByteMalformedValueRejected_case10", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyByteUnderflowOverflow_case0", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyByteUnderflowOverflow_case1", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyByteUnderflowOverflow_case2", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyByteUnderflowOverflow_case3", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonWithBodyExpectsApplicationJsonContentType", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonWithPayloadExpectsImpliedContentType", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonWithPayloadExpectsModeledContentType", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonWithoutBodyExpectsEmptyContentType", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyIntegerMalformedValueRejected_case2", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyIntegerMalformedValueRejected_case6", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyIntegerMalformedValueRejected_case8", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyIntegerMalformedValueRejected_case10", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyIntegerUnderflowOverflow_case0", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyIntegerUnderflowOverflow_case1", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyMalformedListNullItem", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyMalformedMapNullValue", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyLongMalformedValueRejected_case2", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyLongMalformedValueRejected_case6", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyLongMalformedValueRejected_case8", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyLongMalformedValueRejected_case10", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonMalformedSetDuplicateItems", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonMalformedSetNullItem", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyShortMalformedValueRejected_case2", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyShortMalformedValueRejected_case6", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyShortMalformedValueRejected_case8", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyShortMalformedValueRejected_case10", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyShortUnderflowOverflow_case0", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyShortUnderflowOverflow_case1", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyShortUnderflowOverflow_case2", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyShortUnderflowOverflow_case3", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonHeaderMalformedStringInvalidBase64MediaType_case1", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyTimestampDateTimeRejectsUTCOffsets_case0", TestType.MalformedRequest),
FailingTest(RestJson, "RestJsonBodyTimestampDefaultRejectsMalformedEpochSeconds_case5", TestType.MalformedRequest),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,23 @@ class JsonParserGenerator(
}

private fun RustWriter.deserializeNumber(target: NumberShape) {
val symbol = symbolProvider.toSymbol(target)
rustTemplate("#{expect_number_or_null}(tokens.next())?.map(|v| v.to_#{T}())", "T" to symbol, *codegenScope)
if (target.isFloatShape) {
rustTemplate("#{expect_number_or_null}(tokens.next())?.map(|v| v.to_f32_lossy())", *codegenScope)
} else if (target.isDoubleShape) {
rustTemplate("#{expect_number_or_null}(tokens.next())?.map(|v| v.to_f64_lossy())", *codegenScope)
} else {
rustTemplate(
"""
#{expect_number_or_null}(tokens.next())?
.map(|v| {
use std::convert::TryInto;
david-perez marked this conversation as resolved.
Show resolved Hide resolved
v.try_into()
})
.transpose()?
""",
*codegenScope
)
}
}

private fun RustWriter.deserializeTimestamp(member: MemberShape) {
Expand Down
4 changes: 2 additions & 2 deletions rust-runtime/aws-smithy-json/src/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ impl<'a> JsonTokenIterator<'a> {
/// returns `(start_index, end_index, negative, floating)`, with `start_index`
/// and `end_index` representing the slice of the stream that is the number,
/// `negative` whether or not it is a negative number, and `floating` whether or not
/// it is a floating point number.
/// the number contains a decimal point and/or an exponent.
fn scan_number(&mut self) -> (usize, usize, bool, bool) {
let start_index = self.index;
let negative = if self.peek_byte() == Some(b'-') {
Expand Down Expand Up @@ -338,7 +338,7 @@ impl<'a> JsonTokenIterator<'a> {
if negative > 0 {
Number::Float(-(positive as f64))
} else {
Number::NegInt(negative as i64)
Number::NegInt(negative)
}
} else {
Number::PosInt(
Expand Down
9 changes: 9 additions & 0 deletions rust-runtime/aws-smithy-json/src/deserialize/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,12 @@ impl From<EscapeError> for Error {
}
}
}

impl From<aws_smithy_types::TryFromNumberError> for Error {
fn from(_: aws_smithy_types::TryFromNumberError) -> Self {
Error {
reason: ErrorReason::InvalidNumber,
offset: None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

if we throw away this offset is it going to make it really hard to debug? Or do we have another mechanism to track the exact field where this was a problem?

Copy link
Contributor Author

@david-perez david-perez Apr 11, 2022

Choose a reason for hiding this comment

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

We don't have the offset here. We only have the offset when working with the token directly in the code-generated deserializer, or when calling the expect_ functions which auto-fill it from the token in case of errors. When unescaping strings, we're also bubbling up errors without offsets. Making both cases bubble up offsets would require some refactoring in the parser generation.

}
}
}
2 changes: 1 addition & 1 deletion rust-runtime/aws-smithy-json/src/deserialize/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ pub fn expect_timestamp_or_null(
) -> Result<Option<DateTime>, Error> {
Ok(match timestamp_format {
Format::EpochSeconds => {
expect_number_or_null(token)?.map(|v| DateTime::from_secs_f64(v.to_f64()))
expect_number_or_null(token)?.map(|v| DateTime::from_secs_f64(v.to_f64_lossy()))
}
Format::DateTime | Format::HttpDate => expect_string_or_null(token)?
.map(|v| DateTime::from_str(v.as_escaped_str(), timestamp_format))
Expand Down
Loading