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

Feature: add rename field attribute #29

Merged
merged 3 commits into from
Nov 26, 2024
Merged

Feature: add rename field attribute #29

merged 3 commits into from
Nov 26, 2024

Conversation

MrMarble
Copy link
Contributor

Hi, Thanks for this great crate!

I added the rename field attribute so you can rename one field to the value you want, like with serde(rename).


My use case for this is that I use structs for API queries using serde rename to serialize into camelCase but keeping snake_case for the struct names.

example

#[derive(Deserialize, Serialize, Clone, Debug, FieldNamesAsArray)]
struct UserView {
	#[serde(rename = "userID")]
	#[field_names_as_array(rename = "userID")]
	pub user_id: u64,
	#[serde(rename = "userName")]
	#[field_names_as_array(rename = "userName")]
	pub user_name: String,
	pub followers: u64,
}

fn get_user_view(user_id: u64) -> Result<UserView> {
	let fields = UserView::FIELD_NAMES_AS_ARRAY;

	let view = client.user.get(&fields)?;
	// ^ This will generate a query with the
	// following format https://example.org/user/id?include_fields=userID,userName,followers

	Ok(view)
}

Copy link
Owner

@jofas jofas left a comment

Choose a reason for hiding this comment

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

Thank you very much for your PR!

MrMarble and others added 2 commits November 26, 2024 13:33
Co-authored-by: Jonas Fassbender <jonas@fassbender.dev>
Co-authored-by: Jonas Fassbender <jonas@fassbender.dev>
@MrMarble
Copy link
Contributor Author

I committed the changes directly on Github if that's okay, I can squash if needed

@jofas
Copy link
Owner

jofas commented Nov 26, 2024

No need, thank you. Looks like the codecov action is broken at the moment, sorry about that. Do you need the 0.3.1 release with the rename attribute fast (i.e. today)? If not I'd have a look at the open issues and the actions before making the release, this repo hasn't seen much love recently. I'd get the release done later this week then.

@MrMarble
Copy link
Contributor Author

No need, thank you. Looks like the codecov action is broken at the moment, sorry about that. Do you need the 0.3.1 release with the rename attribute fast (i.e. today)? If not I'd have a look at the open issues and the actions before making the release, this repo hasn't seen much love recently. I'd get the release done later this week then.

No rush, I'm using my git fork until the release is made so you can take your time.
Thanks!

@jofas jofas merged commit 8da0ad8 into jofas:master Nov 26, 2024
1 check failed
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.

2 participants