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

Use typed-builder crate for queries #2379

Merged
merged 4 commits into from
Aug 4, 2022
Merged

Use typed-builder crate for queries #2379

merged 4 commits into from
Aug 4, 2022

Conversation

Nutomic
Copy link
Member

@Nutomic Nutomic commented Jul 29, 2022

Only trying this out for now. If this looks good, i will also implement it for PostQuery, CommentQuery etc, after changes to those files are merged. Should shorten the code significantly.

https://crates.io/crates/typed-builder

@dessalines
Copy link
Member

Damn nice, had no idea something like that existed.

)
} else {
(posts_query.build().list()?, comments_query.build().list()?)
};
Copy link
Member Author

Choose a reason for hiding this comment

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

This is pretty ugly but i dont see a better way, because the builder type changes when a setter is called. So the previous mut solution doesnt work.

Copy link
Member

Choose a reason for hiding this comment

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

You could also do creator_id(None) I spose for those cases, but its not necessary. That's dissapointing, because one of the points of builder pattern is to be able to set things conditionally before you build. Oh well.

Copy link
Member Author

Choose a reason for hiding this comment

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

There is another builder macro which should support is, but disadvantage is that it doesnt get checked at compile time, and build() returns a result. Thats probably why this conditional isnt working.

@Nutomic
Copy link
Member Author

Nutomic commented Aug 4, 2022

Generally whenever the same impl is copy-pasted for many different structs, that code can instead be generated with a derive macro.

The PR is finished, but tests are failing and i cant tell why. There dont seem to be any logic changes in relevant code.

@Nutomic Nutomic marked this pull request as ready for review August 4, 2022 11:25
@Nutomic Nutomic changed the title Use typed-builder crate for PrivateMessageQuery Use typed-builder crate for queries Aug 4, 2022
@dessalines
Copy link
Member

The commentreport and postreport order is coming back incorrectly.

my_person_id: PersonId,
#[builder(!default)]
Copy link
Member

Choose a reason for hiding this comment

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

These are just mandatory fields that probably shouldn't have a default.

community_id: None,
page: None,
limit: None,
unresolved_only: Some(true),
Copy link
Member Author

Choose a reason for hiding this comment

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

Found it, i didnt notice before that this is being initialized here. My change should be equivalent, and tests are passing now.

Copy link
Member

@dessalines dessalines left a comment

Choose a reason for hiding this comment

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

Looks great, thx for finding this. Eliminates a ton of boilerplate builder-pattern required for most languages.

)
} else {
(posts_query.build().list()?, comments_query.build().list()?)
};
Copy link
Member

Choose a reason for hiding this comment

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

You could also do creator_id(None) I spose for those cases, but its not necessary. That's dissapointing, because one of the points of builder pattern is to be able to set things conditionally before you build. Oh well.

fn get_optional(self) -> Option<T> {
self
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Thx for getting rid of these, they're pointless now with this.

@dessalines dessalines merged commit 8a4d9cc into main Aug 4, 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