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

Commit

Permalink
handle doc on type_value (paritytech#10132)
Browse files Browse the repository at this point in the history
  • Loading branch information
gui1117 authored Nov 4, 2021
1 parent 4f3b9c3 commit e40710a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
3 changes: 3 additions & 0 deletions frame/support/procedural/src/pallet/expand/type_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ pub fn expand_type_values(def: &mut Def) -> proc_macro2::TokenStream {
(Default::default(), Default::default())
};

let docs = &type_value.docs;

expand.extend(quote::quote_spanned!(type_value.attr_span =>
#( #[doc = #docs] )*
#vis struct #ident<#struct_use_gen>(core::marker::PhantomData<((), #struct_use_gen)>);
impl<#struct_impl_gen> #frame_support::traits::Get<#type_> for #ident<#struct_use_gen>
#where_clause
Expand Down
18 changes: 15 additions & 3 deletions frame/support/procedural/src/pallet/parse/type_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub struct TypeValueDef {
pub where_clause: Option<syn::WhereClause>,
/// The span of the pallet::type_value attribute.
pub attr_span: proc_macro2::Span,
/// Docs on the item.
pub docs: Vec<syn::Lit>,
}

impl TypeValueDef {
Expand All @@ -53,9 +55,18 @@ impl TypeValueDef {
return Err(syn::Error::new(item.span(), msg))
};

if !item.attrs.is_empty() {
let msg = "Invalid pallet::type_value, unexpected attribute";
return Err(syn::Error::new(item.attrs[0].span(), msg))
let mut docs = vec![];
for attr in &item.attrs {
if let Ok(syn::Meta::NameValue(meta)) = attr.parse_meta() {
if meta.path.get_ident().map_or(false, |ident| ident == "doc") {
docs.push(meta.lit);
continue
}
}

let msg = "Invalid pallet::type_value, unexpected attribute, only doc attribute are \
allowed";
return Err(syn::Error::new(attr.span(), msg))
}

if let Some(span) = item
Expand Down Expand Up @@ -106,6 +117,7 @@ impl TypeValueDef {
type_,
instances,
where_clause,
docs,
})
}
}
1 change: 1 addition & 0 deletions frame/support/test/tests/pallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ pub mod pallet {
#[pallet::storage_prefix = "Value2"]
pub type RenamedValue<T> = StorageValue<Value = u64>;

/// Test some doc
#[pallet::type_value]
pub fn MyDefault<T: Config>() -> u16
where
Expand Down

0 comments on commit e40710a

Please sign in to comment.