prepor-beefcake 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b05eb3a8400e0af12563f45411a2e95cfb762ab6
4
- data.tar.gz: ea1e377af18cbfb73ee824731e2ecbb0b3550d98
3
+ metadata.gz: 14f8f3b9ce47e2ce5d95ffbc30ac2cb85c9d16a9
4
+ data.tar.gz: b8438b597123b5a75e3e261255d93b36c959aff0
5
5
  SHA512:
6
- metadata.gz: 21a7d5536ea0f5181357987ccfb8a9e4ff20757e2ee5e34a52b450d966cc62f5330b0d7b11c2aadbcbc92b22656d2afb8c1ed4ecd77ba9953deef87d8e46a052
7
- data.tar.gz: 40ae3e963de1fbcad69d9f8b48fbdaf5bfdfbea1de6b99b8e8c3e8803487d965b142a24e27ddc5d76a95902c448627329571fba5cd0aa2d9b95d0e45b91343ae
6
+ metadata.gz: ef80c4dc456a091607a1626987d794a0c0ecf81ef7b6f3dcfa6d8e84562bb7ead6941a6d05cd399c1db72bcb64a7ccc6efc4781aae82898de28f40bb10a327e3
7
+ data.tar.gz: f4916f34a41889d77a55893f5f8160143bfe47af18e37f920c6ff1cc4ae2db35239d91fca0b8078b2d8f22f8b77dfe5231d39cffe0a0eeb0f68a70833bfee871
data/.travis.yml CHANGED
@@ -3,6 +3,16 @@ rvm:
3
3
  - 1.9.3
4
4
  - 2.0.0
5
5
  - 2.1.0
6
- - 2.1.1
7
- - 2.1.2
6
+ - 2.1.6
7
+ - 2.2.0
8
+ - 2.2.1
9
+ - 2.2.2
8
10
  - jruby-19mode
11
+ - rbx-2.2.7
12
+ - jruby-9.0.0.0.pre2
13
+ - ruby-head
14
+ - jruby-head
15
+ matrix:
16
+ allow_failures:
17
+ - rvm: jruby-head
18
+ - rvm: ruby-head
data/README.md CHANGED
@@ -1,7 +1,6 @@
1
1
  # Beefcake
2
2
 
3
- A sane Google Protocol Buffers library for Ruby. It's all about being Buf;
4
- ProtoBuf.
3
+ A pure-Ruby Google Protocol Buffers library. It's all about being Buf; ProtoBuf.
5
4
 
6
5
  ## Installation
7
6
 
data/RELEASE_NOTES.md CHANGED
@@ -1,6 +1,17 @@
1
1
  # Beefcake Release Notes
2
2
 
3
- # 0.1.0 - 2014-09-05
3
+ # 1.1.0 - 2015-05-26
4
+
5
+ Release 1.1.0 includes improvements and bug fixes.
6
+
7
+ * Correct message-hash conversion behavior, fixed by Peter "kybu" Vrabel.
8
+ * Remove trailing whitespace in generator output without package name.
9
+ * Allow a Hash instead of an Array<Hash> when assigning a repeated field that's
10
+ only repeated once.
11
+ * Ensure object identities are persisted through message initialization,
12
+ reported by Peter Neubauer.
13
+
14
+ # 1.0.0 - 2014-09-05
4
15
 
5
16
  Release 1.0.0 includes changes and improvements.
6
17
 
@@ -21,7 +32,7 @@ Release 0.5.0 corrects a few behaviors.
21
32
  * Don't crash when attempting to reencode frozen strings,
22
33
  found thanks to Kyle "Aphyr" Kingsbury
23
34
  * Return `nil` instead of raising a generic Ruby error when trying to
24
- decode a zero-length buffer, fixed by Tobias "grobie" Schmidt
35
+ decode a zero-length buffer, fixed by Tobias "grobie" Schmidt
25
36
 
26
37
  # 0.4.0 - 2013-10-10
27
38
 
data/beefcake.gemspec CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |s|
9
9
  s.authors = ["Blake Mizerany", "Matt Proud", "Bryce Kerley"]
10
10
  s.email = ["blake.mizerany@gmail.com", "matt.proud@gmail.com", "bkerley@brycekerley.net"]
11
11
  s.homepage = "https://github.com/protobuf-ruby/beefcake"
12
- s.summary = %q{A sane protobuf library for Ruby}
13
- s.description = %q{A sane protobuf library for Ruby}
12
+ s.summary = %q{A pure-Ruby protobuf library}
13
+ s.description = %q{A pure-Ruby Protocol Buffers library}
14
14
  s.license = 'MIT'
15
15
 
16
16
  s.files = `git ls-files`.split("\n")
data/lib/beefcake.rb CHANGED
@@ -33,6 +33,22 @@ module Beefcake
33
33
  def <=>(o)
34
34
  fn <=> o.fn
35
35
  end
36
+
37
+ def same_type?(obj)
38
+ type == obj
39
+ end
40
+
41
+ def matches_type?(obj)
42
+ obj.is_a? type
43
+ end
44
+
45
+ def is_protobuf?
46
+ type.is_a?(Class) and type.include?(Beefcake::Message)
47
+ end
48
+
49
+ def required? ; rule == :required end
50
+ def repeated? ; rule == :repeated end
51
+ def optional? ; rule == :optional end
36
52
  end
37
53
 
38
54
 
@@ -185,7 +201,8 @@ module Beefcake
185
201
  end
186
202
  elsif fld.rule == :repeated
187
203
  val = buf.read(fld.type)
188
- (o[fld.name] ||= []) << val
204
+ o[fld.name] ||= []
205
+ o[fld.name] << val
189
206
  else
190
207
  val = buf.read(fld.type)
191
208
  o[fld.name] = val
@@ -224,10 +241,66 @@ module Beefcake
224
241
  o.send(:include, Encode)
225
242
  end
226
243
 
244
+ # (see #assign)
227
245
  def initialize(attrs={})
246
+ assign attrs
247
+ end
248
+
249
+ # Handles filling a protobuf message from a hash. Embedded messages can
250
+ # be passed in two ways, by a pure hash or as an instance of embedded class(es).
251
+ #
252
+ # @example By a pure hash.
253
+ # {:field1 => 2, :embedded => {:embedded_f1 => 'lala'}}
254
+ #
255
+ # @example Repeated embedded message by a pure hash.
256
+ # {:field1 => 2, :embedded => [
257
+ # {:embedded_f1 => 'lala'},
258
+ # {:embedded_f1 => 'lulu'}
259
+ # ]}
260
+ #
261
+ # @example As an instance of embedded class.
262
+ # {:field1 => 2, :embedded => EmbeddedMsg.new({:embedded_f1 => 'lala'})}
263
+ #
264
+ # @param [Hash] data to fill a protobuf message with.
265
+ def assign(attrs)
228
266
  __beefcake_fields__.values.each do |fld|
229
- self[fld.name] = attrs[fld.name]
267
+ attribute = attrs[fld.name]
268
+
269
+ if attribute.nil?
270
+ self[fld.name] = nil
271
+ next
272
+ end
273
+
274
+ unless fld.is_protobuf?
275
+ self[fld.name] = attribute
276
+ next
277
+ end
278
+
279
+ if fld.repeated? && attribute.is_a?(Hash)
280
+ self[fld.name] = fld.type.new(attribute)
281
+ next
282
+ end
283
+
284
+ if fld.repeated? && attribute.is_a?(fld.type)
285
+ self[fld.name] = [attribute]
286
+ next
287
+ end
288
+
289
+ if fld.repeated?
290
+ self[fld.name] = attribute.map do |i|
291
+ fld.matches_type?(i) ? i : fld.type.new(i)
292
+ end
293
+ next
294
+ end
295
+
296
+ if fld.matches_type? attribute
297
+ self[fld.name] = attribute
298
+ next
299
+ end
300
+
301
+ self[fld.name] = fld.type.new(attribute)
230
302
  end
303
+ self
231
304
  end
232
305
 
233
306
  def __beefcake_fields__
@@ -270,10 +343,22 @@ module Beefcake
270
343
 
271
344
  def to_hash
272
345
  __beefcake_fields__.values.inject({}) do |h, fld|
273
- value = self[fld.name]
274
- unless value.nil?
275
- h[fld.name] = value
276
- end
346
+ v = self[fld.name]
347
+ next h if v.nil?
348
+
349
+ h[fld.name] =
350
+ case
351
+ when v.respond_to?(:to_hash)
352
+ # A nested protobuf message, so let's call its 'to_hash' method.
353
+ v.to_hash
354
+ when v.is_a?(Array)
355
+ # There can be two field types stored in array.
356
+ # Primitive type or nested another protobuf message.
357
+ # The later one has got a 'to_hash' method.
358
+ v.map { |i| i.respond_to?(:to_hash) ? i.to_hash : i }
359
+ else
360
+ v
361
+ end
277
362
  h
278
363
  end
279
364
  end
@@ -1,3 +1,3 @@
1
1
  module Beefcake
2
- VERSION = "1.1.0"
2
+ VERSION = "1.1.1"
3
3
  end
data/test/message_test.rb CHANGED
@@ -102,6 +102,70 @@ class FieldsMessage
102
102
  repeated :fields, :string, 1
103
103
  end
104
104
 
105
+ class FieldTest < Minitest::Test
106
+ FIELD = Beefcake::Message::Field
107
+
108
+ def test_required?
109
+ f = FIELD.new :required, :field1, :string, 1
110
+ assert f.required?
111
+ refute f.optional?
112
+ refute f.repeated?
113
+
114
+ f = FIELD.new :optional, :field1, :int32, 1
115
+ refute f.required?
116
+
117
+ f = FIELD.new :repeated, :field1, :int32, 1
118
+ refute f.required?
119
+ end
120
+
121
+ def test_optional?
122
+ f = FIELD.new :optional, :field1, :string, 1
123
+ assert f.optional?
124
+ refute f.required?
125
+ refute f.repeated?
126
+
127
+ f = FIELD.new :required, :field1, :int32, 1
128
+ refute f.optional?
129
+
130
+ f = FIELD.new :repeated, :field1, :int32, 1
131
+ refute f.optional?
132
+ end
133
+
134
+ def test_repeated?
135
+ f = FIELD.new :repeated, :field1, :string, 1
136
+ assert f.repeated?
137
+ refute f.required?
138
+ refute f.optional?
139
+
140
+ f = FIELD.new :optional, :field1, :int32, 1
141
+ refute f.repeated?
142
+
143
+ f = FIELD.new :required, :field1, :int32, 1
144
+ refute f.optional?
145
+ end
146
+
147
+ def test_same_type
148
+ f = FIELD.new(:required, :field1, :int32, 1)
149
+ assert f.same_type?(:int32)
150
+ refute f.same_type?(:int64)
151
+ refute f.same_type?(SimpleMessage)
152
+
153
+ f = FIELD.new(:required, :field1, SimpleMessage, 1)
154
+ assert f.same_type?(SimpleMessage)
155
+ refute f.same_type?(:int32)
156
+ refute f.same_type?(:string)
157
+ refute f.same_type?(NumericsMessage)
158
+ end
159
+
160
+ def test_is_protobuf?
161
+ f = FIELD.new(:required, :field1, SimpleMessage, 1)
162
+ assert f.is_protobuf?
163
+
164
+ f = FIELD.new(:required, :field1, :string, 1)
165
+ refute f.is_protobuf?
166
+ end
167
+ end
168
+
105
169
  class MessageTest < Minitest::Test
106
170
  B = Beefcake::Buffer
107
171
 
@@ -163,6 +227,12 @@ class MessageTest < Minitest::Test
163
227
  )
164
228
 
165
229
  assert_equal buf2.to_s, msg.encode.to_s
230
+
231
+ msg = CompositeMessage.new(
232
+ :encodable => {:a => 123}
233
+ )
234
+ assert_equal buf2.to_s, msg.encode.to_s
235
+
166
236
  end
167
237
 
168
238
  def test_encode_to_string
@@ -196,6 +266,9 @@ class MessageTest < Minitest::Test
196
266
 
197
267
  msg = EnumsMessage.new :a => EnumsMessage::X::A
198
268
  assert_equal "\b\001", msg.encode.to_s
269
+
270
+ msg = EnumsMessage.new :a => 1
271
+ assert_equal "\b\001", msg.encode.to_s
199
272
  end
200
273
 
201
274
  def test_encode_invalid_enum_value
@@ -269,6 +342,16 @@ class MessageTest < Minitest::Test
269
342
  msg.encode.to_s
270
343
  end
271
344
 
345
+ def test_repeated_with_hash
346
+ before = { simple: { b: 'hello' } }
347
+ RepeatedNestedMessage.new before
348
+ end
349
+
350
+ def test_repeated_with_scalar
351
+ inner = SimpleMessage.new(b: 'hello')
352
+ RepeatedNestedMessage.new simple: inner
353
+ end
354
+
272
355
  ## Decoding
273
356
  def test_decode_numerics
274
357
  msg = NumericsMessage.new({
@@ -361,6 +444,16 @@ class MessageTest < Minitest::Test
361
444
  assert_equal 2, got.simple.size
362
445
  assert_equal 1, got.simple[0].a
363
446
  assert_equal "hello", got.simple[1].b
447
+
448
+ simple_pure = [
449
+ {:a => 1},
450
+ {:b => "hello"}
451
+ ]
452
+ msg = RepeatedNestedMessage.new(:simple => simple_pure).encode
453
+ got = RepeatedNestedMessage.decode(msg)
454
+ assert_equal 2, got.simple.size
455
+ assert_equal 1, got.simple[0].a
456
+ assert_equal "hello", got.simple[1].b
364
457
  end
365
458
 
366
459
  def test_equality
@@ -378,6 +471,13 @@ class MessageTest < Minitest::Test
378
471
  refute_equal :symbol, d
379
472
  end
380
473
 
474
+ def test_object_identity
475
+ inside = [SimpleMessage.new(a: 12345)]
476
+ outside = RepeatedNestedMessage.new simple: inside
477
+
478
+ assert_equal outside.simple.first.object_id, inside.first.object_id
479
+ end
480
+
381
481
  def test_inspect
382
482
  msg = SimpleMessage.new :a => 1
383
483
  assert_equal "<SimpleMessage a: 1>", msg.inspect
@@ -409,7 +509,23 @@ class MessageTest < Minitest::Test
409
509
  def test_to_hash
410
510
  msg = SimpleMessage.new :a => 1
411
511
  exp = { :a => 1 }
412
- assert_equal(exp, msg.to_hash)
512
+ assert_equal exp, msg.to_hash
513
+
514
+ msg = RepeatedNestedMessage.new(
515
+ :simple => [
516
+ SimpleMessage.new(:a => 1),
517
+ SimpleMessage.new(:b => 'abc dfg'),
518
+ SimpleMessage.new(:a => 2, :b => 'ijk lmn')
519
+ ]
520
+ )
521
+ exp = {
522
+ :simple =>[
523
+ {:a => 1},
524
+ {:b => 'abc dfg'},
525
+ {:a => 2, :b => 'ijk lmn'}
526
+ ]
527
+ }
528
+ assert_equal exp, msg.to_hash
413
529
  end
414
530
 
415
531
  def test_duplicate_index
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prepor-beefcake
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blake Mizerany
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-04-07 00:00:00.000000000 Z
13
+ date: 2015-06-01 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -40,7 +40,7 @@ dependencies:
40
40
  - - "~>"
41
41
  - !ruby/object:Gem::Version
42
42
  version: '5.3'
43
- description: A sane protobuf library for Ruby
43
+ description: A pure-Ruby Protocol Buffers library
44
44
  email:
45
45
  - blake.mizerany@gmail.com
46
46
  - matt.proud@gmail.com
@@ -97,7 +97,7 @@ rubyforge_project:
97
97
  rubygems_version: 2.4.3
98
98
  signing_key:
99
99
  specification_version: 4
100
- summary: A sane protobuf library for Ruby
100
+ summary: A pure-Ruby protobuf library
101
101
  test_files:
102
102
  - test/benchmark_test.rb
103
103
  - test/buffer_decode_test.rb