Reformat types string for decl_storage (#1298)

* Clean space from types when converting to string

* Missing cases.

* Add unit test for complex type, complete clean fn.
This commit is contained in:
cheme
2018-12-19 17:51:46 +01:00
committed by Gav Wood
parent 66ad1ed974
commit 9c8e3ffb52
3 changed files with 47 additions and 5 deletions
@@ -19,7 +19,7 @@
// end::description[]
use srml_support_procedural_tools::syn_ext as ext;
use srml_support_procedural_tools::{generate_crate_access, generate_hidden_includes};
use srml_support_procedural_tools::{generate_crate_access, generate_hidden_includes, clean_type_string};
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
@@ -577,7 +577,7 @@ fn store_functions_to_metadata (
let is_option = extracted_opt.is_some();
let typ = extracted_opt.unwrap_or(quote!( #gettype ));
let stype = if is_simple {
let styp = typ.to_string();
let styp = clean_type_string(&typ.to_string());
quote!{
#scrate::storage::generator::StorageFunctionType::Plain(
#scrate::storage::generator::DecodeDifferent::Encode(#styp),
@@ -585,8 +585,8 @@ fn store_functions_to_metadata (
}
} else {
let kty = stk.expect("is not simple; qed");
let kty = quote!(#kty).to_string();
let styp = typ.to_string();
let kty = clean_type_string(&quote!(#kty).to_string());
let styp = clean_type_string(&typ.to_string());
quote!{
#scrate::storage::generator::StorageFunctionType::Map {
key: #scrate::storage::generator::DecodeDifferent::Encode(#kty),
@@ -92,3 +92,22 @@ pub fn generate_hidden_includes(unique_id: &str, def_crate: &str, crate_id: &str
)
}.into()
}
// fn to remove white spaces arount string types
// (basically whitespaces arount tokens)
pub fn clean_type_string(input: &str) -> String {
input
.replace(" ::", "::")
.replace(":: ", "::")
.replace(" ,", ",")
.replace(" ;", ";")
.replace(" [", "[")
.replace("[ ", "[")
.replace(" ]", "]")
.replace(" (", "(")
.replace("( ", "(")
.replace(" )", ")")
.replace(" <", "<")
.replace("< ", "<")
.replace(" >", ">")
}
@@ -633,6 +633,10 @@ mod tests {
GETMAPU32MYDEF get(map_u32_getter_mydef): map u32 => String = "map".into();
pub PUBGETMAPU32MYDEF get(pub_map_u32_getter_mydef): map u32 => String = "pubmap".into();
COMPLEX_TYPE1: ::std::vec::Vec<<T as Trait>::Origin>;
COMPLEX_TYPE2: (Vec<Vec<(u16,Box<( )>)>>, u32);
COMPLEX_TYPE3: ([u32;25]);
}
add_extra_genesis {
build(|_, _, _| {});
@@ -677,7 +681,7 @@ mod tests {
StorageFunctionMetadata {
name: DecodeDifferent::Encode("GETU32"),
modifier: StorageFunctionModifier::Default,
ty: StorageFunctionType::Plain(DecodeDifferent::Encode("T :: Origin")),
ty: StorageFunctionType::Plain(DecodeDifferent::Encode("T::Origin")),
documentation: DecodeDifferent::Encode(&[]),
},
StorageFunctionMetadata {
@@ -796,6 +800,24 @@ mod tests {
},
documentation: DecodeDifferent::Encode(&[]),
},
StorageFunctionMetadata {
name: DecodeDifferent::Encode("COMPLEX_TYPE1"),
modifier: StorageFunctionModifier::Default,
ty: StorageFunctionType::Plain(DecodeDifferent::Encode("::std::vec::Vec<<T as Trait>::Origin>")),
documentation: DecodeDifferent::Encode(&[]),
},
StorageFunctionMetadata {
name: DecodeDifferent::Encode("COMPLEX_TYPE2"),
modifier: StorageFunctionModifier::Default,
ty: StorageFunctionType::Plain(DecodeDifferent::Encode("(Vec<Vec<(u16, Box<()>)>>, u32)")),
documentation: DecodeDifferent::Encode(&[]),
},
StorageFunctionMetadata {
name: DecodeDifferent::Encode("COMPLEX_TYPE3"),
modifier: StorageFunctionModifier::Default,
ty: StorageFunctionType::Plain(DecodeDifferent::Encode("([u32; 25])")),
documentation: DecodeDifferent::Encode(&[]),
},
])
};
@@ -816,6 +838,7 @@ mod tests {
assert_eq!(config.pub_u32_getter_with_config_mydef, 1u32);
assert_eq!(config.pub_u32_getter_with_config_mydef_opt, 100u32);
}
}
#[cfg(test)]