-
Notifications
You must be signed in to change notification settings - Fork 257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Codegen for custom values in metadata #1117
Conversation
…-custom-values-codegen
…-custom-values-codegen
codegen/src/api/custom_values.rs
Outdated
(!custom_value_fns.is_empty()).then(|| { | ||
quote!( | ||
pub fn custom() -> custom::CustomValuesApi { | ||
custom::CustomValuesApi | ||
} | ||
|
||
pub mod custom{ | ||
use super::#types_mod_ident; | ||
|
||
pub struct CustomValuesApi; | ||
|
||
impl CustomValuesApi { | ||
#( #custom_value_fns )* | ||
} | ||
} | ||
|
||
|
||
) | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I guess either this or the below could be removed
(!custom_value_fns.is_empty()).then(|| { | |
quote!( | |
pub fn custom() -> custom::CustomValuesApi { | |
custom::CustomValuesApi | |
} | |
pub mod custom{ | |
use super::#types_mod_ident; | |
pub struct CustomValuesApi; | |
impl CustomValuesApi { | |
#( #custom_value_fns )* | |
} | |
} | |
) | |
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with Alex that if custom_value_fns.is_empty()
is easier for me to understand here but I always struggle to understand then/then_some on bool's
codegen/src/api/custom_values.rs
Outdated
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some extra spaces here
codegen/src/api/custom_values.rs
Outdated
None | ||
} else { | ||
Some(quote!( | ||
pub fn custom() -> custom::CustomValuesApi { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this implementation, I wonder if the metadata doesn't expose any custom values, then the users will not have the fn custom()
exposed.
Wouldn't it be more consistent to have the pub fn custom()
either way? Similar to how we expose pub fn tx() -> TransactionApi
, then it would be an implementation detail that the CustomValuesApi
doesn't expose anything.
Another benefit of mimicking the API would be that we don't have to collect the let custom_value_fns: Vec<_>
values, saving an allocation here and there, since quote!
works for iterators as well.
I can think of a couple of ways to test this but I'm not sure which is cleanest offhand:
So yeah probably it's worth doing 1 to generate some metadata and use that for test/doc example purposes. When Substrate has custom metadata appearing in it for whatever reason, we can move to using that instead if we prefer. |
Looking good overall; just a bunch of small things to fix up re consistency or whatever, and I raised #1134 to do a little followup piece of work to spit out custom metadata for more thorough testing :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome stuff, good job @tadeohepperle!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
This PR adds static addresses for custom values, accessible via codegen.
So for example, if there is a custom value with name "Foo" referring to a type
struct Foo { n: u8, b: bool }
and the attached value is scale encoded (e.g. [5,0] for Foo), then we get back a decoded Foo fromcustom_value_client.at(&static_address)?
:After some back and forth, I decided not to add a test for this, because it would involve creating some artificial metadata scale or json file, using it in the subxt macro and then using the interface with it. I think if people are moving to using custom types or they appear in polkadot in the future we should add an integration test for it.