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

Inherits:
Tapioca::Dsl::Compiler show all
Extended by:
T::Sig
Defined in:
lib/tapioca/dsl/compilers/protobuf.rb

Overview

‘Tapioca::Dsl::Compilers::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 compiler 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 ~~~

Defined Under Namespace

Classes: Field

Constant Summary collapse

ConstantType =
type_member(fixed: Module)

Constants included from Runtime::Reflection

Runtime::Reflection::ANCESTORS_METHOD, Runtime::Reflection::CLASS_METHOD, Runtime::Reflection::CONSTANTS_METHOD, Runtime::Reflection::EQUAL_METHOD, Runtime::Reflection::METHOD_METHOD, Runtime::Reflection::NAME_METHOD, Runtime::Reflection::OBJECT_ID_METHOD, Runtime::Reflection::PRIVATE_INSTANCE_METHODS_METHOD, Runtime::Reflection::PROTECTED_INSTANCE_METHODS_METHOD, Runtime::Reflection::PUBLIC_INSTANCE_METHODS_METHOD, Runtime::Reflection::SINGLETON_CLASS_METHOD, Runtime::Reflection::SUPERCLASS_METHOD

Instance Attribute Summary

Attributes inherited from Tapioca::Dsl::Compiler

#constant, #root

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Tapioca::Dsl::Compiler

#add_error, #compiler_enabled?, handles?, #initialize, processable_constants

Methods included from T::Generic::TypeStoragePatch

#[], #type_member, #type_template

Methods included from Runtime::Reflection

#ancestors_of, #are_equal?, #class_of, #constantize, #constants_of, #descendants_of, #inherited_ancestors_of, #method_of, #name_of, #name_of_type, #object_id_of, #private_instance_methods_of, #protected_instance_methods_of, #public_instance_methods_of, #qualified_name_of, #signature_of, #singleton_class_of, #superclass_of

Methods included from Helpers::ParamHelper

#create_block_param, #create_kw_opt_param, #create_kw_param, #create_kw_rest_param, #create_opt_param, #create_param, #create_rest_param, #create_typed_param

Constructor Details

This class inherits a constructor from Tapioca::Dsl::Compiler

Class Method Details

.gather_constantsObject



97
98
99
100
101
# File 'lib/tapioca/dsl/compilers/protobuf.rb', line 97

def self.gather_constants
  marker = Google::Protobuf::MessageExts::ClassMethods
  results = T.cast(ObjectSpace.each_object(marker).to_a, T::Array[Module])
  results.any? ? results + [Google::Protobuf::RepeatedField, Google::Protobuf::Map] : []
end

Instance Method Details

#decorateObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/tapioca/dsl/compilers/protobuf.rb', line 76

def decorate
  root.create_path(constant) do |klass|
    if constant == Google::Protobuf::RepeatedField
      create_type_members(klass, "Elem")
    elsif constant == Google::Protobuf::Map
      create_type_members(klass, "Key", "Value")
    else
      descriptor = T.let(T.unsafe(constant).descriptor, Google::Protobuf::Descriptor)
      fields = descriptor.map { |desc| create_descriptor_method(klass, desc) }
      fields.sort_by!(&:name)

      parameters = fields.map do |field|
        create_kw_opt_param(field.name, type: field.init_type, default: field.default)
      end

      klass.create_method("initialize", parameters: parameters, return_type: "void")
    end
  end
end