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

Derive default for api request structs, move type enums #2245

Merged
merged 3 commits into from
May 6, 2022

Conversation

Nutomic
Copy link
Member

@Nutomic Nutomic commented May 3, 2022

No description provided.

@Nutomic Nutomic requested a review from dessalines as a code owner May 3, 2022 20:13
@@ -69,7 +69,7 @@ pub struct CreateCommentLike {
pub auth: Sensitive<String>,
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Default)]
Copy link
Member Author

@Nutomic Nutomic May 3, 2022

Choose a reason for hiding this comment

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

Deriving default is useful for client development, when you just want to specify one or two params, and not put all the None values explicitly.

There are still some structs where derive(Default) would make sense, such as CreateSite. In cases like that, there is a mandatory string value which would automatically be filled with "" if default is used. Still, it would be nice if there was a way to avoid writing out all 14 lines to init that struct. Maybe default would be okay if we ensure that empty strings are rejected.

Copy link
Member

Choose a reason for hiding this comment

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

I'd probably prefer all of them to derive default, and just make sure the string cases are done by convention. API clients should be will likely find those issues during development anyway.

@@ -4,7 +4,7 @@ use std::{
ops::{Deref, DerefMut},
};

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize, Default)]
Copy link
Member Author

@Nutomic Nutomic May 3, 2022

Choose a reason for hiding this comment

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

Necessary to make derive(Default) work for some of the structs, even though it obviously doesnt make so much sense. Hopefully the error messages are clear enough. It could also make sense to move the auth param out of the structs somehow.

Communities,
Users,
Url,
}
Copy link
Member Author

Choose a reason for hiding this comment

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

These enums are very useful for api clients, and should be available without the full feature, so i had to move them.

@Nutomic
Copy link
Member Author

Nutomic commented May 3, 2022

Added another commit to remove stringly typing from the api, and use enums directly.

@Nutomic Nutomic force-pushed the api-derive-default branch 2 times, most recently from 9056e40 to 15a05e5 Compare May 3, 2022 22:48
pub type_: Option<String>,
pub sort: Option<String>,
pub type_: Option<ListingType>,
pub sort: Option<SortType>,
Copy link
Member

Choose a reason for hiding this comment

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

This is an API change, and would require the front end clients to use these enums. I'm not against it, just know that its a breaking change

Copy link
Member

Choose a reason for hiding this comment

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

Also, just to be sure, remove the from_opt_str_to_opt_enum function, that way we can get rid of all of these.

Copy link
Member Author

Choose a reason for hiding this comment

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

No, clients work just as before, because serde parses the strings into the correct enum variant. I already tested locally with lemmy-ui, and different sort/listing options worked without any problems.

Copy link
Member Author

Choose a reason for hiding this comment

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

from_opt_str_to_opt_enum function is already removed.

Copy link
Member

Choose a reason for hiding this comment

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

That's kind of wild, because every language does enums differently, and a lot don't even have the concept of string enums. In typescript you have to manually define them as string enums, not the standard integer ones. Okay then, hopefully it doesn't break clients, but we'll know when we cut the RC's.

Also I would still want to change lemmy-js-client to use the enums rather than strings as they do now. I'll open up an issue for that.

@@ -69,7 +69,7 @@ pub struct CreateCommentLike {
pub auth: Sensitive<String>,
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Default)]
Copy link
Member

Choose a reason for hiding this comment

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

I'd probably prefer all of them to derive default, and just make sure the string cases are done by convention. API clients should be will likely find those issues during development anyway.

@Nutomic Nutomic force-pushed the api-derive-default branch 2 times, most recently from cbf7a58 to b4a3ac5 Compare May 6, 2022 12:57
@Nutomic
Copy link
Member Author

Nutomic commented May 6, 2022

Okay, added clone to all api structs, and default to all request (not response) structs (except for a few which only have a single field).

@dessalines dessalines merged commit 3053e14 into main May 6, 2022
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