Skip to content

Commit

Permalink
Do not call has_presence? on Protobuf::FieldDescriptor unless met…
Browse files Browse the repository at this point in the history
…hod exists

`FieldDescriptor#has_presence?` was defined in `google-protobuf` version 3.26,
so any application using an older version would have run into an error when
compiling Protobuf DSLs.

Co-authored-by: Ufuk Kayserilioglu <[email protected]>
  • Loading branch information
egiurleo and paracycle committed Mar 26, 2024
1 parent 265bea2 commit b5d08d9
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/tapioca/dsl/compilers/protobuf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,18 @@ def gather_constants

private

sig { params(desc: Google::Protobuf::FieldDescriptor).returns(T::Boolean) }
def has_presence?(desc)
if desc.respond_to?(:has_presence?)
# This method is only defined in google-protobuf 3.26.0 and later
desc.has_presence?
else
# In older versions of the gem, the only way we can get this information is
# by checking if an instance of the class responds to the expected method
T.unsafe(constant.allocate).respond_to?("has_#{desc.name}?")
end
end

sig { params(klass: RBI::Scope, names: String).void }
def create_type_members(klass, *names)
klass.create_extend("T::Generic")
Expand Down Expand Up @@ -302,7 +314,7 @@ def create_descriptor_method(klass, desc)
return_type: "void",
)

if desc.has_presence?
if has_presence?(desc)
klass.create_method(
"has_#{field.name}?",
return_type: "Object",
Expand Down

0 comments on commit b5d08d9

Please sign in to comment.