protobuf 1.4.2 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (198) hide show
  1. data/.gitignore +4 -0
  2. data/Rakefile +7 -7
  3. data/bin/rpc_server +2 -188
  4. data/bin/rprotoc +44 -38
  5. data/examples/addressbook.pb.rb +2 -2
  6. data/ext/Makefile +11 -0
  7. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/code_generator.h +142 -0
  8. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/command_line_interface.h +318 -0
  9. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum.h +99 -0
  10. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum_field.h +103 -0
  11. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_extension.h +85 -0
  12. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_field.h +167 -0
  13. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_file.h +98 -0
  14. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_generator.h +72 -0
  15. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_helpers.h +159 -0
  16. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message.h +170 -0
  17. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message_field.h +102 -0
  18. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_primitive_field.h +103 -0
  19. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_service.h +118 -0
  20. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_string_field.h +104 -0
  21. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h +2721 -0
  22. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/importer.h +303 -0
  23. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum.h +84 -0
  24. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum_field.h +121 -0
  25. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_extension.h +77 -0
  26. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_field.h +108 -0
  27. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_file.h +101 -0
  28. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_generator.h +72 -0
  29. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_helpers.h +213 -0
  30. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message.h +109 -0
  31. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message_field.h +134 -0
  32. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_primitive_field.h +121 -0
  33. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_service.h +113 -0
  34. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_string_field.h +120 -0
  35. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/mock_code_generator.h +113 -0
  36. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/package_info.h +64 -0
  37. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/parser.h +434 -0
  38. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.h +73 -0
  39. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.pb.h +790 -0
  40. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/python/python_generator.h +156 -0
  41. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/subprocess.h +108 -0
  42. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/zip_writer.h +93 -0
  43. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.h +1367 -0
  44. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.pb.h +5223 -0
  45. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor_database.h +366 -0
  46. data/ext/protobuf-2.4.1/src/google/protobuf/dynamic_message.h +136 -0
  47. data/ext/protobuf-2.4.1/src/google/protobuf/extension_set.h +904 -0
  48. data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_reflection.h +424 -0
  49. data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_util.h +82 -0
  50. data/ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream.h +1102 -0
  51. data/ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream_inl.h +64 -0
  52. data/ext/protobuf-2.4.1/src/google/protobuf/io/gzip_stream.h +207 -0
  53. data/ext/protobuf-2.4.1/src/google/protobuf/io/package_info.h +54 -0
  54. data/ext/protobuf-2.4.1/src/google/protobuf/io/printer.h +136 -0
  55. data/ext/protobuf-2.4.1/src/google/protobuf/io/tokenizer.h +313 -0
  56. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream.h +238 -0
  57. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl.h +357 -0
  58. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl_lite.h +340 -0
  59. data/ext/protobuf-2.4.1/src/google/protobuf/message.h +692 -0
  60. data/ext/protobuf-2.4.1/src/google/protobuf/message_lite.h +239 -0
  61. data/ext/protobuf-2.4.1/src/google/protobuf/package_info.h +64 -0
  62. data/ext/protobuf-2.4.1/src/google/protobuf/reflection_ops.h +80 -0
  63. data/ext/protobuf-2.4.1/src/google/protobuf/repeated_field.h +1295 -0
  64. data/ext/protobuf-2.4.1/src/google/protobuf/service.h +291 -0
  65. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/common.h +1211 -0
  66. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/hash.h +220 -0
  67. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/map-util.h +119 -0
  68. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/once.h +123 -0
  69. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/stl_util-inl.h +121 -0
  70. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/strutil.h +457 -0
  71. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/substitute.h +170 -0
  72. data/ext/protobuf-2.4.1/src/google/protobuf/test_util.h +174 -0
  73. data/ext/protobuf-2.4.1/src/google/protobuf/test_util_lite.h +101 -0
  74. data/ext/protobuf-2.4.1/src/google/protobuf/testing/file.h +83 -0
  75. data/ext/protobuf-2.4.1/src/google/protobuf/testing/googletest.h +98 -0
  76. data/ext/protobuf-2.4.1/src/google/protobuf/text_format.h +285 -0
  77. data/ext/protobuf-2.4.1/src/google/protobuf/unittest.pb.h +11915 -0
  78. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_custom_options.pb.h +2895 -0
  79. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_embed_optimize_for.pb.h +211 -0
  80. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_empty.pb.h +56 -0
  81. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import.pb.h +188 -0
  82. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import_lite.pb.h +151 -0
  83. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite.pb.h +4752 -0
  84. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite_imports_nonlite.pb.h +150 -0
  85. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_mset.pb.h +816 -0
  86. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_no_generic_services.pb.h +197 -0
  87. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_optimize_for.pb.h +403 -0
  88. data/ext/protobuf-2.4.1/src/google/protobuf/unknown_field_set.h +268 -0
  89. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format.h +304 -0
  90. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite.h +620 -0
  91. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite_inl.h +774 -0
  92. data/ext/ruby_generator/RubyGenerator.cpp +435 -0
  93. data/ext/ruby_generator/RubyGenerator.h +180 -0
  94. data/ext/ruby_generator/extconf.rb +24 -0
  95. data/lib/protobuf.rb +55 -33
  96. data/lib/protobuf/cli.rb +176 -0
  97. data/lib/protobuf/common/logger.rb +11 -11
  98. data/lib/protobuf/{message/enum.rb → enum.rb} +0 -10
  99. data/lib/protobuf/evented.rb +22 -0
  100. data/lib/protobuf/field.rb +57 -0
  101. data/lib/protobuf/field/base_field.rb +210 -0
  102. data/lib/protobuf/field/bool_field.rb +24 -0
  103. data/lib/protobuf/field/bytes_field.rb +38 -0
  104. data/lib/protobuf/field/double_field.rb +19 -0
  105. data/lib/protobuf/field/enum_field.rb +58 -0
  106. data/lib/protobuf/field/extension_fields.rb +24 -0
  107. data/lib/protobuf/field/field_array.rb +55 -0
  108. data/lib/protobuf/field/fixed32_field.rb +19 -0
  109. data/lib/protobuf/field/fixed64_field.rb +22 -0
  110. data/lib/protobuf/field/float_field.rb +29 -0
  111. data/lib/protobuf/field/int32_field.rb +10 -0
  112. data/lib/protobuf/field/int64_field.rb +10 -0
  113. data/lib/protobuf/field/integer_field.rb +19 -0
  114. data/lib/protobuf/field/message_field.rb +51 -0
  115. data/lib/protobuf/field/sfixed32_field.rb +21 -0
  116. data/lib/protobuf/field/sfixed64_field.rb +24 -0
  117. data/lib/protobuf/field/signed_integer_field.rb +23 -0
  118. data/lib/protobuf/field/sint32_field.rb +10 -0
  119. data/lib/protobuf/field/sint64_field.rb +10 -0
  120. data/lib/protobuf/field/string_field.rb +14 -0
  121. data/lib/protobuf/field/uint32_field.rb +10 -0
  122. data/lib/protobuf/field/uint64_field.rb +10 -0
  123. data/lib/protobuf/field/varint_field.rb +63 -0
  124. data/lib/protobuf/message.rb +376 -0
  125. data/lib/protobuf/message/message.rb +1 -475
  126. data/lib/protobuf/rpc/buffer.rb +1 -1
  127. data/lib/protobuf/rpc/client.rb +26 -26
  128. data/lib/protobuf/rpc/connector.rb +15 -14
  129. data/lib/protobuf/rpc/connectors/eventmachine.rb +13 -13
  130. data/lib/protobuf/rpc/rpc.pb.rb +39 -28
  131. data/lib/protobuf/rpc/server.rb +7 -28
  132. data/lib/protobuf/rpc/servers/evented_runner.rb +15 -15
  133. data/lib/protobuf/rpc/servers/socket/server.rb +2 -1
  134. data/lib/protobuf/rpc/servers/socket_runner.rb +5 -6
  135. data/lib/protobuf/rpc/servers/zmq_runner.rb +4 -5
  136. data/lib/protobuf/socket.rb +22 -0
  137. data/lib/protobuf/version.rb +2 -1
  138. data/lib/protobuf/zmq.rb +21 -0
  139. data/proto/rpc.pb.rb +48 -0
  140. data/protobuf.gemspec +14 -6
  141. data/spec/benchmark/tasks.rb +42 -23
  142. data/spec/functional/evented_server_spec.rb +2 -0
  143. data/spec/functional/socket_server_spec.rb +22 -26
  144. data/spec/functional/zmq_server_spec.rb +22 -26
  145. data/spec/lib/protobuf/cli_spec.rb +182 -0
  146. data/spec/{unit → lib/protobuf}/common/logger_spec.rb +0 -0
  147. data/spec/{unit → lib/protobuf/message}/enum_spec.rb +0 -0
  148. data/spec/{unit → lib/protobuf/message}/message_spec.rb +0 -0
  149. data/spec/{unit → lib/protobuf}/rpc/client_spec.rb +6 -4
  150. data/spec/lib/protobuf/rpc/connector_spec.rb +41 -0
  151. data/spec/{unit → lib/protobuf}/rpc/connectors/base_spec.rb +18 -18
  152. data/spec/{unit → lib/protobuf}/rpc/connectors/common_spec.rb +20 -20
  153. data/spec/{unit → lib/protobuf}/rpc/connectors/socket_spec.rb +9 -8
  154. data/spec/{unit → lib/protobuf}/rpc/connectors/zmq_spec.rb +1 -1
  155. data/spec/{unit → lib/protobuf}/rpc/servers/evented_server_spec.rb +4 -4
  156. data/spec/{unit → lib/protobuf}/rpc/servers/socket_server_spec.rb +11 -23
  157. data/spec/{unit → lib/protobuf}/rpc/servers/zmq/broker_spec.rb +5 -0
  158. data/spec/{unit → lib/protobuf}/rpc/servers/zmq/server_spec.rb +5 -0
  159. data/spec/{unit → lib/protobuf}/rpc/servers/zmq/util_spec.rb +5 -0
  160. data/spec/{unit → lib/protobuf}/rpc/servers/zmq/worker_spec.rb +5 -0
  161. data/spec/{unit → lib/protobuf}/rpc/service_spec.rb +0 -0
  162. data/spec/proto/test.pb.rb +48 -25
  163. data/spec/spec_helper.rb +9 -15
  164. data/spec/support/all.rb +7 -0
  165. data/spec/{helper → support}/server.rb +5 -7
  166. data/spec/{helper → support}/silent_constants.rb +4 -0
  167. data/spec/support/test_app_file.rb +2 -0
  168. data/spec/{helper → support}/tolerance_matcher.rb +0 -0
  169. data/test/proto/types.pb.rb +1 -1
  170. data/test/test_addressbook.rb +1 -1
  171. data/test/test_enum_value.rb +1 -1
  172. data/test/test_standard_message.rb +1 -1
  173. metadata +243 -107
  174. data/lib/protobuf/common/util.rb +0 -59
  175. data/lib/protobuf/compiler/compiler.rb +0 -52
  176. data/lib/protobuf/compiler/nodes.rb +0 -323
  177. data/lib/protobuf/compiler/proto.y +0 -216
  178. data/lib/protobuf/compiler/proto2.ebnf +0 -79
  179. data/lib/protobuf/compiler/proto_parser.rb +0 -1425
  180. data/lib/protobuf/compiler/template/rpc_service_implementation.erb +0 -42
  181. data/lib/protobuf/compiler/visitors.rb +0 -282
  182. data/lib/protobuf/descriptor/descriptor.proto +0 -286
  183. data/lib/protobuf/descriptor/descriptor.rb +0 -55
  184. data/lib/protobuf/descriptor/descriptor_builder.rb +0 -143
  185. data/lib/protobuf/descriptor/descriptor_proto.rb +0 -138
  186. data/lib/protobuf/descriptor/enum_descriptor.rb +0 -33
  187. data/lib/protobuf/descriptor/field_descriptor.rb +0 -49
  188. data/lib/protobuf/descriptor/file_descriptor.rb +0 -37
  189. data/lib/protobuf/message/field.rb +0 -684
  190. data/lib/protobuf/message/protoable.rb +0 -38
  191. data/spec/helper/all.rb +0 -7
  192. data/spec/unit/common/util_spec.rb +0 -17
  193. data/spec/unit/rpc/connector_spec.rb +0 -31
  194. data/spec/unit/rpc/connectors/eventmachine_client_spec.rb +0 -32
  195. data/spec/unit/rpc/connectors/eventmachine_spec.rb +0 -0
  196. data/test/check_unbuild.rb +0 -30
  197. data/test/test_compiler.rb +0 -325
  198. data/test/test_descriptor.rb +0 -122
@@ -0,0 +1,22 @@
1
+ require 'protobuf/field/uint64_field'
2
+
3
+ module Protobuf
4
+ module Field
5
+ class Fixed64Field < Uint64Field
6
+ def wire_type
7
+ ::Protobuf::WireType::FIXED64
8
+ end
9
+
10
+ def decode(bytes)
11
+ # we don't use 'Q' for pack/unpack. 'Q' is machine-dependent.
12
+ values = bytes.unpack('VV')
13
+ values[0] + (values[1] << 32)
14
+ end
15
+
16
+ def encode(value)
17
+ # we don't use 'Q' for pack/unpack. 'Q' is machine-dependent.
18
+ [value & 0xffff_ffff, value >> 32].pack('VV')
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,29 @@
1
+ require 'protobuf/field/base_field'
2
+
3
+ module Protobuf
4
+ module Field
5
+ class FloatField < BaseField
6
+ def self.default; 0.0; end
7
+ def self.max; 1.0/0; end
8
+ def self.min; -1.0/0; end
9
+
10
+ def wire_type
11
+ WireType::FIXED32
12
+ end
13
+
14
+ def decode(bytes)
15
+ bytes.unpack('e').first
16
+ end
17
+
18
+ def encode(value)
19
+ [value].pack('e')
20
+ end
21
+
22
+ def acceptable?(val)
23
+ raise TypeError, val.class.name unless val.is_a?(Numeric)
24
+ raise RangeError if val < min || max < val
25
+ true
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,10 @@
1
+ require 'protobuf/field/integer_field'
2
+
3
+ module Protobuf
4
+ module Field
5
+ class Int32Field < IntegerField
6
+ def self.max; INT32_MAX; end
7
+ def self.min; INT32_MIN; end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ require 'protobuf/field/integer_field'
2
+
3
+ module Protobuf
4
+ module Field
5
+ class Int64Field < IntegerField
6
+ def self.max; INT64_MAX; end
7
+ def self.min; INT64_MIN; end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,19 @@
1
+ require 'protobuf/field/varint_field'
2
+
3
+ module Protobuf
4
+ module Field
5
+ class IntegerField < VarintField
6
+
7
+ def encode(value)
8
+ # original Google's library uses 64bits integer for negative value
9
+ VarintField.encode(value & 0xffff_ffff_ffff_ffff)
10
+ end
11
+
12
+ def decode(value)
13
+ value -= 0x1_0000_0000_0000_0000 if (value & 0x8000_0000_0000_0000).nonzero?
14
+ value
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,51 @@
1
+ require 'protobuf/field/base_field'
2
+
3
+ module Protobuf
4
+ module Field
5
+ class MessageField < BaseField
6
+ ##
7
+ # Public Instance Methods
8
+ #
9
+ def acceptable?(val)
10
+ raise TypeError unless val.instance_of?(type) || val.instance_of?(Hash)
11
+ true
12
+ end
13
+
14
+ def decode(bytes)
15
+ message = type.new
16
+ message.parse_from_string(bytes)
17
+ message
18
+ end
19
+
20
+ def encode(value)
21
+ bytes = value.serialize_to_string
22
+ result = VarintField.encode(bytes.size)
23
+ result << bytes
24
+ end
25
+
26
+ def wire_type
27
+ ::Protobuf::WireType::LENGTH_DELIMITED
28
+ end
29
+
30
+ private
31
+
32
+ def define_setter
33
+ field = self
34
+ @message_class.class_eval do
35
+ define_method("#{field.name}=") do |val|
36
+ case val
37
+ when nil then
38
+ @values.delete(field.name)
39
+ when Hash then
40
+ @values[field.name] = field.type.new(val)
41
+ when field.type then
42
+ @values[field.name] = val
43
+ else
44
+ raise TypeError, "Expected value of type '#{field.type}', but got '#{val.class}'"
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,21 @@
1
+ require 'protobuf/field/int32_field'
2
+
3
+ module Protobuf
4
+ module Field
5
+ class Sfixed32Field < Int32Field
6
+ def wire_type
7
+ ::Protobuf::WireType::FIXED32
8
+ end
9
+
10
+ def decode(bytes)
11
+ value = bytes.unpack('V').first
12
+ value -= 0x1_0000_0000 if (value & 0x8000_0000).nonzero?
13
+ value
14
+ end
15
+
16
+ def encode(value)
17
+ [value].pack('V')
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,24 @@
1
+ require 'protobuf/field/int64_field'
2
+
3
+ module Protobuf
4
+ module Field
5
+ class Sfixed64Field < Int64Field
6
+ def wire_type
7
+ ::Protobuf::WireType::FIXED64
8
+ end
9
+
10
+ def decode(bytes)
11
+ # we don't use 'Q' for pack/unpack. 'Q' is machine-dependent.
12
+ values = bytes.unpack('VV')
13
+ value = values[0] + (values[1] << 32)
14
+ value -= 0x1_0000_0000_0000_0000 if (value & 0x8000_0000_0000_0000).nonzero?
15
+ value
16
+ end
17
+
18
+ def encode(value)
19
+ # we don't use 'Q' for pack/unpack. 'Q' is machine-dependent.
20
+ [value & 0xffff_ffff, value >> 32].pack('VV')
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ require 'protobuf/field/varint_field'
2
+
3
+ module Protobuf
4
+ module Field
5
+ class SignedIntegerField < VarintField
6
+ def decode(value)
7
+ if (value & 1).zero?
8
+ value >> 1 # positive value
9
+ else
10
+ ~value >> 1 # negative value
11
+ end
12
+ end
13
+
14
+ def encode(value)
15
+ if value >= 0
16
+ VarintField.encode(value << 1)
17
+ else
18
+ VarintField.encode(~(value << 1))
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,10 @@
1
+ require 'protobuf/field/signed_integer_field'
2
+
3
+ module Protobuf
4
+ module Field
5
+ class Sint32Field < SignedIntegerField
6
+ def self.max; INT32_MAX; end
7
+ def self.min; INT32_MIN; end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ require 'protobuf/field/signed_integer_field'
2
+
3
+ module Protobuf
4
+ module Field
5
+ class Sint64Field < SignedIntegerField
6
+ def self.max; INT64_MAX; end
7
+ def self.min; INT64_MIN; end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,14 @@
1
+ require 'protobuf/field/bytes_field'
2
+
3
+ module Protobuf
4
+ module Field
5
+ class StringField < BytesField
6
+ ENCODING = 'UTF-8'.freeze
7
+
8
+ def decode(bytes)
9
+ bytes.force_encoding(::Protobuf::Field::StringField::ENCODING) if bytes.respond_to?(:force_encoding)
10
+ bytes
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ require 'protobuf/field/varint_field'
2
+
3
+ module Protobuf
4
+ module Field
5
+ class Uint32Field < VarintField
6
+ def self.max; UINT32_MAX; end
7
+ def self.min; 0; end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ require 'protobuf/field/varint_field'
2
+
3
+ module Protobuf
4
+ module Field
5
+ class Uint64Field < VarintField
6
+ def self.max; UINT64_MAX; end
7
+ def self.min; 0; end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,63 @@
1
+ require 'protobuf/field/base_field'
2
+
3
+ module Protobuf
4
+ module Field
5
+ class VarintField < BaseField
6
+ INT32_MAX = 2**31 - 1
7
+ INT32_MIN = -2**31
8
+ INT64_MAX = 2**63 - 1
9
+ INT64_MIN = -2**63
10
+ UINT32_MAX = 2**32 - 1
11
+ UINT64_MAX = 2**64 - 1
12
+
13
+ ##
14
+ # Class Methods
15
+ #
16
+ def self.default
17
+ 0
18
+ end
19
+
20
+ def self.decode(bytes)
21
+ value = 0
22
+ bytes.each_with_index do |byte, index|
23
+ value |= byte << (7 * index)
24
+ end
25
+ value
26
+ end
27
+
28
+ def self.encode(value)
29
+ raise RangeError, "#{value} is negative" if value < 0
30
+ return [value].pack('C') if value < 128
31
+ bytes = []
32
+ until value == 0
33
+ bytes << (0x80 | (value & 0x7f))
34
+ value >>= 7
35
+ end
36
+ bytes[-1] &= 0x7f
37
+ bytes.pack('C*')
38
+ end
39
+
40
+ ##
41
+ # Public Instance Methods
42
+ #
43
+ def acceptable?(val)
44
+ raise TypeError, val.class.name unless val.is_a?(Integer)
45
+ raise RangeError if val < min || max < val
46
+ true
47
+ end
48
+
49
+ def decode(value)
50
+ value
51
+ end
52
+
53
+ def encode(value)
54
+ self.class.encode(value)
55
+ end
56
+
57
+ def wire_type
58
+ ::Protobuf::WireType::VARINT
59
+ end
60
+
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,376 @@
1
+ require 'set'
2
+ require 'protobuf/field'
3
+ require 'protobuf/enum'
4
+ require 'protobuf/message/decoder'
5
+ require 'protobuf/message/encoder'
6
+
7
+ module Protobuf
8
+ class Message
9
+ STRING_ENCODING = "ASCII-8BIT".freeze
10
+
11
+ def self.inherited(klass)
12
+ @_children ||= Set.new
13
+ @_children << klass
14
+ end
15
+
16
+ def self.all_fields
17
+ @all_fields ||= begin
18
+ fields_hash = fields.merge(extension_fields)
19
+ ordered_keys = fields_hash.keys.sort
20
+ ordered_keys.map { |key| fields_hash[key] }
21
+ end
22
+ end
23
+
24
+ # Reserve field numbers for extensions. Don't use this method directly.
25
+ def self.extensions(range)
26
+ @extension_fields = ::Protobuf::Field::ExtensionFields.new(range)
27
+ end
28
+
29
+ # Define a required field. Don't use this method directly.
30
+ def self.required(type, name, tag, options={})
31
+ define_field(:required, type, name, tag, options)
32
+ end
33
+
34
+ # Define a optional field. Don't use this method directly.
35
+ def self.optional(type, name, tag, options={})
36
+ define_field(:optional, type, name, tag, options)
37
+ end
38
+
39
+ # Define a repeated field. Don't use this method directly.
40
+ def self.repeated(type, name, tag, options={})
41
+ define_field(:repeated, type, name, tag, options)
42
+ end
43
+
44
+ # Define a field. Don't use this method directly.
45
+ def self.define_field(rule, type, fname, tag, options)
46
+ field_hash = options[:extension] ? extension_fields : fields
47
+ field_name_hash = options[:extension] ? extension_fields_by_name : fields_by_name
48
+ repeated_collection = options[:extension] ? repeated_extension_fields : repeated_fields
49
+
50
+ if field_hash.keys.include?(tag)
51
+ raise TagCollisionError, %!{Field number #{tag} has already been used in "#{self.name}" by field "#{fname}".!
52
+ end
53
+
54
+ field_definition = Field.build(self, rule, type, fname, tag, options)
55
+ field_name_hash[fname.to_sym] = field_definition
56
+ repeated_collection << field_definition if rule == :repeated
57
+ field_hash[tag] = field_definition
58
+ end
59
+
60
+ def self.extension_tag?(tag)
61
+ extension_fields.include_tag?(tag)
62
+ end
63
+
64
+ # An extension field object.
65
+ def self.extension_fields
66
+ @extension_fields ||= ::Protobuf::Field::ExtensionFields.new
67
+ end
68
+
69
+ def self.extension_fields_by_name
70
+ @extension_fields_by_name ||= {}
71
+ end
72
+
73
+ # A collection of field object.
74
+ def self.fields
75
+ @fields ||= {}
76
+ end
77
+
78
+ def self.fields_by_name
79
+ @field_by_name ||= {}
80
+ end
81
+
82
+ def self.repeated_fields
83
+ @repeated_fields ||= []
84
+ end
85
+
86
+ def self.repeated_extension_fields
87
+ @repeated_extension_fields ||= []
88
+ end
89
+
90
+ # Find a field object by +name+.
91
+ def self.get_field_by_name(name)
92
+ # Check if the name has been used before, if not then set it to the sym value
93
+ fields_by_name[name] ||= fields_by_name[name.to_sym]
94
+ end
95
+
96
+ # Find a field object by +tag+ number.
97
+ def self.get_field_by_tag(tag)
98
+ fields[tag]
99
+ end
100
+
101
+ def self.field_cache
102
+ @field_cache ||= {}
103
+ end
104
+
105
+ # Find a field object by +tag_or_name+.
106
+ def self.get_field(tag_or_name)
107
+ field_cache[tag_or_name] ||= case tag_or_name
108
+ when Integer then get_field_by_tag(tag_or_name)
109
+ when String, Symbol then get_field_by_name(tag_or_name)
110
+ else raise TypeError, tag_or_name.class
111
+ end
112
+ end
113
+
114
+ def self.get_ext_field_by_name(name)
115
+ # Check if the name has been used before, if not then set it to the sym value
116
+ extension_fields_by_name[name] ||= extension_fields_by_name[name.to_sym]
117
+ end
118
+
119
+ def self.get_ext_field_by_tag(tag)
120
+ extension_fields[tag]
121
+ end
122
+
123
+ def self.get_ext_field(tag_or_name)
124
+ case tag_or_name
125
+ when Integer then get_ext_field_by_tag(tag_or_name)
126
+ when String, Symbol then get_ext_field_by_name(tag_or_name)
127
+ else raise TypeError, tag_or_name.class
128
+ end
129
+ end
130
+
131
+ ##
132
+ # Constructor
133
+ #
134
+ def initialize(values={})
135
+ @values = {}
136
+
137
+ self.class.repeated_fields.each do |field|
138
+ @values[field.name] = Field::FieldArray.new(field)
139
+ end
140
+
141
+ self.class.repeated_extension_fields.each do |field|
142
+ @values[field.name] = Field::FieldArray.new(field)
143
+ end
144
+
145
+ values.each { |tag, val| self[tag] = val}
146
+ end
147
+
148
+ def initialized?
149
+ fields.all? {|tag, field| field.initialized?(self) } && \
150
+ extension_fields.all? {|tag, field| field.initialized?(self) }
151
+ end
152
+
153
+ def has_field?(tag_or_name)
154
+ field = get_field(tag_or_name) || get_ext_field(tag_or_name)
155
+ raise ArgumentError, "unknown field: #{tag_or_name.inspect}" unless field
156
+ @values.has_key?(field.name)
157
+ end
158
+
159
+ def ==(obj)
160
+ return false unless obj.is_a?(self.class)
161
+ each_field do |field, value|
162
+ return false unless value == obj.__send__(field.name)
163
+ end
164
+ true
165
+ end
166
+
167
+ def clear!
168
+ @values.delete_if do |_, value|
169
+ if value.is_a?(Field::FieldArray)
170
+ value.clear
171
+ false
172
+ else
173
+ true
174
+ end
175
+ end
176
+ self
177
+ end
178
+
179
+ def dup
180
+ copy_to(super, :dup)
181
+ end
182
+
183
+ def clone
184
+ copy_to(super, :clone)
185
+ end
186
+
187
+ def copy_to(object, method)
188
+ duplicate = proc { |obj|
189
+ case obj
190
+ when Message, String then obj.__send__(method)
191
+ else obj
192
+ end
193
+ }
194
+
195
+ object.__send__(:initialize)
196
+ @values.each do |name, value|
197
+ if value.is_a?(Field::FieldArray)
198
+ object.__send__(name).replace(value.map {|v| duplicate.call(v)})
199
+ else
200
+ object.__send__("#{name}=", duplicate.call(value))
201
+ end
202
+ end
203
+ object
204
+ end
205
+ private :copy_to
206
+
207
+ def inspect(indent=0)
208
+ result = []
209
+ i = ' ' * indent
210
+ field_value_to_string = lambda { |field, value|
211
+ result << \
212
+ if field.optional? && ! has_field?(field.name)
213
+ ''
214
+ else
215
+ case field
216
+ when Field::MessageField then
217
+ if value.nil?
218
+ "#{i}#{field.name} {}\n"
219
+ else
220
+ "#{i}#{field.name} {\n#{value.inspect(indent + 1)}#{i}}\n"
221
+ end
222
+ when Field::EnumField then
223
+ if value.is_a?(EnumValue)
224
+ "#{i}#{field.name}: #{value.name}\n"
225
+ else
226
+ "#{i}#{field.name}: #{field.type.name_by_value(value)}\n"
227
+ end
228
+ else
229
+ "#{i}#{field.name}: #{value.inspect}\n"
230
+ end
231
+ end
232
+ }
233
+ each_field do |field, value|
234
+ if field.repeated?
235
+ value.each do |v|
236
+ field_value_to_string.call(field, v)
237
+ end
238
+ else
239
+ field_value_to_string.call(field, value)
240
+ end
241
+ end
242
+ result.join
243
+ end
244
+
245
+ def parse_from_string(string)
246
+ parse_from(StringIO.new(string))
247
+ end
248
+
249
+ def parse_from(stream)
250
+ Decoder.decode(stream, self)
251
+ end
252
+
253
+ def serialize_to_string(string='')
254
+ io = StringIO.new(string)
255
+ serialize_to(io)
256
+ result = io.string
257
+ result.force_encoding(::Protobuf::Message::STRING_ENCODING) if result.respond_to?(:force_encoding)
258
+ result
259
+ end
260
+ alias to_s serialize_to_string
261
+
262
+ def serialize_to(stream)
263
+ Encoder.encode(stream, self)
264
+ end
265
+
266
+ def set_field(tag, bytes)
267
+ field = (get_field_by_tag(tag) || get_ext_field_by_tag(tag))
268
+ field.set(self, bytes) if field
269
+ end
270
+
271
+ def [](tag_or_name)
272
+ if field = get_field(tag_or_name) || get_ext_field(tag_or_name)
273
+ __send__(field.name)
274
+ else
275
+ raise NoMethodError, "No such field: #{tag_or_name.inspect}"
276
+ end
277
+ end
278
+
279
+ def []=(tag_or_name, value)
280
+ if field = get_field(tag_or_name) || get_ext_field(tag_or_name)
281
+ # __send__("#{field.name}=", value)
282
+ __send__(field.setter_method_name, value)
283
+ else
284
+ raise NoMethodError, "No such field: #{tag_or_name.inspect}"
285
+ end
286
+ end
287
+
288
+ # Returns a hash; which key is a tag number, and value is a field object.
289
+ def all_fields
290
+ @_all_fields ||= self.class.all_fields
291
+ end
292
+
293
+ def fields
294
+ @_fields ||= self.class.fields
295
+ end
296
+
297
+ # Returns field object or +nil+.
298
+ def get_field_by_name(name)
299
+ self.class.get_field_by_name(name)
300
+ end
301
+
302
+ # Returns field object or +nil+.
303
+ def get_field_by_tag(tag)
304
+ self.class.get_field_by_tag(tag)
305
+ end
306
+
307
+ # Returns field object or +nil+.
308
+ def get_field(tag_or_name)
309
+ self.class.get_field(tag_or_name)
310
+ end
311
+
312
+ # Returns extension fields. See Message#fields method.
313
+ def extension_fields
314
+ self.class.extension_fields
315
+ end
316
+
317
+ def get_ext_field_by_name(name) # :nodoc:
318
+ self.class.get_ext_field_by_name(name)
319
+ end
320
+
321
+ def get_ext_field_by_tag(tag) # :nodoc:
322
+ self.class.get_ext_field_by_tag(tag)
323
+ end
324
+
325
+ def get_ext_field(tag_or_name) # :nodoc:
326
+ self.class.get_ext_field(tag_or_name)
327
+ end
328
+
329
+ # Iterate over a field collection.
330
+ # message.each_field do |field_object, value|
331
+ # # do something
332
+ # end
333
+ def each_field
334
+ all_fields.each do |field|
335
+ value = __send__(field.name)
336
+ yield(field, value)
337
+ end
338
+ end
339
+
340
+ def to_hash
341
+ result = {}
342
+ build_value = lambda { |field, value|
343
+ if !field.optional? || (field.optional? && has_field?(field.name))
344
+ case field
345
+ when Field::MessageField then
346
+ value.to_hash
347
+ when Field::EnumField then
348
+ if value.is_a?(EnumValue)
349
+ value.to_i
350
+ elsif value.is_a?(Symbol)
351
+ field.type[value].to_i
352
+ else
353
+ value
354
+ end
355
+ else
356
+ value
357
+ end
358
+ end
359
+ }
360
+ each_field do |field, value|
361
+ if field.repeated?
362
+ result[field.name] = value.map do |v|
363
+ build_value.call(field, v)
364
+ end
365
+ else
366
+ result[field.name] = build_value.call(field, value)
367
+ end
368
+ end
369
+ result
370
+ end
371
+
372
+ def to_json
373
+ to_hash.to_json
374
+ end
375
+ end
376
+ end