Skip to content

Commit

Permalink
Include the 'has_FIELD?' methods in RBIs generated from protobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
tgwizard committed Mar 22, 2024
1 parent ba2951e commit 1d596b5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/tapioca/dsl/compilers/protobuf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,13 @@ def create_descriptor_method(klass, desc)
return_type: "void",
)

if desc.label == :optional
klass.create_method(
"has_#{field.name}?",
return_type: "T.any(Integer, T::Boolean)",
)
end

field
end

Expand Down
54 changes: 54 additions & 0 deletions spec/tapioca/dsl/compilers/protobuf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ def customer_id; end
sig { params(value: Integer).void }
def customer_id=(value); end
sig { returns(T.any(Integer, T::Boolean)) }
def has_customer_id?; end
sig { returns(T.any(Integer, T::Boolean)) }
def has_shop_id?; end
sig { returns(Integer) }
def shop_id; end
Expand Down Expand Up @@ -111,6 +117,9 @@ def events; end
sig { params(value: String).void }
def events=(value); end
sig { returns(T.any(Integer, T::Boolean)) }
def has_events?; end
end
RBI

Expand Down Expand Up @@ -145,6 +154,9 @@ def cart_item_index=(value); end
sig { void }
def clear_cart_item_index; end
sig { returns(T.any(Integer, T::Boolean)) }
def has_cart_item_index?; end
end
RBI

Expand Down Expand Up @@ -179,6 +191,9 @@ def _value_type; end
sig { void }
def clear_value_type; end
sig { returns(T.any(Integer, T::Boolean)) }
def has_value_type?; end
sig { returns(T.any(Symbol, Integer)) }
def value_type; end
Expand Down Expand Up @@ -253,6 +268,39 @@ def indices=(value); end
assert_equal(expected, rbi_for(:Cart))
end

it "generates methods in RBI files for classes with Protobuf with non-optional integer field type" do
add_proto_file("cart", <<~PROTO)
syntax = "proto3";
message Cart {
int32 shop_id = 1;
}
PROTO

expected = <<~RBI
# typed: strong
class Cart
sig { params(shop_id: T.nilable(Integer)).void }
def initialize(shop_id: nil); end
sig { void }
def clear_shop_id; end
sig { returns(T.any(Integer, T::Boolean)) }
def has_shop_id?; end
sig { returns(Integer) }
def shop_id; end
sig { params(value: Integer).void }
def shop_id=(value); end
end
RBI

assert_equal(expected, rbi_for(:Cart))
end

it "generates methods in RBI files for map fields in Protobufs" do
add_proto_file("cart", <<~PROTO)
syntax = "proto3";
Expand Down Expand Up @@ -400,6 +448,12 @@ def clear_ShopID; end
sig { void }
def clear_ShopName; end
sig { returns(T.any(Integer, T::Boolean)) }
def has_ShopID?; end
sig { returns(T.any(Integer, T::Boolean)) }
def has_ShopName?; end
end
RBI

Expand Down

0 comments on commit 1d596b5

Please sign in to comment.