Class: Protobuf::Generators::FileGenerator

Inherits:
Base
  • Object
show all
Defined in:
lib/protobuf/generators/file_generator.rb

Constant Summary

Constants included from Printable

Printable::PARENT_CLASS_ENUM, Printable::PARENT_CLASS_MESSAGE, Printable::PARENT_CLASS_SERVICE

Instance Attribute Summary collapse

Attributes inherited from Base

#descriptor, #namespace, #options

Instance Method Summary collapse

Methods inherited from Base

#fully_qualified_type_namespace, #run_once, #serialize_value, #to_s, #type_namespace, validate_tags

Methods included from Printable

#init_printer

Constructor Details

#initialize(*args) ⇒ FileGenerator

Returns a new instance of FileGenerator.



11
12
13
14
15
16
17
18
# File 'lib/protobuf/generators/file_generator.rb', line 11

def initialize(*args)
  super
  @output_file = ::Google::Protobuf::Compiler::CodeGeneratorResponse::File.new(:name => file_name)
  @extension_fields = Hash.new { |h, k| h[k] = [] }
  @known_messages = {}
  @known_enums = {}
  @dangling_messages = {}
end

Instance Attribute Details

#output_fileObject (readonly)

Returns the value of attribute output_file.



9
10
11
# File 'lib/protobuf/generators/file_generator.rb', line 9

def output_file
  @output_file
end

Instance Method Details

#compileObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/protobuf/generators/file_generator.rb', line 24

def compile
  run_once(:compile) do
    map_extensions(descriptor, [descriptor.package])

    print_file_comment
    print_generic_requires
    print_import_requires

    print_package do
      inject_optionable
      group = GroupGenerator.new(current_indent)
      group.add_options(descriptor.options) if descriptor.options
      group.add_enums(descriptor.enum_type, :namespace => [descriptor.package])
      group.add_message_declarations(descriptor.message_type)
      group.add_messages(descriptor.message_type, :extension_fields => @extension_fields, :namespace => [descriptor.package])
      group.add_extended_messages(unknown_extensions)
      group.add_services(descriptor.service)

      group.add_header(:enum, 'Enum Classes')
      group.add_header(:message_declaration, 'Message Classes')
      group.add_header(:options, 'File Options')
      group.add_header(:message, 'Message Fields')
      group.add_header(:extended_message, 'Extended Message Fields')
      group.add_header(:service, 'Service Classes')
      print group.to_s
    end

  end
end

#eval_unknown_extensions!Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/protobuf/generators/file_generator.rb', line 144

def eval_unknown_extensions!
  @@evaled_dependencies ||= Set.new # rubocop:disable Style/ClassVars
  @@all_messages ||= {} # rubocop:disable Style/ClassVars
  @@all_enums ||= {} # rubocop:disable Style/ClassVars

  map_extensions(descriptor, [descriptor.package])
  @known_messages.each do |name, descriptor|
    @@all_messages[name] = descriptor
  end
  @known_enums.each do |name, descriptor|
    @@all_enums[name] = descriptor
  end

  # create package namespace
  print_package {}
  eval_code

  unknown_extensions.each do |extendee, fields|
    eval_dependencies(extendee)
    fields.each do |field|
      eval_dependencies(field.type_name)
    end
  end
  group = GroupGenerator.new(0)
  group.add_extended_messages(unknown_extensions, false)
  print group.to_s
  eval_code
rescue => e
  warn "Error loading unknown extensions #{descriptor.name.inspect} error=#{e}"
  raise e
end

#file_nameObject



20
21
22
# File 'lib/protobuf/generators/file_generator.rb', line 20

def file_name
  convert_filename(descriptor.name, false)
end

#generate_output_fileObject



68
69
70
71
72
# File 'lib/protobuf/generators/file_generator.rb', line 68

def generate_output_file
  compile
  output_file.content = to_s
  output_file
end

#map_extensions(descriptor, namespaces) ⇒ Object

Recursively map out all extensions known in this file. The key is the type_name of the message being extended, and the value is an array of field descriptors.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/protobuf/generators/file_generator.rb', line 78

def map_extensions(descriptor, namespaces)
  if fully_qualified_token?(descriptor.name)
    fully_qualified_namespace = descriptor.name
  elsif !(namespace = namespaces.reject(&:empty?).join(".")).empty?
    fully_qualified_namespace = ".#{namespace}"
  end
  # Record all the message descriptor name's we encounter (should be the whole tree).
  if descriptor.is_a?(::Google::Protobuf::DescriptorProto)
    @known_messages[fully_qualified_namespace || descriptor.name] = descriptor
  elsif descriptor.is_a?(::Google::Protobuf::EnumDescriptorProto)
    @known_enums[fully_qualified_namespace || descriptor.name] = descriptor
    return
  end

  descriptor.extension.each do |field_descriptor|
    unless fully_qualified_token?(field_descriptor.name) && fully_qualified_namespace
      field_descriptor.name = "#{fully_qualified_namespace}.#{field_descriptor.name}"
    end
    @extension_fields[field_descriptor.extendee] << field_descriptor
  end

  [:message_type, :nested_type, :enum_type].each do |type|
    next unless descriptor.respond_to_has_and_present?(type)

    descriptor.public_send(type).each do |type_descriptor|
      map_extensions(type_descriptor, (namespaces + [type_descriptor.name]))
    end
  end
end


108
109
110
111
112
113
114
# File 'lib/protobuf/generators/file_generator.rb', line 108

def print_file_comment
  puts "# encoding: utf-8"
  puts
  puts "##"
  puts "# This file is auto-generated. DO NOT EDIT!"
  puts "#"
end


116
117
118
119
120
# File 'lib/protobuf/generators/file_generator.rb', line 116

def print_generic_requires
  print_require("protobuf")
  print_require("protobuf/rpc/service") if descriptor.service.count > 0
  puts
end


122
123
124
125
126
127
128
129
130
131
132
# File 'lib/protobuf/generators/file_generator.rb', line 122

def print_import_requires
  return if descriptor.dependency.empty?

  header "Imports"

  descriptor.dependency.each do |dependency|
    print_require(convert_filename(dependency), ENV.key?('PB_REQUIRE_RELATIVE'))
  end

  puts
end


134
135
136
137
138
139
140
141
142
# File 'lib/protobuf/generators/file_generator.rb', line 134

def print_package(&block)
  namespaces = descriptor.package.split('.')
  if namespaces.empty? && ENV.key?('PB_ALLOW_DEFAULT_PACKAGE_NAME')
    namespaces = [File.basename(descriptor.name).sub('.proto', '')]
  end
  namespaces.reverse.reduce(block) do |previous, namespace|
    -> { print_module(namespace, &previous) }
  end.call
end

#unknown_extensionsObject



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/protobuf/generators/file_generator.rb', line 54

def unknown_extensions
  @unknown_extensions ||= @extension_fields.map do |message_name, fields|
    message_klass = modulize(message_name).safe_constantize
    if message_klass
      unknown_fields = fields.reject do |field|
        @known_messages[message_name] && message_klass.get_field(field.name, true)
      end
      [message_name, unknown_fields]
    else
      [message_name, fields]
    end
  end
end