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)

Sets the attribute marshal_class_method

Parameters:

  • value

    the value to set the attribute marshal_class_method to.



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

def marshal_class_method=(value)
  @marshal_class_method = value
end

#service_name=(value) ⇒ Object (writeonly)

Sets the attribute service_name

Parameters:

  • value

    the value to set the attribute service_name to.



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

def service_name=(value)
  @service_name = value
end

#unmarshal_class_method=(value) ⇒ Object (writeonly)

Sets the attribute unmarshal_class_method

Parameters:

  • value

    the value to set the attribute unmarshal_class_method to.



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

def unmarshal_class_method=(value)
  @unmarshal_class_method = value
end

Instance Method Details

#inherited(subclass) ⇒ Object



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

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

#rpc(name, marshal, unmarshal) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/grpc_kit/grpc/dsl.rb', line 18

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

  define_method(rpc_desc.ruby_style_name) do |_, _|
    raise GrpcKit::Errors::Unimplemented, "Method not found: #{name}"
  end
end

#rpc_descsObject



93
94
95
# File 'lib/grpc_kit/grpc/dsl.rb', line 93

def rpc_descs
  @rpc_descs ||= {}
end

#rpc_stub_classObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
# File 'lib/grpc_kit/grpc/dsl.rb', line 50

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) ⇒ Object



46
47
48
# File 'lib/grpc_kit/grpc/dsl.rb', line 46

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