From 840221dddb75ada42991224ddb224145c6cdbe37 Mon Sep 17 00:00:00 2001 From: Thomas Cameron Date: Wed, 31 May 2023 03:47:41 +0900 Subject: [PATCH] Add serde support to number type (#2645) ## Motivation and Context This is a child PR of https://github.com/awslabs/smithy-rs/pull/2616 The changes that this PR introduces is same as the ones that were merged to `unstable-serde-support` branch before. Initially, we tried to make commit to unstable-serde-support branch and merge changes one by one in small PRs. However, in order to make it up to date with the main branch, we would need to go through a large PR of over 700 files. Thus, I decided to create individual PRs that commits directly to `main` branch. ## Description - Implements `serde` support to `Number` ## Testing - Test checks whether the serialized/de-serialized data matches with the expected value ## Checklist NA ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._ --------- Co-authored-by: John DiSanti Co-authored-by: John DiSanti --- rust-runtime/aws-smithy-types/src/number.rs | 49 +++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/rust-runtime/aws-smithy-types/src/number.rs b/rust-runtime/aws-smithy-types/src/number.rs index bb771798d0..85488b1299 100644 --- a/rust-runtime/aws-smithy-types/src/number.rs +++ b/rust-runtime/aws-smithy-types/src/number.rs @@ -3,11 +3,33 @@ * SPDX-License-Identifier: Apache-2.0 */ +//! A number type that implements Javascript / JSON semantics. + use crate::error::{TryFromNumberError, TryFromNumberErrorKind}; +#[cfg(all( + aws_sdk_unstable, + any(feature = "serde-serialize", feature = "serde-deserialize") +))] +use serde; /// A number type that implements Javascript / JSON semantics, modeled on serde_json: /// #[derive(Debug, Clone, Copy, PartialEq)] +#[cfg_attr( + all(aws_sdk_unstable, feature = "serde-deserialize"), + derive(serde::Deserialize) +)] +#[cfg_attr( + all(aws_sdk_unstable, feature = "serde-serialize"), + derive(serde::Serialize) +)] +#[cfg_attr( + any( + all(aws_sdk_unstable, feature = "serde-deserialize"), + all(aws_sdk_unstable, feature = "serde-serialize") + ), + serde(untagged) +)] pub enum Number { /// Unsigned 64-bit integer value. PosInt(u64), @@ -441,4 +463,31 @@ mod test { 1452089100f32 ); } + + #[test] + #[cfg(all( + test, + aws_sdk_unstable, + feature = "serde-deserialize", + feature = "serde-serialize" + ))] + /// ensures that numbers are deserialized as expected + /// 0 <= PosInt + /// 0 > NegInt + /// non integer values == Float + fn number_serde() { + let n: Number = serde_json::from_str("1.1").unwrap(); + assert_eq!(n, Number::Float(1.1)); + let n: Number = serde_json::from_str("1").unwrap(); + assert_eq!(n, Number::PosInt(1)); + let n: Number = serde_json::from_str("0").unwrap(); + assert_eq!(n, Number::PosInt(0)); + let n: Number = serde_json::from_str("-1").unwrap(); + assert_eq!(n, Number::NegInt(-1)); + + assert_eq!("1.1", serde_json::to_string(&Number::Float(1.1)).unwrap()); + assert_eq!("1", serde_json::to_string(&Number::PosInt(1)).unwrap()); + assert_eq!("0", serde_json::to_string(&Number::PosInt(0)).unwrap()); + assert_eq!("-1", serde_json::to_string(&Number::NegInt(-1)).unwrap()); + } }