module Yrpc module Controllers class Request attr_reader :message # @var [GRPC::ActiveCall] active_call attr_reader :active_call # @var [Symbol] method_key attr_reader :method_key # @var [Grpc::Controllers::Request::Type] type attr_reader :type # @var [Class] service attr_reader :service delegate :metadata, to: :active_call delegate :messages, :client_streamer?, :server_streamer?, :bidi_streamer?, :request_response?, to: :type ## # Abstract representation of a gRPC request type # class Type delegate :client_streamer?, :server_streamer?, :bidi_streamer?, :request_response?, to: :@rpc_desc ## # Initialize a new request type object # # @param [GRPC::RpcDesc] rpc_desc The RPC descriptor for the request type # def initialize(rpc_desc) @rpc_desc = rpc_desc end end ## #struct GRPC::RpcDesc def initialize(method_key:, service:, rpc_desc:, active_call:, message:) @method_key = method_key @service = service @active_call = active_call @message = message @rpc_desc = rpc_desc @type = Type.new(rpc_desc) end def service_key @service.name.underscore.tr('/', '.').gsub('.service', '') end def method_missing(method_id, *arguments, &block) method_message = *arguments.join if (method_id.to_s =~ /^raise_[\w]+/) == 0 error_type = method_id.to_s.split('raise_')[1].upcase! code = "Yrpc::Response::Code::#{error_type}".constantize message = method_message raise Yrpc::Error::ResponseError.new(code, message) else super end end def response_class @rpc_desc.output end def request_class @rpc_desc.input end # grpc 中的 service.method def method_name "#{service_key}.#{method_key}" end # 返回这个request的所有mesage 要根据stremer类型来判断 def messages if client_streamer? message.call { |msg| yield msg } elsif bidi_streamer? message else [message] end end end end end