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

Pallet macro: support pallet::extra_constants renaming #8826

Closed
shaunxw opened this issue May 16, 2021 · 10 comments
Closed

Pallet macro: support pallet::extra_constants renaming #8826

shaunxw opened this issue May 16, 2021 · 10 comments
Labels
Z1-easy Can be fixed primarily by duplicating and adapting code by an intermediate coder Z6-mentor An easy task where a mentor is available. Please indicate in the issue who the mentor could be.

Comments

@shaunxw
Copy link
Contributor

shaunxw commented May 16, 2021

The syntax for extra constants is as following, where the constants as function name should be snake cases according to Rust naming convention:

#[pallet::extra_constants]
impl<T: Config> Pallet<T> where $optional_where_clause {
	/// $some_doc
	$vis fn $fn_name() -> $some_return_type {
		...
	}
	...
}

But pallet constants are typically camel cased, as defined in old decl_module and new pallet::constants. pallet::extra_constants should provide a way to rename the constants.

@stale
Copy link

stale bot commented Jul 7, 2021

Hey, is anyone still working on this? Due to the inactivity this issue has been automatically marked as stale. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the A5-stale Pull request did not receive any updates in a long time. No review needed at this stage. Close it. label Jul 7, 2021
@emostov
Copy link
Contributor

emostov commented Jul 22, 2021

This is still useful

@shawntabrizi shawntabrizi added Z1-easy Can be fixed primarily by duplicating and adapting code by an intermediate coder Z6-mentor An easy task where a mentor is available. Please indicate in the issue who the mentor could be. and removed A5-stale Pull request did not receive any updates in a long time. No review needed at this stage. Close it. labels Jul 23, 2021
@ericjmiller
Copy link
Contributor

I'll work on this. Might have some questions though.

@ericjmiller
Copy link
Contributor

ericjmiller commented Jul 30, 2021

Questions:

  1. You want constants to remain camel cased and extra_constants to change to snake_case only?

  2. Why does casing matter between the extra_constants and constants? Is it so the idents match in

 pub fn expand_constants(def: &mut Def) -> proc_macro2::TokenStream

within frame/support/procedural/src/pallet/expand/constants.rs?

  1. What's the relationship between constants and extra_constants? I couldn't find a lot of info on extra_constants.

  2. When you say that "extra_constants should provide a way to rename the constants" - do you mean that the expand_constants function should find similar idents with other casings and rename them during expansion to be the same as the expand_constant?

Thanks for any help you can provide.

@shawntabrizi
Copy link
Member

cc @thiolliere @KiChjang

@gui1117
Copy link
Contributor

gui1117 commented Aug 2, 2021

  1. You want constants to remain camel cased and extra_constants to change to snake_case only?

Until recently there were only camel case constant, any cases were accepted but the convention was to use camel case.

Maybe we should provide a way to rename the constant independently from the function name like:

#[pallet::extra_constant_name(Foo)]
fn foo() -> u32 {..}

Or maybe we can automatically convert to camel case in the metadata.

  1. Why does casing matter between the extra_constants and constants? Is it so the idents match in

Both declares some constant to be part of the pallet metadata https://substrate.dev/rustdocs/latest/frame_metadata/struct.ModuleMetadata.html#structfield
It is 2 way to declare the same stuff.
But not all constant can be declared using the 1st way pallet::constant this is why pallet::extra_constant exist:

  • pallet::constant allow to put in the constants metadata any associated type that implement Get<$type> for the trait Config.
  • pallet::extra_constant allow to put in the constants metadata any value, by declaring a function returning the given value.
  1. What's the relationship between constants and extra_constants? I couldn't find a lot of info on extra_constants.

extra_constant information is https://substrate.dev/rustdocs/latest/frame_support/attr.pallet.html#extra-constants-palletextra_constants-optional
It is only an additional way to declare some constant.

  1. When you say that "extra_constants should provide a way to rename the constants" - do you mean that the expand_constants function should find similar idents with other casings and rename them during expansion to be the same as the expand_constant?

I think by providing a way to rename the constant he meant have some additional syntax which allows to specify the name of the constant. like:

#[pallet::extra_constant]
impl<T: Config> Pallet<T> {
  #[pallet::constant_name(Foo)]
  fn foo() -> u32 {..}
}

@ericjmiller
Copy link
Contributor

After thinking about this, I like the idea of an inert attribute, as @thiolliere's last example demonstrates. This way, extra_constant names are explicit and more configurable. Also, I think it will be more flexible for future changes.

Plus, I don't like convention when you can be explicit. So if a user wants to break away from convention for some reason, that should probably be allowed. And it should be optional, so that if no inert attr is passed, the function name is taken.

Let me know if you agree/disagree. I'll put out a PR in the meantime...

@gui1117
Copy link
Contributor

gui1117 commented Aug 3, 2021

yes I agree, I also think we can make this attribute optional so that we don't break existing pallets

@ericjmiller
Copy link
Contributor

Added failing tests in draft above. I'll be adding logic to extra_constants.rs and helper.rs, such that when pallet:: Attributes are discovered, a new ExtraConstantDef is created with ident being the Attribute token rather than the method ident.

This will, of course, be optional.

@ericjmiller
Copy link
Contributor

I've updated the draft PR and could use some feedback.

I've passed the failed tests, but had to comment the code that explicitly initializes ExtraConstantDef with the new ident. It's very odd.

Most of the commented code should work in a final solution to this, but I think perhaps something weird is going on in the tests.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Z1-easy Can be fixed primarily by duplicating and adapting code by an intermediate coder Z6-mentor An easy task where a mentor is available. Please indicate in the issue who the mentor could be.
Projects
None yet
Development

No branches or pull requests

6 participants