protobuf 2.4.3.rc1 → 2.4.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,6 +4,15 @@ require 'protobuf/field/field_array'
4
4
  module Protobuf
5
5
  module Field
6
6
  class BaseField
7
+ ##
8
+ # Constants
9
+ #
10
+ PACKED_TYPES = [
11
+ ::Protobuf::WireType::VARINT,
12
+ ::Protobuf::WireType::FIXED32,
13
+ ::Protobuf::WireType::FIXED64
14
+ ].freeze
15
+
7
16
  ##
8
17
  # Attributes
9
18
  #
@@ -29,24 +38,11 @@ module Protobuf
29
38
  @extension = options.delete(:extension)
30
39
  @packed = repeated? && options.delete(:packed)
31
40
  @deprecated = options.delete(:deprecated)
32
- unless options.empty?
33
- warn "WARNING: Invalid options: #{options.inspect} (in #{@message_class.name.split('::').last}.#{@name})"
34
- end
35
-
36
- if packed? && ! [WireType::VARINT, WireType::FIXED32, WireType::FIXED64].include?(wire_type)
37
- raise "Can't use packed encoding for `#{@type}' type"
38
- end
39
-
40
- @default_value = \
41
- case @rule
42
- when :repeated then
43
- ::Protobuf::Field::FieldArray.new(self).freeze
44
- when :required then
45
- nil
46
- when :optional then
47
- typed_default_value
48
- end
49
41
 
42
+ set_rule_predicates
43
+ set_default_value
44
+ warn_excess_options(options) unless options.empty?
45
+ validate_packed_field if packed?
50
46
  define_accessor
51
47
  end
52
48
 
@@ -58,13 +54,11 @@ module Protobuf
58
54
  end
59
55
 
60
56
  def enum?
61
- return @_enum unless @_enum.nil?
62
- @_enum = (self.class < ::Protobuf::Field::EnumField)
57
+ false
63
58
  end
64
59
 
65
60
  def message?
66
- return @_message unless @_message.nil?
67
- @_message = (self.class < ::Protobuf::Field::MessageField)
61
+ false
68
62
  end
69
63
 
70
64
  # Decode +bytes+ and pass to +message_instance+.
@@ -103,25 +97,22 @@ module Protobuf
103
97
  end
104
98
 
105
99
  def extension?
106
- !!@extension
100
+ !! @extension
107
101
  end
108
102
 
109
103
  # Is this a repeated field?
110
104
  def repeated?
111
- return @_repeated unless @_repeated.nil?
112
- @_repeated = (@rule == :repeated)
105
+ !! @repeated
113
106
  end
114
107
 
115
108
  # Is this a required field?
116
109
  def required?
117
- return @_required unless @_required.nil?
118
- @_required = (@rule == :required)
110
+ !! @required
119
111
  end
120
112
 
121
113
  # Is this a optional field?
122
114
  def optional?
123
- return @_optional unless @_optional.nil?
124
- @_optional = (@rule == :optional)
115
+ !! @optional
125
116
  end
126
117
 
127
118
  # Is this a deprecated field?
@@ -184,8 +175,9 @@ module Protobuf
184
175
  @message_class.class_eval do
185
176
  define_method(field.setter_method_name) do |val|
186
177
  field.warn_if_deprecated
178
+ val.compact! if val.respond_to?(:compact!)
187
179
 
188
- if val.nil?
180
+ if val.nil? || (val.respond_to?(:empty?) && val.empty?)
189
181
  @values.delete(field.name)
190
182
  else
191
183
  @values[field.name] ||= ::Protobuf::Field::FieldArray.new(field)
@@ -211,7 +203,7 @@ module Protobuf
211
203
  define_method(field.setter_method_name) do |val|
212
204
  field.warn_if_deprecated
213
205
 
214
- if val.nil?
206
+ if val.nil? || (val.respond_to?(:empty?) && val.empty?)
215
207
  @values.delete(field.name)
216
208
  elsif field.acceptable?(val)
217
209
  @values[field.name] = val
@@ -222,6 +214,28 @@ module Protobuf
222
214
  end
223
215
  end
224
216
 
217
+ def set_default_value
218
+ @default_value = case
219
+ when repeated? then ::Protobuf::Field::FieldArray.new(self).freeze
220
+ when required? then nil
221
+ when optional? then typed_default_value
222
+ end
223
+ end
224
+
225
+ def set_rule_predicates
226
+ case @rule
227
+ when :repeated then
228
+ @required = @optional = false
229
+ @repeated = true
230
+ when :required then
231
+ @repeated = @optional = false
232
+ @required = true
233
+ when :optional then
234
+ @repeated = @required = false
235
+ @optional = true
236
+ end
237
+ end
238
+
225
239
  def typed_default_value
226
240
  if @default.nil?
227
241
  self.class.default
@@ -230,6 +244,16 @@ module Protobuf
230
244
  end
231
245
  end
232
246
 
247
+ def validate_packed_field
248
+ if packed? && ! ::Protobuf::Field::BaseField::PACKED_TYPES.include?(wire_type)
249
+ raise "Can't use packed encoding for `#{@type}' type"
250
+ end
251
+ end
252
+
253
+ def warn_excess_options(options)
254
+ warn "WARNING: Invalid options: #{options.inspect} (in #{@message_class.name.split('::').last}.#{@name})"
255
+ end
256
+
233
257
  end
234
258
  end
235
259
  end
@@ -1,14 +1,14 @@
1
+ require 'protobuf/wire_type'
2
+
1
3
  module Protobuf
2
4
  module Field
3
5
  class BytesField < BaseField
6
+ BYTES_ENCODING = "ASCII-8BIT".freeze
7
+
4
8
  def self.default
5
9
  ''
6
10
  end
7
11
 
8
- def wire_type
9
- WireType::LENGTH_DELIMITED
10
- end
11
-
12
12
  def acceptable?(val)
13
13
  if val.nil? || val.is_a?(::Protobuf::Message) || val.instance_of?(String)
14
14
  return true
@@ -18,20 +18,21 @@ module Protobuf
18
18
  end
19
19
 
20
20
  def decode(bytes)
21
- bytes.force_encoding(::Protobuf::Message::STRING_ENCODING) if bytes.respond_to?(:force_encoding)
21
+ bytes.force_encoding(::Protobuf::Field::BytesField::BYTES_ENCODING)
22
22
  bytes
23
23
  end
24
24
 
25
25
  def encode(value)
26
- if value.is_a?(::Protobuf::Message)
27
- value = value.serialize_to_string
28
- else
29
- value.force_encoding(::Protobuf::Message::STRING_ENCODING) if value.respond_to?(:force_encoding)
30
- end
26
+ value = value.serialize_to_string if value.is_a?(::Protobuf::Message)
27
+ value.force_encoding(::Protobuf::Field::BytesField::BYTES_ENCODING)
31
28
 
32
- string_size = VarintField.encode(value.size)
29
+ string_size = ::Protobuf::Field::VarintField.encode(value.size)
33
30
  string_size << value
34
31
  end
32
+
33
+ def wire_type
34
+ ::Protobuf::WireType::LENGTH_DELIMITED
35
+ end
35
36
  end
36
37
  end
37
38
  end
@@ -19,6 +19,10 @@ module Protobuf
19
19
  super(value.to_i)
20
20
  end
21
21
 
22
+ def enum?
23
+ true
24
+ end
25
+
22
26
  private
23
27
 
24
28
  def typed_default_value
@@ -28,7 +28,7 @@ module Protobuf
28
28
  end
29
29
 
30
30
  def replace(val)
31
- raise TypeError unless val.is_a?(Array)
31
+ raise_type_error(val) unless val.is_a?(Array)
32
32
  val = val.map { |v| normalize(v) }
33
33
  super(val)
34
34
  end
@@ -60,6 +60,15 @@ module Protobuf
60
60
  value
61
61
  end
62
62
  end
63
+
64
+ def raise_type_error(val)
65
+ error_text = <<-TYPE_ERROR
66
+ Expected value of type '#{@field.type}'
67
+ Got '#{val.class}' for protobuf field #{@field.name}
68
+ TYPE_ERROR
69
+
70
+ raise TypeError, error_text
71
+ end
63
72
  end
64
73
  end
65
74
  end
@@ -21,10 +21,14 @@ module Protobuf
21
21
 
22
22
  def encode(value)
23
23
  bytes = value.serialize_to_string
24
- result = VarintField.encode(bytes.size)
24
+ result = ::Protobuf::Field::VarintField.encode(bytes.size)
25
25
  result << bytes
26
26
  end
27
27
 
28
+ def message?
29
+ true
30
+ end
31
+
28
32
  def wire_type
29
33
  ::Protobuf::WireType::LENGTH_DELIMITED
30
34
  end
@@ -6,9 +6,15 @@ module Protobuf
6
6
  ENCODING = 'UTF-8'.freeze
7
7
 
8
8
  def decode(bytes)
9
- bytes.force_encoding(::Protobuf::Field::StringField::ENCODING) if bytes.respond_to?(:force_encoding)
9
+ bytes.force_encoding(::Protobuf::Field::StringField::ENCODING)
10
10
  bytes
11
11
  end
12
+
13
+ def encode(value)
14
+ value.force_encoding(::Protobuf::Field::StringField::ENCODING)
15
+ string_size = ::Protobuf::Field::VarintField.encode(value.size)
16
+ string_size << value
17
+ end
12
18
  end
13
19
  end
14
20
  end
@@ -49,7 +49,15 @@ module Protobuf
49
49
  end
50
50
 
51
51
  def encode(value)
52
- self.class.encode(value)
52
+ return [value].pack('C') if value < 128
53
+
54
+ bytes = []
55
+ until value == 0
56
+ bytes << (0x80 | (value & 0x7f))
57
+ value >>= 7
58
+ end
59
+ bytes[-1] &= 0x7f
60
+ bytes.pack('C*')
53
61
  end
54
62
 
55
63
  def wire_type
@@ -6,10 +6,6 @@ require 'protobuf/message/decoder'
6
6
 
7
7
  module Protobuf
8
8
  class Message
9
- ##
10
- # Constants
11
- #
12
- STRING_ENCODING = "ASCII-8BIT".freeze
13
9
 
14
10
  ##
15
11
  # Class Methods
@@ -160,7 +156,7 @@ module Protobuf
160
156
 
161
157
  def each_field_for_serialization
162
158
  all_fields.each do |field|
163
- next unless _field_must_be_serialized?(field)
159
+ next unless __field_must_be_serialized__?(field)
164
160
 
165
161
  value = @values[field.name]
166
162
 
@@ -269,6 +265,10 @@ module Protobuf
269
265
  to_hash.to_json
270
266
  end
271
267
 
268
+ def to_proto
269
+ self
270
+ end
271
+
272
272
  def ==(obj)
273
273
  return false unless obj.is_a?(self.class)
274
274
  each_field do |field, value|
@@ -297,6 +297,7 @@ module Protobuf
297
297
  # Instance Aliases
298
298
  #
299
299
  alias_method :to_hash_value, :to_hash
300
+ alias_method :to_proto_hash, :to_hash
300
301
  alias_method :to_s, :serialize_to_string
301
302
  alias_method :bytes, :serialize_to_string
302
303
  alias_method :serialize, :serialize_to_string
@@ -334,15 +335,13 @@ module Protobuf
334
335
  object
335
336
  end
336
337
 
337
- def _field_must_be_serialized?(field)
338
- return true if field.required?
339
-
340
- if field.repeated? && has_field?(field.name)
341
- @values[field.name].compact!
342
- @values.delete(field.name) if @values[field.name].empty?
343
- end
338
+ def __field_must_be_serialized__?(field)
339
+ field.required? || __field_value_is_present__?(field)
340
+ end
344
341
 
345
- return has_field?(field.name)
342
+ def __field_value_is_present__?(field)
343
+ value = @values[field.name]
344
+ !value.nil? && (value.present? || [true, false].include?(value))
346
345
  end
347
346
 
348
347
  # Encode key and value, and write to +stream+.
@@ -10,7 +10,7 @@ module Protobuf
10
10
  extend Forwardable
11
11
  include Protobuf::Logger::LogMethods
12
12
 
13
- delegate [:options, :complete_cb, :success_cb, :failure_cb] => :@connector
13
+ def_delegators :@connector, :options, :complete_cb, :success_cb, :failure_cb
14
14
  attr_reader :connector
15
15
 
16
16
  # Create a new client with default options (defined in ClientConnection)
@@ -9,7 +9,7 @@ module Protobuf
9
9
  include ::Protobuf::Rpc::Server
10
10
  include ::Protobuf::Logger::LogMethods
11
11
 
12
- AUTO_COLLECT_TIMEOUT = 20 # seconds
12
+ AUTO_COLLECT_TIMEOUT = 5 # seconds
13
13
 
14
14
  def self.cleanup?
15
15
  # every 10 connections run a cleanup routine after closing the response
@@ -1,4 +1,4 @@
1
1
  module Protobuf
2
- VERSION = '2.4.3.rc1'
2
+ VERSION = '2.4.3'
3
3
  PROTOC_VERSION = '2.4.1'
4
4
  end
@@ -40,6 +40,17 @@ describe Protobuf::Message do
40
40
  message.serialize_to_string
41
41
  }.to_not raise_error
42
42
  end
43
+
44
+ it "sets the value to nil when empty array is passed" do
45
+ message.repeated_enum = []
46
+ message.instance_variable_get("@values")[:repeated_enum].should be_nil
47
+ end
48
+
49
+ it "raises TypeError when a non-array replaces it" do
50
+ expect {
51
+ message.repeated_enum = 2
52
+ }.to raise_error(/value of type/)
53
+ end
43
54
  end
44
55
  end
45
56
 
@@ -9,7 +9,7 @@ describe Protobuf::Rpc::Client do
9
9
  context "when using fiber based calls" do
10
10
  it "waits for response" do
11
11
  EventMachine.fiber_run do
12
- StubServer.new(:delay => 3) do |server|
12
+ StubServer.new(:delay => 1) do |server|
13
13
  client = Test::ResourceService.client
14
14
  start = now
15
15
 
metadata CHANGED
@@ -1,8 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protobuf
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.3.rc1
5
- prerelease: 6
4
+ version: 2.4.3
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - BJ Neilsen
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-11-16 00:00:00.000000000 Z
13
+ date: 2012-11-17 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -498,13 +498,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
498
498
  version: '0'
499
499
  segments:
500
500
  - 0
501
- hash: 952004748095048200
501
+ hash: 1946796344651768063
502
502
  required_rubygems_version: !ruby/object:Gem::Requirement
503
503
  none: false
504
504
  requirements:
505
- - - ! '>'
505
+ - - ! '>='
506
506
  - !ruby/object:Gem::Version
507
- version: 1.3.1
507
+ version: '0'
508
+ segments:
509
+ - 0
510
+ hash: 1946796344651768063
508
511
  requirements: []
509
512
  rubyforge_project:
510
513
  rubygems_version: 1.8.24