Sha256: b0bda3c934a7c83d5705de7a9cc85236961cb52256913b204a699961df6f7f1f

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

require 'grpc_kit/grpc/stream'

module GrpcKit
  class RpcDesc
    def initialize(name:, input:, output:, marshal_method:, unmarshal_method:)
      @name = name
      @input = input
      @output = output
      @marshal_method = marshal_method
      @unmarshal_method = unmarshal_method
    end

    def encode(val)
      @output.send(@marshal_method, val)
    end

    def decode(val)
      @input.send(@unmarshal_method, val)
    end

    def encode2(val)
      @input.send(@marshal_method, val)
    end

    def decode2(val)
      @output.send(@unmarshal_method, val)
    end

    def invoke(rpc, val)
      args = decode(val)
      ret = rpc.send(to_underscore(@name), args, nil) # nil is GRPC::Call object
      encode(ret)
    end

    def ruby_style_name
      @ruby_style_name ||= to_underscore(@name).to_sym
    end

    def path(service_name)
      "/#{service_name}/#{@name}".to_sym
    end

    def request_response?
      !@input.is_a?(GrpcKit::GRPC::Stream) && !@output.is_a?(GrpcKit::GRPC::Stream)
    end

    def client_streamer?
      @input.is_a?(GrpcKit::GRPC::Stream) && !@output.is_a?(GrpcKit::GRPC::Stream)
    end

    def server_streamer?
      !@input.is_a?(GrpcKit::GRPC::Stream) && @output.is_a?(GrpcKit::GRPC::Stream)
    end

    def bidi_streamer?
      @input.is_a?(GrpcKit::GRPC::Stream) && @output.is_a?(GrpcKit::GRPC::Stream)
    end

    private

    def to_underscore(val)
      val
        .to_s
        .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
        .gsub(/([a-z\d])([A-Z])/, '\1_\2')
        .tr('-', '_')
        .downcase
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grpc_kit-0.1.0 lib/grpc_kit/rpc_desc.rb