Explicitly declare decl_storage! getters as functions (#3870)

* parse decl_storage getters with fn keyword

* test for get in decl_storage

* update all decl_storage! getters

* bump version

* adjust missed doc line
This commit is contained in:
Robert Habermeier
2019-10-22 03:53:58 -04:00
committed by Bastian Köcher
parent 1111d79ac1
commit 5d5e71028e
36 changed files with 190 additions and 175 deletions
+3 -3
View File
@@ -33,7 +33,7 @@ use proc_macro::TokenStream;
/// ```nocompile
/// decl_storage! {
/// trait Store for Module<T: Trait> as Example {
/// Foo get(foo) config(): u32=12;
/// Foo get(fn foo) config(): u32=12;
/// Bar: map u32 => u32;
/// pub Zed build(|config| vec![(0, 0)]): linked_map u32 => u32;
/// }
@@ -120,11 +120,11 @@ use proc_macro::TokenStream;
///
/// Basic storage can be extended as such:
///
/// `#vis #name get(#getter) config(#field_name) build(#closure): #type = #default;`
/// `#vis #name get(fn #getter) config(#field_name) build(#closure): #type = #default;`
///
/// * `#vis`: Set the visibility of the structure. `pub` or nothing.
/// * `#name`: Name of the storage item, used as a prefix in storage.
/// * [optional] `get(#getter)`: Implements the function #getter to `Module`.
/// * [optional] `get(fn #getter)`: Implements the function #getter to `Module`.
/// * [optional] `config(#field_name)`: `field_name` is optional if get is set.
/// Will include the item in `GenesisConfig`.
/// * [optional] `build(#closure)`: Closure called with storage overlays.
@@ -113,11 +113,16 @@ struct DeclStorageLine {
pub default_value: ext::Opt<DeclStorageDefault>,
}
#[derive(Parse, ToTokens, Debug)]
struct DeclStorageGetterBody {
fn_keyword: Option<Token![fn]>,
ident: Ident,
}
#[derive(Parse, ToTokens, Debug)]
struct DeclStorageGetter {
pub getter_keyword: keyword::get,
pub getfn: ext::Parens<Ident>,
pub getfn: ext::Parens<DeclStorageGetterBody>,
}
#[derive(Parse, ToTokens, Debug)]
@@ -301,17 +306,17 @@ pub fn parse(input: syn::parse::ParseStream) -> syn::Result<super::DeclStorageDe
let mut storage_lines = vec![];
for line in def.content.content.inner.into_iter() {
let getter = line.getter.inner.map(|o| o.getfn.content);
let getter = line.getter.inner.map(|o| o.getfn.content.ident);
let config = if let Some(config) = line.config.inner {
if let Some(ident) = config.expr.content {
Some(ident)
} else if let Some(ident) = getter.clone() {
Some(ident)
} else if let Some(ref ident) = getter {
Some(ident.clone())
} else {
return Err(syn::Error::new(
config.span(),
"Invalid storage definiton, couldn't find config identifier: storage must either have a get \
identifier `get(ident)` or a defined config identifier `config(ident)`"
identifier `get(fn ident)` or a defined config identifier `config(ident)`"
))
}
} else {
+13 -3
View File
@@ -260,10 +260,11 @@ mod tests {
decl_storage! {
trait Store for Module<T: Trait> as Example {
pub Data get(data) build(|_| vec![(15u32, 42u64)]): linked_map hasher(twox_64_concat) u32 => u64;
pub Data get(fn data) build(|_| vec![(15u32, 42u64)]): linked_map hasher(twox_64_concat) u32 => u64;
pub OptionLinkedMap: linked_map u32 => Option<u32>;
pub GenericData get(generic_data): linked_map hasher(twox_128) T::BlockNumber => T::BlockNumber;
pub GenericData2 get(generic_data2): linked_map T::BlockNumber => Option<T::BlockNumber>;
pub GenericData get(fn generic_data): linked_map hasher(twox_128) T::BlockNumber => T::BlockNumber;
pub GenericData2 get(fn generic_data2): linked_map T::BlockNumber => Option<T::BlockNumber>;
pub GetterNoFnKeyword get(no_fn): Option<u32>;
pub DataDM config(test_config) build(|_| vec![(15u32, 16u32, 42u64)]):
double_map hasher(twox_64_concat) u32, blake2_256(u32) => u64;
@@ -516,6 +517,15 @@ mod tests {
),
documentation: DecodeDifferent::Encode(&[]),
},
StorageEntryMetadata {
name: DecodeDifferent::Encode("GetterNoFnKeyword"),
modifier: StorageEntryModifier::Optional,
ty: StorageEntryType::Plain(DecodeDifferent::Encode("u32")),
default: DecodeDifferent::Encode(
DefaultByteGetter(&__GetByteStructGetterNoFnKeyword(PhantomData::<Test>))
),
documentation: DecodeDifferent::Encode(&[]),
},
StorageEntryMetadata {
name: DecodeDifferent::Encode("DataDM"),
modifier: StorageEntryModifier::Default,
@@ -307,15 +307,15 @@ mod tests {
// getters: pub / $default
// we need at least one type which uses T, otherwise GenesisConfig will complain.
GETU32 get(u32_getter): T::Origin;
pub PUBGETU32 get(pub_u32_getter) build(|config: &GenesisConfig| config.u32_getter_with_config): u32;
GETU32WITHCONFIG get(u32_getter_with_config) config(): u32;
pub PUBGETU32WITHCONFIG get(pub_u32_getter_with_config) config(): u32;
GETU32MYDEF get(u32_getter_mydef): Option<u32>;
pub PUBGETU32MYDEF get(pub_u32_getter_mydef) config(): u32 = 3;
GETU32WITHCONFIGMYDEF get(u32_getter_with_config_mydef) config(): u32 = 2;
pub PUBGETU32WITHCONFIGMYDEF get(pub_u32_getter_with_config_mydef) config(): u32 = 1;
PUBGETU32WITHCONFIGMYDEFOPT get(pub_u32_getter_with_config_mydef_opt) config(): Option<u32>;
GETU32 get(fn u32_getter): T::Origin;
pub PUBGETU32 get(fn pub_u32_getter) build(|config: &GenesisConfig| config.u32_getter_with_config): u32;
GETU32WITHCONFIG get(fn u32_getter_with_config) config(): u32;
pub PUBGETU32WITHCONFIG get(fn pub_u32_getter_with_config) config(): u32;
GETU32MYDEF get(fn u32_getter_mydef): Option<u32>;
pub PUBGETU32MYDEF get(fn pub_u32_getter_mydef) config(): u32 = 3;
GETU32WITHCONFIGMYDEF get(fn u32_getter_with_config_mydef) config(): u32 = 2;
pub PUBGETU32WITHCONFIGMYDEF get(fn pub_u32_getter_with_config_mydef) config(): u32 = 1;
PUBGETU32WITHCONFIGMYDEFOPT get(fn pub_u32_getter_with_config_mydef_opt) config(): Option<u32>;
// map non-getters: pub / $default
MAPU32 : map u32 => Option<String>;
@@ -324,17 +324,17 @@ mod tests {
pub PUBMAPU32MYDEF : map u32 => Option<String>;
// map getters: pub / $default
GETMAPU32 get(map_u32_getter): map u32 => String;
pub PUBGETMAPU32 get(pub_map_u32_getter): map u32 => String;
GETMAPU32 get(fn map_u32_getter): map u32 => String;
pub PUBGETMAPU32 get(fn pub_map_u32_getter): map u32 => String;
GETMAPU32MYDEF get(map_u32_getter_mydef): map u32 => String = "map".into();
pub PUBGETMAPU32MYDEF get(pub_map_u32_getter_mydef): map u32 => String = "pubmap".into();
GETMAPU32MYDEF get(fn map_u32_getter_mydef): map u32 => String = "map".into();
pub PUBGETMAPU32MYDEF get(fn pub_map_u32_getter_mydef): map u32 => String = "pubmap".into();
// linked map
LINKEDMAPU32 : linked_map u32 => Option<String>;
pub PUBLINKEDMAPU32MYDEF : linked_map u32 => Option<String>;
GETLINKEDMAPU32 get(linked_map_u32_getter): linked_map u32 => String;
pub PUBGETLINKEDMAPU32MYDEF get(pub_linked_map_u32_getter_mydef): linked_map u32 => String = "pubmap".into();
GETLINKEDMAPU32 get(fn linked_map_u32_getter): linked_map u32 => String;
pub PUBGETLINKEDMAPU32MYDEF get(fn pub_linked_map_u32_getter_mydef): linked_map u32 => String = "pubmap".into();
COMPLEXTYPE1: ::std::vec::Vec<<T as Trait>::Origin>;
COMPLEXTYPE2: (Vec<Vec<(u16,Box<( )>)>>, u32);
@@ -741,7 +741,7 @@ mod test3 {
}
crate::decl_storage! {
trait Store for Module<T: Trait> as Test {
Foo get(foo) config(initial_foo): u32;
Foo get(fn foo) config(initial_foo): u32;
}
}
@@ -44,8 +44,8 @@ mod no_instance {
pub DoubleMap: double_map u32, blake2_256(u32) => u32;
pub DoubleMap2: double_map hasher(twox_128) u32, blake2_128(u32) => u32;
pub TestGenericValue get(test_generic_value) config(): Option<T::BlockNumber>;
pub TestGenericDoubleMap get(foo2) config(test_generic_double_map):
pub TestGenericValue get(fn test_generic_value) config(): Option<T::BlockNumber>;
pub TestGenericDoubleMap get(fn foo2) config(test_generic_double_map):
double_map u32, blake2_256(T::BlockNumber) => Option<u32>;
}
}
@@ -74,8 +74,8 @@ mod instance {
pub DoubleMap: double_map u32, blake2_256(u32) => u32;
pub DoubleMap2: double_map hasher(twox_128) u32, blake2_128(u32) => u32;
pub TestGenericValue get(test_generic_value) config(): Option<T::BlockNumber>;
pub TestGenericDoubleMap get(foo2) config(test_generic_double_map):
pub TestGenericValue get(fn test_generic_value) config(): Option<T::BlockNumber>;
pub TestGenericDoubleMap get(fn foo2) config(test_generic_double_map):
double_map u32, blake2_256(T::BlockNumber) => Option<u32>;
}
add_extra_genesis {
@@ -102,7 +102,7 @@ mod module {
support::decl_storage! {
trait Store for Module<T: Trait> as Actors {
/// requirements to enter and maintain status in roles
pub Parameters get(parameters) build(|config: &GenesisConfig| {
pub Parameters get(fn parameters) build(|config: &GenesisConfig| {
if config.enable_storage_role {
let storage_params: RoleParameters<T> = Default::default();
vec![(Role::Storage, storage_params)]
@@ -112,7 +112,7 @@ mod module {
}): map Role => Option<RoleParameters<T>>;
/// the roles members can enter into
pub AvailableRoles get(available_roles) build(|config: &GenesisConfig| {
pub AvailableRoles get(fn available_roles) build(|config: &GenesisConfig| {
if config.enable_storage_role {
vec![(Role::Storage)]
} else {
@@ -121,13 +121,13 @@ mod module {
}): Vec<Role>;
/// Actors list
pub ActorAccountIds get(actor_account_ids) : Vec<T::AccountId>;
pub ActorAccountIds get(fn actor_account_ids) : Vec<T::AccountId>;
/// actor accounts associated with a role
pub AccountIdsByRole get(account_ids_by_role) : map Role => Vec<T::AccountId>;
pub AccountIdsByRole get(fn account_ids_by_role) : map Role => Vec<T::AccountId>;
/// tokens locked until given block number
pub Bondage get(bondage) : map T::AccountId => T::BlockNumber;
pub Bondage get(fn bondage) : map T::AccountId => T::BlockNumber;
/// First step before enter a role is registering intent with a new account/key.
/// This is done by sending a role_entry_request() from the new account.
@@ -135,10 +135,10 @@ mod module {
/// The account making the request will be bonded and must have
/// sufficient balance to cover the minimum stake for the role.
/// Bonding only occurs after successful entry into a role.
pub RoleEntryRequests get(role_entry_requests) : Requests<T>;
pub RoleEntryRequests get(fn role_entry_requests) : Requests<T>;
/// Entry request expires after this number of blocks
pub RequestLifeTime get(request_life_time) config(request_life_time) : u64 = 0;
pub RequestLifeTime get(fn request_life_time) config(request_life_time) : u64 = 0;
}
add_extra_genesis {
config(enable_storage_role): bool;