Protocol Buffers - Google's data interchange format (grpc依赖) https://developers.google.com/protocol-buffers/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.4 KiB

Implement service & method descriptor lookup in Ruby (#15817) This PR implements lookup of service descriptor and method descriptor objects in Ruby as described in issue https://github.com/protocolbuffers/protobuf/issues/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 https://github.com/protocolbuffers/upb/commit/01fed1cc1ba255bf22b49393ba054b8d270e6ba3 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=https://github.com/protocolbuffers/protobuf/pull/15817 from KJTsanaktsidis:ktsanaktsidis/add_service_method_descriptors 54d72184310d7d9bfc06d4a58956e3871170c43d PiperOrigin-RevId: 618221016
9 months ago
#!/usr/bin/ruby
require 'google/protobuf'
require 'service_test_pb'
require 'test/unit'
require 'json'
class ServiceTest < Test::Unit::TestCase
def setup
@test_service = Google::Protobuf::DescriptorPool.generated_pool.lookup('service_test_protos.TestService')
@deprecated_service = Google::Protobuf::DescriptorPool.generated_pool.lookup('service_test_protos.DeprecatedService')
@test_service_methods = @test_service.to_h { |method| [method.name, method] }
end
def test_lookup_service_descriptor
assert_kind_of Google::Protobuf::ServiceDescriptor, @test_service
assert_equal 'service_test_protos.TestService', @test_service.name
end
def test_file_descriptor
assert_kind_of Google::Protobuf::FileDescriptor, @test_service.file_descriptor
assert_equal 'service_test.proto', @test_service.file_descriptor.name
end
def test_method_iteration
@test_service.each { |method| assert_kind_of Google::Protobuf::MethodDescriptor, method }
assert_equal %w(UnaryOne UnaryTwo), @test_service.map { |method| method.name }.first(2)
end
def test_service_options
assert @deprecated_service.options.deprecated
refute @test_service.options.deprecated
end
def test_service_options_extensions
extension_field = Google::Protobuf::DescriptorPool.generated_pool.lookup('service_test_protos.test_options')
assert_equal 8325, extension_field.get(@test_service.options).int_option_value
end
def test_method_options
assert_equal :IDEMPOTENT, @test_service_methods['IdempotentMethod'].options.idempotency_level
assert_equal :NO_SIDE_EFFECTS, @test_service_methods['PureMethod'].options.idempotency_level
end
def test_method_input_type
unary_request_type = Google::Protobuf::DescriptorPool.generated_pool.lookup('service_test_protos.UnaryRequestType')
assert_same unary_request_type, @test_service_methods['UnaryOne'].input_type
end
def test_method_output_type
unary_response_type = Google::Protobuf::DescriptorPool.generated_pool.lookup('service_test_protos.UnaryResponseType')
assert_same unary_response_type, @test_service_methods['UnaryOne'].output_type
end
def test_method_streaming_flags
refute @test_service_methods['UnaryOne'].client_streaming
refute @test_service_methods['UnaryOne'].server_streaming
assert @test_service_methods['StreamingMethod'].client_streaming
assert @test_service_methods['StreamingMethod'].server_streaming
end
end