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
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1793f24401f3a3def50cf704b6beb4bb27887efc
4
+ data.tar.gz: 6dbc2f1080b4d2a3f3c01cfa2248ea8945aec972
5
+ SHA512:
6
+ metadata.gz: 6107077595047bd1ce774463faf020075b3a3d1b494ce8a814dac9eab5d124b36a6eb6dfc7fd3f9010c0a54d9c7ea6bbc18d4c04181a07827814c3609dc4bd94
7
+ data.tar.gz: c2598cb57886e62c86fb77cccdbfc8629ca9c78c3a26eabb8c9cd675be58000cddbc7c71928d53a3e0245b34cccbc4670334f5ebd9869c2b94831f2cf914c68b
data/.gitignore CHANGED
@@ -9,7 +9,6 @@ doc
9
9
  .yardoc
10
10
  .DS_Store
11
11
  *.so
12
- *.bin
13
12
  Gemfile.lock
14
13
  tmp/*
15
14
  ext/defs
@@ -1,23 +1,31 @@
1
- # Upgrading from 1.X to 2.0
1
+ 2.8.0
2
+ -----------
2
3
 
3
- I'm sure I'm missing quite a few of the smaller changes that have been made, but here is a list of changes that should affect any external programs or applications using `protobuf`.
4
+ - New compiler supports protobuf compilation/runtime with protoc <= v2.5.0 (c++ compiler removed). [#109]
5
+ - Deprecated rprotoc in favor of protoc. [0bc9674]
6
+ - Added service dynamic discovery to the ZMQ connector and server. [#91, @devin-c]
7
+ - No longer creating `-java` platform gem due to removal of c++ compiler.
8
+ - Added WTFPL license.
4
9
 
5
- ## `rprotoc` changes
10
+ 2.0.0
11
+ -----------
12
+
13
+ #### `rprotoc` changes
6
14
 
7
15
  * New option `--ruby_out` to specify the output directory to place generated ruby files. If not provided, ruby code will not be generated.
8
16
  * Extends `libprotoc` to hook in directly to google's provided compiler mechanism.
9
17
  * Removed all previous compiler code including the racc parser, node visitors, etc.
10
18
  * See `protoc --help` for default options.
11
19
 
12
- ## `rprotoc` generated files changes
20
+ #### `rprotoc` generated files changes
13
21
 
14
22
  * Import `require`s now occur outside of any module or class declaration which solves ruby vm warnings previously seen.
15
23
  * Empty inherited Message and Enum classes are pre-defined in the file, then reopened and their fields applied. This solves the issue of recursive field dependencies of two or more types in the same file.
16
- * Generated DSL lines for message fields include the fully qualified name of the type (e.g. optional `::Protobuf::Field::StringField`, :name, 1)
24
+ * Generated DSL lines for message fields include the fully qualified name of the type (e.g. `optional ::Protobuf::Field::StringField, :name, 1`)
17
25
  * Support for any combination of `packed`, `deprecated`, and `default` as options to pass to a field definition.
18
26
  * Services are now generated in the corresponding `.pb.rb` file instead of their own `*_service.rb` files as before.
19
27
 
20
- ## `rpc_server` changes
28
+ #### `rpc_server` changes
21
29
 
22
30
  * Removed `--env` option. The running application or program is solely in charge of ensuring it's environment is properly loaded.
23
31
  * Removed reading of `PB_CLIENT_TYPE`, `PB_SERVER_TYPE` environment variables. Should use mode switches or custom requires (see below) instead.
@@ -31,30 +39,30 @@ I'm sure I'm missing quite a few of the smaller changes that have been made, but
31
39
  * Added `--print-deprecation-warnings` switch to tell the server whether or not to print deprecation warnings on field usage. Enabled by default.
32
40
  * See `rpc_server help start` for all options and usage. Note: the `start` task is the default and not necessary when running the `rpc_server`.
33
41
 
34
- ## Message changes
42
+ #### Message changes
35
43
 
36
44
  * `Message#get_field` usage should now specify either `Message#get_field_by_name` or `Message#get_field_by_tag`, depending on your lookup criteria.
37
45
  * Support for STDERR output when accessing a message field which has been defined as `[deprecated=true]`. Deprecated warnings can be skipped by running your application or program with `PB_IGNORE_DEPRECATIONS=1`.
38
46
  * Significant internal refactoring which provides huge boosts in speed and efficiency both in accessing/writing Message field values, as well as serialization and deserialization routines.
39
47
  * Refactor `Message#to_hash` to delegate hash representations to the field values, simply collecting the display values and returning a hash of fields that are set. This also affects `to_json` output.
40
48
 
41
- ## Enum changes
49
+ #### Enum changes
42
50
 
43
51
  * Add `Enum.fetch` class method to polymorphically retrieve an `EnumValue` object.
44
52
  * Add `Enum.value_by_name` to retrieve the corresponding `EnumValue` to the given symbol name.
45
53
  * Add `Enum.enum_by_value` to retrieve the corresponding `EnumValue` to the given integer value.
46
54
 
47
- ## RPC Service changes
55
+ #### RPC Service changes
48
56
 
49
57
  * `async_responder` paradigm is no longer supported.
50
58
  * `self.response=` paradigm should be converted to using `respond_with(object)`.
51
59
  * Significant internal changes that should not bleed beyond the API but which make maintaining the code much easier.
52
60
 
53
- ## RPC Client changes
61
+ #### RPC Client changes
54
62
 
55
63
  * In the absence of `PB_CLIENT_TYPE` environment var, you should be requiring the specific connector type specifically. For instance, if you wish to run in zmq mode for client requests, update your Gemfile: `gem 'protobuf', :require => 'protobuf/zmq'`.
56
64
  * `:async` option on client calls is no longer recognized.
57
65
 
58
- ## Other changes
66
+ #### Other changes
59
67
 
60
- * Moved files out of `lib/protobuf/common` folder into `lib/protobuf`. Files affected are logger, wire_type, util. The only update would need to be the require path to these files since the modules were always `Protobuf::{TYPE}`.
68
+ * Moved files out of `lib/protobuf/common` folder into `lib/protobuf`. Files affected are logger, wire\_type, util. The only update would need to be the require path to these files since the modules were always `Protobuf::{TYPE}`.
data/LICENSE.txt ADDED
@@ -0,0 +1,14 @@
1
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2
+ Version 2, December 2004
3
+
4
+ Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
5
+
6
+ Everyone is permitted to copy and distribute verbatim or modified
7
+ copies of this license document, and changing it is allowed as long
8
+ as the name is changed.
9
+
10
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12
+
13
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
14
+
data/README.md CHANGED
@@ -3,20 +3,17 @@
3
3
  [![Gem Version](https://badge.fury.io/rb/protobuf.png)](http://badge.fury.io/rb/protobuf)
4
4
  [![Build Status](https://secure.travis-ci.org/localshred/protobuf.png?branch=master)](https://travis-ci.org/localshred/protobuf)
5
5
 
6
- ___IMPORTANT: Those upgrading from version 1.4.2 to 2.X should read the
7
- [UPGRADING.md](https://github.com/localshred/protobuf/blob/master/UPGRADING.md) notes___
6
+ ___See [CHANGES.md](https://github.com/localshred/protobuf/blob/master/CHANGES.md) for up-to-date
7
+ API changes.___
8
8
 
9
- Protobuf is an implementation of [Google's protocol buffers][google-pb] in ruby.
10
- We currently support version 2.4.1 with support for the new 2.5 coming shortly after
11
- it becomes final.
9
+ Protobuf is an implementation of [Google's protocol buffers][google-pb] in ruby, version 2.5.0 is currently supported.
12
10
 
13
11
  ---
14
12
 
15
13
  ## Install
16
14
 
17
- You will likely need to install protobuf from your favorite package manager
18
- or from source. This gem currently supports protobuf 2.4.1. You may alternatively
19
- specify a `PROTOC_SRC` when installing with rubygems (see below).
15
+ You will need to install protobuf from your favorite package manager
16
+ or from source. This gem currently supports protobuf <= 2.5.0.
20
17
 
21
18
  ### OSX Install
22
19
 
@@ -31,21 +28,17 @@ $ sudo apt-get install -y protobuf
31
28
 
32
29
  ### Gem Install
33
30
 
34
- Once the protobuf package is installed, go ahead and install with rubygems.
35
- If you'd like to skip installing the protobuf package (above) and specify an
36
- alternate location for the protobuf package, for instance if you have a custom
37
- protoc package, then specify it with `PROTOC_SRC=/path/to/src`.
31
+ Once the protobuf package is installed, install this gem with rubygems/bundler.
38
32
  _Please note that this will void your warranty as it were. If you compiled with
39
33
  a custom protobuf package and are having issues it may be difficult to troubleshoot._
40
34
 
41
35
  ```shell
42
36
  $ gem install protobuf
43
-
44
- # Provide an alternative protoc source directory to build from.
45
- $ PROTOC_SRC=/path/to/protobuf/src gem install protobuf
46
37
  ```
47
38
 
48
- ## 1. Generating ruby classes from `.proto` files
39
+ ------
40
+
41
+ ## Generating ruby classes from `.proto` files
49
42
 
50
43
  Protocol Buffers are great because they allow you to clearly define data storage
51
44
  or data transfer packets. Google officially supports Java, C++, and Python for
@@ -53,24 +46,23 @@ compilation and usage. Let's make it ruby aware!
53
46
 
54
47
  Let's say you have a `defs.proto` file that defines a User message.
55
48
 
56
- ```
49
+ ```protobuf
57
50
  package foo;
58
51
  message User {
59
- required string first_name = 1;
60
- required string last_name = 2;
52
+ optional string first_name = 1;
53
+ optional string last_name = 2;
61
54
  }
62
55
  ```
63
56
 
64
57
  Now let's compile that definition to ruby:
65
58
 
66
- ```
67
- $ rprotoc defs.proto --ruby_out ./lib
59
+ ```shell
60
+ $ protoc defs.proto --ruby_out ./lib
68
61
  ```
69
62
 
70
63
  The previous line will take whatever is defined in `defs.proto` and
71
64
  output ruby classes to the `./lib` directory, obeying the package
72
- directive. Assuming that's all `defs.proto` had defined, `./lib`
73
- should now look like this:
65
+ directive. Your `./lib` should now look like this:
74
66
 
75
67
  ```
76
68
  - lib
@@ -85,18 +77,16 @@ module Foo
85
77
  class User < ::Protobuf::Message; end
86
78
 
87
79
  class User
88
- required :string, :first_name, 1
89
- required :string, :last_name, 2
80
+ optional ::Protobuf::Field::StringField, :first_name, 1
81
+ optional ::Protobuf::Field::StringField, :last_name, 2
90
82
  end
91
83
  end
92
84
  ```
93
85
 
94
- ___Note:__ The generator will pre-define all the classes empty and then
95
- re-open to apply the defined fields. This is an optimization to prevent
96
- recursive field errors._
86
+ __Note:__ The generator will pre-define all message/enum classes empty and then
87
+ re-open to apply the defined fields. This is to prevent field dependency errors.
97
88
 
98
- The generated class is now just a plain old ruby object.
99
- You can use it however you wish.
89
+ The generated class is now just a plain old ruby object. You can use it however you wish.
100
90
 
101
91
  ```ruby
102
92
  require 'lib/foo/user.pb'
@@ -129,25 +119,20 @@ inflated_user = Foo::User.new.parse_from_string(bytes)
129
119
  inflated_user == user #=> true
130
120
  ```
131
121
 
132
- ## 2. RPC
122
+ ## Callings Services with RPC
133
123
 
134
- RPC is one of many technologies that tries to solve the problem of getting
135
- smaller pieces of data from one place to another. Many will argue for or
136
- against RPC and its usefulness, but I'm not going to do that here. Google's
137
- Protocol Buffers provides support for Services with RPC and that's why you're here.
124
+ Google's Protocol Buffers provides support for Services with RPC.
138
125
 
139
- Any discussion about RPC leads to a discussion about clients and servers
140
- and the remote procedures themselves. For our purposes, we'll talk about
141
- a `Client` (process that is calling the server/service), a `Service`
142
- (the remote procedure), and a `Server` (the process that manages one or more
143
- services). We'll start with the Service first.
126
+ For our purposes, we'll talk about a `Client` (process that is calling
127
+ the server/service), a `Service` (the remote procedure), and a `Server`
128
+ (the process that manages one or more services). We'll start with the Service first.
144
129
 
145
130
  ### Services
146
131
 
147
132
  Services are simply classes that have endpoint methods defined. Here's what
148
133
  one looks like in protobuf:
149
134
 
150
- ```
135
+ ```protobuf
151
136
  package foo;
152
137
  message UserRequest {
153
138
  optional string email = 1;
@@ -177,14 +162,12 @@ __Important Note: The UserService class here is a *stub*. You should not
177
162
  provide your implementation in this generated file as subsequent generations
178
163
  will wipe out your implmentation. Read on to learn how to use this stub.__
179
164
 
180
- Did you read the note above? Go read it. I'll wait.
181
-
182
- Ok, now that you have a generated service stub, you'll want to require it
183
- from `lib` and implement the methods. Create a service implementation file
184
- in your project. In rails I'd put this in `app/services/user_service.rb`.
165
+ Now that you have a generated service stub, you'll want to require it
166
+ from `lib` and provide the implementation. Create a service implementation file
167
+ in your project. In rails I'd put this in `app/services/foo/user_service.rb`.
185
168
 
186
169
  ```ruby
187
- # app/services/user_service.rb
170
+ # app/services/foo/user_service.rb
188
171
  require 'lib/foo/user.pb'
189
172
 
190
173
  # Reopen the class and provide the implementation for each rpc method defined.
@@ -195,10 +178,12 @@ module Foo
195
178
  # response -> Foo::UserResponse
196
179
  def find
197
180
  # request.email will be the unpacked string that was sent by the client request
181
+ users = []
198
182
  User.find_by_email(request.email).each do |user|
199
- # must only use a proto instance of Foo::User when appending to the `users` field
200
- response.users << user.to_proto
183
+ users << user.to_proto
201
184
  end
185
+
186
+ respond_with(:users => users)
202
187
  end
203
188
 
204
189
  end
@@ -209,7 +194,7 @@ Simply implement the instance method for the defined rpc. You can provide
209
194
  any other methods in this class as helpers, but only those defined in the
210
195
  proto file will be callable by remote clients. Every request made by a client
211
196
  will provide a non-empty request of the defined type. The server creates a new
212
- service instance based on the request, so you should not be constrained to just
197
+ service instance for every request, so you should not be constrained to just
213
198
  the endpoint method. This is similar to rails controllers where only methods
214
199
  defined by the routes file are hooked up to HTTP requests, but it's very common
215
200
  to implement private methods to aid in code quality and simpilicity.
@@ -221,7 +206,7 @@ entirely. If you need to create your own response object (a valid case), simply
221
206
  `respond_with(new_response)`. The returned object should conform to one of three properties:
222
207
 
223
208
  1. Response should be of same type as defined by the rpc definition (in this case, `Foo::UserList`), or
224
- 2. Response should be a hash. This hash will be used to construct an instance of the defined type and should therefore conform to the appropriate fields for that type.
209
+ 2. Response should be a hash, respond to `to_hash`, or respond to `to_proto_hash`. The hash will be used to construct an instance of the defined type and should therefore conform to the appropriate fields for that type.
225
210
  3. Response should respond to the `to_proto` method. The object returned by `to_proto` should be an instance of the defined response type.
226
211
 
227
212
  If at any time the implementation encounters an error, the client can be
@@ -300,8 +285,8 @@ or `respond_with` just like you can in the endpoint methods.
300
285
 
301
286
  __Before Filters__ – Returning `false` from a before filter will cancel
302
287
  any other filter calls which would run afterwards, as well as canceling
303
- invocation of the service method. Note: You must actually return false,
304
- not just a "falsey" value such as nil.
288
+ invocation of the service method. _Note: You must actually return `false`,
289
+ not just a "falsey" value such as `nil`._
305
290
 
306
291
  __After Filters__ – No request shortcutting.
307
292
 
@@ -333,10 +318,8 @@ would have the filter applied.
333
318
 
334
319
  ### Servers
335
320
 
336
- A service is nothing without being hooked up to a socket. It's the
337
- nerdy kid waiting by the telephone for someone to call without knowing
338
- that the phone company disconnected their house. Sad and pathetic.
339
- So hook up the phone lines!
321
+ Services need to be hooked up to a socket to be called by clients.
322
+ Use the provided program `rpc_server`:
340
323
 
341
324
  ```
342
325
  $ rpc_server -o myserver.com -p 9399 -l ./log/protobuf.log ./config/environment.rb
@@ -363,7 +346,7 @@ result in shutting the server down gracefully.
363
346
 
364
347
  ```
365
348
  $ ps aux | grep rpc_server
366
- 1234 ... rpc_server myservice.com:9399
349
+ 1234 ... rpc_server myserver.com:9399
367
350
 
368
351
  $ kill -QUIT 1234
369
352
  rpc_server shutdown
@@ -371,8 +354,11 @@ rpc_server shutdown
371
354
 
372
355
  ### Clients
373
356
 
374
- A lot of work has gone into making the client calls simple and easy
375
- to use yet still powerful. Clients have a DSL that feels very ajaxy.
357
+ Calling a service with a clients feels very ajaxy. If you prefer
358
+ a simpler syntax I highly recommend using [ActiveRemote](https://github.com/liveh2o/active_remote)
359
+ which was developed in conjunction with this library to make
360
+ interacting with a remote service feel identical to using an
361
+ ActiveRecord model. Seriously, it's wicked cool.
376
362
 
377
363
  ```ruby
378
364
  # require the defs from the shared gem/repo
@@ -384,18 +370,18 @@ req = Foo::UserRequest.new(:email => 'jeff@gmail.com')
384
370
  # Use the UserService class to generate a client, invoke the rpc method
385
371
  # while passing the request object.
386
372
  # We could also simply pass a hash to find.
387
- Foo::UserService.client.find(req) do |c|
373
+ Foo::UserService.client.find(req) do |client|
388
374
  # This block will be executed (registering the callbacks)
389
375
  # before the request actualy occurs.
390
- # the `c` param in this block is the `.client` object
391
- # that is generated from the call above
376
+ # the `client` param in this block is the object
377
+ # that is created by `Foo::UserService.client`.
392
378
 
393
379
  # Register a block for execution when the response
394
380
  # is deemed successful from the service. Accepts
395
- # the unpacked response as its only parameter
381
+ # the unpacked response as its only parameter.
396
382
  c.on_success do |response|
397
- response.users.each do |u|
398
- puts u.inspect
383
+ response.users.each do |user|
384
+ puts user.inspect
399
385
  end
400
386
  end
401
387
 
@@ -404,8 +390,8 @@ Foo::UserService.client.find(req) do |c|
404
390
  # or server-side failure. The object passed to the
405
391
  # block has a `message` and a `code` attribute
406
392
  # to aid in logging/diagnosing the failure.
407
- c.on_failure do |err|
408
- puts 'It failed: ' + err.message
393
+ c.on_failure do |error|
394
+ puts 'It failed: ' + error.message
409
395
  end
410
396
  end
411
397
  ```
@@ -414,9 +400,9 @@ Many different options can be passed to the `.client` call above
414
400
  (such as `:timeout => 600`). See the `lib/protobuf/rpc/client.rb`
415
401
  and `lib/protobuf/rpc/service.rb` files for more documentation.
416
402
 
417
- ### Dynamic Discovery (ZMQ Only)
403
+ ### Dynamic Service Discovery (ZMQ Only)
418
404
  It is possible to setup the RPC server and client in a way that
419
- allows servers to be dynamically discovered by the client.
405
+ allows servers/services to be dynamically discovered by client processes.
420
406
 
421
407
  #### In the client
422
408
  ```ruby
@@ -428,14 +414,14 @@ end
428
414
  # given port when sending beacons and have its own service
429
415
  # directory. You can prevent this code from running on the
430
416
  # server if needed:
431
- unless defined? ::Protobuf::CLI
417
+ unless defined?(::Protobuf::CLI)
432
418
  ServiceDirectory.start do |config|
433
419
  config.port = 53000
434
420
  end
435
421
  end
436
422
  ```
437
423
 
438
- #### Starting the server
424
+ #### Starting the server with dynamic discovery enabled
439
425
  ```
440
426
  $ rpc_server --broadcast-beacons --beacon-port 53000 ...
441
427
  ```
@@ -447,10 +433,10 @@ desired service each time a request is made.
447
433
 
448
434
  __CAUTION:__ When running multiple environments on a single network,
449
435
  e.g., qa and staging, be sure that each environment is setup with
450
- a unique beacon port; otherwise, clients in one environment _will_
436
+ a unique beacon port; otherwise, clients in one environment ___will___
451
437
  make requests to servers in the other environment.
452
438
 
453
- Check out {Protobuf::ServiceDirectory} for more details.
439
+ Check out the source for Protobuf::ServiceDirectory for more details.
454
440
 
455
441
  ## 3. RPC Interop
456
442
 
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Before requiring protobuf, ensure that we will not load any
4
+ # server or client code.
5
+ #
6
+ ENV['PB_NO_NETWORKING'] = '1'
7
+
8
+ $LOAD_PATH << ::File.expand_path("../../lib", __FILE__)
9
+ require 'protobuf'
10
+
11
+ # Setup the loadpath so that plugin.pb will
12
+ # be able to require the descriptor.pb file.
13
+ #
14
+ $LOAD_PATH << ::File.expand_path("../../lib/protobuf/descriptors", __FILE__)
15
+ require 'google/protobuf/compiler/plugin.pb'
16
+
17
+ # Read the request bytes from STDIN, pass to the CodeGenerator, and
18
+ # write to STDOUT the generated response_bytes.
19
+ #
20
+ require 'protobuf/code_generator'
21
+
22
+ request_bytes = STDIN.read
23
+ code_generator = ::Protobuf::CodeGenerator.new(request_bytes)
24
+ response_bytes = code_generator.response_bytes
25
+ STDOUT.print(response_bytes)
26
+
data/bin/rprotoc CHANGED
@@ -1,62 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'rubygems'
4
- require 'ffi'
3
+ command = [ 'protoc', ARGV ].flatten.join(' ')
5
4
 
6
- base_dir = File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))
5
+ $stderr.puts "[DEPRECATED] `rprotoc` is deprecated. Use `protoc --ruby_out ...` instead."
6
+ $stdout.puts command
7
7
 
8
- GENERATOR_FILE_PATH = case
9
- when File.exists?(File.join(base_dir, "ruby_generator.bundle")) then
10
- File.join(base_dir, "ruby_generator.bundle")
11
- when File.exists?(File.join(base_dir, "ruby_generator.dll")) then
12
- File.join(base_dir, "ruby_generator.dll")
13
- else
14
- File.join(base_dir, "ruby_generator.so")
15
- end
16
-
17
- unless File.exists?(GENERATOR_FILE_PATH)
18
- $stdout << <<-WARNING
19
- Cannot locate shared object to plugin to protocol buffers generator.
20
- Thought the file would be located at #{GENERATOR_FILE_PATH}
21
-
22
- If you are running on Windows you should compile protocol buffer
23
- definitions on another VM and then use those definitions locally.
24
-
25
- Only the compiler is restricted by this. The definitions should work without issue.
26
- WARNING
27
- $stdout << $/
28
-
29
- exit 1
30
- end
31
-
32
- module Protobuf
33
- module RProtoC
34
- extend FFI::Library
35
- ffi_lib ::GENERATOR_FILE_PATH
36
- attach_function :_rprotoc_extern, [:int, :pointer], :int32
37
-
38
- def self.compile_proto(args)
39
- GC.disable # Don't want strings to be GC'd while protoc has them
40
- args = args.dup
41
- args.unshift("rprotoc")
42
- args << "--help" if args.size == 1
43
-
44
- ptr_params = []
45
- args.each do |param|
46
- ptr_params << ::FFI::MemoryPointer.from_string(param.dup)
47
- end
48
- ptr_params << nil
49
-
50
- argv = ::FFI::MemoryPointer.new(:pointer, ptr_params.size)
51
- ptr_params.each_with_index do |param_pointer, index|
52
- argv[index].put_pointer(0, param_pointer)
53
- end
54
-
55
- self._rprotoc_extern(ptr_params.compact.size, argv)
56
- ensure
57
- GC.enable
58
- end
59
- end
60
- end
61
-
62
- ::Protobuf::RProtoC.compile_proto(ARGV)
8
+ exec command