Module: GrpcKit::Grpc::Dsl

Defined in:
lib/grpc_kit/grpc/dsl.rb

Overview

Methods under GrpcKit::Grpc::Dsl is available for classes include GRPC::GenericService. See also: GrpcKit::Grpc::GenericService

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#marshal_class_method=(value) ⇒ Object (writeonly)

Parameters:

  • value (Symbol)


17
18
19
# File 'lib/grpc_kit/grpc/dsl.rb', line 17

def marshal_class_method=(value)
  @marshal_class_method = value
end

#service_nameObject

Parameters:

  • value (String)


14
15
16
# File 'lib/grpc_kit/grpc/dsl.rb', line 14

def service_name
  @service_name
end

#unmarshal_class_method=(value) ⇒ Object (writeonly)

Parameters:

  • value (Symbol)


20
21
22
# File 'lib/grpc_kit/grpc/dsl.rb', line 20

def unmarshal_class_method=(value)
  @unmarshal_class_method = value
end

Instance Method Details

#inherited(subclass) ⇒ Object



22
23
24
25
# File 'lib/grpc_kit/grpc/dsl.rb', line 22

def inherited(subclass)
  subclass.rpc_descs.merge!(rpc_descs)
  subclass.service_name = @service_name
end

#rpc(name, marshal, unmarshal) ⇒ void

This method returns an undefined value.

Parameters:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/grpc_kit/grpc/dsl.rb', line 31

def rpc(name, marshal, unmarshal)
  if rpc_descs.key?(name)
    raise "rpc (#{name}) is already defined"
  end

  unless marshal.respond_to?(@marshal_class_method)
    raise "#{marshal} must implement #{marshal}.#{@marshal_class_method}"
  end

  unless unmarshal.respond_to?(@unmarshal_class_method)
    raise "#{unmarshal} must implement #{unmarshal}.#{@unmarshal_class_method}"
  end

  rpc_desc = GrpcKit::RpcDesc.new(
    name: name,
    marshal: marshal,
    unmarshal: unmarshal,
    marshal_method: @marshal_class_method,
    unmarshal_method: @unmarshal_class_method,
    service_name: @service_name,
  )
  rpc_descs[rpc_desc.path] = rpc_desc

  # Should use `*` since each rpc's sigunature is different.
  define_method(rpc_desc.ruby_style_name) do |*|
    raise GrpcKit::Errors::Unimplemented, "Method not found: #{name}"
  end
end

#rpc_descsHash<String,GrpcKit::RpcDesc>

Returns:



110
111
112
# File 'lib/grpc_kit/grpc/dsl.rb', line 110

def rpc_descs
  @rpc_descs ||= {}
end

#rpc_stub_classGrpcKit::Client

Returns:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/grpc_kit/grpc/dsl.rb', line 66

def rpc_stub_class
  rpc_descs_ = {}
  rpc_descs.each_value do |rpc_desc|
    rpc_descs_[rpc_desc.ruby_style_name] = rpc_desc
  end

  Class.new(GrpcKit::Client) do
    def initialize(*, **)
      @rpcs = {}
      super
    end

    define_method(:build_rpcs) do |interceptors, **options|
      rpc_descs_.each do |method_name, rpc_desc|
        @rpcs[method_name] = rpc_desc.build_client(interceptors: interceptors, **options)
      end
    end
    private :build_rpcs

    rpc_descs_.each do |method_name, rpc_desc|
      if rpc_desc.request_response?
        define_method(method_name) do |request, **opts|
          request_response(@rpcs.fetch(method_name), request, **opts)
        end
      elsif rpc_desc.client_streamer?
        define_method(method_name) do |**opts|
          client_streamer(@rpcs.fetch(method_name), **opts)
        end
      elsif rpc_desc.server_streamer?
        define_method(method_name) do |request, **opts|
          server_streamer(@rpcs.fetch(method_name), request, **opts)
        end
      elsif rpc_desc.bidi_streamer?
        define_method(method_name) do |requests, **opts, &blk|
          bidi_streamer(@rpcs.fetch(method_name), requests, **opts, &blk)
        end
      else
        raise "unknown #{rpc_desc}"
      end
    end
  end
end

#stream(cls) ⇒ GrpcKit::Grpc::Stream



61
62
63
# File 'lib/grpc_kit/grpc/dsl.rb', line 61

def stream(cls)
  GrpcKit::Grpc::Stream.new(cls)
end