Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for :oneof fields on protobuf messages #929

Merged
merged 1 commit into from May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/tapioca/dsl/compilers/protobuf.rb
Expand Up @@ -83,6 +83,7 @@ def decorate
create_type_members(klass, "Key", "Value")
else
descriptor = T.let(T.unsafe(constant).descriptor, Google::Protobuf::Descriptor)
descriptor.each_oneof { |oneof| create_oneof_method(klass, oneof) }
fields = descriptor.map { |desc| create_descriptor_method(klass, desc) }
fields.sort_by!(&:name)

Expand Down Expand Up @@ -216,6 +217,19 @@ def create_descriptor_method(klass, desc)

field
end

sig do
params(
klass: RBI::Scope,
desc: Google::Protobuf::OneofDescriptor
).void
end
def create_oneof_method(klass, desc)
klass.create_method(
desc.name,
return_type: "T.nilable(Symbol)"
)
end
end
end
end
Expand Down
24 changes: 24 additions & 0 deletions spec/tapioca/dsl/compilers/protobuf_spec.rb
Expand Up @@ -411,6 +411,30 @@ def ShopName=(value); end

assert_equal(expected, rbi_for(:Cart))
end

it "generates methods in RBI files with oneof fields" do
add_ruby_file("protobuf.rb", <<~RUBY)
Google::Protobuf::DescriptorPool.generated_pool.build do
add_file("cart.proto", :syntax => :proto3) do
add_message "MyCart" do
oneof :contact_info do
optional :phone_number, :int32, 1
optional :email, :string, 2
end
end
end
end

Cart = Google::Protobuf::DescriptorPool.generated_pool.lookup("MyCart").msgclass
RUBY

rbi_output = rbi_for(:Cart)

assert_includes(rbi_output, indented(<<~RBI, 2))
sig { returns(T.nilable(Symbol)) }
def contact_info; end
RBI
end
end
end
end
Expand Down