Class: Tapioca::Compilers::Dsl::Protobuf

Inherits:
Base
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/tapioca/compilers/dsl/protobuf.rb

Overview

‘Tapioca::Compilers::Dsl::Protobuf` decorates RBI files for subclasses of [`Google::Protobuf::MessageExts`](github.com/protocolbuffers/protobuf/tree/master/ruby).

For example, with the following “cart.rb” file:

~~~rb Google::Protobuf::DescriptorPool.generated_pool.build do

add_file("cart.proto", :syntax => :proto3) do
  add_message "MyCart" do
    optional :shop_id, :int32, 1
    optional :customer_id, :int64, 2
    optional :number_value, :double, 3
    optional :string_value, :string, 4
  end
end

end ~~~

this generator will produce the RBI file ‘cart.rbi` with the following content:

~~~rbi # cart.rbi # typed: strong class Cart

sig { returns(Integer) }
def customer_id; end

sig { params(month: Integer).returns(Integer) }
def customer_id=(value); end

sig { returns(Integer) }
def shop_id; end

sig { params(value: Integer).returns(Integer) }
def shop_id=(value); end

sig { returns(String) }
def string_value; end

sig { params(value: String).returns(String) }
def string_value=(value); end

sig { returns(Float) }
def number_value; end

sig { params(value: Float).returns(Float) }
def number_value=(value); end

end ~~~

Instance Attribute Summary

Attributes inherited from Base

#processable_constants

Instance Method Summary collapse

Methods inherited from Base

#handles?, #initialize

Constructor Details

This class inherits a constructor from Tapioca::Compilers::Dsl::Base

Instance Method Details

#decorate(root, constant) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/tapioca/compilers/dsl/protobuf.rb', line 73

def decorate(root, constant)
  descriptor = T.let(T.unsafe(constant).descriptor, Google::Protobuf::Descriptor)
  return unless descriptor.any?

  root.path(constant) do |klass|
    descriptor.each do |desc|
      create_descriptor_method(klass, desc)
    end
  end
end

#gather_constantsObject



85
86
87
88
# File 'lib/tapioca/compilers/dsl/protobuf.rb', line 85

def gather_constants
  classes = T.cast(ObjectSpace.each_object(Class), T::Enumerable[Class])
  classes.select { |c| c < Google::Protobuf::MessageExts && !c.singleton_class? }
end