protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

[Ruby] Provide Ruby wrapper classes for ServiceDescriptor and MethodDescriptor

Open dazuma opened this issue 2 years ago • 3 comments

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 avatar Nov 29 '23 20:11 dazuma

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 avatar Jan 07 '24 20:01 haberman

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

haberman avatar Jan 30 '24 22:01 haberman

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 https://github.com/protocolbuffers/protobuf/pull/15817

KJTsanaktsidis avatar Feb 13 '24 10:02 KJTsanaktsidis

Hey folks. I see that #15817 was closed/merged, but has it been released into a stable version yet? Is it slated for v27.x?

cocoahero avatar May 15 '24 19:05 cocoahero

This change indeed made the cut for 27.x, and is announced in the release notes for 27.0-rc1: https://github.com/protocolbuffers/protobuf/releases/tag/v27.0-rc1

haberman avatar May 15 '24 21:05 haberman