protobuf 3.1.0 → 3.2.0

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: 8ad445d76d68be198e63dfba389d2b5deaf314ee
4
- data.tar.gz: 3d36e1ce0747a04e82419c2a1ea8670fff05ace9
3
+ metadata.gz: 8e5a341fcb307dfd13d03e56530493f316dadff4
4
+ data.tar.gz: 1dde8b64fe387bd0bcd87e5823e02bec8322bf25
5
5
  SHA512:
6
- metadata.gz: a9f9e6009fc897608088233604949cbde5353950a2984108185e7b167e00335f53156ba46e0c60b1945a2d13052ce96508932b43e6aaf2b22964378485ec9701
7
- data.tar.gz: d1b13590c827e3208b8a41bc267faf40e98aa1371f08255cfe02e490e77fb73f9b2667ee3c94a0430db7c274dcda2c5889c6d2810d3a56b0f04b69436845267f
6
+ metadata.gz: 5511c46d74eb0e669400ab394fef865e4e1a8c0a919a967a1d1b97fba19e0dcb8073c08155b9a4fada0043bafb803adaad1d3019f3f293e367c41d9873ba6c10
7
+ data.tar.gz: 9c5f985a0ab726096f1bb662e48b2a9fe78133a5d523421ccdb93b43aad6aabc1fd45cce0e7d9643c9176ff0bc2bf02f16b2b8ad82e3ab99a176539ce392acbf
@@ -83,6 +83,19 @@ module Protobuf
83
83
  @_print_deprecation_warnings = !!value
84
84
  end
85
85
 
86
+ # Permit unknown field on Message initialization
87
+ #
88
+ # Default: true
89
+ #
90
+ # Simple boolean to define whether we want to permit unknown fields
91
+ # on Message intialization; otherwise a ::Protobuf::FieldNotDefinedError is thrown.
92
+ def self.ignore_unknown_fields?
93
+ !defined?(@_ignore_unknown_fields) || @_ignore_unknown_fields
94
+ end
95
+
96
+ def self.ignore_unknown_fields=(value)
97
+ @_ignore_unknown_fields = !!value
98
+ end
86
99
  end
87
100
 
88
101
  unless ENV.key?('PB_NO_NETWORKING')
@@ -30,7 +30,7 @@ module Protobuf
30
30
  @values = {}
31
31
 
32
32
  fields.to_hash.each_pair do |name, value|
33
- self[name] = value unless value.nil?
33
+ self[name] = value
34
34
  end
35
35
  end
36
36
 
@@ -135,7 +135,11 @@ module Protobuf
135
135
 
136
136
  def []=(name, value)
137
137
  if field = self.class.get_field(name, true)
138
- __send__(field.setter_method_name, value)
138
+ __send__(field.setter_method_name, value) unless value.nil?
139
+ else
140
+ unless ::Protobuf.ignore_unknown_fields?
141
+ raise ::Protobuf::FieldNotDefinedError, name
142
+ end
139
143
  end
140
144
  end
141
145
 
@@ -1,3 +1,3 @@
1
1
  module Protobuf
2
- VERSION = '3.1.0'
2
+ VERSION = '3.2.0'
3
3
  end
@@ -157,6 +157,55 @@ describe Protobuf::Message do
157
157
  test_enum = Test::EnumTestMessage.new(hashie_object)
158
158
  expect(test_enum.non_default_enum).to eq(2)
159
159
  end
160
+
161
+ context 'ignoring unknown fields' do
162
+ before { ::Protobuf.ignore_unknown_fields = true }
163
+
164
+ context 'with valid fields' do
165
+ let(:values) { { :name => "Jim" } }
166
+
167
+ it "does not raise an error" do
168
+ expect { ::Test::Resource.new(values) }.to_not raise_error
169
+ end
170
+ end
171
+
172
+ context 'with non-existent field' do
173
+ let(:values) { { :name => "Jim", :othername => "invalid" } }
174
+
175
+ it "does not raise an error" do
176
+ expect { ::Test::Resource.new(values) }.to_not raise_error
177
+ end
178
+ end
179
+ end
180
+
181
+ context 'not ignoring unknown fields' do
182
+ before { ::Protobuf.ignore_unknown_fields = false }
183
+ after { ::Protobuf.ignore_unknown_fields = true }
184
+
185
+ context 'with valid fields' do
186
+ let(:values) { { :name => "Jim" } }
187
+
188
+ it "does not raise an error" do
189
+ expect { ::Test::Resource.new(values) }.to_not raise_error
190
+ end
191
+ end
192
+
193
+ context 'with non-existent field' do
194
+ let(:values) { { :name => "Jim", :othername => "invalid" } }
195
+
196
+ it "raises an error and mentions the erroneous field" do
197
+ expect { ::Test::Resource.new(values) }.to raise_error(::Protobuf::FieldNotDefinedError, /othername/)
198
+ end
199
+
200
+ context 'with a nil value' do
201
+ let(:values) { { :name => "Jim", :othername => nil } }
202
+
203
+ it "raises an error and mentions the erroneous field" do
204
+ expect { ::Test::Resource.new(values) }.to raise_error(::Protobuf::FieldNotDefinedError, /othername/)
205
+ end
206
+ end
207
+ end
208
+ end
160
209
  end
161
210
 
162
211
  describe '#encode' do
@@ -75,4 +75,24 @@ describe ::Protobuf do
75
75
  end
76
76
  end
77
77
 
78
+ describe '.ignore_unknown_fields?' do
79
+ before do
80
+ if described_class.instance_variable_defined?(:@_ignore_unknown_fields)
81
+ described_class.send(:remove_instance_variable, :@_ignore_unknown_fields)
82
+ end
83
+ end
84
+
85
+ it 'defaults to a true value' do
86
+ expect(described_class.ignore_unknown_fields?).to be true
87
+ end
88
+
89
+ it 'is settable' do
90
+ expect {
91
+ described_class.ignore_unknown_fields = false
92
+ }.to change {
93
+ described_class.ignore_unknown_fields?
94
+ }.from(true).to(false)
95
+ end
96
+ end
97
+
78
98
  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: 3.1.0
4
+ version: 3.2.0
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: 2014-07-28 00:00:00.000000000 Z
14
+ date: 2014-08-01 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport