Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Use pallet names to name enum variants #8990

Merged
27 commits merged into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
95f2e51
Remove unused event_metadata variable
KiChjang Jun 2, 2021
89d2636
Eliminate mod_name and use pallet names to name enum variants
KiChjang Jun 2, 2021
716f4c1
Rename field name `pallet` to `path`
KiChjang Jun 2, 2021
a0e92ac
Use only the pallet name to generate variant names
KiChjang Jun 2, 2021
f3889c0
Use new naming scheme for Event enum in sudo pallet tests
KiChjang Jun 3, 2021
d8d2de8
Use new naming scheme for Event enum in offences pallet tests
KiChjang Jun 3, 2021
541e98d
Use new naming scheme for Event enum in contracts pallet tests
KiChjang Jun 3, 2021
24ed7f6
Use new naming scheme for Event enum in collective pallet tests
KiChjang Jun 3, 2021
668ca5d
Use new naming scheme for Event enum in bounties pallet tests
KiChjang Jun 3, 2021
e783c27
Use new naming scheme for Event enum in balances pallet tests
KiChjang Jun 3, 2021
28163ef
Use new naming scheme for Event enum in assets pallet tests
KiChjang Jun 3, 2021
42f2edb
Use new naming scheme for Event enum in frame support tests
KiChjang Jun 3, 2021
e7c30bb
Use new naming scheme for Event enum in tips pallet tests
KiChjang Jun 3, 2021
0dc40cf
Use new naming scheme for Event enum in transaction payment pallet tests
KiChjang Jun 3, 2021
5392bd8
Use new naming scheme for GenesisConfig fields in example pallet tests
KiChjang Jun 4, 2021
2df6019
Use new naming scheme for GenesisConfig fields in elections pallet tests
KiChjang Jun 4, 2021
cb7ec66
Use new naming scheme for Event enum in election provider multi-phase…
KiChjang Jun 4, 2021
47f113e
Use new naming scheme for Event enum in elections phragmen pallet tests
KiChjang Jun 4, 2021
2fc3d21
Use new naming scheme for GenesisConfig fields in chain spec
KiChjang Jun 4, 2021
9ab7561
Use new naming scheme for Event enum in staking pallet mock
KiChjang Jun 4, 2021
cd21371
Use new naming scheme for GenesisConfig fields in node-template chain…
KiChjang Jun 4, 2021
ff307ff
Use new naming scheme for GenesisConfig fields in node-testing chain …
KiChjang Jun 4, 2021
adc3269
Use new naming scheme for Event enum in node executor tests
KiChjang Jun 4, 2021
e42bc9d
Merge remote-tracking branch 'origin/master' into kckyeung/pallet-nam…
KiChjang Jun 4, 2021
98269a5
Use new naming scheme for GenesisConfig fields in transaction storage…
KiChjang Jun 4, 2021
f976731
Merge branch 'master' into kckyeung/pallet-name-variants
KiChjang Jun 8, 2021
fd92af0
Refactor match conditions
KiChjang Jun 8, 2021
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
12 changes: 6 additions & 6 deletions frame/support/procedural/src/construct_runtime/expand/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// limitations under the License

use crate::construct_runtime::Pallet;
use inflector::Inflector;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::Ident;
Expand All @@ -32,11 +33,11 @@ pub fn expand_outer_config(
for decl in pallet_decls {
if let Some(pallet_entry) = decl.find_part("Config") {
let config = format_ident!("{}Config", decl.name);
let mod_name = decl.pallet.mod_name();
let pallet_name = &decl.name.to_string().to_snake_case();
let field_name = if let Some(inst) = decl.instance.as_ref() {
format_ident!("{}_{}", mod_name, inst)
format_ident!("{}_{}", pallet_name, inst.to_string().to_snake_case())
KiChjang marked this conversation as resolved.
Show resolved Hide resolved
} else {
mod_name
Ident::new(pallet_name, decl.name.span())
};
let part_is_generic = !pallet_entry.generics.params.is_empty();

Expand All @@ -56,7 +57,6 @@ pub fn expand_outer_config(
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
#[serde(crate = "__genesis_config_serde_import__")]
#[allow(non_snake_case)]
pub struct GenesisConfig {
#fields
}
Expand Down Expand Up @@ -85,7 +85,7 @@ fn expand_config_types(
config: &Ident,
part_is_generic: bool,
) -> TokenStream {
let path = &decl.pallet;
let path = &decl.path;

match (decl.instance.as_ref(), part_is_generic) {
(Some(inst), true) => quote!{
Expand All @@ -109,7 +109,7 @@ fn expand_config_build_storage_call(
decl: &Pallet,
field_name: &Ident,
) -> TokenStream {
let path = &decl.pallet;
let path = &decl.path;
let instance = if let Some(inst) = decl.instance.as_ref() {
quote!(#path::#inst)
} else {
Expand Down
58 changes: 24 additions & 34 deletions frame/support/procedural/src/construct_runtime/expand/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// See the License for the specific language governing permissions and
// limitations under the License

use crate::construct_runtime::{Pallet, parse::PalletPath};
use crate::construct_runtime::Pallet;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::{Generics, Ident};
Expand All @@ -27,11 +27,10 @@ pub fn expand_outer_event(
) -> syn::Result<TokenStream> {
let mut event_variants = TokenStream::new();
let mut event_conversions = TokenStream::new();
let mut events_metadata = TokenStream::new();

for pallet_decl in pallet_decls {
if let Some(pallet_entry) = pallet_decl.find_part("Event") {
let path = &pallet_decl.pallet;
let path = &pallet_decl.path;
let index = pallet_decl.index;
let instance = pallet_decl.instance.as_ref();
let generics = &pallet_entry.generics;
Expand All @@ -53,9 +52,8 @@ pub fn expand_outer_event(
(None, false) => quote!(#path::Event),
};

event_variants.extend(expand_event_variant(runtime, path, index, instance, generics));
event_conversions.extend(expand_event_conversion(scrate, path, instance, &pallet_event));
events_metadata.extend(expand_event_metadata(scrate, path, &pallet_event));
event_variants.extend(expand_event_variant(runtime, pallet_decl, index, instance, generics));
event_conversions.extend(expand_event_conversion(scrate, pallet_decl, instance, &pallet_event));
}
}

Expand All @@ -77,43 +75,45 @@ pub fn expand_outer_event(

fn expand_event_variant(
runtime: &Ident,
path: &PalletPath,
pallet: &Pallet,
index: u8,
instance: Option<&Ident>,
generics: &Generics,
) -> TokenStream {
let path = &pallet.path;
let pallet_name = &pallet.name;
let part_is_generic = !generics.params.is_empty();
let mod_name = &path.mod_name();

match (instance, part_is_generic) {
(Some(inst), true) => {
let variant = format_ident!("{}_{}", mod_name, inst);
quote!(#[codec(index = #index)] #variant(#path::Event<#runtime, #path::#inst>),)
}
(Some(inst), false) => {
let variant = format_ident!("{}_{}", mod_name, inst);
quote!(#[codec(index = #index)] #variant(#path::Event<#path::#inst>),)
match instance {
Some(inst) => {
let variant = format_ident!("{}_{}", pallet_name, inst);
KiChjang marked this conversation as resolved.
Show resolved Hide resolved
KiChjang marked this conversation as resolved.
Show resolved Hide resolved

if part_is_generic {
quote!(#[codec(index = #index)] #variant(#path::Event<#runtime, #path::#inst>),)
} else {
quote!(#[codec(index = #index)] #variant(#path::Event<#path::#inst>),)
}
}
(None, true) => {
quote!(#[codec(index = #index)] #mod_name(#path::Event<#runtime>),)
None if part_is_generic => {
quote!(#[codec(index = #index)] #pallet_name(#path::Event<#runtime>),)
}
(None, false) => {
quote!(#[codec(index = #index)] #mod_name(#path::Event),)
None => {
quote!(#[codec(index = #index)] #pallet_name(#path::Event),)
}
}
}

fn expand_event_conversion(
scrate: &TokenStream,
path: &PalletPath,
pallet: &Pallet,
instance: Option<&Ident>,
pallet_event: &TokenStream,
) -> TokenStream {
let mod_name = path.mod_name();
let pallet_name = &pallet.name;
let variant = if let Some(inst) = instance {
format_ident!("{}_{}", mod_name, inst)
format_ident!("{}_{}", pallet_name, inst)
KiChjang marked this conversation as resolved.
Show resolved Hide resolved
} else {
mod_name
pallet_name.clone()
};

quote!{
Expand All @@ -134,13 +134,3 @@ fn expand_event_conversion(
}
}
}

fn expand_event_metadata(
scrate: &TokenStream,
path: &PalletPath,
pallet_event: &TokenStream,
) -> TokenStream {
let mod_name = path.mod_name();

quote!{(stringify!(#mod_name), #scrate::event::FnEncode(#pallet_event::metadata)),}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ fn expand_pallet_metadata_storage(
) -> TokenStream {
if filtered_names.contains(&"Storage") {
let instance = decl.instance.as_ref().into_iter();
let path = &decl.pallet;
let path = &decl.path;

quote!{
Some(#scrate::metadata::DecodeDifferent::Encode(
Expand All @@ -114,7 +114,7 @@ fn expand_pallet_metadata_calls(
) -> TokenStream {
if filtered_names.contains(&"Call") {
let instance = decl.instance.as_ref().into_iter();
let path = &decl.pallet;
let path = &decl.path;

quote!{
Some(#scrate::metadata::DecodeDifferent::Encode(
Expand All @@ -135,7 +135,7 @@ fn expand_pallet_metadata_events(
decl: &Pallet,
) -> TokenStream {
if filtered_names.contains(&"Event") {
let path = &decl.pallet;
let path = &decl.path;
let part_is_generic =
!decl.find_part("Event").expect("Event part exists; qed").generics.params.is_empty();
let pallet_event = match (decl.instance.as_ref(), part_is_generic) {
Expand All @@ -160,7 +160,7 @@ fn expand_pallet_metadata_constants(
scrate: &TokenStream,
decl: &Pallet,
) -> TokenStream {
let path = &decl.pallet;
let path = &decl.path;
let instance = decl.instance.as_ref().into_iter();

quote!{
Expand All @@ -177,7 +177,7 @@ fn expand_pallet_metadata_errors(
scrate: &TokenStream,
decl: &Pallet,
) -> TokenStream {
let path = &decl.pallet;
let path = &decl.path;
let instance = decl.instance.as_ref().into_iter();

quote!{
Expand Down
62 changes: 28 additions & 34 deletions frame/support/procedural/src/construct_runtime/expand/origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
// See the License for the specific language governing permissions and
// limitations under the License

use crate::construct_runtime::{parse::PalletPath, Pallet, SYSTEM_PALLET_NAME};
use crate::construct_runtime::{Pallet, SYSTEM_PALLET_NAME};
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use quote::quote;
use syn::{token, Ident, Generics};

pub fn expand_outer_origin(
Expand All @@ -39,7 +39,6 @@ pub fn expand_outer_origin(

for pallet_decl in pallets.iter().filter(|pallet| pallet.name != SYSTEM_PALLET_NAME) {
if let Some(pallet_entry) = pallet_decl.find_part("Origin") {
let path = &pallet_decl.pallet;
let instance = pallet_decl.instance.as_ref();
let index = pallet_decl.index;
let generics = &pallet_entry.generics;
Expand All @@ -54,15 +53,15 @@ pub fn expand_outer_origin(
}

caller_variants.extend(
expand_origin_caller_variant(runtime, path, index, instance, generics),
expand_origin_caller_variant(runtime, pallet_decl, index, instance, generics),
);
pallet_conversions.extend(
expand_origin_pallet_conversions(scrate, runtime, path, instance, generics),
expand_origin_pallet_conversions(scrate, runtime, pallet_decl, instance, generics),
);
}
}

let system_path = &system_pallet.pallet;
let system_path = &system_pallet.path;
let system_index = system_pallet.index;

Ok(quote!{
Expand Down Expand Up @@ -251,58 +250,53 @@ pub fn expand_outer_origin(

fn expand_origin_caller_variant(
runtime: &Ident,
path: &PalletPath,
pallet: &Pallet,
index: u8,
instance: Option<&Ident>,
generics: &Generics,
) -> TokenStream {
let part_is_generic = !generics.params.is_empty();
let mod_name = &path.mod_name();
let variant_name = &pallet.name;
let path = &pallet.path;

match (instance, part_is_generic) {
(Some(inst), true) => {
let variant = format_ident!("{}_{}", mod_name, inst);
quote!(#[codec(index = #index)] #variant(#path::Origin<#runtime, #path::#inst>),)
match instance {
Some(inst) if part_is_generic => {
quote!(#[codec(index = #index)] #variant_name(#path::Origin<#runtime, #path::#inst>),)
}
(Some(inst), false) => {
let variant = format_ident!("{}_{}", mod_name, inst);
quote!(#[codec(index = #index)] #variant(#path::Origin<#path::#inst>),)
Some(inst) => {
quote!(#[codec(index = #index)] #variant_name(#path::Origin<#path::#inst>),)
}
(None, true) => {
quote!(#[codec(index = #index)] #mod_name(#path::Origin<#runtime>),)
None if part_is_generic => {
quote!(#[codec(index = #index)] #variant_name(#path::Origin<#runtime>),)
}
(None, false) => {
quote!(#[codec(index = #index)] #mod_name(#path::Origin),)
None => {
quote!(#[codec(index = #index)] #variant_name(#path::Origin),)
}
}
}

fn expand_origin_pallet_conversions(
scrate: &TokenStream,
runtime: &Ident,
path: &PalletPath,
pallet: &Pallet,
instance: Option<&Ident>,
generics: &Generics,
) -> TokenStream {
let mod_name = path.mod_name();
let variant = if let Some(inst) = instance {
format_ident!("{}_{}", mod_name, inst)
} else {
mod_name
};
let path = &pallet.path;
let variant_name = &pallet.name;

let part_is_generic = !generics.params.is_empty();
let pallet_origin = match (instance, part_is_generic) {
(Some(inst), true) => quote!(#path::Origin<#runtime, #path::#inst>),
(Some(inst), false) => quote!(#path::Origin<#path::#inst>),
(None, true) => quote!(#path::Origin<#runtime>),
(None, false) => quote!(#path::Origin),
let pallet_origin = match instance {
Some(inst) if part_is_generic => quote!(#path::Origin<#runtime, #path::#inst>),
Some(inst) => quote!(#path::Origin<#path::#inst>),
None if part_is_generic => quote!(#path::Origin<#runtime>),
None => quote!(#path::Origin),
};

quote!{
impl From<#pallet_origin> for OriginCaller {
fn from(x: #pallet_origin) -> Self {
OriginCaller::#variant(x)
OriginCaller::#variant_name(x)
}
}

Expand All @@ -317,7 +311,7 @@ fn expand_origin_pallet_conversions(
impl From<Origin> for #scrate::sp_std::result::Result<#pallet_origin, Origin> {
/// NOTE: converting to pallet origin loses the origin filter information.
fn from(val: Origin) -> Self {
if let OriginCaller::#variant(l) = val.caller {
if let OriginCaller::#variant_name(l) = val.caller {
Ok(l)
} else {
Err(val)
Expand All @@ -330,7 +324,7 @@ fn expand_origin_pallet_conversions(
fn try_from(
x: OriginCaller,
) -> #scrate::sp_std::result::Result<#pallet_origin, OriginCaller> {
if let OriginCaller::#variant(l) = x {
if let OriginCaller::#variant_name(l) = x {
Ok(l)
} else {
Err(x)
Expand Down
8 changes: 4 additions & 4 deletions frame/support/procedural/src/construct_runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const SYSTEM_PALLET_NAME: &str = "System";
pub struct Pallet {
pub name: Ident,
pub index: u8,
pub pallet: PalletPath,
pub path: PalletPath,
pub instance: Option<Ident>,
pub pallet_parts: Vec<PalletPart>,
}
Expand Down Expand Up @@ -101,7 +101,7 @@ fn complete_pallets(decl: impl Iterator<Item = PalletDeclaration>) -> syn::Resul
Ok(Pallet {
name: pallet.name,
index: final_index,
pallet: pallet.pallet,
path: pallet.path,
instance: pallet.instance,
pallet_parts: pallet.pallet_parts,
})
Expand Down Expand Up @@ -252,7 +252,7 @@ fn decl_outer_dispatch<'a>(
let pallets_tokens = pallet_declarations
.filter(|pallet_declaration| pallet_declaration.exists_part("Call"))
.map(|pallet_declaration| {
let pallet = &pallet_declaration.pallet.inner.segments.last().unwrap();
let pallet = &pallet_declaration.path.inner.segments.last().unwrap();
let name = &pallet_declaration.name;
let index = pallet_declaration.index;
quote!(#[codec(index = #index)] #pallet::#name)
Expand All @@ -275,7 +275,7 @@ fn decl_all_pallets<'a>(
let mut names = Vec::new();
for pallet_declaration in pallet_declarations {
let type_name = &pallet_declaration.name;
let pallet = &pallet_declaration.pallet;
let pallet = &pallet_declaration.path;
let mut generics = vec![quote!(#runtime)];
generics.extend(
pallet_declaration
Expand Down
Loading