protobuf 3.8.2 → 3.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ef917148ada7494b0e7924434b0faa4e3a36155ac0893f687360f01b55b829d6
4
- data.tar.gz: be5ae5cb6ff72a6c9151834da970137c0382f7177a2c8792458115c8a5d53df7
3
+ metadata.gz: c906a5d207f967c1bf49df971175e6e3f702be223e5a28eee2995bfe79f23d5e
4
+ data.tar.gz: c8a1b8ab9e6a8fc8bb2442d1c76cd7f926c8b1a4517e19d6933694779acc752f
5
5
  SHA512:
6
- metadata.gz: 5594c91e41d5d0787874210f81865d39a5931b27ddf3a66178bce2e7125c6c293414492ad3e9ec9c02eda898e6c106874b2656549d61ffeb1f982fd596637a00
7
- data.tar.gz: 2f6b14d78465a055be950fa1ba49a24f49e75548f28625ef8830eb8b77386a4a95266664693cac3ae02079071d27e44bc9e2e0f73ef32e56f7a4861003f5dfdd
6
+ metadata.gz: db8270d6a319265c66d79d735d477f8079eb1bf13147dc8a8e907b6be3e01bddb987249a2ecbd3e0985c47faf66344eb6b710e593c892ce9aa216f8b87496a5a
7
+ data.tar.gz: f40a2fdfff2fe27bb602873785699020f0567fb60fb9cdbfdf88002461e513789e624c644d67cc59d1c80ed4d9c76a76854fdd05e527640265bc3f8bef09f750
@@ -1,6 +1,7 @@
1
+ require 'base64'
1
2
  require 'logger'
2
- require 'socket'
3
3
  require 'pp'
4
+ require 'socket'
4
5
  require 'stringio'
5
6
 
6
7
  require 'active_support/core_ext/object/blank'
@@ -283,6 +283,14 @@ module Protobuf
283
283
  tag.to_int
284
284
  end
285
285
 
286
+ ##
287
+ # This fixes a reflection bug in JrJackson RubyAnySerializer that does not
288
+ # render Protobuf enums correctly because to_json is not defined.
289
+ #
290
+ def to_json
291
+ to_i
292
+ end
293
+
286
294
  def to_s(format = :tag)
287
295
  case format
288
296
  when :tag
@@ -316,5 +324,6 @@ module Protobuf
316
324
  # Instance Aliases
317
325
  #
318
326
  alias :to_hash_value to_i
327
+ alias :to_json_hash_value to_i
319
328
  end
320
329
  end
@@ -69,6 +69,10 @@ module Protobuf
69
69
  fail TypeError, "Unacceptable value #{value} for field #{name} of type #{type_class}"
70
70
  end
71
71
  end
72
+
73
+ def json_encode(value)
74
+ Base64.strict_encode64(value)
75
+ end
72
76
  end
73
77
  end
74
78
  end
@@ -46,6 +46,21 @@ module Protobuf
46
46
  end
47
47
  end
48
48
 
49
+ # Return a hash-representation of the given values for this field type
50
+ # that is safe to convert to JSON.
51
+ # The value in this case would be an array.
52
+ def to_json_hash_value
53
+ if field.respond_to?(:json_encode)
54
+ map do |value|
55
+ field.json_encode(value)
56
+ end
57
+ else
58
+ map do |value|
59
+ value.respond_to?(:to_json_hash_value) ? value.to_json_hash_value : value
60
+ end
61
+ end
62
+ end
63
+
49
64
  def to_s
50
65
  "[#{field.name}]"
51
66
  end
@@ -52,6 +52,24 @@ module Protobuf
52
52
  end
53
53
  end
54
54
 
55
+ # Return a hash-representation of the given values for this field type
56
+ # that is safe to convert to JSON.
57
+ #
58
+ # The value in this case would be the hash itself, right? Unfortunately
59
+ # not because the values of the map could be messages themselves that we
60
+ # need to transform.
61
+ def to_json_hash_value
62
+ if field.respond_to?(:json_encode)
63
+ each_with_object({}) do |(key, value), hash|
64
+ hash[key] = field.json_encode(value)
65
+ end
66
+ else
67
+ each_with_object({}) do |(key, value), hash|
68
+ hash[key] = value.respond_to?(:to_json_hash_value) ? value.to_json_hash_value : value
69
+ end
70
+ end
71
+ end
72
+
55
73
  def to_s
56
74
  "{#{field.name}}"
57
75
  end
@@ -38,6 +38,9 @@ module Protobuf
38
38
  stream << tag_encoded << byte_size << value
39
39
  end
40
40
 
41
+ def json_encode(value)
42
+ value
43
+ end
41
44
  end
42
45
  end
43
46
  end
@@ -145,7 +145,32 @@ module Protobuf
145
145
  end
146
146
 
147
147
  def to_json(options = {})
148
- to_hash.to_json(options)
148
+ to_json_hash.to_json(options)
149
+ end
150
+
151
+ # Return a hash-representation of the given fields for this message type that
152
+ # is safe to convert to JSON.
153
+ def to_json_hash
154
+ result = {}
155
+
156
+ @values.each_key do |field_name|
157
+ value = self[field_name]
158
+ field = self.class.get_field(field_name, true)
159
+
160
+ # NB: to_json_hash_value should come before json_encode so as to handle
161
+ # repeated fields without extra logic.
162
+ hashed_value = if value.respond_to?(:to_json_hash_value)
163
+ value.to_json_hash_value
164
+ elsif field.respond_to?(:json_encode)
165
+ field.json_encode(value)
166
+ else
167
+ value
168
+ end
169
+
170
+ result[field.name] = hashed_value
171
+ end
172
+
173
+ result
149
174
  end
150
175
 
151
176
  def to_proto
@@ -179,6 +204,7 @@ module Protobuf
179
204
  # Instance Aliases
180
205
  #
181
206
  alias :to_hash_value to_hash
207
+ alias :to_json_hash_value to_json_hash
182
208
  alias :to_proto_hash to_hash
183
209
  alias :responds_to_has? respond_to_has?
184
210
  alias :respond_to_and_has? respond_to_has?
@@ -1,3 +1,3 @@
1
1
  module Protobuf
2
- VERSION = '3.8.2' # rubocop:disable Style/MutableConstant
2
+ VERSION = '3.8.3' # rubocop:disable Style/MutableConstant
3
3
  end
@@ -160,6 +160,13 @@ RSpec.describe Protobuf::Enum do
160
160
  end
161
161
  end
162
162
 
163
+ describe '.to_json' do
164
+ it 'renders the enum value' do
165
+ expect(Test::EnumTestType::ONE.to_json).to eq(1)
166
+ expect({ :value => Test::EnumTestType::ONE }.to_json).to eq(%({"value":1}))
167
+ end
168
+ end
169
+
163
170
  describe '.valid_tag?' do
164
171
  context 'when tag is defined' do
165
172
  specify { expect(Test::EnumTestType.valid_tag?(tag)).to be true }
@@ -427,6 +427,16 @@ RSpec.describe Protobuf::Message do
427
427
  end
428
428
 
429
429
  specify { expect(subject.to_json).to eq '{"name":"Test Name","active":false}' }
430
+
431
+ context 'for byte fields' do
432
+ let(:bytes) { "\x06\x8D1HP\x17:b" }
433
+
434
+ subject do
435
+ ::Test::ResourceFindRequest.new(:widget_bytes => [bytes])
436
+ end
437
+
438
+ specify { expect(subject.to_json).to eq '{"widget_bytes":["Bo0xSFAXOmI="]}' }
439
+ end
430
440
  end
431
441
 
432
442
  describe '.to_json' do
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: 3.8.2
4
+ version: 3.8.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - BJ Neilsen
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2018-02-22 00:00:00.000000000 Z
14
+ date: 2018-06-08 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport
@@ -468,7 +468,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
468
468
  version: '0'
469
469
  requirements: []
470
470
  rubyforge_project:
471
- rubygems_version: 2.7.4
471
+ rubygems_version: 2.7.6
472
472
  signing_key:
473
473
  specification_version: 4
474
474
  summary: Google Protocol Buffers serialization and RPC implementation for Ruby.