Class: Protobuf::Generators::MessageGenerator

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

Constant Summary

Constants included from Printable

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

Instance Attribute Summary

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(descriptor, indent_level, options = {}) ⇒ MessageGenerator

Returns a new instance of MessageGenerator.



8
9
10
11
12
# File 'lib/protobuf/generators/message_generator.rb', line 8

def initialize(descriptor, indent_level, options = {})
  super
  @only_declarations = options.fetch(:declaration) { false }
  @extension_fields = options.fetch(:extension_fields) { {} }
end

Instance Method Details

#compileObject



14
15
16
17
18
19
20
21
22
# File 'lib/protobuf/generators/message_generator.rb', line 14

def compile
  run_once(:compile) do
    if @only_declarations
      compile_declaration
    else
      compile_message
    end
  end
end

#compile_declarationObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/protobuf/generators/message_generator.rb', line 24

def compile_declaration
  run_once(:compile_declaration) do
    if printable?
      print_class(descriptor.name, :message) do
        group = GroupGenerator.new(current_indent)
        group.add_enums(descriptor.enum_type, :namespace => type_namespace)
        group.add_message_declarations(descriptor.nested_type)
        print group.to_s
      end
    else
      print_class(descriptor.name, :message)
    end
  end
end

#compile_messageObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/protobuf/generators/message_generator.rb', line 39

def compile_message
  run_once(:compile_message) do
    if printable?
      print_class(descriptor.name, nil) do
        group = GroupGenerator.new(current_indent)
        group.add_messages(descriptor.nested_type, :extension_fields => @extension_fields, :namespace => type_namespace)
        group.add_comment(:options, 'Message Options')
        group.add_options(descriptor.options) if options?
        group.add_message_fields(descriptor.field, descriptor)
        self.class.validate_tags(fully_qualified_type_namespace, descriptor.field.map(&:number))

        group.add_comment(:extension_range, 'Extension Fields')
        group.add_extension_ranges(descriptor.extension_range) do |extension_range|
          "extensions #{extension_range.start}...#{extension_range.end}"
        end

        group.add_extension_fields(message_extension_fields)

        group.order = [:message, :options, :field, :extension_range, :extension_field]
        print group.to_s
      end
    end
  end
end