-
-
Notifications
You must be signed in to change notification settings - Fork 880
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
Conversation
Damn nice, had no idea something like that existed. |
) | ||
} else { | ||
(posts_query.build().list()?, comments_query.build().list()?) | ||
}; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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. |
The commentreport and postreport order is coming back incorrectly. |
my_person_id: PersonId, | ||
#[builder(!default)] |
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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.
There was a problem hiding this 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()?) | ||
}; |
There was a problem hiding this comment.
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 | ||
} | ||
} |
There was a problem hiding this comment.
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.
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