rbs_protobuf 1.0.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,8 +19,8 @@ module RBSProtobuf
19
19
 
20
20
  attr_reader :accept_nil_writer
21
21
 
22
- def initialize(input, upcase_enum:, nested_namespace:, extension:, accept_nil_writer:, stderr: STDERR)
23
- super(input)
22
+ def initialize(input, filters=[], upcase_enum:, nested_namespace:, extension:, accept_nil_writer:, stderr: STDERR)
23
+ super(input, filters)
24
24
  @upcase_enum = upcase_enum
25
25
  @nested_namespace = nested_namespace
26
26
  @extension = extension
@@ -49,7 +49,7 @@ module RBSProtobuf
49
49
  end
50
50
 
51
51
  def rbs_content(file)
52
- decls = []
52
+ decls = [] #: Array[RBS::AST::Declarations::t]
53
53
 
54
54
  source_code_info = file.source_code_info
55
55
 
@@ -127,9 +127,7 @@ module RBSProtobuf
127
127
  end
128
128
  end
129
129
 
130
- StringIO.new.tap do |io|
131
- RBS::Writer.new(out: io).write(decls)
132
- end.string
130
+ decls
133
131
  end
134
132
 
135
133
  def message_to_decl(message, prefix:, message_path:, source_code_info:, path:)
@@ -188,96 +186,124 @@ module RBSProtobuf
188
186
 
189
187
  class_decl.members << RBS::AST::Members::MethodDefinition.new(
190
188
  name: :initialize,
191
- types: [
192
- factory.method_type(
193
- type: factory.function().update(
194
- optional_keywords: field_types.transform_values {|pair|
195
- _, _, init_type = pair
196
- factory.param(init_type)
197
- }
189
+ overloads: [
190
+ RBS::AST::Members::MethodDefinition::Overload.new(
191
+ method_type: factory.method_type(
192
+ type: factory.function().update(
193
+ optional_keywords: field_types.transform_values {|pair|
194
+ _, _, init_type = pair
195
+ factory.param(init_type)
196
+ }
197
+ )
198
+ ),
199
+ annotations: []
200
+ ),
201
+ unless field_types.empty?
202
+ RBS::AST::Members::MethodDefinition::Overload.new(
203
+ method_type: factory.method_type(
204
+ type: factory.function().update(
205
+ required_positionals: [factory.param(factory.alias_type("record"))]
206
+ )
207
+ ),
208
+ annotations: []
198
209
  )
199
- )
200
- ],
210
+ end,
211
+ ].compact,
201
212
  annotations: [],
202
213
  comment: nil,
203
214
  location: nil,
204
- overload: false,
215
+ overloading: false,
216
+ visibility: nil,
205
217
  kind: :instance
206
218
  )
207
219
 
208
220
  unless field_types.empty?
209
221
  class_decl.members << RBS::AST::Members::MethodDefinition.new(
210
222
  name: :[],
211
- types:
223
+ overloads:
212
224
  field_types.map do |field_name, pair|
213
225
  read_type, _ = pair
214
226
 
215
- factory.method_type(
216
- type: factory.function(read_type).update(
217
- required_positionals: [
218
- factory.param(factory.literal_type(field_name))
219
- ]
220
- )
221
- )
222
- end +
223
- [
224
- factory.method_type(
225
- type: factory.function(factory.untyped).update(
227
+ RBS::AST::Members::MethodDefinition::Overload.new(
228
+ method_type: factory.method_type(
229
+ type: factory.function(read_type).update(
226
230
  required_positionals: [
227
- factory.param(RBS::BuiltinNames::Symbol.instance_type)
231
+ factory.param(factory.literal_type(field_name))
228
232
  ]
229
233
  )
234
+ ),
235
+ annotations: []
236
+ )
237
+ end +
238
+ [
239
+ RBS::AST::Members::MethodDefinition::Overload.new(
240
+ method_type: factory.method_type(
241
+ type: factory.function(factory.untyped).update(
242
+ required_positionals: [
243
+ factory.param(RBS::BuiltinNames::Symbol.instance_type)
244
+ ]
245
+ )
246
+ ),
247
+ annotations: []
230
248
  )
231
249
  ],
232
250
  annotations: [],
233
251
  comment: nil,
234
252
  location: nil,
235
- overload: false,
253
+ overloading: false,
254
+ visibility: nil,
236
255
  kind: :instance
237
256
  )
238
257
 
239
258
  class_decl.members << RBS::AST::Members::MethodDefinition.new(
240
259
  name: :[]=,
241
- types:
260
+ overloads:
242
261
  field_types.flat_map do |field_name, pair|
243
262
  read_type, write_types = pair
244
263
 
245
264
  [read_type, *write_types].map do |type|
246
- if (type_param, type_var = interface_type?(type))
247
- factory.method_type(
248
- type: factory.function(type_var).update(
249
- required_positionals: [
250
- factory.literal_type(field_name),
251
- type_var
252
- ].map {|t| factory.param(t) }
253
- )
254
- ).update(type_params: [type_param])
255
- else
256
- factory.method_type(
257
- type: factory.function(type).update(
258
- required_positionals: [
259
- factory.literal_type(field_name),
260
- type
261
- ].map {|t| factory.param(t) }
265
+ method_type =
266
+ if (type_param, type_var = interface_type?(type))
267
+ factory.method_type(
268
+ type: factory.function(type_var).update(
269
+ required_positionals: [
270
+ factory.literal_type(field_name),
271
+ type_var
272
+ ].map {|t| factory.param(t) }
273
+ )
274
+ ).update(type_params: [type_param])
275
+ else
276
+ factory.method_type(
277
+ type: factory.function(type).update(
278
+ required_positionals: [
279
+ factory.literal_type(field_name),
280
+ type
281
+ ].map {|t| factory.param(t) }
282
+ )
262
283
  )
263
- )
264
- end
284
+ end
285
+
286
+ RBS::AST::Members::MethodDefinition::Overload.new(method_type: method_type, annotations: [])
265
287
  end
266
288
  end +
267
289
  [
268
- factory.method_type(
269
- type: factory.function(factory.untyped).update(
270
- required_positionals: [
271
- RBS::BuiltinNames::Symbol.instance_type,
272
- factory.untyped
273
- ].map {|t| factory.param(t) }
274
- )
290
+ RBS::AST::Members::MethodDefinition::Overload.new(
291
+ method_type: factory.method_type(
292
+ type: factory.function(factory.untyped).update(
293
+ required_positionals: [
294
+ RBS::BuiltinNames::Symbol.instance_type,
295
+ factory.untyped
296
+ ].map {|t| factory.param(t) }
297
+ )
298
+ ),
299
+ annotations: []
275
300
  )
276
301
  ],
277
302
  annotations: [],
278
303
  comment: nil,
279
304
  location: nil,
280
- overload: false,
305
+ overloading: false,
306
+ visibility: nil,
281
307
  kind: :instance
282
308
  )
283
309
  end
@@ -286,15 +312,19 @@ module RBSProtobuf
286
312
  if field.type == FieldDescriptorProto::Type::TYPE_BOOL
287
313
  class_decl.members << RBS::AST::Members::MethodDefinition.new(
288
314
  name: :"#{field.name}?",
289
- types: [
290
- factory.method_type(
291
- type: factory.function(factory.bool_type)
315
+ overloads: [
316
+ RBS::AST::Members::MethodDefinition::Overload.new(
317
+ method_type: factory.method_type(
318
+ type: factory.function(factory.bool_type)
319
+ ),
320
+ annotations: []
292
321
  )
293
322
  ],
294
323
  annotations: [],
295
324
  comment: nil,
296
325
  location: nil,
297
- overload: false,
326
+ overloading: false,
327
+ visibility: nil,
298
328
  kind: :instance
299
329
  )
300
330
  end
@@ -310,29 +340,37 @@ module RBSProtobuf
310
340
  ).tap do |interface_decl|
311
341
  interface_decl.members << RBS::AST::Members::MethodDefinition.new(
312
342
  name: :to_proto,
313
- types: [
314
- factory.method_type(
315
- type: factory.function(class_instance_type)
343
+ overloads: [
344
+ RBS::AST::Members::MethodDefinition::Overload.new(
345
+ method_type: factory.method_type(
346
+ type: factory.function(class_instance_type)
347
+ ),
348
+ annotations: []
316
349
  )
317
350
  ],
318
351
  annotations: [],
319
352
  comment: nil,
320
353
  location: nil,
321
- overload: false,
354
+ overloading: false,
355
+ visibility: nil,
322
356
  kind: :instance
323
357
  )
324
358
  end
325
359
 
326
- class_decl.members << RBS::AST::Declarations::Alias.new(
360
+ class_decl.members << RBS::AST::Declarations::TypeAlias.new(
327
361
  name: TypeName("init"),
328
362
  type_params: [],
329
- type: factory.union_type(class_instance_type, TO_PROTO[]),
363
+ type: factory.union_type(
364
+ class_instance_type,
365
+ TO_PROTO[],
366
+ field_types.empty? ? nil : factory.alias_type("record")
367
+ ),
330
368
  annotations: [],
331
369
  comment: RBS::AST::Comment.new(string: "The type of `#initialize` parameter.", location: nil),
332
370
  location: nil
333
371
  )
334
372
 
335
- class_decl.members << RBS::AST::Declarations::Alias.new(
373
+ class_decl.members << RBS::AST::Declarations::TypeAlias.new(
336
374
  name: TypeName("field_array"),
337
375
  type_params: [],
338
376
  type: FIELD_ARRAY[
@@ -344,7 +382,7 @@ module RBSProtobuf
344
382
  location: nil
345
383
  )
346
384
 
347
- class_decl.members << RBS::AST::Declarations::Alias.new(
385
+ class_decl.members << RBS::AST::Declarations::TypeAlias.new(
348
386
  name: TypeName("field_hash"),
349
387
  type_params: [RBS::AST::TypeParam.new(name: :KEY, variance: :invariant, upper_bound: nil, location: nil)],
350
388
  type: FIELD_HASH[
@@ -357,7 +395,7 @@ module RBSProtobuf
357
395
  location: nil
358
396
  )
359
397
 
360
- class_decl.members << RBS::AST::Declarations::Alias.new(
398
+ class_decl.members << RBS::AST::Declarations::TypeAlias.new(
361
399
  name: TypeName("array"),
362
400
  type_params: [],
363
401
  type: RBS::BuiltinNames::Array.instance_type(factory.union_type(class_instance_type, TO_PROTO[])),
@@ -366,7 +404,7 @@ module RBSProtobuf
366
404
  location: nil
367
405
  )
368
406
 
369
- class_decl.members << RBS::AST::Declarations::Alias.new(
407
+ class_decl.members << RBS::AST::Declarations::TypeAlias.new(
370
408
  name: TypeName("hash"),
371
409
  type_params: [RBS::AST::TypeParam.new(name: :KEY, variance: :invariant, upper_bound: nil, location: nil)],
372
410
  type: RBS::BuiltinNames::Hash.instance_type(
@@ -377,6 +415,18 @@ module RBSProtobuf
377
415
  comment: nil,
378
416
  location: nil
379
417
  )
418
+
419
+ class_decl.members << RBS::AST::Declarations::TypeAlias.new(
420
+ name: TypeName("record"),
421
+ type_params: [],
422
+ type: RBS::Types::Record.new(
423
+ fields: field_types.transform_values {|_, _, type| optional_type(type) },
424
+ location: nil
425
+ ),
426
+ annotations: [],
427
+ location: nil,
428
+ comment: nil
429
+ )
380
430
  end
381
431
  end
382
432
 
@@ -567,41 +617,52 @@ module RBSProtobuf
567
617
  unless write_types.empty?
568
618
  members << RBS::AST::Members::MethodDefinition.new(
569
619
  name: :"#{name}=",
570
- types:
620
+ overloads:
571
621
  write_types.map do |write_type|
572
- if (type_param, type = interface_type?(write_type))
573
- factory.method_type(
574
- type: factory.function(type).update(
575
- required_positionals:[factory.param(type)]
576
- )
577
- ).update(type_params: [type_param])
578
- else
579
- factory.method_type(
580
- type: factory.function(write_type).update(
581
- required_positionals:[factory.param(write_type)]
622
+ method_type =
623
+ if (type_param, type = interface_type?(write_type))
624
+ factory.method_type(
625
+ type: factory.function(type).update(
626
+ required_positionals:[factory.param(type)]
627
+ )
628
+ ).update(type_params: [type_param])
629
+ else
630
+ factory.method_type(
631
+ type: factory.function(write_type).update(
632
+ required_positionals:[factory.param(write_type)]
633
+ )
582
634
  )
583
- )
584
- end
635
+ end
636
+
637
+ RBS::AST::Members::MethodDefinition::Overload.new(
638
+ method_type: method_type,
639
+ annotations: []
640
+ )
585
641
  end,
586
642
  annotations: [],
587
643
  comment: comment,
588
644
  location: nil,
589
- overload: true,
645
+ overloading: true,
646
+ visibility: nil,
590
647
  kind: :instance
591
648
  )
592
649
  end
593
650
 
594
651
  members << RBS::AST::Members::MethodDefinition.new(
595
652
  name: :"#{name}!",
596
- types: [
597
- factory.method_type(
598
- type: factory.function(factory.optional_type(read_type))
653
+ overloads: [
654
+ RBS::AST::Members::MethodDefinition::Overload.new(
655
+ method_type: factory.method_type(
656
+ type: factory.function(factory.optional_type(read_type))
657
+ ),
658
+ annotations: []
599
659
  )
600
660
  ],
601
661
  annotations: [],
602
662
  comment: nil,
603
663
  location: nil,
604
- overload: false,
664
+ overloading: false,
665
+ visibility: nil,
605
666
  kind: :instance
606
667
  )
607
668
  end
@@ -638,7 +699,7 @@ module RBSProtobuf
638
699
  factory.literal_type(v.number)
639
700
  end.uniq
640
701
 
641
- enum_decl.members << RBS::AST::Declarations::Alias.new(
702
+ enum_decl.members << RBS::AST::Declarations::TypeAlias.new(
642
703
  name: factory.type_name("names"),
643
704
  type_params: [],
644
705
  type: factory.union_type(*names),
@@ -647,7 +708,7 @@ module RBSProtobuf
647
708
  annotations: []
648
709
  )
649
710
 
650
- enum_decl.members << RBS::AST::Declarations::Alias.new(
711
+ enum_decl.members << RBS::AST::Declarations::TypeAlias.new(
651
712
  name: factory.type_name("strings"),
652
713
  type_params: [],
653
714
  type: factory.union_type(*strings),
@@ -656,7 +717,7 @@ module RBSProtobuf
656
717
  annotations: []
657
718
  )
658
719
 
659
- enum_decl.members << RBS::AST::Declarations::Alias.new(
720
+ enum_decl.members << RBS::AST::Declarations::TypeAlias.new(
660
721
  name: factory.type_name("tags"),
661
722
  type_params: [],
662
723
  type: factory.union_type(*tags),
@@ -665,7 +726,7 @@ module RBSProtobuf
665
726
  annotations: []
666
727
  )
667
728
 
668
- enum_decl.members << RBS::AST::Declarations::Alias.new(
729
+ enum_decl.members << RBS::AST::Declarations::TypeAlias.new(
669
730
  name: factory.type_name("values"),
670
731
  type_params: [],
671
732
  type: factory.union_type(
@@ -712,7 +773,7 @@ module RBSProtobuf
712
773
  enum_instance_type = factory.instance_type(RBS::TypeName.new(name: enum_name.to_sym, namespace: RBS::Namespace.empty))
713
774
  values_type = factory.alias_type(RBS::TypeName.new(name: :values, namespace: RBS::Namespace.empty))
714
775
 
715
- enum_decl.members << RBS::AST::Declarations::Alias.new(
776
+ enum_decl.members << RBS::AST::Declarations::TypeAlias.new(
716
777
  name: TypeName("init"),
717
778
  type_params: [],
718
779
  type: factory.union_type(enum_instance_type, values_type),
@@ -721,7 +782,7 @@ module RBSProtobuf
721
782
  location: nil
722
783
  )
723
784
 
724
- enum_decl.members << RBS::AST::Declarations::Alias.new(
785
+ enum_decl.members << RBS::AST::Declarations::TypeAlias.new(
725
786
  name: TypeName("field_array"),
726
787
  type_params: [],
727
788
  type: FIELD_ARRAY[
@@ -733,7 +794,7 @@ module RBSProtobuf
733
794
  location: nil
734
795
  )
735
796
 
736
- enum_decl.members << RBS::AST::Declarations::Alias.new(
797
+ enum_decl.members << RBS::AST::Declarations::TypeAlias.new(
737
798
  name: TypeName("field_hash"),
738
799
  type_params: [RBS::AST::TypeParam.new(name: :KEY, variance: :invariant, upper_bound: nil, location: nil)],
739
800
  type: FIELD_HASH[
@@ -746,7 +807,7 @@ module RBSProtobuf
746
807
  location: nil
747
808
  )
748
809
 
749
- enum_decl.members << RBS::AST::Declarations::Alias.new(
810
+ enum_decl.members << RBS::AST::Declarations::TypeAlias.new(
750
811
  name: TypeName("array"),
751
812
  type_params: [],
752
813
  type: RBS::BuiltinNames::Array.instance_type(factory.union_type(enum_instance_type, values_type)),
@@ -755,7 +816,7 @@ module RBSProtobuf
755
816
  location: nil
756
817
  )
757
818
 
758
- enum_decl.members << RBS::AST::Declarations::Alias.new(
819
+ enum_decl.members << RBS::AST::Declarations::TypeAlias.new(
759
820
  name: TypeName("hash"),
760
821
  type_params: [RBS::AST::TypeParam.new(name: :KEY, variance: :invariant, upper_bound: nil, location: nil)],
761
822
  type: RBS::BuiltinNames::Hash.instance_type(
@@ -790,49 +851,59 @@ module RBSProtobuf
790
851
 
791
852
  class_decl.members << RBS::AST::Members::MethodDefinition.new(
792
853
  name: :[],
793
- types: [
794
- factory.method_type(
795
- type: factory.function(read_type).update(
796
- required_positionals: [
797
- factory.param(factory.literal_type(field_name))
798
- ]
799
- )
854
+ overloads: [
855
+ RBS::AST::Members::MethodDefinition::Overload.new(
856
+ method_type: factory.method_type(
857
+ type: factory.function(read_type).update(
858
+ required_positionals: [
859
+ factory.param(factory.literal_type(field_name))
860
+ ]
861
+ )
862
+ ),
863
+ annotations: []
800
864
  )
801
865
  ],
802
866
  annotations: [],
803
867
  comment: nil,
804
868
  location: nil,
805
- overload: true,
869
+ overloading: true,
870
+ visibility: nil,
806
871
  kind: :instance
807
872
  )
808
873
 
809
874
  class_decl.members << RBS::AST::Members::MethodDefinition.new(
810
875
  name: :[]=,
811
- types: [read_type, *write_types].map do |write_type|
812
- if (type_param, type_var = interface_type?(write_type))
813
- factory.method_type(
814
- type: factory.function(type_var).update(
815
- required_positionals: [
816
- factory.param(factory.literal_type(field_name)),
817
- factory.param(type_var)
818
- ]
819
- )
820
- ).update(type_params: [type_param])
821
- else
822
- factory.method_type(
823
- type: factory.function(write_type).update(
824
- required_positionals: [
825
- factory.param(factory.literal_type(field_name)),
826
- factory.param(write_type)
827
- ]
876
+ overloads: [read_type, *write_types].map do |write_type|
877
+ method_type =
878
+ if (type_param, type_var = interface_type?(write_type))
879
+ factory.method_type(
880
+ type: factory.function(type_var).update(
881
+ required_positionals: [
882
+ factory.param(factory.literal_type(field_name)),
883
+ factory.param(type_var)
884
+ ]
885
+ )
886
+ ).update(type_params: [type_param])
887
+ else
888
+ factory.method_type(
889
+ type: factory.function(write_type).update(
890
+ required_positionals: [
891
+ factory.param(factory.literal_type(field_name)),
892
+ factory.param(write_type)
893
+ ]
894
+ )
828
895
  )
829
- )
830
- end
896
+ end
897
+ RBS::AST::Members::MethodDefinition::Overload.new(
898
+ method_type: method_type,
899
+ annotations: []
900
+ )
831
901
  end,
832
902
  annotations: [],
833
903
  comment: nil,
834
904
  location: nil,
835
- overload: true,
905
+ overloading: true,
906
+ visibility: nil,
836
907
  kind: :instance
837
908
  )
838
909
  end
@@ -849,11 +920,81 @@ module RBSProtobuf
849
920
  def service_to_decl(service, prefix:, source_code_info:, path:)
850
921
  service_name = ActiveSupport::Inflector.camelize(service.name)
851
922
 
923
+ members = [] #: Array[RBS::AST::Declarations::Class::member]
924
+
925
+ service.method.each do |method|
926
+ method_name = ActiveSupport::Inflector.underscore(method.name).to_sym #: Symbol
927
+
928
+ interface_name = "_#{ActiveSupport::Inflector.camelize(method.name)}Method"
929
+
930
+ members << RBS::AST::Declarations::Interface.new(
931
+ name: factory.type_name(interface_name),
932
+ type_params: [],
933
+ members: [
934
+ RBS::AST::Members::MethodDefinition.new(
935
+ name: :request,
936
+ kind: :instance,
937
+ overloads: [
938
+ RBS::AST::Members::MethodDefinition::Overload.new(
939
+ method_type: factory.method_type(type: factory.function(message_type(method.input_type))),
940
+ annotations: []
941
+ )
942
+ ],
943
+ annotations: [],
944
+ location: nil,
945
+ comment: nil,
946
+ overloading: false,
947
+ visibility: nil
948
+ ),
949
+ RBS::AST::Members::MethodDefinition.new(
950
+ name: :respond_with,
951
+ kind: :instance,
952
+ overloads: [
953
+ RBS::AST::Members::MethodDefinition::Overload.new(
954
+ method_type: factory.method_type(
955
+ type: factory.function().update(
956
+ required_positionals: [
957
+ factory.param(message_init_type(message_type(method.output_type)))
958
+ ]
959
+ )
960
+ ),
961
+ annotations: []
962
+ )
963
+ ],
964
+ annotations: [],
965
+ location: nil,
966
+ comment: nil,
967
+ overloading: false,
968
+ visibility: nil
969
+ )
970
+ ],
971
+ annotations: [],
972
+ location: nil,
973
+ comment: nil
974
+ )
975
+
976
+ members << RBS::AST::Members::MethodDefinition.new(
977
+ name: method_name,
978
+ kind: :instance,
979
+ overloads: [
980
+ RBS::AST::Members::MethodDefinition::Overload.new(
981
+ method_type: factory.method_type(type: factory.function()),
982
+ annotations: []
983
+ )
984
+ ],
985
+ annotations: [],
986
+ location: nil,
987
+ comment: nil,
988
+ overloading: false,
989
+ visibility: nil
990
+ )
991
+ end
992
+
852
993
  RBS::AST::Declarations::Class.new(
853
994
  name: RBS::TypeName.new(name: service_name.to_sym, namespace: prefix),
854
995
  super_class: service_base_class,
855
996
  type_params: factory.module_type_params(),
856
- members: [],
997
+ members: members,
857
998
  comment: comment_for_path(source_code_info, path, options: nil),
858
999
  location: nil,
859
1000
  annotations: []
@@ -1,3 +1,3 @@
1
1
  module RBSProtobuf
2
- VERSION = "1.0.0"
2
+ VERSION = "1.2.0"
3
3
  end