-
Notifications
You must be signed in to change notification settings - Fork 177
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
feat: alias attribute for proc macros #442
Conversation
My preference would be to let the macro build the unsubscribe method and any aliases itself, so that it'd look like this: #[subscription(
name = "subscribeStorage",
item = Vec<usize>,
alias = "foo",
)]
fn subscribe_storage(&self, keys: Option<Vec<u8>>); The unsubscribe method name would be |
I like it it's much more readable and I like it but it won't work with this example without doing some clever parsing (strip the name space identifier) The whole point of this PR is make it possible to port everything in substrate to work with polkadot UI In addition for you example to work the current: if the name starts with |
Maybe we can solve the backwards compatibility using aliases? It would look something like this: #[rpc(server, namespace = "state")]
pub trait StateApi<Hash> {
…
#[subscription(
name = "subscribeRuntimeVersion",
item = RuntimeVersion,
alias = "chain_subscribeRuntimeVersion",
unsubscribe_alias = "state_unsubscribeRuntimeVersion, chain_unsubscribeRuntimeVersion"
)]
fn subscribe_runtime_version(&self, keys: Option<Vec<u8>>);
} (here the aliases would be used verbatim and not get the namespace prepended) EDIT: I guess the point I'm trying to make is: it's ok if the current RPCs look ugly but we should strive to make new code look as neat as we can. |
Yes, I totally agree in the long-term to make it "not-ugly" but short term I want it compatible with polkadot UI (so not really jsonrpc related) but I think your last example should quite straight-forward to implement. |
@dvdplm @jsdw @maciejhirsz This is now ready for review |
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.
lgtm, let's see how this works in substrate.
@@ -79,12 +79,12 @@ impl RpcDescription { | |||
|
|||
let mut registered = HashSet::new(); |
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 tracks method names right? Maybe name it registered_methods
or seen_method_names
? And that brings the question, do we need to check aliases too? I guess it's not a big problem if users define duplicate aliases but might be good to keep things tight.
EDIT: nvm, you do check aliases too.
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.
Exactly, the code is little !DRY but that's mainly because of that inner closure check_names
, should be "ok" here
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 think it makes sense to have this check otherwise it would panic with duplicate names or something, also good to have if you unintentionally register the same alias for two different methods.
Co-authored-by: David <dvdplm@gmail.com>
let method = existing_method.trim(); | ||
let mut new_method = String::from("unsubscribe"); | ||
if method.starts_with("subscribe") { | ||
new_method.extend(method.chars().skip(9)); |
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.
Just for clarification, you are just appending anything after "subscribe" to the "unsubscribe" new method, and this method name originates from the subscriptions attributes, which can technically be any string?
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.
in a nutshell if the existing string is named subscribeTarik
the unsubscribe method is named unsubscribeTarik
hence the skip(9)
otherwise only unsubcribe
is pushed at the start of the subscribe method
for example someSub
-> unsubscribesomeSub
It can be any string but if a duplicate string is used then it will end-up in compile-time error
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.
Cool, makes sense!
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.
LGTM
Closing #440
The new format was suggested by @dvdplm:
The unsubscribe method naming is handled by jsonrpsee and is
unsubscribeRuntimeVersion
.If the subscribe method starts with
subscribe
we just pushun
to the string otherwiseunsubscribe
Then users can provide their own aliases if they don't like default unsubscribe name.
After some discussion we agreed that aliases should be kept outside the namespace (if one exist) because
aliases
are often mistake and might be a PITA to refactor the entire trait just to manually add the namespace to each method to work properly.