Client call with named params#541
Conversation
|
It looks like @willemolding hasn't signed our Contributor License Agreement, yet.
You can read and sign our full Contributor License Agreement at the following URL: https://cla.parity.io Once you've signed, please reply to this thread with Many thanks, Parity Technologies CLA Bot |
|
[clabot:check] |
|
It looks like @willemolding signed our Contributor License Agreement. 👍 Many thanks, Parity Technologies CLA Bot |
|
|
||
| #[rpc(client)] | ||
| pub trait Rpc { | ||
| #[rpc(name = "call_with_named", named_params)] |
There was a problem hiding this comment.
Would it make sense to specify named_params in the trait level attribute instead? On the one hand it's good to have the fine grained control - but in practice wouldn't all methods be either named or positional?
There was a problem hiding this comment.
We already have raw_params on the method level. I think this is a good idea to have it here, but an additional attribute on the trait level would be cool tool, especially in the future if we implement server side as well.
|
|
||
| #[rpc(client)] | ||
| pub trait Rpc { | ||
| #[rpc(name = "call_with_named", named_params)] |
There was a problem hiding this comment.
We already have raw_params on the method level. I think this is a good idea to have it here, but an additional attribute on the trait level would be cool tool, especially in the future if we implement server side as well.
|
I like the idea of having it at the trait level to set the default, which can then be overridden at the method level. That way we have the fined grained control as well. I'm picturing something like #[rpc(client, params = "named")] // sets the default to named params
pub trait Rpc {
#[rpc(name = "call_with_named")] // uses the trait default
fn call_with_named() -> {
...
#[rpc(name = "call_positional", params = "positional")] // overrides trait default
fn call_positional() -> {
} |
|
That looks really nice, are you willing to change how I guess we would need to keep |
…mpatible params type
|
Ok getting pretty close. There are now three options for params: This can be set at the trait level (which then becomes the default for all trait methods without a I'm done for today but I would appreciate suggestions for any more tests this might need to be sure it is solid |
tomusdrw
left a comment
There was a problem hiding this comment.
Looks good, few minor things.
| let raw_params = | ||
| get_meta_list(meta).map_or(false, |ml| has_meta_word(RAW_PARAMS_META_WORD, ml)); | ||
| let params_style = match raw_params { | ||
| true => Ok(Some(ParamStyle::Raw)), |
There was a problem hiding this comment.
Can we print a deprecation warning here?
There was a problem hiding this comment.
Unfortunately I can't figure out a way to emit a warning without using the proc_macro::Diagnostic feature which is unstable
Not sure if we have any other ideas for this or if it is ok to just add a depreciation note to the docs but not the macro
There was a problem hiding this comment.
I think in the past we simply used to println the warning. @ascjones do you recall?
There was a problem hiding this comment.
Yes we did use a simple println IIRC
Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com>
|
Thanks for the review @tomusdrw. The only thing lacking is a deprecation warning for using the now-deprecated |
tomusdrw
left a comment
There was a problem hiding this comment.
Looks good to me! I think deprecation warning (via println if works) would be nice, but is not critical.
| let raw_params = | ||
| get_meta_list(meta).map_or(false, |ml| has_meta_word(RAW_PARAMS_META_WORD, ml)); | ||
| let params_style = match raw_params { | ||
| true => Ok(Some(ParamStyle::Raw)), |
There was a problem hiding this comment.
I think in the past we simply used to println the warning. @ascjones do you recall?
| let raw_params = | ||
| get_meta_list(meta).map_or(false, |ml| has_meta_word(RAW_PARAMS_META_WORD, ml)); | ||
| let params_style = match raw_params { | ||
| true => Ok(Some(ParamStyle::Raw)), |
There was a problem hiding this comment.
Yes we did use a simple println IIRC
|
It looks like using I left a placeholder warning so if/when the proc-macro diagnostics becomes stable it will be simple to add it in. |
|
Apparently it seems to be upstream issue, could you fix the |
Hmm this doesn't seem to have fixed it. It sure is a pain not being able to see the CI output |
Yeah, sorry about that. Seems that |
|
Thanks! |

Add named_params option to RPC derive macro
Closes #540
While the server generated by the derive macro doesn't support calling functions with named params some other systems do (some exclusively). It would still be nice to use the clean derive macro syntax to generate a client in this case.
This PR adds a flag
named_paramsto the rpc method attribute. Example:In this case the json-rpc call object produced by calling the generated method on a client will have a params object that takes the key names from the function parameter names.
{ "jsonrpc": "2.0", "params": {"a": 4, "b": 2}, ... }As the server doesn't support this it is important that this cannot be used when deriving server RPC methods. The rpc derive macro will throw a compile time error if the
named_paramsswitch is used in any case other than#[rpc(client)]. See (derive/tests/ui/attr-named-params-on-server.rs)