forked from hyperledger-iroha/iroha
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] hyperledger-iroha#3737: Add support for usage of Self type …
…in serde partially tagged enums Signed-off-by: Nikita Strygin <[email protected]>
- Loading branch information
Showing
5 changed files
with
132 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 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 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,63 @@ | ||
use syn2::visit_mut::VisitMut; | ||
|
||
struct Visitor<'a> { | ||
self_ty: &'a syn2::Type, | ||
} | ||
|
||
impl VisitMut for Visitor<'_> { | ||
fn visit_type_mut(&mut self, ty: &mut syn2::Type) { | ||
match ty { | ||
syn2::Type::Path(path_ty) | ||
if path_ty.qself.is_none() && path_ty.path.is_ident("Self") => | ||
{ | ||
*ty = self.self_ty.clone(); | ||
} | ||
_ => syn2::visit_mut::visit_type_mut(self, ty), | ||
} | ||
} | ||
} | ||
|
||
/// Transforms the [`resolving_ty`] by replacing `Self` with [`self_ty`]. | ||
/// | ||
/// This is required to be able to use `Self` in `PartiallyTaggedSerialize` and `PartiallyTaggedDeserialize`, | ||
/// as they define an additional intermediate type during serialization/deserialization. Using `Self` there would refer to an incorrect type. | ||
pub fn resolve_self(self_ty: &syn2::Type, mut resolving_ty: syn2::Type) -> syn2::Type { | ||
Visitor { self_ty }.visit_type_mut(&mut resolving_ty); | ||
resolving_ty | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use quote::ToTokens; | ||
use syn2::{parse_quote, Type}; | ||
|
||
#[test] | ||
fn test_resolve_self() { | ||
let test_types = [ | ||
parse_quote!(i32), | ||
parse_quote!(Self), | ||
parse_quote!(Vec<Self>), | ||
parse_quote!((Self, Self)), | ||
parse_quote!(<Self as Trait>::Type), | ||
]; | ||
let expected_types = [ | ||
parse_quote!(i32), | ||
parse_quote!(()), | ||
parse_quote!(Vec<()>), | ||
parse_quote!(((), ())), | ||
parse_quote!(<() as Trait>::Type), | ||
]; | ||
let _: &Type = &test_types[0]; | ||
let _: &Type = &expected_types[0]; | ||
|
||
for (test_type, expected_type) in test_types.iter().zip(expected_types.iter()) { | ||
let resolved = super::resolve_self(&parse_quote!(()), test_type.clone()); | ||
assert_eq!( | ||
resolved, | ||
*expected_type, | ||
"Failed to resolve `Self` in `{}`", | ||
test_type.to_token_stream().to_string() | ||
); | ||
} | ||
} | ||
} |
This file contains 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,39 @@ | ||
//! A test for `PartiallyTaggedSerialize` and `PartiallyTaggedDeserialize` which uses `Self` as a type | ||
use iroha_data_model_derive::{PartiallyTaggedDeserialize, PartiallyTaggedSerialize}; | ||
|
||
#[derive(Debug, PartialEq, Eq, PartiallyTaggedSerialize, PartiallyTaggedDeserialize)] | ||
enum Expr<T> { | ||
Negate(Box<Self>), | ||
#[serde_partially_tagged(untagged)] | ||
Atom(T), | ||
} | ||
|
||
#[test] | ||
fn partially_tagged_serde() { | ||
use Expr::*; | ||
|
||
let values = [ | ||
Atom(42), | ||
Negate(Box::new(Atom(42))), | ||
Negate(Box::new(Negate(Box::new(Atom(42))))), | ||
]; | ||
let serialized_values = [r#"42"#, r#"{"Negate":42}"#, r#"{"Negate":{"Negate":42}}"#]; | ||
|
||
for (value, serialized_value) in values.iter().zip(serialized_values.iter()) { | ||
let serialized = serde_json::to_string(value) | ||
.unwrap_or_else(|e| panic!("Failed to serialize `{:?}`: {:?}", value, e)); | ||
assert_eq!( | ||
serialized, *serialized_value, | ||
"Serialized form of `{:?}` does not match the expected value", | ||
value | ||
); | ||
let deserialized: Expr<i32> = serde_json::from_str(serialized_value) | ||
.unwrap_or_else(|e| panic!("Failed to deserialize `{:?}`: {:?}", serialized_value, e)); | ||
assert_eq!( | ||
*value, deserialized, | ||
"Deserialized form of `{:?}` does not match the expected value", | ||
value | ||
); | ||
} | ||
} |
This file contains 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