protobuf 2.8.0.beta9 → 2.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (204) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +0 -1
  3. data/{UPGRADING.md → CHANGES.md} +20 -12
  4. data/LICENSE.txt +14 -0
  5. data/README.md +60 -74
  6. data/bin/protoc-gen-ruby +26 -0
  7. data/bin/rprotoc +4 -58
  8. data/lib/protobuf.rb +15 -13
  9. data/lib/protobuf/code_generator.rb +41 -0
  10. data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +51 -0
  11. data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +248 -0
  12. data/lib/protobuf/field/base_field.rb +0 -1
  13. data/lib/protobuf/field/bytes_field.rb +1 -1
  14. data/lib/protobuf/field/message_field.rb +2 -4
  15. data/lib/protobuf/field/varint_field.rb +4 -7
  16. data/lib/protobuf/generators/base.rb +71 -0
  17. data/lib/protobuf/generators/enum_generator.rb +31 -0
  18. data/lib/protobuf/generators/extension_generator.rb +28 -0
  19. data/lib/protobuf/generators/field_generator.rb +132 -0
  20. data/lib/protobuf/generators/file_generator.rb +140 -0
  21. data/lib/protobuf/generators/group_generator.rb +113 -0
  22. data/lib/protobuf/generators/message_generator.rb +99 -0
  23. data/lib/protobuf/generators/printable.rb +161 -0
  24. data/lib/protobuf/generators/service_generator.rb +27 -0
  25. data/lib/protobuf/lifecycle.rb +7 -3
  26. data/lib/protobuf/message.rb +51 -34
  27. data/lib/protobuf/rpc/connectors/common.rb +4 -6
  28. data/lib/protobuf/rpc/server.rb +1 -1
  29. data/lib/protobuf/rpc/servers/zmq/server.rb +2 -2
  30. data/lib/protobuf/rpc/servers/zmq/worker.rb +2 -2
  31. data/lib/protobuf/rpc/service.rb +3 -3
  32. data/lib/protobuf/rpc/service_directory.rb +24 -12
  33. data/lib/protobuf/version.rb +1 -1
  34. data/proto/google/protobuf/compiler/plugin.proto +147 -0
  35. data/proto/google/protobuf/descriptor.proto +620 -0
  36. data/protobuf.gemspec +12 -21
  37. data/spec/bin/protoc-gen-ruby_spec.rb +18 -0
  38. data/spec/data/data.bin +3 -0
  39. data/{test/data/unk.png → spec/data/types.bin} +0 -0
  40. data/spec/encoding/all_types_spec.rb +91 -0
  41. data/spec/encoding/extreme_values_spec.rb +0 -0
  42. data/spec/lib/protobuf/code_generator_spec.rb +60 -0
  43. data/spec/lib/protobuf/generators/base_spec.rb +87 -0
  44. data/spec/lib/protobuf/generators/enum_generator_spec.rb +45 -0
  45. data/spec/lib/protobuf/generators/extension_generator_spec.rb +43 -0
  46. data/spec/lib/protobuf/generators/field_generator_spec.rb +99 -0
  47. data/spec/lib/protobuf/generators/file_generator_spec.rb +29 -0
  48. data/spec/lib/protobuf/generators/message_generator_spec.rb +0 -0
  49. data/spec/lib/protobuf/generators/service_generator_spec.rb +43 -0
  50. data/spec/lib/protobuf/lifecycle_spec.rb +31 -1
  51. data/spec/lib/protobuf/logger_spec.rb +5 -0
  52. data/spec/lib/protobuf/message_spec.rb +22 -8
  53. data/spec/lib/protobuf/rpc/connectors/common_spec.rb +1 -1
  54. data/spec/lib/protobuf/rpc/service_directory_spec.rb +45 -7
  55. data/spec/lib/protobuf/rpc/service_spec.rb +3 -3
  56. data/spec/spec_helper.rb +15 -16
  57. data/spec/support/test/all_types.data.bin +0 -0
  58. data/spec/support/test/all_types.data.txt +119 -0
  59. data/spec/support/test/defaults.pb.rb +25 -0
  60. data/spec/support/test/defaults.proto +9 -0
  61. data/spec/support/test/enum.pb.rb +10 -8
  62. data/spec/support/test/extended.pb.rb +5 -3
  63. data/spec/support/test/extreme_values.data.bin +0 -0
  64. data/spec/support/test/google_unittest.pb.rb +543 -0
  65. data/spec/support/test/google_unittest.proto +713 -0
  66. data/spec/support/test/google_unittest_import.pb.rb +37 -0
  67. data/{ext/protobuf-2.4.1/src/google/protobuf/io/package_info.h → spec/support/test/google_unittest_import.proto} +27 -17
  68. data/spec/support/test/google_unittest_import_public.pb.rb +8 -0
  69. data/{ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream_inl.h → spec/support/test/google_unittest_import_public.proto} +5 -31
  70. data/spec/support/test/multi_field_extensions.pb.rb +29 -7
  71. data/spec/support/test/multi_field_extensions.proto +12 -2
  72. data/spec/support/test/resource.pb.rb +20 -16
  73. metadata +149 -236
  74. data/examples/addressbook.pb.rb +0 -55
  75. data/examples/addressbook.proto +0 -24
  76. data/examples/reading_a_message.rb +0 -32
  77. data/examples/writing_a_message.rb +0 -46
  78. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/code_generator.h +0 -142
  79. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/command_line_interface.h +0 -318
  80. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum.h +0 -99
  81. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum_field.h +0 -103
  82. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_extension.h +0 -85
  83. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_field.h +0 -167
  84. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_file.h +0 -98
  85. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_generator.h +0 -72
  86. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_helpers.h +0 -159
  87. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message.h +0 -170
  88. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message_field.h +0 -102
  89. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_primitive_field.h +0 -103
  90. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_service.h +0 -118
  91. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_string_field.h +0 -104
  92. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h +0 -2721
  93. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/importer.h +0 -303
  94. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum.h +0 -84
  95. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum_field.h +0 -121
  96. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_extension.h +0 -77
  97. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_field.h +0 -108
  98. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_file.h +0 -101
  99. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_generator.h +0 -72
  100. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_helpers.h +0 -213
  101. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message.h +0 -109
  102. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message_field.h +0 -134
  103. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_primitive_field.h +0 -121
  104. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_service.h +0 -113
  105. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_string_field.h +0 -120
  106. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/mock_code_generator.h +0 -113
  107. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/package_info.h +0 -64
  108. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/parser.h +0 -434
  109. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.h +0 -73
  110. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.pb.h +0 -790
  111. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/python/python_generator.h +0 -156
  112. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/subprocess.h +0 -108
  113. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/zip_writer.h +0 -93
  114. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.h +0 -1367
  115. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.pb.h +0 -5223
  116. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor_database.h +0 -366
  117. data/ext/protobuf-2.4.1/src/google/protobuf/dynamic_message.h +0 -136
  118. data/ext/protobuf-2.4.1/src/google/protobuf/extension_set.h +0 -904
  119. data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_reflection.h +0 -424
  120. data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_util.h +0 -82
  121. data/ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream.h +0 -1102
  122. data/ext/protobuf-2.4.1/src/google/protobuf/io/gzip_stream.h +0 -207
  123. data/ext/protobuf-2.4.1/src/google/protobuf/io/printer.h +0 -136
  124. data/ext/protobuf-2.4.1/src/google/protobuf/io/tokenizer.h +0 -313
  125. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream.h +0 -238
  126. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl.h +0 -357
  127. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl_lite.h +0 -340
  128. data/ext/protobuf-2.4.1/src/google/protobuf/message.h +0 -692
  129. data/ext/protobuf-2.4.1/src/google/protobuf/message_lite.h +0 -239
  130. data/ext/protobuf-2.4.1/src/google/protobuf/package_info.h +0 -64
  131. data/ext/protobuf-2.4.1/src/google/protobuf/reflection_ops.h +0 -80
  132. data/ext/protobuf-2.4.1/src/google/protobuf/repeated_field.h +0 -1295
  133. data/ext/protobuf-2.4.1/src/google/protobuf/service.h +0 -291
  134. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/common.h +0 -1211
  135. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/hash.h +0 -220
  136. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/map-util.h +0 -119
  137. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/once.h +0 -123
  138. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/stl_util-inl.h +0 -121
  139. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/strutil.h +0 -457
  140. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/substitute.h +0 -170
  141. data/ext/protobuf-2.4.1/src/google/protobuf/test_util.h +0 -174
  142. data/ext/protobuf-2.4.1/src/google/protobuf/test_util_lite.h +0 -101
  143. data/ext/protobuf-2.4.1/src/google/protobuf/testing/file.h +0 -83
  144. data/ext/protobuf-2.4.1/src/google/protobuf/testing/googletest.h +0 -98
  145. data/ext/protobuf-2.4.1/src/google/protobuf/text_format.h +0 -285
  146. data/ext/protobuf-2.4.1/src/google/protobuf/unittest.pb.h +0 -11915
  147. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_custom_options.pb.h +0 -2895
  148. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_embed_optimize_for.pb.h +0 -211
  149. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_empty.pb.h +0 -56
  150. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import.pb.h +0 -188
  151. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import_lite.pb.h +0 -151
  152. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite.pb.h +0 -4752
  153. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite_imports_nonlite.pb.h +0 -150
  154. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_mset.pb.h +0 -816
  155. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_no_generic_services.pb.h +0 -197
  156. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_optimize_for.pb.h +0 -403
  157. data/ext/protobuf-2.4.1/src/google/protobuf/unknown_field_set.h +0 -268
  158. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format.h +0 -304
  159. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite.h +0 -620
  160. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite_inl.h +0 -774
  161. data/ext/ruby_generator/Makefile +0 -10
  162. data/ext/ruby_generator/RubyGenerator.cpp +0 -544
  163. data/ext/ruby_generator/RubyGenerator.h +0 -206
  164. data/ext/ruby_generator/extconf.rb +0 -35
  165. data/test/data/data_source.py +0 -14
  166. data/test/data/types_source.py +0 -22
  167. data/test/proto/addressbook.pb.rb +0 -66
  168. data/test/proto/addressbook.proto +0 -33
  169. data/test/proto/addressbook_base.pb.rb +0 -58
  170. data/test/proto/addressbook_base.proto +0 -26
  171. data/test/proto/addressbook_ext.pb.rb +0 -20
  172. data/test/proto/addressbook_ext.proto +0 -6
  173. data/test/proto/collision.pb.rb +0 -17
  174. data/test/proto/collision.proto +0 -5
  175. data/test/proto/ext_collision.pb.rb +0 -24
  176. data/test/proto/ext_collision.proto +0 -8
  177. data/test/proto/ext_range.pb.rb +0 -22
  178. data/test/proto/ext_range.proto +0 -7
  179. data/test/proto/float_default.proto +0 -10
  180. data/test/proto/lowercase.pb.rb +0 -30
  181. data/test/proto/lowercase.proto +0 -9
  182. data/test/proto/merge.pb.rb +0 -39
  183. data/test/proto/merge.proto +0 -15
  184. data/test/proto/nested.pb.rb +0 -30
  185. data/test/proto/nested.proto +0 -9
  186. data/test/proto/optional_field.pb.rb +0 -35
  187. data/test/proto/optional_field.proto +0 -12
  188. data/test/proto/packed.pb.rb +0 -22
  189. data/test/proto/packed.proto +0 -6
  190. data/test/proto/rpc.proto +0 -6
  191. data/test/proto/types.pb.rb +0 -84
  192. data/test/proto/types.proto +0 -37
  193. data/test/test_addressbook.rb +0 -56
  194. data/test/test_enum_value.rb +0 -41
  195. data/test/test_extension.rb +0 -36
  196. data/test/test_lowercase.rb +0 -11
  197. data/test/test_message.rb +0 -128
  198. data/test/test_optional_field.rb +0 -103
  199. data/test/test_packed_field.rb +0 -40
  200. data/test/test_parse.rb +0 -15
  201. data/test/test_repeated_types.rb +0 -132
  202. data/test/test_serialize.rb +0 -61
  203. data/test/test_standard_message.rb +0 -96
  204. data/test/test_types.rb +0 -226
@@ -0,0 +1,31 @@
1
+ require 'protobuf/generators/base'
2
+
3
+ module Protobuf
4
+ module Generators
5
+ class EnumGenerator < Base
6
+
7
+ def compile
8
+ run_once(:compile) do
9
+ tags = []
10
+
11
+ print_class(descriptor.name, :enum) do
12
+ descriptor.value.each do |enum_value_descriptor|
13
+ tags << enum_value_descriptor.number
14
+ puts build_value(enum_value_descriptor)
15
+ end
16
+ end
17
+
18
+ self.class.validate_tags(fully_qualified_type_namespace, tags)
19
+ end
20
+ end
21
+
22
+ def build_value(enum_value_descriptor)
23
+ name = enum_value_descriptor.name
24
+ number = enum_value_descriptor.number
25
+ return "define :#{name}, #{number}"
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+
@@ -0,0 +1,28 @@
1
+ require 'protobuf/generators/base'
2
+ require 'protobuf/generators/group_generator'
3
+
4
+ module Protobuf
5
+ module Generators
6
+ class ExtensionGenerator < Base
7
+
8
+ def initialize(message_type, field_descriptors, indent_level)
9
+ super(nil, indent_level)
10
+ @message_type = modulize(message_type)
11
+ @field_descriptors = field_descriptors
12
+ end
13
+
14
+ def compile
15
+ run_once(:compile) do
16
+ print_class(@message_type, :message) do
17
+ group = GroupGenerator.new(current_indent)
18
+ group.add_extension_fields(@field_descriptors)
19
+ group.order = [ :extension_field ]
20
+ print group.to_s
21
+ end
22
+ end
23
+ end
24
+
25
+ end
26
+ end
27
+ end
28
+
@@ -0,0 +1,132 @@
1
+ require 'protobuf/generators/base'
2
+
3
+ module Protobuf
4
+ module Generators
5
+ class FieldGenerator < Base
6
+
7
+ ##
8
+ # Constants
9
+ #
10
+ PROTO_INFINITY_DEFAULT = /^inf$/i.freeze
11
+ PROTO_NEGATIVE_INFINITY_DEFAULT = /^-inf$/i.freeze
12
+ PROTO_NAN_DEFAULT = /^nan$/i.freeze
13
+ RUBY_INFINITY_DEFAULT = '::Float::INFINITY'.freeze
14
+ RUBY_NEGATIVE_INFINITY_DEFAULT = '-::Float::INFINITY'.freeze
15
+ RUBY_NAN_DEFAULT = '::Float::NAN'.freeze
16
+
17
+ ##
18
+ # Attributes
19
+ #
20
+ attr_reader :field_options
21
+
22
+ def applicable_options
23
+ @applicable_options ||= field_options.map { |k, v| ":#{k} => #{v}" }
24
+ end
25
+
26
+ def default_value
27
+ @default_value ||= begin
28
+ if defaulted?
29
+ case descriptor.type.name
30
+ when :TYPE_ENUM then
31
+ enum_default_value
32
+ when :TYPE_STRING, :TYPE_BYTES then
33
+ string_default_value
34
+ when :TYPE_FLOAT, :TYPE_DOUBLE then
35
+ float_double_default_value
36
+ else
37
+ verbatim_default_value
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ def defaulted?
44
+ descriptor.respond_to_has_and_present?(:default_value)
45
+ end
46
+
47
+ def deprecated?
48
+ descriptor.options.try(:deprecated?) { false }
49
+ end
50
+
51
+ def extension?
52
+ descriptor.respond_to_has_and_present?(:extendee)
53
+ end
54
+
55
+ def compile
56
+ run_once(:compile) do
57
+ field_definition = [ "#{label} #{type_name}", name, number, applicable_options ]
58
+ puts field_definition.flatten.compact.join(', ')
59
+ end
60
+ end
61
+
62
+ def label
63
+ @label ||= descriptor.label.name.to_s.downcase.sub(/label_/, '') # required, optional, repeated
64
+ end
65
+
66
+ def name
67
+ @name ||= ":#{descriptor.name}"
68
+ end
69
+
70
+ def number
71
+ @number ||= descriptor.number
72
+ end
73
+
74
+ def field_options
75
+ @field_options ||= begin
76
+ opts = {}
77
+ opts[:default] = default_value if defaulted?
78
+ opts[:packed] = 'true' if packed?
79
+ opts[:deprecated] = 'true' if deprecated?
80
+ opts[:extension] = 'true' if extension?
81
+ opts
82
+ end
83
+ end
84
+
85
+ def packed?
86
+ descriptor.options.try(:packed?) { false }
87
+ end
88
+
89
+ # Determine the field type
90
+ def type_name
91
+ @type_name ||= begin
92
+ case descriptor.type.name
93
+ when :TYPE_MESSAGE, :TYPE_ENUM, :TYPE_GROUP then
94
+ type_name = modulize(descriptor.type_name)
95
+ else
96
+ type_name = descriptor.type.name.to_s.downcase.sub(/type_/, '')
97
+ type_name = "::Protobuf::Field::#{modulize(type_name)}Field"
98
+ end
99
+ end
100
+ end
101
+
102
+ private
103
+
104
+ def enum_default_value
105
+ "#{type_name}::#{verbatim_default_value}"
106
+ end
107
+
108
+ def float_double_default_value
109
+ case verbatim_default_value
110
+ when PROTO_INFINITY_DEFAULT then
111
+ RUBY_INFINITY_DEFAULT
112
+ when PROTO_NEGATIVE_INFINITY_DEFAULT then
113
+ RUBY_NEGATIVE_INFINITY_DEFAULT
114
+ when PROTO_NAN_DEFAULT then
115
+ RUBY_NAN_DEFAULT
116
+ else
117
+ verbatim_default_value
118
+ end
119
+ end
120
+
121
+ def string_default_value
122
+ %Q{"#{verbatim_default_value.gsub(/'/, '\\\\\'')}"}
123
+ end
124
+
125
+ def verbatim_default_value
126
+ descriptor.default_value
127
+ end
128
+
129
+ end
130
+ end
131
+ end
132
+
@@ -0,0 +1,140 @@
1
+ require 'set'
2
+ require 'protobuf/generators/base'
3
+ require 'protobuf/generators/group_generator'
4
+
5
+ module Protobuf
6
+ module Generators
7
+ class FileGenerator < Base
8
+
9
+ attr_reader :output_file
10
+
11
+ def initialize(*args)
12
+ super
13
+ @output_file = ::Google::Protobuf::Compiler::CodeGeneratorResponse::File.new(:name => file_name)
14
+ @extension_fields = Hash.new { |h, k| h[k] = [] }
15
+ @known_messages = []
16
+ @dangling_messages = {}
17
+ end
18
+
19
+ def file_name
20
+ convert_filename(descriptor.name, false)
21
+ end
22
+
23
+ def compile
24
+ run_once(:compile) do
25
+ map_extensions(descriptor, [ descriptor.package ])
26
+ extract_dangling_extensions
27
+
28
+ print_file_comment
29
+ print_generic_requires
30
+ print_import_requires
31
+
32
+ print_package do
33
+ group = GroupGenerator.new(current_indent)
34
+ group.add_enums(descriptor.enum_type, :namespace => [ descriptor.package ])
35
+ group.add_message_declarations(descriptor.message_type)
36
+ group.add_messages(descriptor.message_type, :extension_fields => @extension_fields, :namespace => [ descriptor.package ])
37
+ group.add_extended_messages(@unknown_extensions)
38
+ group.add_services(descriptor.service)
39
+
40
+ group.add_header(:enum, 'Enum Classes')
41
+ group.add_header(:message_declaration, 'Message Classes')
42
+ group.add_header(:message, 'Message Fields')
43
+ group.add_header(:extended_message, 'Extended Message Fields')
44
+ group.add_header(:service, 'Service Classes')
45
+ print group.to_s
46
+ end
47
+
48
+ end
49
+ end
50
+
51
+ def extract_dangling_extensions
52
+ @unknown_extensions = @extension_fields.select do |k, v|
53
+ ! @known_messages.include?(k)
54
+ end
55
+ end
56
+
57
+ def generate_output_file
58
+ compile
59
+ output_file.content = to_s
60
+ output_file
61
+ end
62
+
63
+ # Recursively map out all extensions known in this file.
64
+ # The key is the type_name of the message being extended, and
65
+ # the value is an array of field descriptors.
66
+ #
67
+ def map_extensions(descriptor, namespaces)
68
+ # Record all the message descriptor name's we encounter (should be the whole tree).
69
+ if descriptor.is_a?(::Google::Protobuf::DescriptorProto)
70
+ if fully_qualified_token?(descriptor.name)
71
+ @known_messages << descriptor.name
72
+ else
73
+ fully_qualified_namespace = ".#{namespaces.join('.')}"
74
+ @known_messages << fully_qualified_namespace
75
+ end
76
+ end
77
+
78
+ descriptor.extension.each do |field_descriptor|
79
+ @extension_fields[field_descriptor.extendee] << field_descriptor
80
+ end
81
+
82
+ if descriptor.respond_to_has_and_present?(:message_type)
83
+ descriptor.message_type.each do |message_descriptor|
84
+ map_extensions(message_descriptor, (namespaces + [ message_descriptor.name ]))
85
+ end
86
+ end
87
+
88
+ if descriptor.respond_to_has_and_present?(:nested_type)
89
+ descriptor.nested_type.each do |nested_descriptor|
90
+ map_extensions(nested_descriptor, (namespaces + [ nested_descriptor.name ]))
91
+ end
92
+ end
93
+ end
94
+
95
+ def print_file_comment
96
+ puts "##"
97
+ puts "# This file is auto-generated. DO NOT EDIT!"
98
+ puts "#"
99
+ end
100
+
101
+ def print_generic_requires
102
+ print_require("protobuf/message")
103
+ print_require("protobuf/rpc/service") if descriptor.service.count > 0
104
+ puts
105
+ end
106
+
107
+ def print_import_requires
108
+ if descriptor.dependency.count > 0
109
+ header "Imports"
110
+
111
+ descriptor.dependency.each do |dependency|
112
+ print_require(convert_filename(dependency))
113
+ end
114
+
115
+ puts
116
+ end
117
+ end
118
+
119
+ def print_package(&block)
120
+ final = lambda { block.call }
121
+ namespaces = descriptor.package.split('.')
122
+ namespaces.reverse.inject(final) { |previous, namespace|
123
+ lambda { print_module(namespace, &previous) }
124
+ }.call
125
+ end
126
+
127
+ private
128
+
129
+ def convert_filename(filename, for_require = true)
130
+ filename.sub(/\.proto/, (for_require ? '.pb' : '.pb.rb'))
131
+ end
132
+
133
+ def fully_qualified_token?(token)
134
+ token[0] == '.'
135
+ end
136
+
137
+ end
138
+ end
139
+ end
140
+
@@ -0,0 +1,113 @@
1
+ require 'protobuf/generators/enum_generator'
2
+ require 'protobuf/generators/extension_generator'
3
+ require 'protobuf/generators/field_generator'
4
+ require 'protobuf/generators/message_generator'
5
+ require 'protobuf/generators/service_generator'
6
+
7
+ module Protobuf
8
+ module Generators
9
+ class GroupGenerator
10
+ include ::Protobuf::Generators::Printable
11
+
12
+ attr_reader :groups, :indent_level
13
+ attr_writer :order
14
+
15
+ def initialize(indent_level = 0)
16
+ @groups = Hash.new { |h, k| h[k] = [] }
17
+ @headers = {}
18
+ @comments = {}
19
+ @handlers = {}
20
+ @indent_level = indent_level
21
+ @order = [ :enum, :message_declaration, :message, :extended_message, :service ]
22
+ init_printer(indent_level)
23
+ end
24
+
25
+ def add_enums(enum_descriptors, options)
26
+ enum_descriptors.each do |enum_descriptor|
27
+ @groups[:enum] << EnumGenerator.new(enum_descriptor, indent_level, options)
28
+ end
29
+ end
30
+
31
+ def add_comment(type, message)
32
+ @comments[type] = message
33
+ end
34
+
35
+ def add_extended_messages(extended_messages)
36
+ extended_messages.each do |message_type, field_descriptors|
37
+ @groups[:extended_message] << ExtensionGenerator.new(message_type, field_descriptors, indent_level)
38
+ end
39
+ end
40
+
41
+ def add_extension_fields(field_descriptors)
42
+ field_descriptors.each do |field_descriptor|
43
+ @groups[:extension_field] << FieldGenerator.new(field_descriptor, indent_level)
44
+ end
45
+ end
46
+
47
+ def add_extension_ranges(extension_ranges, &item_handler)
48
+ @groups[:extension_range] = extension_ranges
49
+ @handlers[:extension_range] = item_handler
50
+ end
51
+
52
+ def add_header(type, message)
53
+ @headers[type] = message
54
+ end
55
+
56
+ def add_message_declarations(descriptors)
57
+ descriptors.each do |descriptor|
58
+ @groups[:message_declaration] << MessageGenerator.new(descriptor, indent_level, :declaration => true)
59
+ end
60
+ end
61
+
62
+ def add_message_fields(field_descriptors)
63
+ field_descriptors.each do |field_descriptor|
64
+ @groups[:field] << FieldGenerator.new(field_descriptor, indent_level)
65
+ end
66
+ end
67
+
68
+ def add_messages(descriptors, options = {})
69
+ descriptors.each do |descriptor|
70
+ @groups[:message] << MessageGenerator.new(descriptor, indent_level, options)
71
+ end
72
+ end
73
+
74
+ def add_services(service_descriptors)
75
+ service_descriptors.each do |service_descriptor|
76
+ @groups[:service] << ServiceGenerator.new(service_descriptor, indent_level)
77
+ end
78
+ end
79
+
80
+ def compile
81
+ @order.each do |type|
82
+ items = @groups[type]
83
+ if items.count > 0
84
+ item_handler = @handlers[type]
85
+
86
+ item_header = @headers[type]
87
+ header(item_header) if item_header
88
+
89
+ item_comment = @comments[type]
90
+ comment(item_comment) if item_comment
91
+
92
+ items.each do |item|
93
+ if item_handler
94
+ puts item_handler.call(item)
95
+ else
96
+ print item.to_s
97
+ end
98
+ end
99
+
100
+ puts if type == :message_declaration
101
+ end
102
+ end
103
+ end
104
+
105
+ def to_s
106
+ compile
107
+ print_contents
108
+ end
109
+
110
+ end
111
+ end
112
+ end
113
+