protobuf 3.8.5 → 3.9.0.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -0
- data/.travis.yml +3 -4
- data/lib/protobuf.rb +14 -0
- data/lib/protobuf/code_generator.rb +30 -3
- data/lib/protobuf/encoder.rb +1 -12
- data/lib/protobuf/enum.rb +11 -9
- data/lib/protobuf/field/base_field.rb +177 -34
- data/lib/protobuf/field/base_field_object_definitions.rb +504 -0
- data/lib/protobuf/field/bool_field.rb +13 -8
- data/lib/protobuf/field/bytes_field.rb +8 -19
- data/lib/protobuf/field/enum_field.rb +9 -11
- data/lib/protobuf/field/int64_field.rb +13 -0
- data/lib/protobuf/field/message_field.rb +1 -1
- data/lib/protobuf/field/string_field.rb +20 -15
- data/lib/protobuf/field/varint_field.rb +13 -26
- data/lib/protobuf/message.rb +44 -99
- data/lib/protobuf/message/fields.rb +16 -0
- data/lib/protobuf/message/serialization.rb +1 -1
- data/lib/protobuf/varint.rb +11 -0
- data/lib/protobuf/varint_pure.rb +18 -0
- data/lib/protobuf/version.rb +1 -1
- data/profile.html +5103 -0
- data/protobuf.gemspec +3 -1
- data/varint_prof.rb +82 -0
- metadata +52 -151
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a0644d2a9a19095bc50b2c0eb970b2037dd85cb339e62507bda428c71ef83940
|
4
|
+
data.tar.gz: 7791374dff01001a27eb34d66a7e474344e2d97b5c02b49b69bb37d79aa1569b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c5d8304f1bd3e709594582d227899044c936d287bf406ff8171345d9ed0574a5f683494668ecc08cd432d6fe8c9b6b509650fef5d8d8006080b98ffa4a3ee188
|
7
|
+
data.tar.gz: 7e655f51518aae671afb0b9e2c6436a995ce8027ebd00471956f37a4a374439e871a33ce4c26e47af5bd58ba000f8655948213d6a7f06c8160a51e780c955d41
|
data/.rubocop.yml
CHANGED
data/.travis.yml
CHANGED
@@ -11,8 +11,6 @@ before_install:
|
|
11
11
|
- java -Xmx1g -version
|
12
12
|
- javac -J-Xmx1g -version
|
13
13
|
- export JRUBY_OPTS=-J-Xmx1g
|
14
|
-
# Required for rainbow installation issue, https://github.com/sickill/rainbow/issues/44
|
15
|
-
- gem update --system
|
16
14
|
- gem update bundler
|
17
15
|
language: ruby
|
18
16
|
rvm:
|
@@ -20,10 +18,11 @@ rvm:
|
|
20
18
|
- 2.0.0
|
21
19
|
- 2.1
|
22
20
|
- 2.2
|
23
|
-
- 2.2.2
|
24
21
|
- 2.3
|
25
22
|
- 2.4
|
26
|
-
-
|
23
|
+
- 2.5
|
24
|
+
- jruby-9.1.17.0
|
25
|
+
- jruby-9.2.5.0
|
27
26
|
- rbx-2
|
28
27
|
env:
|
29
28
|
- PROTOBUF_VERSION=2.6.1
|
data/lib/protobuf.rb
CHANGED
@@ -9,6 +9,20 @@ require 'active_support/core_ext/object/try'
|
|
9
9
|
require 'active_support/inflector'
|
10
10
|
require 'active_support/json'
|
11
11
|
require 'active_support/notifications'
|
12
|
+
# Under MRI, this optimizes proto decoding by around 15% in tests.
|
13
|
+
# When unavailable, we fall to pure Ruby.
|
14
|
+
# rubocop:disable Lint/HandleExceptions
|
15
|
+
begin
|
16
|
+
require 'varint/varint'
|
17
|
+
rescue LoadError
|
18
|
+
end
|
19
|
+
# rubocop:enable Lint/HandleExceptions
|
20
|
+
# rubocop:disable Lint/HandleExceptions
|
21
|
+
begin
|
22
|
+
require 'protobuf_java_helpers'
|
23
|
+
rescue LoadError
|
24
|
+
end
|
25
|
+
# rubocop:enable Lint/HandleExceptions
|
12
26
|
|
13
27
|
# All top-level run time code requires, ordered by necessity
|
14
28
|
require 'protobuf/wire_type'
|
@@ -50,6 +50,35 @@ module Protobuf
|
|
50
50
|
end
|
51
51
|
|
52
52
|
Protobuf::Field::BaseField.module_eval do
|
53
|
+
def define_set_method!
|
54
|
+
end
|
55
|
+
|
56
|
+
def set_without_options(message_instance, bytes)
|
57
|
+
return message_instance[name] = decode(bytes) unless repeated?
|
58
|
+
|
59
|
+
if map?
|
60
|
+
hash = message_instance[name]
|
61
|
+
entry = decode(bytes)
|
62
|
+
# decoded value could be nil for an
|
63
|
+
# enum value that is not recognized
|
64
|
+
hash[entry.key] = entry.value unless entry.value.nil?
|
65
|
+
return hash[entry.key]
|
66
|
+
end
|
67
|
+
|
68
|
+
return message_instance[name] << decode(bytes) unless packed?
|
69
|
+
|
70
|
+
array = message_instance[name]
|
71
|
+
stream = StringIO.new(bytes)
|
72
|
+
|
73
|
+
if wire_type == ::Protobuf::WireType::VARINT
|
74
|
+
array << decode(Varint.decode(stream)) until stream.eof?
|
75
|
+
elsif wire_type == ::Protobuf::WireType::FIXED64
|
76
|
+
array << decode(stream.read(8)) until stream.eof?
|
77
|
+
elsif wire_type == ::Protobuf::WireType::FIXED32
|
78
|
+
array << decode(stream.read(4)) until stream.eof?
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
53
82
|
# Sets a MessageField that is known to be an option.
|
54
83
|
# We must allow fields to be set one at a time, as option syntax allows us to
|
55
84
|
# set each field within the option using a separate "option" line.
|
@@ -72,6 +101,7 @@ module Protobuf
|
|
72
101
|
|
73
102
|
set_without_options(message_instance, bytes)
|
74
103
|
end
|
104
|
+
alias_method :set, :set_with_options
|
75
105
|
|
76
106
|
def option_set(message_field, subfield, subvalue)
|
77
107
|
return unless yield
|
@@ -85,9 +115,6 @@ module Protobuf
|
|
85
115
|
message_field[subfield.tag] = subvalue
|
86
116
|
end
|
87
117
|
end
|
88
|
-
|
89
|
-
alias_method :set_without_options, :set
|
90
|
-
alias_method :set, :set_with_options
|
91
118
|
end
|
92
119
|
end
|
93
120
|
end
|
data/lib/protobuf/encoder.rb
CHANGED
@@ -2,18 +2,7 @@ module Protobuf
|
|
2
2
|
class Encoder
|
3
3
|
def self.encode(message, stream)
|
4
4
|
message.each_field_for_serialization do |field, value|
|
5
|
-
|
6
|
-
if field.packed?
|
7
|
-
packed_value = value.map { |val| field.encode(val) }.join
|
8
|
-
stream << "#{field.tag_encoded}#{::Protobuf::Field::VarintField.encode(packed_value.size)}#{packed_value}"
|
9
|
-
else
|
10
|
-
value.each do |val|
|
11
|
-
field.encode_to_stream(val, stream)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
else
|
15
|
-
field.encode_to_stream(value, stream)
|
16
|
-
end
|
5
|
+
field.encode_to_stream(value, stream)
|
17
6
|
end
|
18
7
|
|
19
8
|
stream
|
data/lib/protobuf/enum.rb
CHANGED
@@ -58,6 +58,7 @@ module Protobuf
|
|
58
58
|
# to be incorrect; reset them
|
59
59
|
@mapped_enums = @values = nil
|
60
60
|
const_set(name, enum)
|
61
|
+
mapped_enums
|
61
62
|
end
|
62
63
|
|
63
64
|
# Internal: A mapping of enum number -> enums defined
|
@@ -132,6 +133,10 @@ module Protobuf
|
|
132
133
|
tag && (mapped_enums[tag.to_i] || []).first
|
133
134
|
end
|
134
135
|
|
136
|
+
def self.enum_for_tag_integer(tag)
|
137
|
+
(@mapped_enums[tag] || []).first
|
138
|
+
end
|
139
|
+
|
135
140
|
# Public: Get an Enum by a variety of type-checking mechanisms.
|
136
141
|
#
|
137
142
|
# candidate - An Enum, Numeric, String, or Symbol object.
|
@@ -153,7 +158,7 @@ module Protobuf
|
|
153
158
|
# Returns an Enum object or nil.
|
154
159
|
#
|
155
160
|
def self.fetch(candidate)
|
156
|
-
return
|
161
|
+
return enum_for_tag_integer(candidate) if candidate.is_a?(::Integer)
|
157
162
|
|
158
163
|
case candidate
|
159
164
|
when self
|
@@ -308,10 +313,6 @@ module Protobuf
|
|
308
313
|
"\#<Protobuf::Enum(#{parent_class})::#{name}=#{tag}>"
|
309
314
|
end
|
310
315
|
|
311
|
-
def to_i
|
312
|
-
tag
|
313
|
-
end
|
314
|
-
|
315
316
|
def to_int
|
316
317
|
tag.to_int
|
317
318
|
end
|
@@ -352,12 +353,13 @@ module Protobuf
|
|
352
353
|
end
|
353
354
|
end
|
354
355
|
|
355
|
-
::Protobuf.deprecator.define_deprecated_methods(self, :value => :to_i)
|
356
|
-
|
357
356
|
##
|
358
357
|
# Instance Aliases
|
359
358
|
#
|
360
|
-
alias :
|
361
|
-
alias :
|
359
|
+
alias :to_i tag
|
360
|
+
alias :to_hash_value tag
|
361
|
+
alias :to_json_hash_value tag
|
362
|
+
|
363
|
+
::Protobuf.deprecator.define_deprecated_methods(self, :value => :to_i)
|
362
364
|
end
|
363
365
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'active_support/core_ext/hash/slice'
|
2
2
|
require 'protobuf/field/field_array'
|
3
3
|
require 'protobuf/field/field_hash'
|
4
|
+
require 'protobuf/field/base_field_object_definitions'
|
4
5
|
|
5
6
|
module Protobuf
|
6
7
|
module Field
|
@@ -11,7 +12,7 @@ module Protobuf
|
|
11
12
|
##
|
12
13
|
# Constants
|
13
14
|
#
|
14
|
-
|
15
|
+
OBJECT_MODULE = ::Protobuf::Field::BaseFieldObjectDefinitions
|
15
16
|
PACKED_TYPES = [
|
16
17
|
::Protobuf::WireType::VARINT,
|
17
18
|
::Protobuf::WireType::FIXED32,
|
@@ -21,7 +22,7 @@ module Protobuf
|
|
21
22
|
##
|
22
23
|
# Attributes
|
23
24
|
#
|
24
|
-
attr_reader :message_class, :name, :fully_qualified_name, :options, :rule, :tag, :type_class
|
25
|
+
attr_reader :default_value, :message_class, :name, :fully_qualified_name, :options, :rule, :tag, :type_class
|
25
26
|
|
26
27
|
##
|
27
28
|
# Class Methods
|
@@ -58,7 +59,27 @@ module Protobuf
|
|
58
59
|
|
59
60
|
validate_packed_field if packed?
|
60
61
|
define_accessor(simple_name, fully_qualified_name) if simple_name
|
61
|
-
|
62
|
+
set_repeated_message!
|
63
|
+
set_map!
|
64
|
+
@value_from_values = nil
|
65
|
+
@value_from_values_for_serialization = nil
|
66
|
+
@field_predicate = nil
|
67
|
+
@field_and_present_predicate = nil
|
68
|
+
@set_field = nil
|
69
|
+
@set_method = nil
|
70
|
+
@to_message_hash = nil
|
71
|
+
@to_message_hash_string_keys = nil
|
72
|
+
@encode_to_stream = nil
|
73
|
+
|
74
|
+
define_value_from_values!
|
75
|
+
define_value_from_values_for_serialization!
|
76
|
+
define_field_predicate!
|
77
|
+
define_field_and_present_predicate!
|
78
|
+
define_set_field!
|
79
|
+
define_set_method!
|
80
|
+
define_to_message_hash!
|
81
|
+
define_encode_to_stream!
|
82
|
+
set_default_value!
|
62
83
|
end
|
63
84
|
|
64
85
|
##
|
@@ -81,7 +102,7 @@ module Protobuf
|
|
81
102
|
options[:default]
|
82
103
|
end
|
83
104
|
|
84
|
-
def
|
105
|
+
def set_default_value!
|
85
106
|
@default_value ||= if optional? || required?
|
86
107
|
typed_default_value
|
87
108
|
elsif map?
|
@@ -93,6 +114,122 @@ module Protobuf
|
|
93
114
|
end
|
94
115
|
end
|
95
116
|
|
117
|
+
def define_encode_to_stream!
|
118
|
+
@encode_to_stream = if repeated? && packed?
|
119
|
+
OBJECT_MODULE::RepeatedPackedEncodeToStream.new(self)
|
120
|
+
elsif repeated?
|
121
|
+
OBJECT_MODULE::RepeatedNotPackedEncodeToStream.new(self)
|
122
|
+
elsif message? || type_class == ::Protobuf::Field::BytesField
|
123
|
+
OBJECT_MODULE::BytesEncodeToStream.new(self)
|
124
|
+
elsif type_class == ::Protobuf::Field::StringField
|
125
|
+
OBJECT_MODULE::StringEncodeToStream.new(self)
|
126
|
+
else
|
127
|
+
OBJECT_MODULE::BaseEncodeToStream.new(self)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def encode_to_stream(value, stream)
|
132
|
+
@encode_to_stream.call(value, stream)
|
133
|
+
end
|
134
|
+
|
135
|
+
def define_field_predicate!
|
136
|
+
@field_predicate = if repeated?
|
137
|
+
OBJECT_MODULE::RepeatedFieldPredicate.new(self)
|
138
|
+
else
|
139
|
+
OBJECT_MODULE::BaseFieldPredicate.new(self)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def field?(values)
|
144
|
+
@field_predicate.call(values)
|
145
|
+
end
|
146
|
+
|
147
|
+
def define_field_and_present_predicate!
|
148
|
+
@field_and_present_predicate = if type_class == ::Protobuf::Field::BoolField # boolean present check
|
149
|
+
OBJECT_MODULE::BoolFieldAndPresentPredicate.new(self)
|
150
|
+
else
|
151
|
+
OBJECT_MODULE::BaseFieldAndPresentPredicate.new(self)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
def field_and_present?(values)
|
156
|
+
@field_and_present_predicate.call(values)
|
157
|
+
end
|
158
|
+
|
159
|
+
def define_value_from_values!
|
160
|
+
@value_from_values = if map?
|
161
|
+
OBJECT_MODULE::MapValueFromValues.new(self)
|
162
|
+
elsif repeated?
|
163
|
+
OBJECT_MODULE::RepeatedFieldValueFromValues.new(self)
|
164
|
+
elsif type_class == ::Protobuf::Field::BoolField # boolean present check
|
165
|
+
OBJECT_MODULE::BoolFieldValueFromValues.new(self)
|
166
|
+
else
|
167
|
+
OBJECT_MODULE::BaseFieldValueFromValues.new(self)
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
def value_from_values(values)
|
172
|
+
@value_from_values.call(values)
|
173
|
+
end
|
174
|
+
|
175
|
+
def define_value_from_values_for_serialization!
|
176
|
+
@value_from_values_for_serialization = if map?
|
177
|
+
OBJECT_MODULE::MapValueFromValuesForSerialization.new(self)
|
178
|
+
elsif repeated?
|
179
|
+
OBJECT_MODULE::RepeatedFieldValueFromValuesForSerialization.new(self)
|
180
|
+
elsif type_class == ::Protobuf::Field::BoolField # boolean present check
|
181
|
+
OBJECT_MODULE::BoolFieldValueFromValuesForSerialization.new(self)
|
182
|
+
else
|
183
|
+
OBJECT_MODULE::BaseFieldValueFromValuesForSerialization.new(self)
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
def value_from_values_for_serialization(values)
|
188
|
+
@value_from_values_for_serialization.call(values)
|
189
|
+
end
|
190
|
+
|
191
|
+
def define_set_field!
|
192
|
+
@set_field = if map? && required?
|
193
|
+
OBJECT_MODULE::RequiredMapSetField.new(self)
|
194
|
+
elsif repeated? && required?
|
195
|
+
OBJECT_MODULE::RequiredRepeatedSetField.new(self)
|
196
|
+
elsif type_class == ::Protobuf::Field::StringField && required?
|
197
|
+
OBJECT_MODULE::RequiredStringSetField.new(self)
|
198
|
+
elsif required?
|
199
|
+
OBJECT_MODULE::RequiredBaseSetField.new(self)
|
200
|
+
elsif map?
|
201
|
+
OBJECT_MODULE::MapSetField.new(self)
|
202
|
+
elsif repeated?
|
203
|
+
OBJECT_MODULE::RepeatedSetField.new(self)
|
204
|
+
elsif type_class == ::Protobuf::Field::StringField
|
205
|
+
OBJECT_MODULE::StringSetField.new(self)
|
206
|
+
else
|
207
|
+
OBJECT_MODULE::BaseSetField.new(self)
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
def set_field(values, value, ignore_nil_for_repeated, message_instance)
|
212
|
+
@set_field.call(values, value, ignore_nil_for_repeated, message_instance)
|
213
|
+
end
|
214
|
+
|
215
|
+
def define_to_message_hash!
|
216
|
+
if message? || enum? || repeated? || map?
|
217
|
+
@to_message_hash = OBJECT_MODULE::ToHashValueToMessageHash.new(self)
|
218
|
+
@to_message_hash_string_keys = OBJECT_MODULE::ToHashValueToMessageHashWithStringKey.new(self)
|
219
|
+
else
|
220
|
+
@to_message_hash = OBJECT_MODULE::BaseToMessageHash.new(self)
|
221
|
+
@to_message_hash_string_keys = OBJECT_MODULE::BaseToMessageHashWithStringKey.new(self)
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
def to_message_hash(values, result)
|
226
|
+
@to_message_hash.call(values, result)
|
227
|
+
end
|
228
|
+
|
229
|
+
def to_message_hash_with_string_key(values, result)
|
230
|
+
@to_message_hash_string_keys.call(values, result)
|
231
|
+
end
|
232
|
+
|
96
233
|
def deprecated?
|
97
234
|
@deprecated
|
98
235
|
end
|
@@ -101,10 +238,6 @@ module Protobuf
|
|
101
238
|
fail NotImplementedError, "#{self.class.name}##{__method__}"
|
102
239
|
end
|
103
240
|
|
104
|
-
def encode_to_stream(value, stream)
|
105
|
-
stream << tag_encoded << encode(value)
|
106
|
-
end
|
107
|
-
|
108
241
|
def extension?
|
109
242
|
@extension
|
110
243
|
end
|
@@ -117,8 +250,13 @@ module Protobuf
|
|
117
250
|
false
|
118
251
|
end
|
119
252
|
|
253
|
+
def set_map!
|
254
|
+
set_repeated_message!
|
255
|
+
@is_map = repeated_message? && type_class.get_option!(:map_entry)
|
256
|
+
end
|
257
|
+
|
120
258
|
def map?
|
121
|
-
@is_map
|
259
|
+
@is_map
|
122
260
|
end
|
123
261
|
|
124
262
|
def optional?
|
@@ -133,39 +271,32 @@ module Protobuf
|
|
133
271
|
@repeated
|
134
272
|
end
|
135
273
|
|
274
|
+
def set_repeated_message!
|
275
|
+
@repeated_message = repeated? && message?
|
276
|
+
end
|
277
|
+
|
136
278
|
def repeated_message?
|
137
|
-
|
279
|
+
@repeated_message
|
138
280
|
end
|
139
281
|
|
140
282
|
def required?
|
141
283
|
@required
|
142
284
|
end
|
143
285
|
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
end
|
156
|
-
|
157
|
-
return message_instance[name] << decode(bytes) unless packed?
|
158
|
-
|
159
|
-
array = message_instance[name]
|
160
|
-
stream = StringIO.new(bytes)
|
286
|
+
def define_set_method!
|
287
|
+
@set_method = if map?
|
288
|
+
OBJECT_MODULE::MapSetMethod.new(self)
|
289
|
+
elsif repeated? && packed?
|
290
|
+
OBJECT_MODULE::RepeatedPackedSetMethod.new(self)
|
291
|
+
elsif repeated?
|
292
|
+
OBJECT_MODULE::RepeatedNotPackedSetMethod.new(self)
|
293
|
+
else
|
294
|
+
OBJECT_MODULE::BaseSetMethod.new(self)
|
295
|
+
end
|
296
|
+
end
|
161
297
|
|
162
|
-
|
163
|
-
|
164
|
-
elsif wire_type == ::Protobuf::WireType::FIXED64
|
165
|
-
array << decode(stream.read(8)) until stream.eof?
|
166
|
-
elsif wire_type == ::Protobuf::WireType::FIXED32
|
167
|
-
array << decode(stream.read(4)) until stream.eof?
|
168
|
-
end
|
298
|
+
def set(message_instance, bytes)
|
299
|
+
@set_method.call(message_instance, bytes)
|
169
300
|
end
|
170
301
|
|
171
302
|
def tag_encoded
|
@@ -192,6 +323,18 @@ module Protobuf
|
|
192
323
|
|
193
324
|
def fully_qualified_name_only!
|
194
325
|
@name = @fully_qualified_name
|
326
|
+
|
327
|
+
##
|
328
|
+
# Recreate all of the meta methods as they may have used the original `name` value
|
329
|
+
#
|
330
|
+
define_value_from_values!
|
331
|
+
define_value_from_values_for_serialization!
|
332
|
+
define_field_predicate!
|
333
|
+
define_field_and_present_predicate!
|
334
|
+
define_set_field!
|
335
|
+
define_set_method!
|
336
|
+
define_to_message_hash!
|
337
|
+
define_encode_to_stream!
|
195
338
|
end
|
196
339
|
|
197
340
|
private
|