Class: Protod::Interpreter::Rpc

Inherits:
Object
  • Object
show all
Defined in:
lib/protod/interpreter/rpc.rb

Class Method Summary collapse

Class Method Details

.setup!Object



6
7
8
9
10
11
12
13
14
15
16
17
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
45
46
47
48
49
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/protod/interpreter/rpc.rb', line 6

def self.setup!
  Interpreter.register_for('Protod::Rpc::Request::Base', force: false, ignore: true) do
    def proto_ident
      "Request"
    end

    def proto_message
      m = Protod.rbs_method_type_for(const.ruby_ident)

      fields = [
        if const.ruby_ident.singleton
          nil
        else
          Protod::Proto::Field.build_from(const.ruby_ident.const_name, ident: const.ruby_ident.const_name)
        end,
        *m.type.required_positionals.map do |arg|
          raise ArgumentError, "Unsupported non-named argument : #{const.ruby_ident}" unless arg.name

          Protod::Proto::Field.build_from_rbs(arg.type, on: const.ruby_ident, ident: arg.name)
        end,
        *m.type.optional_positionals.map do |arg|
          raise ArgumentError, "Unsupported non-named argument : #{const.ruby_ident}" unless arg.name

          Protod::Proto::Field.build_from_rbs(arg.type, on: const.ruby_ident, ident: arg.name, required: false, optional: true)
        end,
        m.type.rest_positionals&.then do |arg|
          raise ArgumentError, "Unsupported non-named argument : #{const.ruby_ident}" unless arg.name

          Protod::Proto::Field.build_from_rbs(arg.type, on: const.ruby_ident, ident: arg.name, as_rest: true, required: false, repeated: true)
        end,
        *m.type.required_keywords.map do |name, arg|
          Protod::Proto::Field.build_from_rbs(arg.type, on: const.ruby_ident, ident: name, as_keyword: true)
        end,
        *m.type.optional_keywords.map do |name, arg|
          Protod::Proto::Field.build_from_rbs(arg.type, on: const.ruby_ident, ident: name, as_keyword: true, required: false, optional: true)
        end,
        m.type.rest_keywords&.then do |arg|
          # Not supporting for now because it's complicated and I can't understand the specification of ruby and rbs about this completely
          raise ArgumentError, "Unsupported rest keywords argument : #{const.ruby_ident}"

          raise ArgumentError, "Unsupported non-named argument : #{const.ruby_ident}" unless arg.name

          Protod::Proto::Field.build_from_rbs(arg.type, on: const.ruby_ident, ident: arg.name, as_keyword: true, as_rest: true, required: false, optional: true)
        end
      ].compact

      Protod::Proto::Message.new(ident: proto_ident, fields: fields)
    end

    def to_rb_from(pb)
      args = []
      kwargs = {}
      receiver_id = nil

      proto_message.fields.each.with_index do |f, i|
        next if Protod::Proto.omits_field?(pb, f.ident) && f.required.!

        f_pb = pb.public_send(f.ident)

        receiver_id = f_pb.protod__object_id if i == 0 && f_pb.respond_to?(:protod__object_id)

        arg = if f.repeated
                f_pb&.map { f.interpreter.to_rb_from(_1) }
              else
                f.interpreter.to_rb_from(f_pb)
              end

        if f.as_keyword
          kwargs[f.ident.to_sym] = arg
        elsif f.as_rest
          args = [*args, *arg]
        else
          args.push(arg)
        end
      end

      receiver = if const.ruby_ident.singleton
                   const.ruby_ident.const_name.constantize
                 else
                   args.shift
                 end

      Protod::Rpc::Handler::RequestPacket.new(receiver_id: receiver_id, receiver: receiver, args: args, kwargs: kwargs)
    end
  end

  Interpreter.register_for('Protod::Rpc::Response::Base', force: false, ignore: true) do
    def proto_ident
      "Response"
    end

    def proto_message
      m = Protod.rbs_method_type_for(const.ruby_ident)

      fields = if m.type.return_type
                 [Protod::Proto::Field.build_from_rbs(m.type.return_type, on: const.ruby_ident, ident: 'value')]
               else
                 []
               end

      Protod::Proto::Message.new(ident: proto_ident, fields: fields)
    end

    def to_pb_from(rb)
      return unless rb

      f = proto_message.find('value', by: :ident, as: 'Protod::Proto::Field')

      return unless f

      pb_maker = ->(v) do
        f.interpreter.to_pb_from(v).tap do
          _1.protod__object_id = v.object_id.to_s if _1.respond_to?(:protod__object_id)
        end
      end

      pb = f.repeated ? rb.map { pb_maker.call(_1) } : pb_maker.call(rb)

      pb_const.new(value: pb)
    end
  end

  Interpreter.register_for('Protod::Rpc::Request::Receiver::Base', force: false, ignore: true) do
    def proto_ident
      "Request"
    end

    def proto_message
      f = Protod::Proto::Oneof.new(ident: Protod::Rpc::Request::Receiver::ONEOF_NAME)

      [
        *proto_fields,
        *const.ruby_ident.const_name.constantize.ancestors.drop(1).filter_map do |c|
          r = Protod::Rpc::Request.find_by(c.name) if c.name
          Interpreter.find_by(r) if r
        end.flat_map(&:proto_fields)
      ].each { f.find_or_push(_1, by: :ident, into: :fields) }

      Protod::Proto::Message.new(ident: proto_ident, fields: [f])
    end

    def to_rb_from(pb)
      procedure = pb.public_send(Protod::Rpc::Request::Receiver::ONEOF_NAME)

      raise Protod::Rpc::Handler::InvalidArgument, "Not set procedure" unless procedure

      f = proto_message
            .find(Protod::Rpc::Request::Receiver::ONEOF_NAME, by: :ident, as: 'Protod::Proto::Oneof')
            .find(procedure, by: :ident, as: 'Protod::Proto::Field')

      raise Protod::Rpc::Handler::InvalidArgument, "Not found acceptable procedure : #{procedure}" unless f

      f.interpreter.to_rb_from(pb.public_send(procedure)).tap { _1.procedure = procedure }
    end

    def proto_fields
      const.procedures.map do |ruby_ident|
        Protod::Proto::Field.build_from(Protod::Rpc::Request.find_by(ruby_ident), ident: ruby_ident.method_name)
      end
    end
  end

  Interpreter.register_for('Protod::Rpc::Response::Receiver::Base', force: false, ignore: true) do
    def proto_ident
      "Response"
    end

    def proto_message
      f = Protod::Proto::Oneof.new(ident: Protod::Rpc::Response::Receiver::ONEOF_NAME)

      [
        *proto_fields,
        *const.ruby_ident.const_name.constantize.ancestors.drop(1).filter_map do |c|
          r = Protod::Rpc::Response.find_by(c.name) if c.name
          Interpreter.find_by(r) if r
        end.flat_map(&:proto_fields)
      ].each { f.find_or_push(_1, by: :ident, into: :fields) }

      Protod::Proto::Message.new(ident: proto_ident, fields: [f])
    end

    def to_pb_from(packet)
      f = proto_message
            .find(Protod::Rpc::Response::Receiver::ONEOF_NAME, by: :ident, as: 'Protod::Proto::Oneof')
            .find(packet.procedure, by: :ident, as: 'Protod::Proto::Field')

      pb = f.interpreter.to_pb_from(packet.object)

      pb_const.new(packet.procedure.to_sym => pb)
    end

    def proto_fields
      const.procedures.map do |ruby_ident|
        Protod::Proto::Field.build_from(Protod::Rpc::Response.find_by(ruby_ident), ident: ruby_ident.method_name)
      end
    end
  end
end