Class: RBSProtobuf::Translator::ProtobufGem

Inherits:
Base
  • Object
show all
Defined in:
lib/rbs_protobuf/translator/protobuf_gem.rb

Constant Summary collapse

FIELD_ARRAY =
Name::Class.new(TypeName("::Protobuf::Field::FieldArray"))
FIELD_HASH =
Name::Class.new(TypeName("::Protobuf::Field::FieldHash"))
ENUM =
Name::Class.new(TypeName("::Protobuf::Enum"))
MESSAGE =
Name::Class.new(TypeName("::Protobuf::Message"))
TO_PROTO =
Name::Interface.new(TypeName("_ToProto"))
FIELD_ARRAY_a =
Name::Alias.new(TypeName("::Protobuf::field_array"))
FIELD_HASH_a =
Name::Alias.new(TypeName("::Protobuf::field_hash"))

Constants inherited from Base

Base::FieldDescriptorProto

Instance Attribute Summary collapse

Attributes inherited from Base

#filters, #input, #rbs_concat_level

Instance Method Summary collapse

Methods inherited from Base

#apply_filter, #base_type, #comment_for_path, #factory, #format_rbs, #generate_rbs!, #message_type, #optional_type, #rbs_name, #rbs_suffix, #response

Constructor Details

#initialize(input, filters = [], upcase_enum:, nested_namespace:, extension:, accept_nil_writer:, stderr: STDERR) ⇒ ProtobufGem

Returns a new instance of ProtobufGem.



22
23
24
25
26
27
28
29
# File 'lib/rbs_protobuf/translator/protobuf_gem.rb', line 22

def initialize(input, filters=[], upcase_enum:, nested_namespace:, extension:, accept_nil_writer:, stderr: STDERR)
  super(input, filters)
  @upcase_enum = upcase_enum
  @nested_namespace = nested_namespace
  @extension = extension
  @accept_nil_writer = accept_nil_writer
  @stderr = stderr
end

Instance Attribute Details

#accept_nil_writerObject (readonly)

Returns the value of attribute accept_nil_writer.



20
21
22
# File 'lib/rbs_protobuf/translator/protobuf_gem.rb', line 20

def accept_nil_writer
  @accept_nil_writer
end

#stderrObject (readonly)

Returns the value of attribute stderr.



18
19
20
# File 'lib/rbs_protobuf/translator/protobuf_gem.rb', line 18

def stderr
  @stderr
end

Instance Method Details

#add_field(members, name:, read_type:, write_types:, comment:) ⇒ Object



606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# File 'lib/rbs_protobuf/translator/protobuf_gem.rb', line 606

def add_field(members, name:, read_type:, write_types:, comment:)
  members << RBS::AST::Members::AttrAccessor.new(
    name: name,
    type: read_type,
    comment: comment,
    location: nil,
    annotations: [],
    ivar_name: false,
    kind: :instance
  )

  unless write_types.empty?
    members << RBS::AST::Members::MethodDefinition.new(
      name: :"#{name}=",
      overloads:
        write_types.map do |write_type|
          method_type =
            if (type_param, type = interface_type?(write_type))
              factory.method_type(
                type: factory.function(type).update(
                  required_positionals:[factory.param(type)]
                )
              ).update(type_params: [type_param])
            else
              factory.method_type(
                type: factory.function(write_type).update(
                  required_positionals:[factory.param(write_type)]
                )
              )
            end

          RBS::AST::Members::MethodDefinition::Overload.new(
            method_type: method_type,
            annotations: []
          )
        end,
      annotations: [],
      comment: comment,
      location: nil,
      overloading: true,
      visibility: nil,
      kind: :instance
    )
  end

  members << RBS::AST::Members::MethodDefinition.new(
    name: :"#{name}!",
    overloads: [
      RBS::AST::Members::MethodDefinition::Overload.new(
        method_type: factory.method_type(
          type: factory.function(factory.optional_type(read_type))
        ),
        annotations: []
      )
    ],
    annotations: [],
    comment: nil,
    location: nil,
    overloading: false,
    visibility: nil,
    kind: :instance
  )
end

#enum_name(name) ⇒ Object



670
671
672
673
674
675
676
# File 'lib/rbs_protobuf/translator/protobuf_gem.rb', line 670

def enum_name(name)
  if upcase_enum?
    name.upcase.to_sym
  else
    name.to_sym
  end
end

#enum_type_to_decl(enum_type, prefix:, source_code_info:, path:) ⇒ Object



678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
# File 'lib/rbs_protobuf/translator/protobuf_gem.rb', line 678

def enum_type_to_decl(enum_type, prefix:, source_code_info:, path:)
  enum_name = ActiveSupport::Inflector.upcase_first(enum_type.name)

  RBS::AST::Declarations::Class.new(
    name: RBS::TypeName.new(name: enum_name.to_sym, namespace: prefix),
    super_class: ENUM.super_class,
    type_params: factory.module_type_params(),
    members: [],
    comment: comment_for_path(source_code_info, path, options: enum_type.options),
    location: nil,
    annotations: []
  ).tap do |enum_decl|
    names = enum_type.value.map do |v|
      factory.literal_type(enum_name(v.name))
    end

    strings = enum_type.value.map do |v|
      factory.literal_type(enum_name(v.name).to_s)
    end

    tags = enum_type.value.map do |v|
      factory.literal_type(v.number)
    end.uniq

    enum_decl.members << RBS::AST::Declarations::TypeAlias.new(
      name: factory.type_name("names"),
      type_params: [],
      type: factory.union_type(*names),
      location: nil,
      comment: nil,
      annotations: []
    )

    enum_decl.members << RBS::AST::Declarations::TypeAlias.new(
      name: factory.type_name("strings"),
      type_params: [],
      type: factory.union_type(*strings),
      location: nil,
      comment: nil,
      annotations: []
    )

    enum_decl.members << RBS::AST::Declarations::TypeAlias.new(
      name: factory.type_name("tags"),
      type_params: [],
      type: factory.union_type(*tags),
      location: nil,
      comment: nil,
      annotations: []
    )

    enum_decl.members << RBS::AST::Declarations::TypeAlias.new(
      name: factory.type_name("values"),
      type_params: [],
      type: factory.union_type(
        factory.alias_type("names"),
        factory.alias_type("strings"),
        factory.alias_type("tags")
      ),
      location: nil,
      comment: nil,
      annotations: []
    )

    enum_decl.members << RBS::AST::Members::AttrReader.new(
      name: :name,
      type: factory.alias_type("names"),
      ivar_name: false,
      annotations: [],
      comment: nil,
      location: nil,
      kind: :instance
    )

    enum_decl.members << RBS::AST::Members::AttrReader.new(
      name: :tag,
      type: factory.alias_type("tags"),
      ivar_name: false,
      annotations: [],
      comment: nil,
      location: nil,
      kind: :instance
    )

    enum_type.value.each.with_index do |v, index|
      comment = comment_for_path(source_code_info, path + [2, index], options: v.options)

      enum_decl.members << RBS::AST::Declarations::Constant.new(
        name: factory.type_name(enum_name(v.name).to_s),
        type: factory.instance_type(RBS::TypeName.new(name: enum_name.to_sym, namespace: prefix)),
        comment: comment,
        location: nil
      )
    end

    enum_instance_type = factory.instance_type(RBS::TypeName.new(name: enum_name.to_sym, namespace: RBS::Namespace.empty))
    values_type = factory.alias_type(RBS::TypeName.new(name: :values, namespace: RBS::Namespace.empty))

    enum_decl.members << RBS::AST::Declarations::TypeAlias.new(
      name: TypeName("init"),
      type_params: [],
      type: factory.union_type(enum_instance_type, values_type),
      annotations: [],
      comment: RBS::AST::Comment.new(string: "The type of `#initialize` parameter.", location: nil),
      location: nil
    )

    enum_decl.members << RBS::AST::Declarations::TypeAlias.new(
      name: TypeName("field_array"),
      type_params: [],
      type: FIELD_ARRAY[
        enum_instance_type,
        factory.union_type(enum_instance_type, values_type)
      ],
      annotations: [],
      comment: RBS::AST::Comment.new(string: "The type of `repeated` field.", location: nil),
      location: nil
    )

    enum_decl.members << RBS::AST::Declarations::TypeAlias.new(
      name: TypeName("field_hash"),
      type_params: [RBS::AST::TypeParam.new(name: :KEY, variance: :invariant, upper_bound: nil, location: nil)],
      type: FIELD_HASH[
        factory.type_var(:KEY),
        enum_instance_type,
        factory.union_type(enum_instance_type, values_type)
      ],
      annotations: [],
      comment: RBS::AST::Comment.new(string: "The type of `map` field.", location: nil),
      location: nil
    )

    enum_decl.members << RBS::AST::Declarations::TypeAlias.new(
      name: TypeName("array"),
      type_params: [],
      type: RBS::BuiltinNames::Array.instance_type(factory.union_type(enum_instance_type, values_type)),
      annotations: [],
      comment: nil,
      location: nil
    )

    enum_decl.members << RBS::AST::Declarations::TypeAlias.new(
      name: TypeName("hash"),
      type_params: [RBS::AST::TypeParam.new(name: :KEY, variance: :invariant, upper_bound: nil, location: nil)],
      type: RBS::BuiltinNames::Hash.instance_type(
        factory.type_var(:KEY),
        factory.union_type(enum_instance_type, values_type)
      ),
      annotations: [],
      comment: nil,
      location: nil
    )
  end
end

#extension_to_decl(extension, prefix:, source_code_info:, path:) ⇒ Object



833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
# File 'lib/rbs_protobuf/translator/protobuf_gem.rb', line 833

def extension_to_decl(extension, prefix:, source_code_info:, path:)
  class_name = message_type(extension.extendee).name

  comment = comment_for_path(source_code_info, path, options: extension.options)
  field_name = extension.name.to_sym

  RBS::AST::Declarations::Class.new(
    name: class_name,
    super_class: nil,
    type_params: [],
    location: nil,
    comment: nil,
    members: [],
    annotations: []
  ).tap do |class_decl|
    read_type, write_types, _ = field_type(extension, {})

    add_field(class_decl.members, name: field_name, read_type: read_type, write_types: write_types, comment: comment)

    class_decl.members << RBS::AST::Members::MethodDefinition.new(
      name: :[],
      overloads: [
        RBS::AST::Members::MethodDefinition::Overload.new(
          method_type: factory.method_type(
            type: factory.function(read_type).update(
              required_positionals: [
                factory.param(factory.literal_type(field_name))
              ]
            )
          ),
          annotations: []
        )
      ],
      annotations: [],
      comment: nil,
      location: nil,
      overloading: true,
      visibility: nil,
      kind: :instance
    )

    class_decl.members << RBS::AST::Members::MethodDefinition.new(
      name: :[]=,
      overloads: [read_type, *write_types].map do |write_type|
        method_type =
          if (type_param, type_var = interface_type?(write_type))
            factory.method_type(
              type: factory.function(type_var).update(
                required_positionals: [
                  factory.param(factory.literal_type(field_name)),
                  factory.param(type_var)
                ]
              )
            ).update(type_params: [type_param])
          else
            factory.method_type(
              type: factory.function(write_type).update(
                required_positionals: [
                  factory.param(factory.literal_type(field_name)),
                  factory.param(write_type)
                ]
              )
            )
          end
        RBS::AST::Members::MethodDefinition::Overload.new(
          method_type: method_type,
          annotations: []
        )
      end,
      annotations: [],
      comment: nil,
      location: nil,
      overloading: true,
      visibility: nil,
      kind: :instance
    )
  end
end

#field_type(field, maps) ⇒ Object



482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'lib/rbs_protobuf/translator/protobuf_gem.rb', line 482

def field_type(field, maps)
  # @type var triple: [RBS::Types::t, Array[RBS::Types::t], RBS::Types::t]
  triple =
    case
    when field.type == FieldDescriptorProto::Type::TYPE_MESSAGE
      if maps.key?(field.type_name)
        key_field, value_field = maps[field.type_name]

        key_type_r, _ = field_type(key_field, maps)
        value_type_r, value_write_types = field_type(value_field, maps)

        value_type_r = factory.unwrap_optional(value_type_r)
        value_write_types = value_write_types.map {|type| factory.unwrap_optional(type) }

        case value_field.type
        when FieldDescriptorProto::Type::TYPE_MESSAGE, FieldDescriptorProto::Type::TYPE_ENUM
          value_type_r.is_a?(RBS::Types::ClassInstance) or raise
          [
            message_field_hash_type(value_type_r, key_type_r),
            [message_hash_type(value_type_r, key_type_r)],
            message_hash_type(value_type_r, key_type_r)
          ]
        else
          hash_type = FIELD_HASH[
            key_type_r,
            value_type_r,
            factory.union_type(value_type_r, *value_write_types)
          ]

          [
            FIELD_HASH_a[key_type_r, value_type_r],
            [RBS::BuiltinNames::Hash.instance_type(key_type_r, value_type_r)],
            RBS::BuiltinNames::Hash.instance_type(key_type_r, value_type_r)
          ]
        end
      else
        type = message_type(field.type_name)

        case field.label
        when FieldDescriptorProto::Label::LABEL_OPTIONAL
          [
            factory.optional_type(type),
            [
              factory.optional_type(message_to_proto_type(type))
            ],
            factory.optional_type(message_init_type(type))
          ]
        when FieldDescriptorProto::Label::LABEL_REPEATED
          [
            message_field_array_type(type),
            [
              message_array_type(type)
            ],
            message_array_type(type)
          ]
        else
          [
            type,
            [message_to_proto_type(type)],
            message_init_type(type)
          ]
        end
      end
    when field.type == FieldDescriptorProto::Type::TYPE_ENUM
      type = message_type(field.type_name)
      enum_namespace = type.name.to_namespace
      values = factory.alias_type(RBS::TypeName.new(name: :values, namespace: enum_namespace))

      if field.label == FieldDescriptorProto::Label::LABEL_REPEATED
        [
          message_field_array_type(type),
          [message_array_type(type)],
          message_array_type(type)
        ]
      else
        [
          type,
          [values],
          message_init_type(type)
        ]
      end
    else
      type = base_type(field.type)

      if field.label == FieldDescriptorProto::Label::LABEL_REPEATED
        [
          FIELD_ARRAY_a[type],
          [RBS::BuiltinNames::Array.instance_type(type)],
          RBS::BuiltinNames::Array.instance_type(type)
        ]
      else
        [type, [], type]
      end
    end

  if accept_nil_writer
    read_type, write_types, init_type = triple
    [
      read_type,
      ([factory.optional_type(read_type)] + write_types.map {|t| factory.optional_type(t) }).uniq,
      factory.optional_type(init_type)
    ]
  else
    triple
  end
end

#ignore_extension?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/rbs_protobuf/translator/protobuf_gem.rb', line 31

def ignore_extension?
  !@extension
end

#interface_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'lib/rbs_protobuf/translator/protobuf_gem.rb', line 589

def interface_type?(type)
  case
  when type.is_a?(RBS::Types::Interface)
    [
      RBS::AST::TypeParam.new(name: :M, upper_bound: type, variance: :invariant, location: nil),
      factory.type_var(:M)
    ]
  when type.is_a?(RBS::Types::Optional)
    if (type = type.type).is_a?(RBS::Types::Interface)
      [
        RBS::AST::TypeParam.new(name: :M, upper_bound: type, variance: :invariant, location: nil),
        factory.optional_type(factory.type_var(:M))
      ]
    end
  end
end

#message_array_type(type) ⇒ Object



458
459
460
461
462
463
464
# File 'lib/rbs_protobuf/translator/protobuf_gem.rb', line 458

def message_array_type(type)
  RBS::Types::Alias.new(
    name: RBS::TypeName.new(name: :array, namespace: type.name.to_namespace),
    args: [],
    location: nil
  )
end

#message_field_array_type(type) ⇒ Object



450
451
452
453
454
455
456
# File 'lib/rbs_protobuf/translator/protobuf_gem.rb', line 450

def message_field_array_type(type)
  RBS::Types::Alias.new(
    name: RBS::TypeName.new(name: :field_array, namespace: type.name.to_namespace),
    args: [],
    location: nil
  )
end

#message_field_hash_type(type, key) ⇒ Object



474
475
476
477
478
479
480
# File 'lib/rbs_protobuf/translator/protobuf_gem.rb', line 474

def message_field_hash_type(type, key)
  RBS::Types::Alias.new(
    name: RBS::TypeName.new(name: :field_hash, namespace: type.name.to_namespace),
    args: [key],
    location: nil
  )
end

#message_hash_type(type, key) ⇒ Object



466
467
468
469
470
471
472
# File 'lib/rbs_protobuf/translator/protobuf_gem.rb', line 466

def message_hash_type(type, key)
  RBS::Types::Alias.new(
    name: RBS::TypeName.new(name: :hash, namespace: type.name.to_namespace),
    args: [key],
    location: nil
  )
end

#message_init_type(type) ⇒ Object



442
443
444
445
446
447
448
# File 'lib/rbs_protobuf/translator/protobuf_gem.rb', line 442

def message_init_type(type)
  RBS::Types::Alias.new(
    name: RBS::TypeName.new(name: :init, namespace: type.name.to_namespace),
    args: [],
    location: nil
  )
end

#message_to_decl(message, prefix:, message_path:, source_code_info:, path:) ⇒ Object



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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/rbs_protobuf/translator/protobuf_gem.rb', line 133

def message_to_decl(message, prefix:, message_path:, source_code_info:, path:)
  class_name = ActiveSupport::Inflector.upcase_first(message.name)

  RBS::AST::Declarations::Class.new(
    name: RBS::TypeName.new(name: class_name.to_sym, namespace: prefix),
    super_class: MESSAGE.super_class,
    type_params: [],
    location: nil,
    comment: comment_for_path(source_code_info, path, options: message.options),
    members: [],
    annotations: []
  ).tap do |class_decl|
    class_instance_type = factory.instance_type(RBS::TypeName.new(name: class_decl.name.name, namespace: RBS::Namespace.empty))

    maps = {}

    message.nested_type.each_with_index do |nested_type, index|
      if nested_type.options&.map_entry
        key_field, value_field = nested_type.field.to_a
        map_type_name = ".#{(message_path + [nested_type.name]).join(".")}"
        maps[map_type_name] = [key_field, value_field]
      else
        class_decl.members << message_to_decl(
          nested_type,
          prefix: RBS::Namespace.empty,
          message_path: message_path + [nested_type.name.to_sym],
          source_code_info: source_code_info,
          path: path + [3, index]
        )
      end
    end

    message.enum_type.each_with_index do |enum, index|
      class_decl.members << enum_type_to_decl(
        enum,
        prefix: RBS::Namespace.empty,
        source_code_info: source_code_info,
        path: path + [4, index]
      )
    end

    # @type var field_types: Hash[Symbol, [RBS::Types::t, Array[RBS::Types::t], RBS::Types::t]]
    field_types = {}

    message.field.each_with_index do |field, index|
      field_name = field.name.to_sym
      comment = comment_for_path(source_code_info, path + [2, index], options: field.options)

      read_type, write_types, init_type = field_type(field, maps)
      field_types[field_name] = [read_type, write_types, init_type]

      add_field(class_decl.members, name: field_name, read_type: read_type, write_types: write_types, comment: comment)
    end

    class_decl.members << RBS::AST::Members::MethodDefinition.new(
      name: :initialize,
      overloads: [
        RBS::AST::Members::MethodDefinition::Overload.new(
          method_type: factory.method_type(
            type: factory.function().update(
              optional_keywords: field_types.transform_values {|pair|
                _, _, init_type = pair
                factory.param(init_type)
              }
            )
          ),
          annotations: []
        ),
        unless field_types.empty?
          RBS::AST::Members::MethodDefinition::Overload.new(
            method_type: factory.method_type(
              type: factory.function().update(
                required_positionals: [factory.param(factory.alias_type("record"))]
              )
            ),
            annotations: []
          )
        end,
      ].compact,
      annotations: [],
      comment: nil,
      location: nil,
      overloading: false,
      visibility: nil,
      kind: :instance
    )

    unless field_types.empty?
      class_decl.members << RBS::AST::Members::MethodDefinition.new(
        name: :[],
        overloads:
          field_types.map do |field_name, pair|
            read_type, _ = pair

            RBS::AST::Members::MethodDefinition::Overload.new(
              method_type: factory.method_type(
                type: factory.function(read_type).update(
                  required_positionals: [
                    factory.param(factory.literal_type(field_name))
                  ]
                )
              ),
              annotations: []
            )
          end +
            [
              RBS::AST::Members::MethodDefinition::Overload.new(
                method_type: factory.method_type(
                  type: factory.function(factory.untyped).update(
                    required_positionals: [
                      factory.param(RBS::BuiltinNames::Symbol.instance_type)
                    ]
                  )
                ),
                annotations: []
              )
            ],
        annotations: [],
        comment: nil,
        location: nil,
        overloading: false,
        visibility: nil,
        kind: :instance
      )

      class_decl.members << RBS::AST::Members::MethodDefinition.new(
        name: :[]=,
        overloads:
          field_types.flat_map do |field_name, pair|
            read_type, write_types = pair

            [read_type, *write_types].map do |type|
              method_type =
                if (type_param, type_var = interface_type?(type))
                  factory.method_type(
                    type: factory.function(type_var).update(
                      required_positionals: [
                        factory.literal_type(field_name),
                        type_var
                      ].map {|t| factory.param(t) }
                    )
                  ).update(type_params: [type_param])
                else
                  factory.method_type(
                    type: factory.function(type).update(
                      required_positionals: [
                        factory.literal_type(field_name),
                        type
                      ].map {|t| factory.param(t) }
                    )
                  )
                end

              RBS::AST::Members::MethodDefinition::Overload.new(method_type: method_type, annotations: [])
            end
          end +
            [
              RBS::AST::Members::MethodDefinition::Overload.new(
                method_type: factory.method_type(
                  type: factory.function(factory.untyped).update(
                    required_positionals: [
                      RBS::BuiltinNames::Symbol.instance_type,
                      factory.untyped
                    ].map {|t| factory.param(t) }
                  )
                ),
                annotations: []
              )
            ],
        annotations: [],
        comment: nil,
        location: nil,
        overloading: false,
        visibility: nil,
        kind: :instance
      )
    end

    message.field.each do |field|
      if field.type == FieldDescriptorProto::Type::TYPE_BOOL
        class_decl.members << RBS::AST::Members::MethodDefinition.new(
          name: :"#{field.name}?",
          overloads: [
            RBS::AST::Members::MethodDefinition::Overload.new(
              method_type: factory.method_type(
                type: factory.function(factory.bool_type)
              ),
              annotations: []
            )
          ],
          annotations: [],
          comment: nil,
          location: nil,
          overloading: false,
          visibility: nil,
          kind: :instance
        )
      end
    end

    class_decl.members << RBS::AST::Declarations::Interface.new(
      name: TO_PROTO.name,
      type_params: [],
      members: [],
      annotations: [],
      comment: nil,
      location: nil
    ).tap do |interface_decl|
      interface_decl.members << RBS::AST::Members::MethodDefinition.new(
        name: :to_proto,
        overloads: [
          RBS::AST::Members::MethodDefinition::Overload.new(
            method_type: factory.method_type(
              type: factory.function(class_instance_type)
            ),
            annotations: []
          )
        ],
        annotations: [],
        comment: nil,
        location: nil,
        overloading: false,
        visibility: nil,
        kind: :instance
      )
    end

    class_decl.members << RBS::AST::Declarations::TypeAlias.new(
      name: TypeName("init"),
      type_params: [],
      type: factory.union_type(
        class_instance_type,
        TO_PROTO[],
        field_types.empty? ? nil : factory.alias_type("record")
      ),
      annotations: [],
      comment: RBS::AST::Comment.new(string: "The type of `#initialize` parameter.", location: nil),
      location: nil
    )

    class_decl.members << RBS::AST::Declarations::TypeAlias.new(
      name: TypeName("field_array"),
      type_params: [],
      type: FIELD_ARRAY[
        class_instance_type,
        factory.union_type(class_instance_type, TO_PROTO[])
      ],
      annotations: [],
      comment: RBS::AST::Comment.new(string: "The type of `repeated` field.", location: nil),
      location: nil
    )

    class_decl.members << RBS::AST::Declarations::TypeAlias.new(
      name: TypeName("field_hash"),
      type_params: [RBS::AST::TypeParam.new(name: :KEY, variance: :invariant, upper_bound: nil, location: nil)],
      type: FIELD_HASH[
        factory.type_var(:KEY),
        class_instance_type,
        factory.union_type(class_instance_type, TO_PROTO[])
      ],
      annotations: [],
      comment: RBS::AST::Comment.new(string: "The type of `map` field.", location: nil),
      location: nil
    )

    class_decl.members << RBS::AST::Declarations::TypeAlias.new(
      name: TypeName("array"),
      type_params: [],
      type: RBS::BuiltinNames::Array.instance_type(factory.union_type(class_instance_type, TO_PROTO[])),
      annotations: [],
      comment: nil,
      location: nil
    )

    class_decl.members << RBS::AST::Declarations::TypeAlias.new(
      name: TypeName("hash"),
      type_params: [RBS::AST::TypeParam.new(name: :KEY, variance: :invariant, upper_bound: nil, location: nil)],
      type: RBS::BuiltinNames::Hash.instance_type(
        factory.type_var(:KEY),
        factory.union_type(class_instance_type, TO_PROTO[])
      ),
      annotations: [],
      comment: nil,
      location: nil
    )

    class_decl.members << RBS::AST::Declarations::TypeAlias.new(
      name: TypeName("record"),
      type_params: [],
      type: RBS::Types::Record.new(
        fields: field_types.transform_values {|_, _, type| optional_type(type) },
        location: nil
        ),
      annotations: [],
      location: nil,
      comment: nil
    )
  end
end

#message_to_proto_type(type) ⇒ Object



433
434
435
436
437
438
439
440
# File 'lib/rbs_protobuf/translator/protobuf_gem.rb', line 433

def message_to_proto_type(type)
  namespace = type.name.to_namespace
  RBS::Types::Interface.new(
    name: RBS::TypeName.new(name: :_ToProto, namespace: namespace),
    args: [],
    location: nil
  )
end

#nested_namespace?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/rbs_protobuf/translator/protobuf_gem.rb', line 47

def nested_namespace?
  @nested_namespace
end

Returns:

  • (Boolean)


39
40
41
# File 'lib/rbs_protobuf/translator/protobuf_gem.rb', line 39

def print_extension?
  @extension == :print
end

Returns:

  • (Boolean)


35
36
37
# File 'lib/rbs_protobuf/translator/protobuf_gem.rb', line 35

def print_extension_message?
  @extension == nil
end

#rbs_content(file) ⇒ Object



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
# File 'lib/rbs_protobuf/translator/protobuf_gem.rb', line 51

def rbs_content(file)
  decls = [] #: Array[RBS::AST::Declarations::t]

  source_code_info = file.source_code_info

  if file.package && !file.package.empty?
    package_namespace = message_type(file.package).name.to_namespace
  else
    package_namespace = RBS::Namespace.empty
  end

  prefix = if nested_namespace?
             RBS::Namespace.empty
           else
             package_namespace
           end

  file.enum_type.each_with_index do |enum, index|
    decls << enum_type_to_decl(enum,
                               prefix: prefix,
                               source_code_info: source_code_info,
                               path: [5, index])
  end

  file.message_type.each_with_index do |message, index|
    decls << message_to_decl(message,
                             prefix: prefix,
                             message_path: if package_namespace.empty?
                                             [message.name.to_sym]
                                           else
                                             [file.package.to_sym, message.name.to_sym]
                                           end,
                             source_code_info: source_code_info,
                             path: [4, index])
  end

  file.service.each_with_index do |service, index|
    decls << service_to_decl(service,
                             prefix: prefix,
                             source_code_info: source_code_info,
                             path: [6, index])
  end

  if nested_namespace?
    package_namespace.path.reverse_each do |name|
      decls = [
        RBS::AST::Declarations::Module.new(
          name: factory.type_name(name.to_s),
          self_types: [],
          type_params: factory.module_type_params,
          location: nil,
          comment: nil,
          annotations: [],
          members: decls
        )
      ]
    end
  end

  file.extension.each.with_index do |extension, index|
    if ignore_extension?
      if print_extension_message?
        stderr.puts "Extension for `#{extension.extendee}` ignored in `#{file.name}`; Set RBS_PROTOBUF_EXTENSION env var to generate RBS for extensions."
      end
    else
      ext = extension_to_decl(extension, prefix: RBS::Namespace.root, source_code_info: source_code_info, path: [7, index])

      if print_extension?
        stderr.puts "#=========================================================="
        stderr.puts "# Printing RBS for extensions from #{file.name}"
        stderr.puts "#"
        RBS::Writer.new(out: stderr).write([ext])
        stderr.puts
      else
        decls.push(ext)
      end
    end
  end

  decls
end

#service_base_classObject



912
913
914
915
916
917
918
# File 'lib/rbs_protobuf/translator/protobuf_gem.rb', line 912

def service_base_class
  RBS::AST::Declarations::Class::Super.new(
    name: factory.type_name("::Protobuf::Rpc::Service"),
    args: [],
    location: nil
  )
end

#service_to_decl(service, prefix:, source_code_info:, path:) ⇒ Object



920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
# File 'lib/rbs_protobuf/translator/protobuf_gem.rb', line 920

def service_to_decl(service, prefix:, source_code_info:, path:)
  service_name = ActiveSupport::Inflector.camelize(service.name)

  members = [] #: Array[RBS::AST::Declarations::Class::member]

  service.method.each do |method|
    method_name = ActiveSupport::Inflector.underscore(method.name).to_sym #: Symbol

    interface_name = "_#{ActiveSupport::Inflector.camelize(method.name)}Method"

    members << RBS::AST::Declarations::Interface.new(
      name: factory.type_name(interface_name),
      type_params: [],
      members: [
        RBS::AST::Members::MethodDefinition.new(
          name: :request,
          kind: :instance,
          overloads: [
            RBS::AST::Members::MethodDefinition::Overload.new(
              method_type: factory.method_type(type: factory.function(message_type(method.input_type))),
              annotations: []
            )
          ],
          annotations: [],
          location: nil,
          comment: nil,
          overloading: false,
          visibility: nil
        ),
        RBS::AST::Members::MethodDefinition.new(
          name: :respond_with,
          kind: :instance,
          overloads: [
            RBS::AST::Members::MethodDefinition::Overload.new(
              method_type: factory.method_type(
                type: factory.function().update(
                  required_positionals: [
                    factory.param(message_init_type(message_type(method.output_type)))
                  ]
                )
              ),
              annotations: []
            )
          ],
          annotations: [],
          location: nil,
          comment: nil,
          overloading: false,
          visibility: nil
        )
      ],
      annotations: [],
      location: nil,
      comment: nil
    )

    members << RBS::AST::Members::MethodDefinition.new(
      name: method_name,
      kind: :instance,
      overloads: [
        RBS::AST::Members::MethodDefinition::Overload.new(
          method_type: factory.method_type(type: factory.function()),
          annotations: []
        )
      ],
      annotations: [],
      location: nil,
      comment: nil,
      overloading: false,
      visibility: nil
    )
  end

  RBS::AST::Declarations::Class.new(
    name: RBS::TypeName.new(name: service_name.to_sym, namespace: prefix),
    super_class: service_base_class,
    type_params: factory.module_type_params(),
    members: members,
    comment: comment_for_path(source_code_info, path, options: nil),
    location: nil,
    annotations: []
  )
end

#upcase_enum?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/rbs_protobuf/translator/protobuf_gem.rb', line 43

def upcase_enum?
  @upcase_enum
end