Skip to content

Commit

Permalink
Merge pull request #929 from Shopify/add-support-for-protobuf-oneof-f…
Browse files Browse the repository at this point in the history
…ields

Add support for :oneof fields on protobuf messages
  • Loading branch information
paracycle committed May 12, 2022
2 parents f29c316 + 5bf728d commit c4263d7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
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

0 comments on commit c4263d7

Please sign in to comment.