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 6332ca0
Show file tree
Hide file tree
Showing 2 changed files with 58 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.has_presence?
klass.create_method(
"has_#{field.name}?",
return_type: "Object",
)
end

field
end

Expand Down
51 changes: 51 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(Object) }
def has_customer_id?; end
sig { returns(Object) }
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(Object) }
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(Object) }
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(Object) }
def has_value_type?; end
sig { returns(T.any(Symbol, Integer)) }
def value_type; end
Expand Down Expand Up @@ -253,6 +268,36 @@ 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(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 +445,12 @@ def clear_ShopID; end
sig { void }
def clear_ShopName; end
sig { returns(Object) }
def has_ShopID?; end
sig { returns(Object) }
def has_ShopName?; end
end
RBI

Expand Down

0 comments on commit 6332ca0

Please sign in to comment.