protobuf 2.3.2 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/protobuf/exceptions.rb +2 -5
- data/lib/protobuf/field/base_field.rb +14 -19
- data/lib/protobuf/field/bool_field.rb +1 -2
- data/lib/protobuf/field/bytes_field.rb +2 -3
- data/lib/protobuf/field/field_array.rb +5 -5
- data/lib/protobuf/field/float_field.rb +1 -3
- data/lib/protobuf/message.rb +40 -18
- data/lib/protobuf/message/decoder.rb +8 -18
- data/lib/protobuf/version.rb +1 -1
- data/spec/functional/evented_server_spec.rb +1 -1
- data/spec/functional/socket_server_spec.rb +1 -1
- data/spec/functional/zmq_server_spec.rb +1 -1
- data/spec/lib/protobuf/message_spec.rb +18 -5
- metadata +4 -6
- data/lib/protobuf/message/encoder.rb +0 -46
- data/spec/lib/protobuf/message/encoder_spec.rb +0 -19
data/lib/protobuf/exceptions.rb
CHANGED
@@ -1,11 +1,8 @@
|
|
1
1
|
module Protobuf
|
2
|
-
|
3
2
|
class Error < StandardError; end
|
4
|
-
|
5
3
|
class InvalidWireType < Error; end
|
6
|
-
|
7
4
|
class NotInitializedError < Error; end
|
8
|
-
|
9
5
|
class TagCollisionError < Error; end
|
10
|
-
|
6
|
+
class SerializationError < StandardError; end
|
7
|
+
class FieldNotDefinedError < StandardError; end
|
11
8
|
end
|
@@ -40,7 +40,7 @@ module Protobuf
|
|
40
40
|
@default_value = \
|
41
41
|
case @rule
|
42
42
|
when :repeated then
|
43
|
-
FieldArray.new(self).freeze
|
43
|
+
::Protobuf::Field::FieldArray.new(self).freeze
|
44
44
|
when :required then
|
45
45
|
nil
|
46
46
|
when :optional then
|
@@ -67,31 +67,20 @@ module Protobuf
|
|
67
67
|
@_message = (self.class < ::Protobuf::Field::MessageField)
|
68
68
|
end
|
69
69
|
|
70
|
-
def initialized?(message_instance)
|
71
|
-
value = message_instance.__send__(getter_method_name)
|
72
|
-
case @rule
|
73
|
-
when :required then
|
74
|
-
! value.nil? && (! kind_of?(MessageField) || value.initialized?)
|
75
|
-
when :repeated then
|
76
|
-
value.all? {|msg| ! kind_of?(MessageField) || msg.initialized? }
|
77
|
-
when :optional then
|
78
|
-
value.nil? || ! kind_of?(MessageField) || value.initialized?
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
70
|
# Decode +bytes+ and pass to +message_instance+.
|
83
71
|
def set(message_instance, bytes)
|
84
72
|
if packed?
|
85
73
|
array = message_instance.__send__(getter_method_name)
|
86
74
|
method = \
|
87
75
|
case wire_type
|
88
|
-
when WireType::FIXED32 then :read_fixed32
|
89
|
-
when WireType::FIXED64 then :read_fixed64
|
90
|
-
when WireType::VARINT then :read_varint
|
76
|
+
when ::Protobuf::WireType::FIXED32 then :read_fixed32
|
77
|
+
when ::Protobuf::WireType::FIXED64 then :read_fixed64
|
78
|
+
when ::Protobuf::WireType::VARINT then :read_varint
|
91
79
|
end
|
92
80
|
stream = StringIO.new(bytes)
|
81
|
+
|
93
82
|
until stream.eof?
|
94
|
-
array << decode(Decoder.__send__(method, stream))
|
83
|
+
array << decode(::Protobuf::Decoder.__send__(method, stream))
|
95
84
|
end
|
96
85
|
else
|
97
86
|
value = decode(bytes)
|
@@ -195,8 +184,13 @@ module Protobuf
|
|
195
184
|
@message_class.class_eval do
|
196
185
|
define_method(field.setter_method_name) do |val|
|
197
186
|
field.warn_if_deprecated
|
198
|
-
|
199
|
-
|
187
|
+
|
188
|
+
if val.nil?
|
189
|
+
@values.delete(field.name)
|
190
|
+
else
|
191
|
+
@values[field.name] ||= ::Protobuf::Field::FieldArray.new(field)
|
192
|
+
@values[field.name].replace(val)
|
193
|
+
end
|
200
194
|
end
|
201
195
|
end
|
202
196
|
end
|
@@ -216,6 +210,7 @@ module Protobuf
|
|
216
210
|
@message_class.class_eval do
|
217
211
|
define_method(field.setter_method_name) do |val|
|
218
212
|
field.warn_if_deprecated
|
213
|
+
|
219
214
|
if val.nil?
|
220
215
|
@values.delete(field.name)
|
221
216
|
elsif field.acceptable?(val)
|
@@ -12,9 +12,9 @@ module Protobuf
|
|
12
12
|
def acceptable?(val)
|
13
13
|
if val.nil? || val.is_a?(::Protobuf::Message) || val.instance_of?(String)
|
14
14
|
return true
|
15
|
+
else
|
16
|
+
raise TypeError, "Cannot set field : #{name} to value : #{val}"
|
15
17
|
end
|
16
|
-
|
17
|
-
raise TypeError, "Cannot set field : #{name} to value : #{val}"
|
18
18
|
end
|
19
19
|
|
20
20
|
def decode(bytes)
|
@@ -26,7 +26,6 @@ module Protobuf
|
|
26
26
|
if value.is_a?(::Protobuf::Message)
|
27
27
|
value = value.serialize_to_string
|
28
28
|
else
|
29
|
-
value = value.dup
|
30
29
|
value.force_encoding(::Protobuf::Message::STRING_ENCODING) if value.respond_to?(:force_encoding)
|
31
30
|
end
|
32
31
|
|
@@ -12,24 +12,24 @@ module Protobuf
|
|
12
12
|
# Public Instance Methods
|
13
13
|
#
|
14
14
|
def []=(nth, val)
|
15
|
-
super(nth, normalize(val))
|
15
|
+
super(nth, normalize(val)) unless val.nil?
|
16
16
|
end
|
17
17
|
|
18
18
|
def <<(val)
|
19
|
-
super(normalize(val))
|
19
|
+
super(normalize(val)) unless val.nil?
|
20
20
|
end
|
21
21
|
|
22
22
|
def push(val)
|
23
|
-
super(normalize(val))
|
23
|
+
super(normalize(val)) unless val.nil?
|
24
24
|
end
|
25
25
|
|
26
26
|
def unshift(val)
|
27
|
-
super(normalize(val))
|
27
|
+
super(normalize(val)) unless val.nil?
|
28
28
|
end
|
29
29
|
|
30
30
|
def replace(val)
|
31
31
|
raise TypeError unless val.is_a?(Array)
|
32
|
-
val = val.map { |v| normalize(v)}
|
32
|
+
val = val.map { |v| normalize(v) }
|
33
33
|
super(val)
|
34
34
|
end
|
35
35
|
|
data/lib/protobuf/message.rb
CHANGED
@@ -1,16 +1,11 @@
|
|
1
1
|
require 'set'
|
2
2
|
require 'protobuf/field'
|
3
3
|
require 'protobuf/enum'
|
4
|
+
require 'protobuf/exceptions'
|
4
5
|
require 'protobuf/message/decoder'
|
5
|
-
require 'protobuf/message/encoder'
|
6
6
|
|
7
7
|
module Protobuf
|
8
8
|
class Message
|
9
|
-
##
|
10
|
-
# Error Classes
|
11
|
-
#
|
12
|
-
class FieldNotDefinedError < StandardError; end
|
13
|
-
|
14
9
|
##
|
15
10
|
# Constants
|
16
11
|
#
|
@@ -165,14 +160,16 @@ module Protobuf
|
|
165
160
|
|
166
161
|
def each_field_for_serialization
|
167
162
|
all_fields.each do |field|
|
168
|
-
next unless
|
163
|
+
next unless has_field_or_required_field?(field)
|
169
164
|
|
170
165
|
value = __send__(field.name)
|
166
|
+
value.compact! if field.repeated?
|
171
167
|
|
172
168
|
if value.present? || [true, false].include?(value)
|
173
169
|
yield(field, value)
|
174
170
|
else
|
175
|
-
|
171
|
+
# Only way you can get here is if you are required and not present
|
172
|
+
raise ::Protobuf::SerializationError, "#{field.name} is required on #{field.message_class}"
|
176
173
|
end
|
177
174
|
end
|
178
175
|
end
|
@@ -208,22 +205,18 @@ module Protobuf
|
|
208
205
|
@values.has_key?(name)
|
209
206
|
end
|
210
207
|
|
211
|
-
def initialized?
|
212
|
-
all_fields.all? { |field| field.initialized?(self) }
|
213
|
-
end
|
214
|
-
|
215
208
|
def inspect
|
216
209
|
to_hash.inspect
|
217
210
|
end
|
218
211
|
|
219
|
-
def parse_from_string(string)
|
220
|
-
parse_from(StringIO.new(string))
|
221
|
-
end
|
222
|
-
|
223
212
|
def parse_from(stream)
|
224
213
|
Decoder.decode(stream, self)
|
225
214
|
end
|
226
215
|
|
216
|
+
def parse_from_string(string)
|
217
|
+
parse_from(StringIO.new(string))
|
218
|
+
end
|
219
|
+
|
227
220
|
def respond_to_has?(key)
|
228
221
|
self.respond_to?(key) && self.has_field?(key)
|
229
222
|
end
|
@@ -234,10 +227,24 @@ module Protobuf
|
|
234
227
|
end
|
235
228
|
|
236
229
|
def serialize_to(stream)
|
237
|
-
|
230
|
+
each_field_for_serialization do |field, value|
|
231
|
+
if field.repeated?
|
232
|
+
if field.packed?
|
233
|
+
key = (field.tag << 3) | ::Protobuf::WireType::LENGTH_DELIMITED
|
234
|
+
packed_value = value.map { |val| field.encode(val) }.join
|
235
|
+
stream.write(::Protobuf::Field::VarintField.encode(key))
|
236
|
+
stream.write(::Protobuf::Field::VarintField.encode(packed_value.size))
|
237
|
+
stream.write(packed_value)
|
238
|
+
else
|
239
|
+
value.each { |val| write_pair(stream, field, val) }
|
240
|
+
end
|
241
|
+
else
|
242
|
+
write_pair(stream, field, value)
|
243
|
+
end
|
244
|
+
end
|
238
245
|
end
|
239
246
|
|
240
|
-
def serialize_to_string(string='')
|
247
|
+
def serialize_to_string(string = '')
|
241
248
|
io = StringIO.new(string)
|
242
249
|
serialize_to(io)
|
243
250
|
result = io.string
|
@@ -296,6 +303,8 @@ module Protobuf
|
|
296
303
|
#
|
297
304
|
alias_method :to_hash_value, :to_hash
|
298
305
|
alias_method :to_s, :serialize_to_string
|
306
|
+
alias_method :bytes, :serialize_to_string
|
307
|
+
alias_method :serialize, :serialize_to_string
|
299
308
|
alias_method :responds_to_has?, :respond_to_has?
|
300
309
|
alias_method :respond_to_and_has?, :respond_to_has?
|
301
310
|
alias_method :responds_to_and_has?, :respond_to_has?
|
@@ -330,5 +339,18 @@ module Protobuf
|
|
330
339
|
object
|
331
340
|
end
|
332
341
|
|
342
|
+
def has_field_or_required_field?(field)
|
343
|
+
has_field?(field.name) || field.required?
|
344
|
+
end
|
345
|
+
|
346
|
+
# Encode key and value, and write to +stream+.
|
347
|
+
def write_pair(stream, field, value)
|
348
|
+
key = (field.tag << 3) | field.wire_type
|
349
|
+
key_bytes = ::Protobuf::Field::VarintField.encode(key)
|
350
|
+
stream.write(key_bytes)
|
351
|
+
bytes = field.encode(value)
|
352
|
+
stream.write(bytes)
|
353
|
+
end
|
354
|
+
|
333
355
|
end
|
334
356
|
end
|
@@ -13,18 +13,18 @@ module Protobuf
|
|
13
13
|
tag, wire_type = read_key(stream)
|
14
14
|
bytes =
|
15
15
|
case wire_type
|
16
|
-
when WireType::VARINT then
|
16
|
+
when ::Protobuf::WireType::VARINT then
|
17
17
|
read_varint(stream)
|
18
|
-
when WireType::FIXED64 then
|
18
|
+
when ::Protobuf::WireType::FIXED64 then
|
19
19
|
read_fixed64(stream)
|
20
|
-
when WireType::LENGTH_DELIMITED then
|
20
|
+
when ::Protobuf::WireType::LENGTH_DELIMITED then
|
21
21
|
read_length_delimited(stream)
|
22
|
-
when WireType::
|
23
|
-
read_start_group(stream)
|
24
|
-
when WireType::END_GROUP then
|
25
|
-
read_end_group(stream)
|
26
|
-
when WireType::FIXED32 then
|
22
|
+
when ::Protobuf::WireType::FIXED32 then
|
27
23
|
read_fixed32(stream)
|
24
|
+
when ::Protobuf::WireType::START_GROUP then
|
25
|
+
raise NotImplementedError, 'Group is deprecated.'
|
26
|
+
when ::Protobuf::WireType::END_GROUP then
|
27
|
+
raise NotImplementedError, 'Group is deprecated.'
|
28
28
|
else
|
29
29
|
raise InvalidWireType, wire_type
|
30
30
|
end
|
@@ -69,15 +69,5 @@ module Protobuf
|
|
69
69
|
stream.read(value_length)
|
70
70
|
end
|
71
71
|
|
72
|
-
# Not implemented.
|
73
|
-
def read_start_group(stream)
|
74
|
-
raise NotImplementedError, 'Group is deprecated.'
|
75
|
-
end
|
76
|
-
|
77
|
-
# Not implemented.
|
78
|
-
def read_end_group(stream)
|
79
|
-
raise NotImplementedError, 'Group is deprecated.'
|
80
|
-
end
|
81
|
-
|
82
72
|
end
|
83
73
|
end
|
data/lib/protobuf/version.rb
CHANGED
@@ -42,7 +42,7 @@ describe 'Functional EventMachine Client' do
|
|
42
42
|
end
|
43
43
|
EM.stop
|
44
44
|
end
|
45
|
-
error.message.should =~ /
|
45
|
+
error.message.should =~ /name.*required/
|
46
46
|
end
|
47
47
|
|
48
48
|
it 'calls the on_failure callback when the request type is wrong' do
|
@@ -41,7 +41,7 @@ describe 'Functional Socket Client' do
|
|
41
41
|
c.on_success { raise "shouldn't pass"}
|
42
42
|
c.on_failure {|e| error = e}
|
43
43
|
end
|
44
|
-
error.message.should =~ /
|
44
|
+
error.message.should =~ /name.*required/
|
45
45
|
end
|
46
46
|
|
47
47
|
it 'calls the on_failure callback when the request type is wrong' do
|
@@ -41,7 +41,7 @@ describe 'Functional ZMQ Client' do
|
|
41
41
|
c.on_success { raise "shouldn't pass"}
|
42
42
|
c.on_failure {|e| error = e}
|
43
43
|
end
|
44
|
-
error.message.should =~ /
|
44
|
+
error.message.should =~ /name.*required/
|
45
45
|
end
|
46
46
|
|
47
47
|
it 'calls the on_failure callback when the request type is wrong' do
|
@@ -20,6 +20,19 @@ describe Protobuf::Message do
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
+
describe '#encode' do
|
24
|
+
context "when there's no value for a required field" do
|
25
|
+
let(:message) { ::Test::Resource.new }
|
26
|
+
let(:stream) { StringIO.new }
|
27
|
+
|
28
|
+
it "raises a 'message not initialized' error" do
|
29
|
+
expect {
|
30
|
+
message.serialize_to(stream)
|
31
|
+
}.to raise_error(Protobuf::SerializationError, /required/i)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
23
36
|
describe "boolean predicate methods" do
|
24
37
|
subject { Test::ResourceFindRequest.new(:name => "resource") }
|
25
38
|
|
@@ -152,19 +165,19 @@ describe Protobuf::Message do
|
|
152
165
|
specify do
|
153
166
|
expect {
|
154
167
|
subject.get_field_by_name(1)
|
155
|
-
}.to raise_error(::Protobuf::
|
168
|
+
}.to raise_error(::Protobuf::FieldNotDefinedError, /.*1.*#{subject.class.name}/)
|
156
169
|
end
|
157
170
|
|
158
171
|
specify do
|
159
172
|
expect {
|
160
173
|
subject.get_field_by_name(:nothere)
|
161
|
-
}.to raise_error(::Protobuf::
|
174
|
+
}.to raise_error(::Protobuf::FieldNotDefinedError, /.*nothere.*#{subject.class.name}/)
|
162
175
|
end
|
163
176
|
|
164
177
|
specify do
|
165
178
|
expect {
|
166
179
|
subject.get_field_by_name(nil)
|
167
|
-
}.to raise_error(::Protobuf::
|
180
|
+
}.to raise_error(::Protobuf::FieldNotDefinedError, /.*nil.*#{subject.class.name}/)
|
168
181
|
end
|
169
182
|
end
|
170
183
|
end
|
@@ -206,13 +219,13 @@ describe Protobuf::Message do
|
|
206
219
|
specify do
|
207
220
|
expect {
|
208
221
|
subject.get_field_by_tag("not a number")
|
209
|
-
}.to raise_error(::Protobuf::
|
222
|
+
}.to raise_error(::Protobuf::FieldNotDefinedError, /.*not a number.*#{subject.class.name}/)
|
210
223
|
end
|
211
224
|
|
212
225
|
specify do
|
213
226
|
expect {
|
214
227
|
subject.get_field_by_tag(nil)
|
215
|
-
}.to raise_error(::Protobuf::
|
228
|
+
}.to raise_error(::Protobuf::FieldNotDefinedError, /.*nil.*#{subject.class.name}/)
|
216
229
|
end
|
217
230
|
end
|
218
231
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: protobuf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-11-
|
13
|
+
date: 2012-11-16 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activesupport
|
@@ -369,7 +369,6 @@ files:
|
|
369
369
|
- lib/protobuf/logger.rb
|
370
370
|
- lib/protobuf/message.rb
|
371
371
|
- lib/protobuf/message/decoder.rb
|
372
|
-
- lib/protobuf/message/encoder.rb
|
373
372
|
- lib/protobuf/message/extend.rb
|
374
373
|
- lib/protobuf/message/message.rb
|
375
374
|
- lib/protobuf/rpc/buffer.rb
|
@@ -416,7 +415,6 @@ files:
|
|
416
415
|
- spec/lib/protobuf/enum_spec.rb
|
417
416
|
- spec/lib/protobuf/enum_value_spec.rb
|
418
417
|
- spec/lib/protobuf/logger_spec.rb
|
419
|
-
- spec/lib/protobuf/message/encoder_spec.rb
|
420
418
|
- spec/lib/protobuf/message_spec.rb
|
421
419
|
- spec/lib/protobuf/rpc/client_spec.rb
|
422
420
|
- spec/lib/protobuf/rpc/connector_spec.rb
|
@@ -501,7 +499,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
501
499
|
version: '0'
|
502
500
|
segments:
|
503
501
|
- 0
|
504
|
-
hash:
|
502
|
+
hash: 1889105763094487003
|
505
503
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
506
504
|
none: false
|
507
505
|
requirements:
|
@@ -510,7 +508,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
510
508
|
version: '0'
|
511
509
|
segments:
|
512
510
|
- 0
|
513
|
-
hash:
|
511
|
+
hash: 1889105763094487003
|
514
512
|
requirements: []
|
515
513
|
rubyforge_project:
|
516
514
|
rubygems_version: 1.8.24
|
@@ -1,46 +0,0 @@
|
|
1
|
-
require 'protobuf/wire_type'
|
2
|
-
require 'protobuf/exceptions'
|
3
|
-
|
4
|
-
module Protobuf
|
5
|
-
|
6
|
-
module Encoder
|
7
|
-
|
8
|
-
module_function
|
9
|
-
|
10
|
-
# Encode +message+ and write to +stream+.
|
11
|
-
def encode(stream, message)
|
12
|
-
# FIXME make this not as ghetto
|
13
|
-
unless message.initialized?
|
14
|
-
raise NotInitializedError, "Message #{message.class.name} is not initialized (one or more fields is improperly set): #{JSON.parse(message.to_json)}"
|
15
|
-
end
|
16
|
-
|
17
|
-
message.each_field_for_serialization do |field, value|
|
18
|
-
if field.repeated?
|
19
|
-
if field.packed?
|
20
|
-
key = (field.tag << 3) | WireType::LENGTH_DELIMITED
|
21
|
-
packed_value = value.map {|val| field.encode(val) }.join
|
22
|
-
stream.write(Field::VarintField.encode(key))
|
23
|
-
stream.write(Field::VarintField.encode(packed_value.size))
|
24
|
-
stream.write(packed_value)
|
25
|
-
else
|
26
|
-
value.each do |val|
|
27
|
-
write_pair(stream, field, val)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
else
|
31
|
-
write_pair(stream, field, value)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
# Encode key and value, and write to +stream+.
|
37
|
-
def write_pair(stream, field, value)
|
38
|
-
key = (field.tag << 3) | field.wire_type
|
39
|
-
key_bytes = Field::VarintField.encode(key)
|
40
|
-
stream.write(key_bytes)
|
41
|
-
bytes = field.encode(value)
|
42
|
-
stream.write(bytes)
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|
46
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
class EncoderTest
|
4
|
-
extend ::Protobuf::Encoder
|
5
|
-
end
|
6
|
-
|
7
|
-
describe Protobuf::Encoder do
|
8
|
-
describe '#encode' do
|
9
|
-
context "when there's no value for a required field" do
|
10
|
-
let(:message) { ::Test::Resource.new }
|
11
|
-
let(:stream) { StringIO.new }
|
12
|
-
it "raises a 'message not initialized' error" do
|
13
|
-
expect {
|
14
|
-
EncoderTest.__send__(:encode, stream, message)
|
15
|
-
}.to raise_error(Protobuf::NotInitializedError, /message.*not initialized/i)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|