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

[Ruby] Provide Ruby wrapper classes for ServiceDescriptor and MethodDescriptor #14891

Open
dazuma opened this issue Nov 29, 2023 · 3 comments
Open

Comments

@dazuma
Copy link
Contributor

dazuma commented Nov 29, 2023

What language does this apply to?

Ruby

Describe the problem you are trying to solve.

The Ruby runtime library provides several Ruby classes Google::Protobuf::Descriptor, Google::Protobuf::FileDescriptor, etc. that wrap the underlying descriptor objects, along with Google::Protobuf::DescriptorPool that wraps the defpool and provides a way to look up needed descriptors. These objects are sometimes critical for understanding protobuf descriptors, especially when implementing protoc plugins.

However, two objects have no corresponding Ruby class: the service and method descriptors. Hence, it is not possible to access service descriptor information from Ruby.

For example, this makes it extremely difficult to get option extensions for services and methods, as the computation of that information happens on the descriptors. This makes it difficult to write client library generators in Ruby. #1198 was recently closed as complete to address this, but it covered only the descriptor types already existing; there is still no way to get custom option info for services and methods.

Describe the solution you'd like

We propose creating the classes Google::Protobuf::ServiceDescriptor and Google::Protobuf::MethodDescriptor, implemented similarly to the existing descriptor wrapper classes (i.e. a C implementation in ruby/ext/google/protobuf_c/defs.[ch], FFI implementations in ruby/lib/google/protobuf/ffi/, and JRuby implementations in src/main/java/com/google/protobuf/jruby/.) Appropriate accessor methods on those classes would need to be determined and designed.

Describe alternatives you've considered

We currently do not seem to have a good alternative for getting runtime descriptor information in Ruby. I currently maintain a client library generator, and we had to resort to directly decoding the encoded form of ServiceOptions and MethodOptions data, and hard-coding field numbers, in order to get the information we need.

Additional context

Other languages do seem to have these classes. Example from Python

@dazuma dazuma added the untriaged auto added to all issues by default when created. label Nov 29, 2023
@anandolee anandolee added ruby and removed untriaged auto added to all issues by default when created. labels Nov 29, 2023
@haberman
Copy link
Member

haberman commented Jan 7, 2024

I think this makes sense to do. We would welcome a contribution. An easy start would be:

  • Extend DescriptorPool#lookup to allow looking up services/methods by fully-qualified name
  • Implement ServiceDescriptor with #name, #methods, and #options
  • Implement MethodDescriptor with #name and #options

@haberman
Copy link
Member

I'm unassigning myself as I have no immediate plans to work on this, but we would welcome a contribution.

@KJTsanaktsidis
Copy link
Contributor

I had a need for this (for generating RPC stubs from the proto descriptors, without having to write a protoc plugin), so I implemented I think what you had in mind in #15817

copybara-service bot pushed a commit that referenced this issue Mar 22, 2024
This PR implements lookup of service descriptor and method descriptor objects in Ruby as described in issue #14891.

It contains three implementations - one for the CRuby extension API, one for JRuby, and one for FFI.

With this patch,

* `DescriptorPool#lookup('fully.qualified.service.name')` works and returns a `Google::Protobuf::ServiceDescriptor` object
* You can call `#options` on that to get the service options
* You can call `#methods` on that to get the services' methods as `Google::Protobuf::MethodDescriptor` objects,
* You can call `MethodDescriptor#options` to get method options
* You can also get the streaming flags & input/output types of the method with `#input_type`, `#output_type`, `#client_streaming`, and `#server_streaming`.

In order to make the FFI implementation work, I had to mark some more methods in the UPB header as exported - I guess that's something which will have to be done on the UPB side, like this protocolbuffers/upb@01fed1c

CC @dazuma & @haberman from the original issue, and @JasonLunn (since you work on protobuf it seems - small world!)

I apologies for the large volume of copy-pasta'd code from the existing descriptor class implementations into the new ones - I felt this was probably better than designing new abstractions to reduce it off the bat though; this feels like it "fits in" with the existing implementation.

Closes #15817

COPYBARA_INTEGRATE_REVIEW=#15817 from KJTsanaktsidis:ktsanaktsidis/add_service_method_descriptors 54d7218
PiperOrigin-RevId: 618221016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants