Module: GrpcKit::GRPC::Dsl

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#marshal_class_method=(value) ⇒ Object (writeonly)

Parameters:

  • value (Symbol)

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

def marshal_class_method=(value)
  @marshal_class_method = value
end

#service_name=(value) ⇒ Object (writeonly)

Parameters:

  • value (String)

12
13
14
# File 'lib/grpc_kit/grpc/dsl.rb', line 12

def service_name=(value)
  @service_name = value
end

#unmarshal_class_method=(value) ⇒ Object (writeonly)

Parameters:

  • value (Symbol)

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

def unmarshal_class_method=(value)
  @unmarshal_class_method = value
end

Instance Method Details

#inherited(subclass) ⇒ Object


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

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:


29
30
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
# File 'lib/grpc_kit/grpc/dsl.rb', line 29

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:


108
109
110
# File 'lib/grpc_kit/grpc/dsl.rb', line 108

def rpc_descs
  @rpc_descs ||= {}
end

#rpc_stub_classGrpcKit::Client

Returns:


64
65
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
# File 'lib/grpc_kit/grpc/dsl.rb', line 64

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|
      rpc_descs_.each do |method_name, rpc_desc|
        @rpcs[method_name] = rpc_desc.build_client(interceptors: interceptors)
      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


59
60
61
# File 'lib/grpc_kit/grpc/dsl.rb', line 59

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