Skip to content

Commit 5c4329e

Browse files
committed
Add option to handle extensions
We provide three options: 1. Generate RBS for extensions 2. Print RBS for extensions so that users can copy and paste or modify them 3. Ignore extensions
1 parent 9cc39e4 commit 5c4329e

File tree

4 files changed

+172
-24
lines changed

4 files changed

+172
-24
lines changed

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace :example do
2525
"example/b.proto"
2626
)
2727
sh(
28-
{ "RBS_PROTOBUF_BACKEND" => "protobuf" },
28+
{ "RBS_PROTOBUF_BACKEND" => "protobuf", "RBS_PROTOBUF_EXTENSION" => "true" },
2929
"protoc",
3030
"--rbs_out=example/protobuf-gem",
3131
"-Iexample",

exe/protoc-gen-rbs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,22 @@ translator = case backend
1919
when "protobuf"
2020
upcase_enum = ENV.key?("PB_UPCASE_ENUMS")
2121
no_nested_namespace = ENV.key?("RBS_PROTOBUF_NO_NESTED_NAMESPACE")
22+
extension = case ENV["RBS_PROTOBUF_EXTENSION"]
23+
when "false"
24+
false
25+
when nil
26+
nil
27+
when "print"
28+
:print
29+
else
30+
true
31+
end
2232

2333
RBSProtobuf::Translator::ProtobufGem.new(
2434
input,
2535
upcase_enum: upcase_enum,
26-
nested_namespace: !no_nested_namespace
36+
nested_namespace: !no_nested_namespace,
37+
extension: extension
2738
)
2839
when "google-protobuf"
2940
raise NotImplementedError

lib/rbs_protobuf/translator/protobuf_gem.rb

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
module RBSProtobuf
22
module Translator
33
class ProtobufGem < Base
4-
def initialize(input, upcase_enum:, nested_namespace:)
4+
attr_reader :stderr
5+
6+
def initialize(input, upcase_enum:, nested_namespace:, extension:, stderr: STDERR)
57
super(input)
68
@upcase_enum = upcase_enum
79
@nested_namespace = nested_namespace
10+
@extension = extension
11+
@stderr = stderr
12+
end
13+
14+
def ignore_extension?
15+
!@extension
16+
end
17+
18+
def print_extension_message?
19+
@extension == nil
20+
end
21+
22+
def print_extension?
23+
@extension == :print
824
end
925

1026
def upcase_enum?
@@ -75,11 +91,27 @@ def rbs_content(file)
7591
end
7692

7793
file.extension.group_by(&:extendee).each.with_index do |(name, extensions), index|
78-
decls.push(*extension_to_decl(name,
79-
extensions,
80-
prefix: RBS::Namespace.root,
81-
source_code_info: source_code_info,
82-
path: [7, index]))
94+
if ignore_extension?
95+
if print_extension_message?
96+
stderr.puts "Extension for `#{name}` ignored in `#{file.name}`; Set RBS_PROTOBUF_EXTENSION env var to generate RBS for extensions."
97+
end
98+
else
99+
exts = extension_to_decl(name,
100+
extensions,
101+
prefix: RBS::Namespace.root,
102+
source_code_info: source_code_info,
103+
path: [7, index])
104+
105+
if print_extension?
106+
stderr.puts "#=========================================================="
107+
stderr.puts "# Printing RBS for extensions from #{file.name}"
108+
stderr.puts "#"
109+
RBS::Writer.new(out: stderr).write(exts)
110+
stderr.puts
111+
else
112+
decls.push(*exts)
113+
end
114+
end
83115
end
84116

85117
StringIO.new.tap do |io|

test/protobuf_gem_test.rb

Lines changed: 121 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ def test_message_with_base_type
1717
translator = RBSProtobuf::Translator::ProtobufGem.new(
1818
input,
1919
upcase_enum: true,
20-
nested_namespace: true
20+
nested_namespace: true,
21+
extension: false
2122
)
2223
content = translator.rbs_content(input.proto_file[0])
2324

@@ -60,7 +61,8 @@ def test_message_with_bool_predicate
6061
translator = RBSProtobuf::Translator::ProtobufGem.new(
6162
input,
6263
upcase_enum: true,
63-
nested_namespace: true
64+
nested_namespace: true,
65+
extension: false
6466
)
6567
content = translator.rbs_content(input.proto_file[0])
6668

@@ -100,7 +102,8 @@ def test_message_with_message
100102
translator = RBSProtobuf::Translator::ProtobufGem.new(
101103
input,
102104
upcase_enum: false,
103-
nested_namespace: true
105+
nested_namespace: true,
106+
extension: false
104107
)
105108
content = translator.rbs_content(input.proto_file[0])
106109

@@ -146,7 +149,8 @@ def test_enum
146149
translator = RBSProtobuf::Translator::ProtobufGem.new(
147150
input,
148151
upcase_enum: false,
149-
nested_namespace: true
152+
nested_namespace: true,
153+
extension: false
150154
)
151155
content = translator.rbs_content(input.proto_file[0])
152156

@@ -185,7 +189,8 @@ def test_enum_with_alias
185189
translator = RBSProtobuf::Translator::ProtobufGem.new(
186190
input,
187191
upcase_enum: false,
188-
nested_namespace: true
192+
nested_namespace: true,
193+
extension: false
189194
)
190195
content = translator.rbs_content(input.proto_file[0])
191196

@@ -228,7 +233,8 @@ def test_message_with_enum
228233
translator = RBSProtobuf::Translator::ProtobufGem.new(
229234
input,
230235
upcase_enum: true,
231-
nested_namespace: true
236+
nested_namespace: true,
237+
extension: false
232238
)
233239
content = translator.rbs_content(input.proto_file[0])
234240

@@ -290,7 +296,8 @@ def test_message_with_package
290296
translator = RBSProtobuf::Translator::ProtobufGem.new(
291297
input,
292298
upcase_enum: true,
293-
nested_namespace: true
299+
nested_namespace: true,
300+
extension: false
294301
)
295302
content = translator.rbs_content(input.proto_file[0])
296303

@@ -332,7 +339,8 @@ def test_message_with_package_flat_namespace
332339
translator = RBSProtobuf::Translator::ProtobufGem.new(
333340
input,
334341
upcase_enum: true,
335-
nested_namespace: false
342+
nested_namespace: false,
343+
extension: false
336344
)
337345
content = translator.rbs_content(input.proto_file[0])
338346

@@ -360,7 +368,8 @@ def test_message_with_one_of
360368
translator = RBSProtobuf::Translator::ProtobufGem.new(
361369
input,
362370
upcase_enum: true,
363-
nested_namespace: true
371+
nested_namespace: true,
372+
extension: false
364373
)
365374
content = translator.rbs_content(input.proto_file[0])
366375

@@ -400,7 +409,8 @@ def test_message_with_map_to_base_and_message
400409
translator = RBSProtobuf::Translator::ProtobufGem.new(
401410
input,
402411
upcase_enum: true,
403-
nested_namespace: true
412+
nested_namespace: true,
413+
extension: false
404414
)
405415
content = translator.rbs_content(input.proto_file[0])
406416

@@ -442,7 +452,8 @@ def test_message_with_map_to_enum
442452
translator = RBSProtobuf::Translator::ProtobufGem.new(
443453
input,
444454
upcase_enum: true,
445-
nested_namespace: true
455+
nested_namespace: true,
456+
extension: false
446457
)
447458
content = translator.rbs_content(input.proto_file[0])
448459

@@ -495,7 +506,8 @@ def test_message_with_map_with_package
495506
translator = RBSProtobuf::Translator::ProtobufGem.new(
496507
input,
497508
upcase_enum: true,
498-
nested_namespace: true
509+
nested_namespace: true,
510+
extension: false
499511
)
500512
content = translator.rbs_content(input.proto_file[0])
501513

@@ -534,7 +546,8 @@ def test_nested_message
534546
translator = RBSProtobuf::Translator::ProtobufGem.new(
535547
input,
536548
upcase_enum: true,
537-
nested_namespace: true
549+
nested_namespace: true,
550+
extension: false
538551
)
539552
content = translator.rbs_content(input.proto_file[0])
540553

@@ -574,7 +587,8 @@ def test_nested_enum
574587
translator = RBSProtobuf::Translator::ProtobufGem.new(
575588
input,
576589
upcase_enum: true,
577-
nested_namespace: true
590+
nested_namespace: true,
591+
extension: false
578592
)
579593
content = translator.rbs_content(input.proto_file[0])
580594

@@ -635,7 +649,8 @@ def test_service
635649
translator = RBSProtobuf::Translator::ProtobufGem.new(
636650
input,
637651
upcase_enum: true,
638-
nested_namespace: true
652+
nested_namespace: true,
653+
extension: false
639654
)
640655
content = translator.rbs_content(input.proto_file[0])
641656

@@ -678,7 +693,8 @@ def test_extension
678693
translator = RBSProtobuf::Translator::ProtobufGem.new(
679694
input,
680695
upcase_enum: true,
681-
nested_namespace: true
696+
nested_namespace: true,
697+
extension: true
682698
)
683699
content = translator.rbs_content(input.proto_file[0])
684700

@@ -710,6 +726,95 @@ def []: (:parent) -> ::Test::M1?
710726
def []=: (:parent, ::Test::M1?) -> ::Test::M1?
711727
| ...
712728
end
729+
RBS
730+
end
731+
732+
def test_extension_ignore
733+
input = read_proto(<<EOP)
734+
syntax = "proto2";
735+
736+
package test;
737+
738+
message M1 {
739+
extensions 100 to max;
740+
}
741+
742+
extend M1 {
743+
optional string name = 100;
744+
}
745+
EOP
746+
stderr = StringIO.new
747+
748+
translator = RBSProtobuf::Translator::ProtobufGem.new(
749+
input,
750+
upcase_enum: true,
751+
nested_namespace: true,
752+
extension: nil,
753+
stderr: stderr
754+
)
755+
content = translator.rbs_content(input.proto_file[0])
756+
757+
assert_equal <<RBS, content
758+
module Test
759+
class M1 < ::Protobuf::Message
760+
def initialize: () -> void
761+
end
762+
end
763+
RBS
764+
assert_equal <<RBS, stderr.string
765+
Extension for `.test.M1` ignored in `a.proto`; Set RBS_PROTOBUF_EXTENSION env var to generate RBS for extensions.
766+
RBS
767+
end
768+
769+
def test_extension_print
770+
input = read_proto(<<EOP)
771+
syntax = "proto2";
772+
773+
package test;
774+
775+
message M1 {
776+
extensions 100 to max;
777+
}
778+
779+
extend M1 {
780+
optional string name = 100;
781+
}
782+
EOP
783+
stderr = StringIO.new
784+
785+
translator = RBSProtobuf::Translator::ProtobufGem.new(
786+
input,
787+
upcase_enum: true,
788+
nested_namespace: true,
789+
extension: :print,
790+
stderr: stderr
791+
)
792+
content = translator.rbs_content(input.proto_file[0])
793+
794+
assert_equal <<RBS, content
795+
module Test
796+
class M1 < ::Protobuf::Message
797+
def initialize: () -> void
798+
end
799+
end
800+
RBS
801+
802+
assert_equal <<RBS, stderr.string
803+
#==========================================================
804+
# Printing RBS for extensions from a.proto
805+
#
806+
class ::Test::M1
807+
attr_reader name(): ::String
808+
809+
attr_writer name(): ::String?
810+
811+
def []: (:name) -> ::String
812+
| ...
813+
814+
def []=: (:name, ::String?) -> ::String?
815+
| ...
816+
end
817+
713818
RBS
714819
end
715820
end

0 commit comments

Comments
 (0)