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
@@ -1,684 +0,0 @@
1
- require 'protobuf/common/util'
2
- require 'protobuf/common/wire_type'
3
- require 'protobuf/descriptor/field_descriptor'
4
-
5
- module Protobuf
6
- module Field
7
-
8
- PREDEFINED_TYPES = [
9
- :double, :float,
10
- :int32, :int64,
11
- :uint32, :uint64,
12
- :sint32, :sint64,
13
- :fixed32, :fixed64,
14
- :sfixed32, :sfixed64,
15
- :string, :bytes,
16
- :bool,
17
- ].freeze
18
-
19
- def self.build(message_class, rule, type, name, tag, options={})
20
- field_class = \
21
- if PREDEFINED_TYPES.include?(type)
22
- const_get("#{type.to_s.capitalize}Field")
23
- else
24
- FieldProxy
25
- end
26
- field_class.new(message_class, rule, type, name, tag, options)
27
- end
28
-
29
- class BaseField
30
-
31
- def self.descriptor
32
- @descriptor ||= Descriptor::FieldDescriptor.new
33
- end
34
-
35
- def self.default
36
- nil
37
- end
38
-
39
- attr_reader :message_class, :rule, :type, :name, :tag, :default
40
- attr_reader :default_value
41
-
42
- def descriptor
43
- @descriptor ||= Descriptor::FieldDescriptor.new(self)
44
- end
45
-
46
- def initialize(message_class, rule, type, name, tag, options)
47
- @message_class, @rule, @type, @name, @tag = \
48
- message_class, rule, type, name, tag
49
-
50
- @default = options.delete(:default)
51
- @extension = options.delete(:extension)
52
- @packed = repeated? && options.delete(:packed)
53
- unless options.empty?
54
- warn "WARNING: Invalid options: #{options.inspect} (in #{@message_class.name.split('::').last}.#{@name})"
55
- end
56
- if packed? && ! [WireType::VARINT, WireType::FIXED32, WireType::FIXED64].include?(wire_type)
57
- raise "Can't use packed encoding for `#{@type}' type"
58
- end
59
-
60
- @default_value = \
61
- case @rule
62
- when :repeated then
63
- FieldArray.new(self).freeze
64
- when :required then
65
- nil
66
- when :optional then
67
- typed_default_value
68
- end
69
-
70
- define_accessor
71
- end
72
-
73
- def ready?
74
- true
75
- end
76
-
77
- def initialized?(message_instance)
78
- value = message_instance.__send__(@name)
79
- case @rule
80
- when :required then
81
- ! value.nil? && (! kind_of?(MessageField) || value.initialized?)
82
- when :repeated then
83
- value.all? {|msg| ! kind_of?(MessageField) || msg.initialized? }
84
- when :optional then
85
- value.nil? || ! kind_of?(MessageField) || value.initialized?
86
- end
87
- end
88
-
89
- # Decode +bytes+ and pass to +message_instance+.
90
- def set(message_instance, bytes)
91
- if packed?
92
- array = message_instance.__send__(@name)
93
- method = \
94
- case wire_type
95
- when WireType::FIXED32 then :read_fixed32
96
- when WireType::FIXED64 then :read_fixed64
97
- when WireType::VARINT then :read_varint
98
- end
99
- stream = StringIO.new(bytes)
100
- until stream.eof?
101
- array << decode(Decoder.__send__(method, stream))
102
- end
103
- else
104
- value = decode(bytes)
105
- if repeated?
106
- message_instance.__send__(@name) << value
107
- else
108
- message_instance.__send__("#{@name}=", value)
109
- end
110
- end
111
- end
112
-
113
- # Decode +bytes+ and return a field value.
114
- def decode(bytes)
115
- raise NotImplementedError, "#{self.class.name}\#decode"
116
- end
117
-
118
- # Encode +value+ and return a byte string.
119
- def encode(value)
120
- raise NotImplementedError, "#{self.class.name}\#encode"
121
- end
122
-
123
- def extension?
124
- !!@extension
125
- end
126
-
127
- # Is this a repeated field?
128
- def repeated?
129
- return @_repeated unless @_repeated.nil?
130
- @_repeated = (@rule == :repeated)
131
- end
132
-
133
- # Is this a required field?
134
- def required?
135
- return @_required unless @_required.nil?
136
- @_required = (@rule == :required)
137
- end
138
-
139
- # Is this a optional field?
140
- def optional?
141
- return @_optional unless @_optional.nil?
142
- @_optional = (@rule == :optional)
143
- end
144
-
145
- # Is this a packed repeated field?
146
- def packed?
147
- !!@packed
148
- end
149
-
150
- # Upper limit for this field.
151
- def max
152
- self.class.max
153
- end
154
-
155
- # Lower limit for this field.
156
- def min
157
- self.class.min
158
- end
159
-
160
- # Is a +value+ acceptable for this field?
161
- def acceptable?(value)
162
- true
163
- end
164
-
165
- def to_s
166
- "#{@rule} #{@type} #{@name} = #{@tag} #{@default ? "[default=#{@default.inspect}]" : ''}"
167
- end
168
-
169
- private
170
-
171
- def define_accessor
172
- define_getter
173
- if repeated?
174
- define_array_setter
175
- else
176
- define_setter
177
- end
178
- end
179
-
180
- def define_getter
181
- field = self
182
- @message_class.class_eval do
183
- define_method(field.name) do
184
- @values.fetch(field.name, field.default_value)
185
- end
186
- end
187
- end
188
-
189
- def define_setter
190
- field = self
191
- @message_class.class_eval do
192
- define_method("#{field.name}=") do |val|
193
- if val.nil?
194
- @values.delete(field.name)
195
- elsif field.acceptable?(val)
196
- @values[field.name] = val
197
- end
198
- end
199
- end
200
- end
201
-
202
- def define_array_setter
203
- field = self
204
- @message_class.class_eval do
205
- define_method("#{field.name}=") do |val|
206
- @values[field.name].replace(val)
207
- end
208
- end
209
- end
210
-
211
- def typed_default_value
212
- if @default.nil?
213
- self.class.default
214
- else
215
- @default
216
- end
217
- end
218
-
219
- end # BaseField
220
-
221
-
222
- class FieldProxy
223
-
224
- def initialize(message_class, rule, type, name, tag, options)
225
- @message_class, @rule, @type, @name, @tag, @options =
226
- message_class, rule, type, name, tag, options
227
- end
228
-
229
- def ready?
230
- false
231
- end
232
-
233
- def setup
234
- type = typename_to_class(@message_class, @type)
235
- field_class = \
236
- if type < Enum
237
- EnumField
238
- elsif type < Message
239
- MessageField
240
- else
241
- raise TypeError, type.inspect
242
- end
243
- field_class.new(@message_class, @rule, type, @name, @tag, @options)
244
- end
245
-
246
- private
247
-
248
- def typename_to_class(message_class, type)
249
- names = type.to_s.split('::').map {|s| Util.camelize(s) }
250
- outer = message_class.to_s.split('::')
251
- args = (Object.method(:const_defined?).arity == 1) ? [] : [false]
252
- while
253
- mod = outer.empty? ? Object : eval(outer.join('::'))
254
- mod = names.inject(mod) {|m, s|
255
- m && m.const_defined?(s, *args) && m.const_get(s)
256
- }
257
- break if mod
258
- raise NameError.new("type not found: #{type}", type) if outer.empty?
259
- outer.pop
260
- end
261
- mod
262
- end
263
-
264
- end
265
-
266
- class FieldArray < Array
267
-
268
- def initialize(field)
269
- @field = field
270
- end
271
-
272
- def []=(nth, val)
273
- super(nth, normalize(val))
274
- end
275
-
276
- def <<(val)
277
- super(normalize(val))
278
- end
279
-
280
- def push(val)
281
- super(normalize(val))
282
- end
283
-
284
- def unshift(val)
285
- super(normalize(val))
286
- end
287
-
288
- def replace(val)
289
- raise TypeError unless val.is_a?(Array)
290
- val = val.map {|v| normalize(v)}
291
- super(val)
292
- end
293
-
294
- def to_s
295
- "[#{@field.name}]"
296
- end
297
-
298
- private
299
-
300
- def normalize(val)
301
- raise TypeError unless @field.acceptable?(val)
302
- if @field.is_a?(MessageField) && val.is_a?(Hash)
303
- @field.type.new(val)
304
- else
305
- val
306
- end
307
- end
308
-
309
- end
310
-
311
- # Field class for +bytes+ type.
312
- class BytesField < BaseField
313
- def self.default
314
- ''
315
- end
316
-
317
- def wire_type
318
- WireType::LENGTH_DELIMITED
319
- end
320
-
321
- def acceptable?(val)
322
- raise TypeError unless val.instance_of?(String)
323
- true
324
- end
325
-
326
- def decode(bytes)
327
- bytes.force_encoding('ASCII-8BIT') if bytes.respond_to?(:force_encoding)
328
- bytes
329
- end
330
-
331
- def encode(value)
332
- value = value.dup
333
- value.force_encoding('ASCII-8BIT') if value.respond_to?(:force_encoding)
334
- string_size = VarintField.encode(value.size)
335
- string_size << value
336
- end
337
- end
338
-
339
- class StringField < BytesField
340
- def decode(bytes)
341
- bytes.force_encoding('UTF-8') if bytes.respond_to?(:force_encoding)
342
- bytes
343
- end
344
- end
345
-
346
- class VarintField < BaseField
347
- INT32_MAX = 2**31 - 1
348
- INT32_MIN = -2**31
349
- INT64_MAX = 2**63 - 1
350
- INT64_MIN = -2**63
351
- UINT32_MAX = 2**32 - 1
352
- UINT64_MAX = 2**64 - 1
353
-
354
- class << self
355
- def default
356
- 0
357
- end
358
-
359
- def decode(bytes)
360
- value = 0
361
- bytes.each_with_index do |byte, index|
362
- value |= byte << (7 * index)
363
- end
364
- value
365
- end
366
-
367
- def encode(value)
368
- raise RangeError, "#{value} is negative" if value < 0
369
- return [value].pack('C') if value < 128
370
- bytes = []
371
- until value == 0
372
- bytes << (0x80 | (value & 0x7f))
373
- value >>= 7
374
- end
375
- bytes[-1] &= 0x7f
376
- bytes.pack('C*')
377
- end
378
- end
379
-
380
- def wire_type
381
- WireType::VARINT
382
- end
383
-
384
- def decode(value)
385
- value
386
- end
387
-
388
- def encode(value)
389
- self.class.encode(value)
390
- end
391
-
392
- def acceptable?(val)
393
- raise TypeError, val.class.name unless val.is_a?(Integer)
394
- raise RangeError if val < min || max < val
395
- true
396
- end
397
- end
398
-
399
- # Base class for int32 and int64
400
- class IntegerField < VarintField
401
- def encode(value)
402
- # original Google's library uses 64bits integer for negative value
403
- VarintField.encode(value & 0xffff_ffff_ffff_ffff)
404
- end
405
-
406
- def decode(value)
407
- value -= 0x1_0000_0000_0000_0000 if (value & 0x8000_0000_0000_0000).nonzero?
408
- value
409
- end
410
- end
411
-
412
- class Int32Field < IntegerField
413
- def self.max; INT32_MAX; end
414
- def self.min; INT32_MIN; end
415
- end
416
-
417
- class Int64Field < IntegerField
418
- def self.max; INT64_MAX; end
419
- def self.min; INT64_MIN; end
420
- end
421
-
422
- class Uint32Field < VarintField
423
- def self.max; UINT32_MAX; end
424
- def self.min; 0; end
425
- end
426
-
427
- class Uint64Field < VarintField
428
- def self.max; UINT64_MAX; end
429
- def self.min; 0; end
430
- end
431
-
432
- # Base class for sint32 and sint64
433
- class SignedIntegerField < VarintField
434
- def decode(value)
435
- if (value & 1).zero?
436
- value >> 1 # positive value
437
- else
438
- ~value >> 1 # negative value
439
- end
440
- end
441
-
442
- def encode(value)
443
- if value >= 0
444
- VarintField.encode(value << 1)
445
- else
446
- VarintField.encode(~(value << 1))
447
- end
448
- end
449
- end
450
-
451
- class Sint32Field < SignedIntegerField
452
- def self.max; INT32_MAX; end
453
- def self.min; INT32_MIN; end
454
- end
455
-
456
- class Sint64Field < SignedIntegerField
457
- def self.max; INT64_MAX; end
458
- def self.min; INT64_MIN; end
459
- end
460
-
461
- class FloatField < BaseField
462
- def self.default; 0.0; end
463
- def self.max; 1.0/0; end
464
- def self.min; -1.0/0; end
465
-
466
- def wire_type
467
- WireType::FIXED32
468
- end
469
-
470
- def decode(bytes)
471
- bytes.unpack('e').first
472
- end
473
-
474
- def encode(value)
475
- [value].pack('e')
476
- end
477
-
478
- def acceptable?(val)
479
- raise TypeError, val.class.name unless val.is_a?(Numeric)
480
- raise RangeError if val < min || max < val
481
- true
482
- end
483
- end
484
-
485
- class DoubleField < FloatField
486
- def wire_type
487
- WireType::FIXED64
488
- end
489
-
490
- def decode(bytes)
491
- bytes.unpack('E').first
492
- end
493
-
494
- def encode(value)
495
- [value].pack('E')
496
- end
497
- end
498
-
499
- class Fixed32Field < Uint32Field
500
- def wire_type
501
- WireType::FIXED32
502
- end
503
-
504
- def decode(bytes)
505
- bytes.unpack('V').first
506
- end
507
-
508
- def encode(value)
509
- [value].pack('V')
510
- end
511
- end
512
-
513
- class Fixed64Field < Uint64Field
514
- def wire_type
515
- WireType::FIXED64
516
- end
517
-
518
- def decode(bytes)
519
- # we don't use 'Q' for pack/unpack. 'Q' is machine-dependent.
520
- values = bytes.unpack('VV')
521
- values[0] + (values[1] << 32)
522
- end
523
-
524
- def encode(value)
525
- # we don't use 'Q' for pack/unpack. 'Q' is machine-dependent.
526
- [value & 0xffff_ffff, value >> 32].pack('VV')
527
- end
528
- end
529
-
530
- class Sfixed32Field < Int32Field
531
- def wire_type
532
- WireType::FIXED32
533
- end
534
-
535
- def decode(bytes)
536
- value = bytes.unpack('V').first
537
- value -= 0x1_0000_0000 if (value & 0x8000_0000).nonzero?
538
- value
539
- end
540
-
541
- def encode(value)
542
- [value].pack('V')
543
- end
544
- end
545
-
546
- class Sfixed64Field < Int64Field
547
- def wire_type
548
- WireType::FIXED64
549
- end
550
-
551
- def decode(bytes)
552
- # we don't use 'Q' for pack/unpack. 'Q' is machine-dependent.
553
- values = bytes.unpack('VV')
554
- value = values[0] + (values[1] << 32)
555
- value -= 0x1_0000_0000_0000_0000 if (value & 0x8000_0000_0000_0000).nonzero?
556
- value
557
- end
558
-
559
- def encode(value)
560
- # we don't use 'Q' for pack/unpack. 'Q' is machine-dependent.
561
- [value & 0xffff_ffff, value >> 32].pack('VV')
562
- end
563
- end
564
-
565
- class BoolField < VarintField
566
- def self.default
567
- false
568
- end
569
-
570
- def acceptable?(val)
571
- raise TypeError unless [true, false].include?(val)
572
- true
573
- end
574
-
575
- def decode(value)
576
- value == 1
577
- end
578
-
579
- def encode(value)
580
- [value ? 1 : 0].pack('C')
581
- end
582
- end
583
-
584
-
585
- class MessageField < BaseField
586
- def wire_type
587
- WireType::LENGTH_DELIMITED
588
- end
589
-
590
- def acceptable?(val)
591
- raise TypeError unless val.instance_of?(type) || val.instance_of?(Hash)
592
- true
593
- end
594
-
595
- def decode(bytes)
596
- message = type.new
597
- message.parse_from_string(bytes)
598
- message
599
- end
600
-
601
- def encode(value)
602
- bytes = value.serialize_to_string
603
- result = VarintField.encode(bytes.size)
604
- result << bytes
605
- end
606
-
607
- private
608
-
609
- def define_setter
610
- field = self
611
- @message_class.class_eval do
612
- define_method("#{field.name}=") do |val|
613
- case val
614
- when nil then
615
- @values.delete(field.name)
616
- when Hash then
617
- @values[field.name] = field.type.new(val)
618
- when field.type then
619
- @values[field.name] = val
620
- else
621
- raise TypeError, "Expected value of type '#{field.type}', but got '#{val.class}'"
622
- end
623
- end
624
- end
625
- end
626
-
627
- end
628
-
629
-
630
- class EnumField < VarintField
631
- def acceptable?(val)
632
- case val
633
- when Symbol then
634
- raise TypeError unless @type.const_defined?(val)
635
- when EnumValue then
636
- raise TypeError if val.parent_class != @type
637
- else
638
- raise TypeError unless @type.valid_tag?(val)
639
- end
640
- true
641
- end
642
-
643
- def encode(value)
644
- super(value.to_i)
645
- end
646
-
647
- private
648
-
649
- def typed_default_value
650
- if @default.is_a?(Symbol)
651
- @type.const_get(@default)
652
- else
653
- self.class.default
654
- end
655
- end
656
-
657
- def define_setter
658
- field = self
659
- @message_class.class_eval do
660
- define_method("#{field.name}=") do |val|
661
- if val.nil?
662
- @values.delete(field.name)
663
- else
664
- val = \
665
- case val
666
- when Symbol then
667
- field.type.const_get(val) rescue nil
668
- when Integer then
669
- field.type.const_get(field.type.name_by_value(val)) rescue nil
670
- when EnumValue then
671
- raise TypeError, "Invalid value: #{val.inspect} for #{field.name}" if val.parent_class != field.type
672
- val
673
- end
674
- raise TypeError, "Invalid value: #{val.inspect} for #{field.name}" unless val
675
-
676
- @values[field.name] = val
677
- end
678
- end
679
- end
680
- end
681
-
682
- end
683
- end
684
- end