Skip to content
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

proc-macros: rename parameter names #1365

Merged
merged 18 commits into from
May 15, 2024
Merged

Conversation

deflator-hacker
Copy link
Contributor

Decoupling rust parameter names from (de)serialized json is essential for me and useful in general.

Some usecases include:

  • supporting parameter names that are reserved keywords in rust, such as type (currently impossible)
  • talking to case sensitive RPC servers only accepting non-snake-case parameters (currently impossible without clippy complaining)

Those are both usecases I personally have.

The PR implements this by introducing a new attribute for the rpc macro called argument.

  • documentation has been updated
  • tests have been written

I previously responded to an outdated similar PR here.

Minimal working example:

#[rpc(client, server)]
pub trait MyRpc {
	#[method(name = "foo")]
	async fn foo(
		&self,
		param_a: u8,
		#[argument(rename = "type")]
		r#type: String
	) -> RpcResult<u16>;
}

I divided the changes into multiple commits because they are a bit lengthy.
This is my first open source contribution, I am eager to learn, let me know what can be improved.

@deflator-hacker deflator-hacker requested a review from a team as a code owner May 6, 2024 14:26
@niklasad1
Copy link
Member

niklasad1 commented May 6, 2024

Looks overall good, nice job

I'm not following why you need to rename parameters when those passed as Array (these are omitted by the client)? In such scenarios then variable doesn't correspond to the actual param name, so why is it needed?

The contrary applies to JSON Objects where the key is the same name passed in the call with some glue to convert snake_case to camelCase. Then I follow why these need to be renamed to support reserved rust keywords etc....

I'll take a closer look tomorrow.

@deflator-hacker
Copy link
Contributor Author

I'm not following why you need to rename parameters when those passed as Array (these are omitted by the client)? In such scenarios then variable doesn't correspond to the actual param name, so why is it needed?

You are absolutely right, there is no point (actually no way) to rename parameters in arrays.
I included a check for array params in proc_macros/tests/ui/correct/rename_fn_params.rs just because I wanted to assert that I did not break anything deserialization-related by accident.

deflator-hacker and others added 5 commits May 7, 2024 16:27
Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
- expose potentially renamed name as String insead of Option in RpcFnArg::name()
- make RpcFnArg::rename_to private to migrate to new RpcFnArg::name()
- adapt render_client.rs and render_server.rs to changes
@niklasad1
Copy link
Member

Looks great, I'll can push a fix to unblock the UI tests tomorrow it's probably the latest rustc release that broke them.

- remove unnecessary param_kind: map
- add array params test to prevent accidental breakage through param renaming
alias_vals
.push_str(&format!(r#"alias = "{}""#, heck::ToLowerCamelCase::to_lower_camel_case(name.as_str())));

let serde_rename = quote!(#[serde(rename = #name)]);
Copy link
Member

@niklasad1 niklasad1 May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed because arg_pat below can't be reserved rust names such as type

Copy link
Member

@niklasad1 niklasad1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work looks good

@niklasad1 niklasad1 requested a review from jsdw May 8, 2024 08:54
proc-macros/src/lib.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@jsdw jsdw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just a couple of nits!

deflator-hacker and others added 2 commits May 14, 2024 19:16
@niklasad1 niklasad1 changed the title [RFC] proc-macros: Rename parameter names proc-macros: rename parameter names May 15, 2024
@niklasad1 niklasad1 merged commit d2c27c5 into paritytech:master May 15, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants