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

chore(build): Refactor server trait gen to dedup some code #2143

Closed
wants to merge 2 commits into from

Conversation

yotamofek
Copy link
Contributor

Motivation

A lot of the code for building the server trait is duplicated.

Solution

Refactor to reduce said duplication.

Comment on lines -260 to -264
let method = match (
method.client_streaming(),
method.server_streaming(),
generate_default_stubs,
) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd like to keep the current styles for the conditions about whether it is client streaming or server streaming or not. This refactoring is well designed to avoid reimplementing logic, but at the same time, I feel that it has become more difficult to grasp the outline of the processing from the perspective of code generation. Taking the trade-off into consideration, how about introducing body_or_semicolon to the conditional branch of generate_default_stubs to reduce the number of branches?

Copy link
Contributor Author

@yotamofek yotamofek Jan 13, 2025

Choose a reason for hiding this comment

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

Yeah I agree, refactored a bit more to look more like the older code. There's a bit of duplication here but I think it's a reasonable trade-off because it makes the branching easier to follow. What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

What I tried to say is that using switch with client streaming and/or server streaming, and then in the each branch using body_or_semicolon variable to handle the default stub option.

And, I would like to write partial_sig and return_ty inline directly without making them variables.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure I understand what you mean. I don't really see a point in doing something like match (method.client_streaming(), method.server_streaming()) since those two settings don't impact one another.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If proper separation seems difficult, it seems likely that refactoring aimed at introducing further complexity will also be difficult.

@yotamofek yotamofek force-pushed the refactor-server-trait branch from 687a9aa to 4050502 Compare January 13, 2025 17:41
@yotamofek
Copy link
Contributor Author

@tottoto BTW, I think the whole server-trait generation logic could go in its own module and be split up into functions. I didn't want to make such an intrusive rewrite without seeing if the crate's maintainers are onboard with the idea, but while we're already cleaning this up - I can give it a go and see if it turns out well. If you think that's a good idea 😊

@tottoto
Copy link
Collaborator

tottoto commented Jan 14, 2025

@tottoto BTW, I think the whole server-trait generation logic could go in its own module and be split up into functions. I didn't want to make such an intrusive rewrite without seeing if the crate's maintainers are onboard with the idea, but while we're already cleaning this up - I can give it a go and see if it turns out well. If you think that's a good idea 😊

If a specific explanation of the proposal is provided, it might help understanding.

@yotamofek
Copy link
Contributor Author

yotamofek commented Jan 14, 2025

If a specific explanation of the proposal is provided, it might help understanding.

I don't haha, just thought I'd start by moving everything into a separate module and start splitting it up into smaller functions. Don't have a specific proposal in mind. I can take a crack at it at some point.

Though to be honest, not sure how much effort I'd wanna put into this, my main goal was to get #2115 in, that would be really helpful for my project @ work, this cleanup was meant to make that feature easier to develop.

@tottoto
Copy link
Collaborator

tottoto commented Jan 15, 2025

Though to be honest, not sure how much effort I'd wanna put into this, my main goal was to get #2115 in, that would be really helpful for my project @ work, this cleanup was meant to make that feature easier to develop.

Even if such a refactoring were to be merged, there is no guarantee that #2115 would be merged. That's a different discussion point.

@yotamofek
Copy link
Contributor Author

Though to be honest, not sure how much effort I'd wanna put into this, my main goal was to get #2115 in, that would be really helpful for my project @ work, this cleanup was meant to make that feature easier to develop.

Even if such a refactoring were to be merged, there is no guarantee that #2115 would be merged. That's a different discussion point.

Yeah, I know, what I'm saying is I don't know how much effort I want to put into this PR because it won't help #2115 (or something similar) get merged.

@tottoto
Copy link
Collaborator

tottoto commented Jan 15, 2025

Yeah, I know, what I'm saying is I don't know how much effort I want to put into this PR because it won't help #2115 (or something similar) get merged.

This pull request is an extraction of a part from #2115 that seems complex and independently discussable and applicable, so it is essentially a part of it in the sense that it is a precursor to #2115. We had the option of reviewing it directly in #2115, but the inclusion of a different perspective, that of adding functionality, would make it more complicated, and since this part itself would be independently useful if successful, proposed splitting it into a separate it as this pull request for discussion.

As a result of the experiments and discussions here, it seems that refactoring is not that easy, so it will be difficult to apply #2115 while maintaining maintainability with the current design.

@yotamofek
Copy link
Contributor Author

yotamofek commented Jan 15, 2025

Refactoring is quite easy, agreeing on what constitutes "better" in matters of style and taste is futile :)

Feel free to take this PR and modify it in any way that suits you. I agree it's a good idea to review and discuss it separately. But seeing as you're not too inclined on adding the feature I suggested in #2115 (though I do think people other than me will find it beneficial), I don't have the motivation to spend more time on this, especially since it seems to me like the changes you're suggesting would be easier to make than describe.

@tottoto
Copy link
Collaborator

tottoto commented Jan 15, 2025

Refactoring is quite easy, agreeing on what constitutes "better" in matters of style and taste is futile :)

It is important to consider not only adding features but also future maintenance, so it is important to consider what kind of refactoring is appropriate. It is not a mere matter of style.

Feel free to take this PR and modify it in any way that suits you. I agree it's a good idea to review and discuss it separately. But seeing as you're not too inclined on adding the feature I suggested in #2115 (though I do think people other than me will #2115 (comment)), I don't have the motivation to spend more time on this, especially since it seems to me like the changes you're suggesting would be easier to make than describe.

Since this is your suggestion and you are not going to proceed it will be closed. Next time, be careful not to submit a pull request that you are not motivated to pursue yourself. The community has limited resources and no one has the resources to put effort into it.

@tottoto tottoto closed this Jan 15, 2025
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