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 +4 -4
- data/lib/protobuf.rb +2 -1
- data/lib/protobuf/enum.rb +9 -0
- data/lib/protobuf/field/bytes_field.rb +4 -0
- data/lib/protobuf/field/field_array.rb +15 -0
- data/lib/protobuf/field/field_hash.rb +18 -0
- data/lib/protobuf/field/string_field.rb +3 -0
- data/lib/protobuf/message.rb +27 -1
- data/lib/protobuf/version.rb +1 -1
- data/spec/lib/protobuf/enum_spec.rb +7 -0
- data/spec/lib/protobuf/message_spec.rb +10 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c906a5d207f967c1bf49df971175e6e3f702be223e5a28eee2995bfe79f23d5e
|
4
|
+
data.tar.gz: c8a1b8ab9e6a8fc8bb2442d1c76cd7f926c8b1a4517e19d6933694779acc752f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: db8270d6a319265c66d79d735d477f8079eb1bf13147dc8a8e907b6be3e01bddb987249a2ecbd3e0985c47faf66344eb6b710e593c892ce9aa216f8b87496a5a
|
7
|
+
data.tar.gz: f40a2fdfff2fe27bb602873785699020f0567fb60fb9cdbfdf88002461e513789e624c644d67cc59d1c80ed4d9c76a76854fdd05e527640265bc3f8bef09f750
|
data/lib/protobuf.rb
CHANGED
data/lib/protobuf/enum.rb
CHANGED
@@ -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
|
@@ -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
|
data/lib/protobuf/message.rb
CHANGED
@@ -145,7 +145,32 @@ module Protobuf
|
|
145
145
|
end
|
146
146
|
|
147
147
|
def to_json(options = {})
|
148
|
-
|
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?
|
data/lib/protobuf/version.rb
CHANGED
@@ -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.
|
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-
|
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.
|
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.
|