-
Notifications
You must be signed in to change notification settings - Fork 220
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
service implementation stub generation #170
Conversation
Whoops. I seem to have misunderstood the test framework. My test doesn't work. I'll get on fixing that. |
Hmm, I don't know how my test could be incorporated into the existing test framework. My test doesn't have a message or service called |
For me, this looks good, but I will wait for others to weigh in. |
- merged __service_name__ and __rpc_methods__ into __mapping__ - __mapping__ is now part of generated class instead of ABC - duplicate code for managing the grpclib stream moved from generated class into ABC
also removed left-over from trying to work around the test framework
I removed I also cut down on some of the duplicate code in the generated class. The code for handling the grpclib stream is now part of the ABC. The original post has been updated. It appears that the CI is broken. Correct me if I'm wrong. |
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.
Thanks for picking this up, it'll be good to have a solution.
I still need to properly look at the code generation part when I get some time, but I found a points worth looking at for now.
added test framework workaround back in
fcd3f9d
to
54af074
Compare
I'm as surprised as you 🤷 |
I'm not sure what the issue with the test framework is. Specifically, I don't see why |
I think maybe you need to register the service in |
Thank you, that seems to have fixed it. |
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.
Almost good to go, I just spotted a couple more minor issue.
src/betterproto/plugin/models.py
Outdated
@@ -279,6 +279,10 @@ def proto_name(self) -> str: | |||
def py_name(self) -> str: | |||
return pythonize_class_name(self.proto_name) | |||
|
|||
@property | |||
def py_name_as_field(self) -> str: |
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 like this isn't used either actually?
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.
Not anymore since 5bbe19a. Nice catch. Good thing you're looking over this. I completely forgot.
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.
Ship it!
This implements #19. An example compiler output can be found here.
When compiling protobuf definitions, a service implementation stub is generated along the existing client stub:
The user is then supposed to create their own class that inherits from and overrides the methods of this generated implementation stub:
Note that the generated implementation stub includes a wrapper for each defined service (
__rpc_example_unary_unary
). This wrapper and the utility functions in the new abstract base classServiceImplementation
take care of translatinggrpclib.server.Stream
'srecv_message
andsend_message
from and toyield
andreturn
statements and parameters. I tried to match this to how the generated client stub is used.Finally, the generated implementation stub can be used as a
grpclib
service handler because of the__mapping__
function. An example of this is included as a new test case (example_service
). I'm not sure if it's good practice to bind ports during tests but I didn't know how else to link the server and client. I also think it's not ideal to test the betterproto server against the betterproto client. Maybe it makes sense to include gRPC inoutput_reference
so that we can create a reference client.EDIT: Updated gist and snippets to reflect changes made in 207233b.