Module: GRPC::GenericService::ClassMethods

Defined in:
lib/grpc_typechecker/grpc_ext.rb

Instance Method Summary collapse

Instance Method Details

#inherited(subclass) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/grpc_typechecker/grpc_ext.rb', line 20

def inherited(subclass)
  super if defined? super

  mod = Module.new do
    singleton_class.class_eval do
      # Some server interceptors try to obtain the service name by `method.owner.service_name` or `method.owner.to_s`.
      # despite `method` is overwritten by the method in this module.
      # We thus redirect class method calls of `service_name` and `to_s` for compatibility.
      define_method :service_name do
        subclass.service_name
      end

      define_method :to_s do
        subclass.to_s
      end
    end
  end

  subclass.class_eval do
    prepend mod
    @_prepended_module_for_type_check_ = mod
  end
end

#method_added(method) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/grpc_typechecker/grpc_ext.rb', line 44

def method_added(method)
  super if defined? super

  rpc_method = rpc_descs[method.to_s.camelize.to_sym]
  return unless rpc_method && @_prepended_module_for_type_check_

  # Overwrite the gRPC method
  @_prepended_module_for_type_check_.class_eval do
    define_method method do |*args, **kwargs|
      response = super(*args, **kwargs)
      # Raise Internal if the response of gRPC method is ill-typed
      unless response.is_a?(rpc_method[:output])
        raise GRPC::Internal, "the response of #{method} is expected to be an instance of #{rpc_method[:output]}, but the response is an instance of #{response.class}"
      end
      response
    end
  end
end