generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 230
Rfc30/blob #2181
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
Merged
Velfi
merged 15 commits into
smithy-lang:unstable-serde-support
from
thomas-k-cameron:rfc30/blob
Jan 19, 2023
Merged
Rfc30/blob #2181
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
fa5c315
rfc30 blob
thomas-k-cameron 4bb26c8
blob import fix
thomas-k-cameron 295d914
change unstable keyword to aws_sdk_unstable
thomas-k-cameron 40e2c3c
change unstable keyword to aws_sdk_unstable
thomas-k-cameron a8ec1b7
add test
thomas-k-cameron 168db00
updater changelog
thomas-k-cameron c289942
Merge branch 'main' into rfc30/blob
thomas-k-cameron e666259
Merge branch 'rfc30/blob' of https://github.com/thomas-k-cameron/smit…
thomas-k-cameron ee5dce2
Merge branch 'main' into rfc30/blob
thomas-k-cameron daba59a
Update CHANGELOG.next.toml
thomas-k-cameron 1d1893b
Merge branch 'main' into rfc30/blob
thomas-k-cameron ac7faf7
Merge branch 'unstable-serde-support' into rfc30/blob
a9c0be5
Merge branch 'unstable-serde-support' into rfc30/blob
c6cfcec
Update lib.rs
Velfi b48c2e7
remove/unused import
Velfi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #[cfg(all(test, aws_sdk_unstable, feature = "serialize", feature = "deserialize"))] | ||
| mod test; | ||
|
|
||
| #[cfg(all(aws_sdk_unstable, feature = "serialize"))] | ||
| use serde::Serialize; | ||
| #[cfg(all(aws_sdk_unstable, feature = "deserialize"))] | ||
| use serde::{de::Visitor, Deserialize}; | ||
|
|
||
| /// Binary Blob Type | ||
| /// | ||
| /// Blobs represent protocol-agnostic binary content. | ||
| #[derive(Debug, PartialEq, Clone)] | ||
| pub struct Blob { | ||
| inner: Vec<u8>, | ||
| } | ||
|
|
||
| impl Blob { | ||
| /// Creates a new blob from the given `input`. | ||
| pub fn new<T: Into<Vec<u8>>>(input: T) -> Self { | ||
| Blob { | ||
| inner: input.into(), | ||
| } | ||
| } | ||
|
|
||
| /// Consumes the `Blob` and returns a `Vec<u8>` with its contents. | ||
| pub fn into_inner(self) -> Vec<u8> { | ||
| self.inner | ||
| } | ||
| } | ||
|
|
||
| impl AsRef<[u8]> for Blob { | ||
| fn as_ref(&self) -> &[u8] { | ||
| &self.inner | ||
| } | ||
| } | ||
|
|
||
| #[cfg(all(aws_sdk_unstable, feature = "serialize"))] | ||
| impl Serialize for Blob { | ||
| fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
| where | ||
| S: serde::Serializer, | ||
| { | ||
| if serializer.is_human_readable() { | ||
| serializer.serialize_str(&crate::base64::encode(&self.inner)) | ||
| } else { | ||
| serializer.serialize_bytes(&self.inner) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(all(aws_sdk_unstable, feature = "deserialize"))] | ||
| struct HumanReadableBlobVisitor; | ||
|
|
||
| #[cfg(all(aws_sdk_unstable, feature = "deserialize"))] | ||
| impl<'de> Visitor<'de> for HumanReadableBlobVisitor { | ||
| type Value = Blob; | ||
| fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| formatter.write_str("expected base64 encoded string") | ||
| } | ||
|
|
||
| fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> | ||
| where | ||
| E: serde::de::Error, | ||
| { | ||
| match base64::decode(v) { | ||
| Ok(inner) => Ok(Blob { inner }), | ||
| Err(e) => Err(serde::de::Error::custom(e)), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(all(aws_sdk_unstable, feature = "deserialize"))] | ||
| struct NotHumanReadableBlobVisitor; | ||
|
|
||
| #[cfg(all(aws_sdk_unstable, feature = "deserialize"))] | ||
| impl<'de> Visitor<'de> for NotHumanReadableBlobVisitor { | ||
| type Value = Blob; | ||
| fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| formatter.write_str("expected base64 encoded string") | ||
| } | ||
|
|
||
| fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E> | ||
| where | ||
| E: serde::de::Error, | ||
| { | ||
| Ok(Blob { inner: v }) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(all(aws_sdk_unstable, feature = "deserialize"))] | ||
| impl<'de> Deserialize<'de> for Blob { | ||
| fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
| where | ||
| D: serde::Deserializer<'de>, | ||
| { | ||
| if deserializer.is_human_readable() { | ||
| deserializer.deserialize_str(HumanReadableBlobVisitor) | ||
| } else { | ||
| deserializer.deserialize_byte_buf(NotHumanReadableBlobVisitor) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| use crate::*; | ||
| use serde::{Deserialize, Serialize}; | ||
| use std::ffi::CString; | ||
|
|
||
| #[derive(Deserialize, Serialize, Debug, PartialEq)] | ||
| struct ForTest { | ||
| blob: Blob, | ||
| } | ||
|
|
||
| #[test] | ||
| fn human_readable_blob() { | ||
| let aws_in_base64 = r#"{"blob":"QVdT"}"#; | ||
| let for_test = ForTest { | ||
| blob: Blob { | ||
| inner: vec![b'A', b'W', b'S'], | ||
| }, | ||
| }; | ||
| assert_eq!(for_test, serde_json::from_str(aws_in_base64).unwrap()); | ||
| assert_eq!(serde_json::to_string(&for_test).unwrap(), aws_in_base64); | ||
| } | ||
|
|
||
| #[test] | ||
| fn not_human_readable_blob() { | ||
| let for_test = ForTest { | ||
| blob: Blob { | ||
| inner: vec![b'A', b'W', b'S'], | ||
| }, | ||
| }; | ||
| let mut buf = vec![]; | ||
| let res = ciborium::ser::into_writer(&for_test, &mut buf); | ||
| assert!(res.is_ok()); | ||
|
|
||
| // checks whether the bytes are deserialiezd properly | ||
| let n: HashMap<String, CString> = | ||
| ciborium::de::from_reader(std::io::Cursor::new(buf.clone())).unwrap(); | ||
| assert!(n.get("blob").is_some()); | ||
| assert!(n.get("blob") == CString::new([65, 87, 83]).ok().as_ref()); | ||
|
|
||
| let de: ForTest = ciborium::de::from_reader(std::io::Cursor::new(buf)).unwrap(); | ||
| assert_eq!(for_test, de); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind moving the tests into a module in this same file?