class RBSProtobuf::Translator::Base

Constants

FieldDescriptorProto

Attributes

input[R]

Public Class Methods

new(input) click to toggle source
# File lib/rbs_protobuf/translator/base.rb, line 8
def initialize(input)
  @input = input
end

Public Instance Methods

base_type(type) click to toggle source
# File lib/rbs_protobuf/translator/base.rb, line 65
def base_type(type)
  case type
  when FieldDescriptorProto::Type::TYPE_STRING,
    FieldDescriptorProto::Type::TYPE_BYTES
    RBS::BuiltinNames::String.instance_type
  when FieldDescriptorProto::Type::TYPE_INT32, FieldDescriptorProto::Type::TYPE_INT64,
    FieldDescriptorProto::Type::TYPE_UINT32, FieldDescriptorProto::Type::TYPE_UINT64,
    FieldDescriptorProto::Type::TYPE_FIXED32, FieldDescriptorProto::Type::TYPE_FIXED64,
    FieldDescriptorProto::Type::TYPE_SINT32, FieldDescriptorProto::Type::TYPE_SINT64,
    FieldDescriptorProto::Type::TYPE_SFIXED32, FieldDescriptorProto::Type::TYPE_SFIXED64
    RBS::BuiltinNames::Integer.instance_type
  when FieldDescriptorProto::Type::TYPE_DOUBLE, FieldDescriptorProto::Type::TYPE_FLOAT
    RBS::BuiltinNames::Float.instance_type
  when FieldDescriptorProto::Type::TYPE_BOOL
    factory.union_type(factory.literal_type(true),
                       factory.literal_type(false))
  else
    raise "Unknown base type: #{type}"
  end
end
comment_for_path(source_code_info, path) click to toggle source
# File lib/rbs_protobuf/translator/base.rb, line 45
def comment_for_path(source_code_info, path)
  loc = source_code_info.location.find {|loc| loc.path == path }
  if loc
    comments = []
    if loc.leading_comments.length > 0
      comments << loc.leading_comments.strip
    end
    if loc.trailing_comments.length > 0
      comments << loc.trailing_comments.strip
    end
    if comments.empty? && !loc.leading_detached_comments.empty?
      comments << loc.leading_detached_comments.join("\n\n").strip
    end
    RBS::AST::Comment.new(
      location: nil,
      string: comments.join("\n\n")
    )
  end
end
factory() click to toggle source
# File lib/rbs_protobuf/translator/base.rb, line 12
def factory
  @factory ||= RBSFactory.new()
end
generate_rbs!() click to toggle source
# File lib/rbs_protobuf/translator/base.rb, line 20
def generate_rbs!
  input.proto_file.each do |file|
    response.file << Google::Protobuf::Compiler::CodeGeneratorResponse::File.new(
      name: rbs_name(file.name),
      content: rbs_content(file)
    )
  end
end
message_type(string) click to toggle source
# File lib/rbs_protobuf/translator/base.rb, line 86
def message_type(string)
  absolute = string.start_with?(".")

  *path, name = string.delete_prefix(".").split(".").map {|s| ActiveSupport::Inflector.upcase_first(s).to_sym }

  factory.instance_type(
    RBS::TypeName.new(
      name: name,
      namespace: RBS::Namespace.new(path: path, absolute: absolute)
    )
  )
end
rbs_content(file) click to toggle source
# File lib/rbs_protobuf/translator/base.rb, line 41
def rbs_content(file)
  raise NotImplementedError
end
rbs_name(proto_name) click to toggle source
# File lib/rbs_protobuf/translator/base.rb, line 29
def rbs_name(proto_name)
  dirname = File.dirname(proto_name)
  basename = File.basename(proto_name, File.extname(proto_name))
  rbs_name = "#{basename}#{rbs_suffix}.rbs"

  File.join(dirname, rbs_name)
end
rbs_suffix() click to toggle source
# File lib/rbs_protobuf/translator/base.rb, line 37
def rbs_suffix
  ""
end
response() click to toggle source
# File lib/rbs_protobuf/translator/base.rb, line 16
def response
  @response ||= Google::Protobuf::Compiler::CodeGeneratorResponse.new
end