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

Add serde support to document type. #2616

Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
bc55504
add serde support on meta data
thomas-k-cameron Apr 20, 2023
931a23e
add serde support to
thomas-k-cameron Apr 20, 2023
f7715fb
update lib.rs
thomas-k-cameron Apr 20, 2023
8cf5e3c
remove unused imports
thomas-k-cameron Apr 20, 2023
eef5b35
- add RUSTFLAGS
thomas-k-cameron Apr 21, 2023
06adf2f
Merge branch 'main' into RFC30/kotlin-buildSrc
thomas-k-cameron Apr 21, 2023
a0f0400
Merge commit 'bc55504b8741bedbf2bac2e214dc7796cf5b9a85' into RFC30/ru…
thomas-k-cameron Apr 21, 2023
749f73d
Merge branch 'main' into RFC30/rust-runtime-impl-serde-on-runtime-types
thomas-k-cameron Apr 21, 2023
c203d2e
FIX
thomas-k-cameron Apr 21, 2023
17ab7ff
serde support for error metadata
thomas-k-cameron Apr 21, 2023
02dda32
Merge branch 'main' into RFC30/kotlin-buildSrc
thomas-k-cameron Apr 26, 2023
81ccbbf
chore: update CHANGELOG.next.toml
thomas-k-cameron Apr 26, 2023
5747deb
Merge commit 'eef5b35a22c4f3c70a7e2387c2cf129da637ef3f'
thomas-k-cameron Apr 26, 2023
59babf7
Merge branch 'main' into RFC30/error-metadata
thomas-k-cameron Apr 26, 2023
6edcd1a
chore: update CHANGELOG.next.toml
thomas-k-cameron Apr 26, 2023
4d8f5dd
Merge branch 'main' into RFC30/rust-runtime-impl-serde-on-runtime-types
thomas-k-cameron Apr 26, 2023
b219038
update
thomas-k-cameron Apr 26, 2023
f6acd67
Merge commit '02dda32725fb38e0b865c8105b3743cae8c7a171' into RFC30/ru…
thomas-k-cameron Apr 26, 2023
7e690c7
Merge commit '6edcd1a418d73c1a6b4244bace78856d5d1a2fa5' into RFC30/ru…
thomas-k-cameron Apr 26, 2023
6138ccd
chore: update CHANGELOG.next.toml
thomas-k-cameron Apr 26, 2023
caaa193
Update CHANGELOG.next.toml
thomas-k-cameron Apr 26, 2023
8b3b640
Update CHANGELOG.next.toml
thomas-k-cameron Apr 26, 2023
7207a9c
Merge branch 'main' into RFC30/rust-runtime-impl-serde-on-runtime-types
thomas-k-cameron Apr 27, 2023
de7971f
Add serde support to number type
thomas-k-cameron Apr 27, 2023
bf7347a
pre-commit
thomas-k-cameron Apr 27, 2023
6e4b68a
Update number.rs
thomas-k-cameron Apr 27, 2023
d89e8f4
Update metadata.rs
thomas-k-cameron Apr 27, 2023
3d62a68
Update metadata.rs
thomas-k-cameron Apr 27, 2023
5d850d9
Update metadata.rs
thomas-k-cameron Apr 27, 2023
e27abc1
Delete error_meta_data.json
thomas-k-cameron Apr 27, 2023
b69160d
Delete error_meta_data_empty.json
thomas-k-cameron Apr 27, 2023
c10824f
Merge commit '6e4b68a061637f7407be2cc633d43be24e4225e7' into RFC30/ru…
thomas-k-cameron Apr 27, 2023
54fa834
FIX
thomas-k-cameron Apr 27, 2023
c8acea1
Update number.rs
thomas-k-cameron Apr 27, 2023
cc4ba98
FIX?
thomas-k-cameron Apr 27, 2023
6c6e344
Merge branch 'main' into RFC30/rust-runtime-impl-serde-on-runtime-types
thomas-k-cameron Apr 28, 2023
e1957e3
Merge branch 'main' into RFC30/rust-runtime-impl-serde-on-runtime-types
thomas-k-cameron Apr 29, 2023
dedf9dc
Merge commit 'e1cf72e52d152519f57a66d95a23322fe1de0759' into RFC30/ru…
thomas-k-cameron Jun 5, 2023
19cb914
Merge commit '74a7204123555ff68c841b42cbe273e08cef1c16' into RFC30/ru…
thomas-k-cameron Jun 5, 2023
4996788
update
thomas-k-cameron Jun 5, 2023
0a8dfef
Update Cargo.toml
thomas-k-cameron Jun 5, 2023
efac2c5
Update document.rs
thomas-k-cameron Jun 5, 2023
c6bc3d9
Merge branch 'main' into RFC30/rust-runtime-impl-serde-on-runtime-types
thomas-k-cameron Jun 13, 2023
875b621
update
thomas-k-cameron Jun 13, 2023
ba06f04
asdf
thomas-k-cameron Jun 13, 2023
9aabc04
Merge branch 'main' into RFC30/rust-runtime-impl-serde-on-runtime-types
thomas-k-cameron Jun 13, 2023
2fdf75c
Merge branch 'main' into RFC30/rust-runtime-impl-serde-on-runtime-types
thomas-k-cameron Jun 14, 2023
ddefd70
Update blob.rs
thomas-k-cameron Jun 14, 2023
4bcfb2a
Merge branch 'main' into RFC30/rust-runtime-impl-serde-on-runtime-types
thomas-k-cameron Jun 14, 2023
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
1 change: 0 additions & 1 deletion rust-runtime/aws-smithy-types/src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ impl AsRef<[u8]> for Blob {
#[cfg(all(aws_sdk_unstable, feature = "serde-serialize"))]
mod serde_serialize {
use super::*;
use crate::base64;
use serde::Serialize;

impl Serialize for Blob {
Expand Down
78 changes: 78 additions & 0 deletions rust-runtime/aws-smithy-types/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ use crate::Number;
use std::borrow::Cow;
use std::collections::HashMap;

#[cfg(any(
all(aws_sdk_unstable, feature = "serde-deserialize"),
all(aws_sdk_unstable, feature = "serde-serialize")
))]
use serde;

/* ANCHOR: document */

/// Document Type
Expand All @@ -16,6 +22,21 @@ use std::collections::HashMap;
/// modeled using rigid types, or data that has a schema that evolves outside of the purview of a model.
/// The serialization format of a document is an implementation detail of a protocol.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(
all(aws_sdk_unstable, feature = "serde-serialize"),
derive(serde::Serialize)
)]
#[cfg_attr(
all(aws_sdk_unstable, feature = "serde-deserialize"),
derive(serde::Deserialize)
)]
#[cfg_attr(
any(
all(aws_sdk_unstable, feature = "serde-deserialize"),
all(aws_sdk_unstable, feature = "serde-serialize")
),
serde(untagged)
)]
pub enum Document {
/// JSON object
Object(HashMap<String, Document>),
Expand Down Expand Up @@ -207,3 +228,60 @@ impl From<Number> for Document {
Document::Number(value)
}
}

/* ANCHOR END: document */

#[cfg(test)]
mod test {
/// checks if a) serialization of json suceeds and b) it is compatible with serde_json
#[test]
#[cfg(all(
aws_sdk_unstable,
feature = "serde-serialize",
feature = "serde-deserialize"
))]
fn serialize_json() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice test!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

:D

use crate::Document;
use crate::Number;
use std::collections::HashMap;
let mut map: HashMap<String, Document> = HashMap::new();
// string
map.insert("hello".into(), "world".to_string().into());
// numbers
map.insert("pos_int".into(), Document::Number(Number::PosInt(1).into()));
map.insert(
"neg_int".into(),
Document::Number(Number::NegInt(-1).into()),
);
map.insert(
"float".into(),
Document::Number(Number::Float(0.1 + 0.2).into()),
);
// booleans
map.insert("true".into(), true.into());
map.insert("false".into(), false.into());
// check if array with different datatypes would succeed
map.insert(
"array".into(),
vec![
map.clone().into(),
"hello-world".to_string().into(),
true.into(),
false.into(),
]
.into(),
);
// map
map.insert("map".into(), map.clone().into());
// null
map.insert("null".into(), Document::Null);
let obj = Document::Object(map);
// comparing string isnt going to work since there is no gurantee for the ordering of the keys
let target_file = include_str!("../test_data/serialize_document.json");
let json: Result<serde_json::Value, _> = serde_json::from_str(target_file);
// serializer
assert_eq!(serde_json::to_value(&obj).unwrap(), json.unwrap());
let doc: Result<Document, _> = serde_json::from_str(target_file);
assert_eq!(obj, doc.unwrap());
}
}
43 changes: 43 additions & 0 deletions rust-runtime/aws-smithy-types/test_data/serialize_document.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"null": null,
"true": true,
"pos_int": 1,
"false": false,
"map": {
"array": [
{
"pos_int": 1,
"float": 0.30000000000000004,
"neg_int": -1,
"hello": "world",
"false": false,
"true": true
},
"hello-world",
true,
false
],
"pos_int": 1,
"float": 0.30000000000000004,
"neg_int": -1,
"hello": "world",
"false": false,
"true": true
},
"float": 0.30000000000000004,
"neg_int": -1,
"hello": "world",
"array": [
{
"pos_int": 1,
"float": 0.30000000000000004,
"neg_int": -1,
"hello": "world",
"false": false,
"true": true
},
"hello-world",
true,
false
]
}