Class: RBSProtobuf::Translator::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs_protobuf/translator/base.rb

Direct Known Subclasses

ProtobufGem

Constant Summary collapse

FieldDescriptorProto =
Google::Protobuf::FieldDescriptorProto

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, filters = []) ⇒ Base

Returns a new instance of Base.



10
11
12
13
# File 'lib/rbs_protobuf/translator/base.rb', line 10

def initialize(input, filters = [])
  @input = input
  @filters = filters
end

Instance Attribute Details

#filtersObject (readonly)

Returns the value of attribute filters.



6
7
8
# File 'lib/rbs_protobuf/translator/base.rb', line 6

def filters
  @filters
end

#inputObject (readonly)

Returns the value of attribute input.



6
7
8
# File 'lib/rbs_protobuf/translator/base.rb', line 6

def input
  @input
end

#rbs_concat_levelObject

Returns the value of attribute rbs_concat_level.



8
9
10
# File 'lib/rbs_protobuf/translator/base.rb', line 8

def rbs_concat_level
  @rbs_concat_level
end

Instance Method Details

#apply_filter(rbs_name, rbs_content, proto_files) ⇒ Object



15
16
17
18
19
# File 'lib/rbs_protobuf/translator/base.rb', line 15

def apply_filter(rbs_name, rbs_content, proto_files)
  filters.inject([rbs_name, rbs_content]) do |(rbs_name, rbs_content), filter| #$ [String, String]
    filter[rbs_name, rbs_content, proto_files] or return
  end
end

#base_type(type) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/rbs_protobuf/translator/base.rb', line 145

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.bool_type()
  else
    raise "Unknown base type: #{type}"
  end
end

#comment_for_path(source_code_info, path, options:) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rbs_protobuf/translator/base.rb', line 101

def comment_for_path(source_code_info, path, options:)
  loc = source_code_info.location.find {|loc| loc.path == path }

  comments = []

  if loc
    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
  end

  if options
    # @type var opts: Array[[Symbol, untyped]]
    opts = []
    options.each_field do |key, value|
      if options.field?(key.fully_qualified_name)
        opts << [key.fully_qualified_name, value]
      end
    end

    unless opts.empty?
      unless comments.empty?
        comments << "----"
      end
      comments << "Protobuf options:"
      list = opts.map {|key, value| "- `#{key} = #{value.inspect}`" }
      comments << list.join("\n")
    end
  end

  unless comments.empty?
    RBS::AST::Comment.new(
      location: nil,
      string: comments.join("\n\n") + "\n\n"
    )
  end
end

#factoryObject



21
22
23
# File 'lib/rbs_protobuf/translator/base.rb', line 21

def factory
  @factory ||= RBSFactory.new()
end

#format_rbs(dirs: [], decls: []) ⇒ Object



79
80
81
82
83
# File 'lib/rbs_protobuf/translator/base.rb', line 79

def format_rbs(dirs: [], decls: [])
  StringIO.new.tap do |io|
    RBS::Writer.new(out: io).write(dirs + decls)
  end.string
end

#generate_rbs!Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rbs_protobuf/translator/base.rb', line 29

def generate_rbs!
  rbs_decls = {} #: Hash[Pathname, [Array[RBS::AST::Declarations::t], untyped]]

  input.proto_file.each do |file|
    path = Pathname(file.name).sub_ext(".rbs")
    decls = rbs_content(file)
    rbs_decls[path] = [decls, file]
  end

  rbs_contents = {} #: Hash[Pathname, [String, Array[untyped]]]

  if (level = rbs_concat_level)&.nonzero?
    groups = rbs_decls.each_key.group_by do |path|
      path.sub_ext("").to_s.split(File::SEPARATOR).take(level).join(File::SEPARATOR)
    end

    groups.each do |prefix, paths|
      path = Pathname(prefix).sub_ext(".rbs")

      decls = [] #: Array[RBS::AST::Declarations::t]
      files = [] #: Array[untyped]

      paths.each do |path|
        ds, file = rbs_decls.fetch(path)
        decls.concat(ds)
        files.push(file)
      end

      rbs_contents[path] = [
        format_rbs(decls: decls),
        files
      ]
    end
  else
    rbs_decls.each do |path, (decls, file)|
      content = format_rbs(decls: decls)
      rbs_contents[path] = [content, [file]]
    end
  end

  rbs_contents.each do |path, (content, files)|
    if (name, content = apply_filter(path.to_s, content, files))
      response.file << Google::Protobuf::Compiler::CodeGeneratorResponse::File.new(
        name: name,
        content: content
      )
    end
  end
end

#message_type(string) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/rbs_protobuf/translator/base.rb', line 165

def message_type(string)
  absolute = string.start_with?(".")

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

  name or raise

  factory.instance_type(
    RBS::TypeName.new(
      name: name,
      namespace: RBS::Namespace.new(path: path, absolute: absolute)
    )
  )
end

#optional_type(type) ⇒ Object



180
181
182
183
184
185
186
187
# File 'lib/rbs_protobuf/translator/base.rb', line 180

def optional_type(type)
  case type
  when RBS::Types::Optional
    type
  else
    RBS::Types::Optional.new(type: type, location: nil)
  end
end

#rbs_content(file) ⇒ Object

Raises:

  • (NotImplementedError)


97
98
99
# File 'lib/rbs_protobuf/translator/base.rb', line 97

def rbs_content(file)
  raise NotImplementedError
end

#rbs_name(proto_name) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/rbs_protobuf/translator/base.rb', line 85

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_suffixObject



93
94
95
# File 'lib/rbs_protobuf/translator/base.rb', line 93

def rbs_suffix
  ""
end

#responseObject



25
26
27
# File 'lib/rbs_protobuf/translator/base.rb', line 25

def response
  @response ||= Google::Protobuf::Compiler::CodeGeneratorResponse.new(:supported_features => ::Google::Protobuf::Compiler::CodeGeneratorResponse::Feature::FEATURE_PROTO3_OPTIONAL.to_i)
end