protobuf 2.8.0.beta9 → 2.8.0

Sign up to get free protection for your applications and to get access to all the features.
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,99 @@
1
+ require 'protobuf/generators/base'
2
+ require 'protobuf/generators/group_generator'
3
+
4
+ module Protobuf
5
+ module Generators
6
+ class MessageGenerator < Base
7
+
8
+ def initialize(descriptor, indent_level, options = {})
9
+ super
10
+ @only_declarations = options.fetch(:declaration) { false }
11
+ @extension_fields = options.fetch(:extension_fields) { {} }
12
+ end
13
+
14
+ def compile
15
+ run_once(:compile) do
16
+ if @only_declarations
17
+ compile_declaration
18
+ else
19
+ compile_message
20
+ end
21
+ end
22
+ end
23
+
24
+ def compile_declaration
25
+ run_once(:compile_declaration) do
26
+ if is_printable?
27
+ print_class(descriptor.name, :message) do
28
+ group = GroupGenerator.new(current_indent)
29
+ group.add_enums(descriptor.enum_type, :namespace => type_namespace)
30
+ group.add_message_declarations(descriptor.nested_type)
31
+ print group.to_s
32
+ end
33
+ else
34
+ print_class(descriptor.name, :message)
35
+ end
36
+ end
37
+ end
38
+
39
+ def compile_message
40
+ run_once(:compile_message) do
41
+ if is_printable?
42
+ print_class(descriptor.name, nil) do
43
+ group = GroupGenerator.new(current_indent)
44
+ group.add_messages(descriptor.nested_type, :extension_fields => @extension_fields, :namespace => type_namespace)
45
+ group.add_message_fields(descriptor.field)
46
+ self.class.validate_tags(fully_qualified_type_namespace, descriptor.field.map(&:number))
47
+
48
+ group.add_comment(:extension_range, 'Extension Fields')
49
+ group.add_extension_ranges(descriptor.extension_range) do |extension_range|
50
+ "extensions #{extension_range.start}...#{extension_range.end}"
51
+ end
52
+
53
+ group.add_extension_fields(message_extension_fields)
54
+
55
+ group.order = [ :message, :field, :extension_range, :extension_field ]
56
+ print group.to_s
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ private
63
+
64
+ def has_extensions?
65
+ ! message_extension_fields.empty?
66
+ end
67
+
68
+ def has_fields?
69
+ descriptor.field.count > 0
70
+ end
71
+
72
+ def has_nested_enums?
73
+ descriptor.enum_type.count > 0
74
+ end
75
+
76
+ def has_nested_messages?
77
+ descriptor.nested_type.count > 0
78
+ end
79
+
80
+ def has_nested_types?
81
+ has_nested_enums? || has_nested_messages?
82
+ end
83
+
84
+ def is_printable?
85
+ if @only_declarations
86
+ has_nested_types?
87
+ else
88
+ has_fields? || has_nested_messages? || has_extensions?
89
+ end
90
+ end
91
+
92
+ def message_extension_fields
93
+ @extension_fields.fetch(fully_qualified_type_namespace) { [] }
94
+ end
95
+
96
+ end
97
+ end
98
+ end
99
+
@@ -0,0 +1,161 @@
1
+ module Protobuf
2
+ module Generators
3
+ module Printable
4
+
5
+ PARENT_CLASS_MESSAGE = "::Protobuf::Message".freeze
6
+ PARENT_CLASS_ENUM = "::Protobuf::Enum".freeze
7
+ PARENT_CLASS_SERVICE = "::Protobuf::Rpc::Service".freeze
8
+
9
+ # Initialize the printer.
10
+ # Must be called by any class/module that includes the Printable module.
11
+ #
12
+ def init_printer(indent_level)
13
+ @io = ::StringIO.new
14
+ @_indent_level = indent_level.to_i || 0
15
+ end
16
+
17
+ private
18
+
19
+ # Print a one-line comment.
20
+ #
21
+ def comment(message)
22
+ puts "# #{message}"
23
+ end
24
+
25
+ def current_indent
26
+ @_indent_level
27
+ end
28
+
29
+ # Print a "header" comment.
30
+ #
31
+ # header("Lorem ipsum dolor")
32
+ # ##
33
+ # # Lorem ipsum dolor
34
+ # #
35
+ def header(message)
36
+ puts
37
+ puts "##"
38
+ puts "# #{message}"
39
+ puts "#"
40
+ end
41
+
42
+ # Increase the indent level. An outdent will only occur if given a block
43
+ # (after the block is finished).
44
+ #
45
+ def indent
46
+ @_indent_level += 1
47
+ yield
48
+ outdent
49
+ end
50
+
51
+ # Take a string and upcase the first character of each namespace.
52
+ # Due to the nature of varying standards about how class/modules are named
53
+ # (e.g. CamelCase, Underscore_Case, SCREAMING_SNAKE_CASE), we only want
54
+ # to capitalize the first character to ensure ruby will treat the value
55
+ # as a constant. Otherwise we do not attempt to change the
56
+ # token's definition.
57
+ #
58
+ # modulize("foo.bar.Baz") -> "::Foo::Bar::Baz"
59
+ # modulize("foo.bar.baz") -> "::Foo::Bar::Baz"
60
+ # modulize("foo.bar.BAZ") -> "::Foo::Bar::BAZ"
61
+ #
62
+ def modulize(name)
63
+ name = name.gsub(/\./, '::')
64
+ name = name.gsub(/(^(?:::)?[a-z]|::[a-z])/) { |match| match.upcase }
65
+ name
66
+ end
67
+
68
+ # Decrease the indent level. Cannot be negative.
69
+ #
70
+ def outdent
71
+ @_indent_level -= 1 unless @_indent_level == 0
72
+ end
73
+
74
+ # Return the parent class for a given type.
75
+ # Valid types are :message, :enum, and :service, otherwise an error
76
+ # will be thrown.
77
+ #
78
+ def parent_class(type)
79
+ case type
80
+ when :message then
81
+ PARENT_CLASS_MESSAGE
82
+ when :enum then
83
+ PARENT_CLASS_ENUM
84
+ when :service then
85
+ PARENT_CLASS_SERVICE
86
+ else
87
+ raise "Unknown parent class type #{type}: #{caller[0..5].join("\n")}"
88
+ end
89
+ end
90
+
91
+ # Print a class or module block, indicated by type.
92
+ # If a class, can be given a parent class to inherit from.
93
+ # If a block is given, call the block from within an indent block.
94
+ # Otherwise, end the block on the same line.
95
+ #
96
+ def print_block(name, parent_klass, type, &block)
97
+ name = modulize(name)
98
+ block_def = "#{type} #{name}"
99
+ block_def += " < #{parent_class(parent_klass)}" if parent_klass
100
+
101
+ if block_given?
102
+ puts block_def
103
+ indent { block.call }
104
+ puts "end"
105
+ puts
106
+ else
107
+ block_def += "; end"
108
+ puts block_def
109
+ end
110
+ end
111
+
112
+ # Use print_block to print a class, with optional parent class
113
+ # to inherit from. Accepts a block for use with print_block.
114
+ #
115
+ def print_class(name, parent_klass, &block)
116
+ print_block(name, parent_klass, :class, &block)
117
+ end
118
+
119
+ # Use print_block to print a module.
120
+ # Accepts a block for use with print_block.
121
+ #
122
+ def print_module(name, &block)
123
+ print_block(name, nil, :module, &block)
124
+ end
125
+
126
+ # Print a file require.
127
+ #
128
+ # print_require('foo/bar/baz') -> "require 'foo/bar/baz'"
129
+ #
130
+ def print_require(file)
131
+ puts "require '#{file}'"
132
+ end
133
+
134
+ # Puts the given message prefixed by the indent level.
135
+ # If no message is given print a newline.
136
+ #
137
+ def puts(message = nil)
138
+ if message
139
+ @io.puts((" " * @_indent_level) + message)
140
+ else
141
+ @io.puts
142
+ end
143
+ end
144
+
145
+ # Print the given message raw, no indent.
146
+ #
147
+ def print(contents)
148
+ @io.print(contents)
149
+ end
150
+
151
+ # Returns the contents of the underlying StringIO object.
152
+ #
153
+ def print_contents
154
+ @io.rewind
155
+ @io.read
156
+ end
157
+
158
+ end
159
+ end
160
+ end
161
+
@@ -0,0 +1,27 @@
1
+ require 'protobuf/generators/base'
2
+
3
+ module Protobuf
4
+ module Generators
5
+ class ServiceGenerator < Base
6
+
7
+ def compile
8
+ run_once(:compile) do
9
+ print_class(descriptor.name, :service) do
10
+ descriptor.method.each do |method_descriptor|
11
+ puts build_method(method_descriptor)
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ def build_method(method_descriptor)
18
+ name = method_descriptor.name
19
+ request_klass = modulize(method_descriptor.input_type)
20
+ response_klass = modulize(method_descriptor.output_type)
21
+ return "rpc :#{name.underscore}, #{request_klass}, #{response_klass}"
22
+ end
23
+
24
+ end
25
+ end
26
+ end
27
+
@@ -9,16 +9,20 @@ module Protobuf
9
9
  lifecycle_events[ event_name ] << blk
10
10
  end
11
11
 
12
- def self.trigger( event_name )
12
+ def self.trigger( event_name, *args )
13
13
  event_name = normalized_event_name( event_name )
14
14
 
15
15
  if lifecycle_events.has_key?( event_name )
16
16
  lifecycle_events[ event_name ].each do |block|
17
- block.call
17
+ if ! args.empty? && block.arity != 0
18
+ block.call(*args)
19
+ else
20
+ block.call
21
+ end
18
22
  end
19
23
  end
20
24
  end
21
-
25
+
22
26
  def self.normalized_event_name( event_name )
23
27
  return "#{event_name}".downcase
24
28
  end
@@ -1,3 +1,4 @@
1
+ require 'stringio'
1
2
  require 'set'
2
3
  require 'protobuf/field'
3
4
  require 'protobuf/enum'
@@ -22,6 +23,10 @@ module Protobuf
22
23
  end
23
24
  end
24
25
 
26
+ def self.decode(bytes)
27
+ self.new.decode(bytes)
28
+ end
29
+
25
30
  # Define a field. Don't use this method directly.
26
31
  def self.define_field(rule, type, fname, tag, options)
27
32
  field_array = options[:extension] ? extension_fields : fields
@@ -46,6 +51,11 @@ module Protobuf
46
51
  end
47
52
  end
48
53
 
54
+ # Create a new object with the given values and return the encoded bytes.
55
+ def self.encode(values = {})
56
+ self.new(values).encode
57
+ end
58
+
49
59
  # Reserve field numbers for extensions. Don't use this method directly.
50
60
  def self.extensions(range)
51
61
  extension_fields.add_range(range)
@@ -92,7 +102,7 @@ module Protobuf
92
102
  # Find a field object by +tag+ number.
93
103
  def self.get_field_by_tag(tag)
94
104
  fields[tag]
95
- rescue TypeError => e
105
+ rescue TypeError
96
106
  tag = tag.nil? ? 'nil' : tag.to_s
97
107
  raise FieldNotDefinedError.new("Tag '#{tag}' does not reference a message field for '#{self.name}'")
98
108
  end
@@ -144,6 +154,16 @@ module Protobuf
144
154
  copy_to(super, :clone)
145
155
  end
146
156
 
157
+ # Decode the given string bytes into this object.
158
+ def decode(string)
159
+ decode_from(::StringIO.new(string))
160
+ end
161
+
162
+ # Decode the given stream into this object.
163
+ def decode_from(stream)
164
+ Decoder.decode(stream, self)
165
+ end
166
+
147
167
  def dup
148
168
  copy_to(super, :dup)
149
169
  end
@@ -174,6 +194,28 @@ module Protobuf
174
194
  end
175
195
  end
176
196
 
197
+ def encode
198
+ stream = ""
199
+
200
+ each_field_for_serialization do |field, value|
201
+ if field.repeated?
202
+ if field.packed?
203
+ key = (field.tag << 3) | ::Protobuf::WireType::LENGTH_DELIMITED
204
+ packed_value = value.map { |val| field.encode(val) }.join
205
+ stream << ::Protobuf::Field::VarintField.encode(key)
206
+ stream << ::Protobuf::Field::VarintField.encode(packed_value.size)
207
+ stream << packed_value
208
+ else
209
+ value.each { |val| write_pair(stream, field, val) }
210
+ end
211
+ else
212
+ write_pair(stream, field, value)
213
+ end
214
+ end
215
+
216
+ return stream
217
+ end
218
+
177
219
  # Returns extension fields. See Message#fields method.
178
220
  def extension_fields
179
221
  self.class.extension_fields
@@ -209,14 +251,6 @@ module Protobuf
209
251
  to_hash.inspect
210
252
  end
211
253
 
212
- def parse_from(stream)
213
- Decoder.decode(stream, self)
214
- end
215
-
216
- def parse_from_string(string)
217
- parse_from(StringIO.new(string))
218
- end
219
-
220
254
  def respond_to_has?(key)
221
255
  self.respond_to?(key) && self.has_field?(key)
222
256
  end
@@ -226,28 +260,6 @@ module Protobuf
226
260
  (self.__send__(key).present? || [true, false].include?(self.__send__(key)))
227
261
  end
228
262
 
229
- def serialize_to_string
230
- stream = ""
231
-
232
- each_field_for_serialization do |field, value|
233
- if field.repeated?
234
- if field.packed?
235
- key = (field.tag << 3) | ::Protobuf::WireType::LENGTH_DELIMITED
236
- packed_value = value.map { |val| field.encode(val) }.join
237
- stream << ::Protobuf::Field::VarintField.encode(key)
238
- stream << ::Protobuf::Field::VarintField.encode(packed_value.size)
239
- stream << packed_value
240
- else
241
- value.each { |val| write_pair(stream, field, val) }
242
- end
243
- else
244
- write_pair(stream, field, value)
245
- end
246
- end
247
-
248
- return stream
249
- end
250
-
251
263
  def set_field(tag, bytes)
252
264
  field = (get_field_by_tag(tag) || get_ext_field_by_tag(tag))
253
265
  field.set(self, bytes) if field
@@ -297,11 +309,16 @@ module Protobuf
297
309
  ##
298
310
  # Instance Aliases
299
311
  #
312
+ alias_method :parse_from_string, :decode
313
+ alias_method :deserialize, :decode
314
+ alias_method :parse_from, :decode_from
315
+ alias_method :deserialize_from, :decode_from
316
+ alias_method :to_s, :encode
317
+ alias_method :bytes, :encode
318
+ alias_method :serialize, :encode
319
+ alias_method :serialize_to_string, :encode
300
320
  alias_method :to_hash_value, :to_hash
301
321
  alias_method :to_proto_hash, :to_hash
302
- alias_method :to_s, :serialize_to_string
303
- alias_method :bytes, :serialize_to_string
304
- alias_method :serialize, :serialize_to_string
305
322
  alias_method :responds_to_has?, :respond_to_has?
306
323
  alias_method :respond_to_and_has?, :respond_to_has?
307
324
  alias_method :responds_to_and_has?, :respond_to_has?