protobuffy 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +21 -0
- data/.travis.yml +12 -0
- data/.yardopts +5 -0
- data/CHANGES.md +261 -0
- data/CONTRIBUTING.md +16 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +14 -0
- data/README.md +58 -0
- data/Rakefile +61 -0
- data/bin/protoc-gen-ruby +17 -0
- data/bin/rpc_server +4 -0
- data/examples/bin/reverse-client-http +4 -0
- data/examples/bin/reverse-client-socket +4 -0
- data/examples/bin/reverse-client-zmq +4 -0
- data/examples/config.ru +6 -0
- data/examples/definitions/example/reverse.proto +12 -0
- data/examples/lib/example/reverse-client.rb +23 -0
- data/examples/lib/example/reverse-service.rb +9 -0
- data/examples/lib/example/reverse.pb.rb +36 -0
- data/lib/protobuf.rb +106 -0
- data/lib/protobuf/cli.rb +249 -0
- data/lib/protobuf/code_generator.rb +41 -0
- data/lib/protobuf/decoder.rb +74 -0
- data/lib/protobuf/deprecator.rb +42 -0
- data/lib/protobuf/descriptors.rb +3 -0
- data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +52 -0
- data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +249 -0
- data/lib/protobuf/encoder.rb +62 -0
- data/lib/protobuf/enum.rb +319 -0
- data/lib/protobuf/exceptions.rb +9 -0
- data/lib/protobuf/field.rb +74 -0
- data/lib/protobuf/field/base_field.rb +280 -0
- data/lib/protobuf/field/bool_field.rb +53 -0
- data/lib/protobuf/field/bytes_field.rb +81 -0
- data/lib/protobuf/field/double_field.rb +26 -0
- data/lib/protobuf/field/enum_field.rb +57 -0
- data/lib/protobuf/field/field_array.rb +86 -0
- data/lib/protobuf/field/fixed32_field.rb +25 -0
- data/lib/protobuf/field/fixed64_field.rb +29 -0
- data/lib/protobuf/field/float_field.rb +38 -0
- data/lib/protobuf/field/int32_field.rb +22 -0
- data/lib/protobuf/field/int64_field.rb +22 -0
- data/lib/protobuf/field/integer_field.rb +24 -0
- data/lib/protobuf/field/message_field.rb +66 -0
- data/lib/protobuf/field/sfixed32_field.rb +28 -0
- data/lib/protobuf/field/sfixed64_field.rb +29 -0
- data/lib/protobuf/field/signed_integer_field.rb +30 -0
- data/lib/protobuf/field/sint32_field.rb +22 -0
- data/lib/protobuf/field/sint64_field.rb +22 -0
- data/lib/protobuf/field/string_field.rb +35 -0
- data/lib/protobuf/field/uint32_field.rb +22 -0
- data/lib/protobuf/field/uint64_field.rb +22 -0
- data/lib/protobuf/field/varint_field.rb +68 -0
- data/lib/protobuf/generators/base.rb +71 -0
- data/lib/protobuf/generators/enum_generator.rb +42 -0
- data/lib/protobuf/generators/extension_generator.rb +28 -0
- data/lib/protobuf/generators/field_generator.rb +132 -0
- data/lib/protobuf/generators/file_generator.rb +140 -0
- data/lib/protobuf/generators/group_generator.rb +113 -0
- data/lib/protobuf/generators/message_generator.rb +99 -0
- data/lib/protobuf/generators/printable.rb +161 -0
- data/lib/protobuf/generators/service_generator.rb +27 -0
- data/lib/protobuf/http.rb +20 -0
- data/lib/protobuf/lifecycle.rb +46 -0
- data/lib/protobuf/logger.rb +86 -0
- data/lib/protobuf/message.rb +182 -0
- data/lib/protobuf/message/fields.rb +122 -0
- data/lib/protobuf/message/serialization.rb +84 -0
- data/lib/protobuf/optionable.rb +23 -0
- data/lib/protobuf/rpc/buffer.rb +79 -0
- data/lib/protobuf/rpc/client.rb +168 -0
- data/lib/protobuf/rpc/connector.rb +21 -0
- data/lib/protobuf/rpc/connectors/base.rb +54 -0
- data/lib/protobuf/rpc/connectors/common.rb +172 -0
- data/lib/protobuf/rpc/connectors/http.rb +90 -0
- data/lib/protobuf/rpc/connectors/socket.rb +73 -0
- data/lib/protobuf/rpc/connectors/zmq.rb +205 -0
- data/lib/protobuf/rpc/dynamic_discovery.pb.rb +47 -0
- data/lib/protobuf/rpc/env.rb +58 -0
- data/lib/protobuf/rpc/error.rb +28 -0
- data/lib/protobuf/rpc/error/client_error.rb +31 -0
- data/lib/protobuf/rpc/error/server_error.rb +43 -0
- data/lib/protobuf/rpc/middleware.rb +25 -0
- data/lib/protobuf/rpc/middleware/exception_handler.rb +36 -0
- data/lib/protobuf/rpc/middleware/logger.rb +91 -0
- data/lib/protobuf/rpc/middleware/request_decoder.rb +83 -0
- data/lib/protobuf/rpc/middleware/response_encoder.rb +88 -0
- data/lib/protobuf/rpc/middleware/runner.rb +18 -0
- data/lib/protobuf/rpc/rpc.pb.rb +53 -0
- data/lib/protobuf/rpc/server.rb +39 -0
- data/lib/protobuf/rpc/servers/http/server.rb +101 -0
- data/lib/protobuf/rpc/servers/http_runner.rb +34 -0
- data/lib/protobuf/rpc/servers/socket/server.rb +113 -0
- data/lib/protobuf/rpc/servers/socket/worker.rb +56 -0
- data/lib/protobuf/rpc/servers/socket_runner.rb +34 -0
- data/lib/protobuf/rpc/servers/zmq/broker.rb +155 -0
- data/lib/protobuf/rpc/servers/zmq/server.rb +313 -0
- data/lib/protobuf/rpc/servers/zmq/util.rb +47 -0
- data/lib/protobuf/rpc/servers/zmq/worker.rb +105 -0
- data/lib/protobuf/rpc/servers/zmq_runner.rb +51 -0
- data/lib/protobuf/rpc/service.rb +179 -0
- data/lib/protobuf/rpc/service_directory.rb +245 -0
- data/lib/protobuf/rpc/service_dispatcher.rb +46 -0
- data/lib/protobuf/rpc/service_filters.rb +273 -0
- data/lib/protobuf/rpc/stat.rb +148 -0
- data/lib/protobuf/socket.rb +22 -0
- data/lib/protobuf/tasks.rb +1 -0
- data/lib/protobuf/tasks/compile.rake +61 -0
- data/lib/protobuf/version.rb +3 -0
- data/lib/protobuf/wire_type.rb +10 -0
- data/lib/protobuf/zmq.rb +21 -0
- data/proto/dynamic_discovery.proto +44 -0
- data/proto/google/protobuf/compiler/plugin.proto +147 -0
- data/proto/google/protobuf/descriptor.proto +620 -0
- data/proto/rpc.proto +62 -0
- data/protobuffy.gemspec +37 -0
- data/spec/benchmark/tasks.rb +113 -0
- data/spec/bin/protoc-gen-ruby_spec.rb +18 -0
- data/spec/data/data.bin +3 -0
- data/spec/data/types.bin +0 -0
- data/spec/encoding/all_types_spec.rb +91 -0
- data/spec/encoding/extreme_values_spec.rb +0 -0
- data/spec/functional/socket_server_spec.rb +59 -0
- data/spec/functional/zmq_server_spec.rb +103 -0
- data/spec/lib/protobuf/cli_spec.rb +267 -0
- data/spec/lib/protobuf/code_generator_spec.rb +60 -0
- data/spec/lib/protobuf/enum_spec.rb +239 -0
- data/spec/lib/protobuf/field/int32_field_spec.rb +7 -0
- data/spec/lib/protobuf/field/string_field_spec.rb +46 -0
- data/spec/lib/protobuf/field_spec.rb +194 -0
- data/spec/lib/protobuf/generators/base_spec.rb +87 -0
- data/spec/lib/protobuf/generators/enum_generator_spec.rb +68 -0
- data/spec/lib/protobuf/generators/extension_generator_spec.rb +43 -0
- data/spec/lib/protobuf/generators/field_generator_spec.rb +99 -0
- data/spec/lib/protobuf/generators/file_generator_spec.rb +29 -0
- data/spec/lib/protobuf/generators/message_generator_spec.rb +0 -0
- data/spec/lib/protobuf/generators/service_generator_spec.rb +43 -0
- data/spec/lib/protobuf/lifecycle_spec.rb +89 -0
- data/spec/lib/protobuf/logger_spec.rb +136 -0
- data/spec/lib/protobuf/message_spec.rb +368 -0
- data/spec/lib/protobuf/optionable_spec.rb +46 -0
- data/spec/lib/protobuf/rpc/client_spec.rb +66 -0
- data/spec/lib/protobuf/rpc/connector_spec.rb +26 -0
- data/spec/lib/protobuf/rpc/connectors/base_spec.rb +50 -0
- data/spec/lib/protobuf/rpc/connectors/common_spec.rb +170 -0
- data/spec/lib/protobuf/rpc/connectors/connector_spec.rb +13 -0
- data/spec/lib/protobuf/rpc/connectors/http_spec.rb +61 -0
- data/spec/lib/protobuf/rpc/connectors/socket_spec.rb +24 -0
- data/spec/lib/protobuf/rpc/connectors/zmq_spec.rb +129 -0
- data/spec/lib/protobuf/rpc/middleware/exception_handler_spec.rb +62 -0
- data/spec/lib/protobuf/rpc/middleware/logger_spec.rb +49 -0
- data/spec/lib/protobuf/rpc/middleware/request_decoder_spec.rb +115 -0
- data/spec/lib/protobuf/rpc/middleware/response_encoder_spec.rb +75 -0
- data/spec/lib/protobuf/rpc/servers/http/server_spec.rb +104 -0
- data/spec/lib/protobuf/rpc/servers/socket_server_spec.rb +38 -0
- data/spec/lib/protobuf/rpc/servers/zmq/server_spec.rb +41 -0
- data/spec/lib/protobuf/rpc/servers/zmq/util_spec.rb +55 -0
- data/spec/lib/protobuf/rpc/servers/zmq/worker_spec.rb +35 -0
- data/spec/lib/protobuf/rpc/service_directory_spec.rb +295 -0
- data/spec/lib/protobuf/rpc/service_dispatcher_spec.rb +52 -0
- data/spec/lib/protobuf/rpc/service_filters_spec.rb +484 -0
- data/spec/lib/protobuf/rpc/service_spec.rb +161 -0
- data/spec/lib/protobuf/rpc/stat_spec.rb +151 -0
- data/spec/lib/protobuf_spec.rb +78 -0
- data/spec/spec_helper.rb +57 -0
- data/spec/support/all.rb +7 -0
- data/spec/support/packed_field.rb +22 -0
- data/spec/support/server.rb +94 -0
- data/spec/support/test/all_types.data.bin +0 -0
- data/spec/support/test/all_types.data.txt +119 -0
- data/spec/support/test/defaults.pb.rb +25 -0
- data/spec/support/test/defaults.proto +9 -0
- data/spec/support/test/enum.pb.rb +59 -0
- data/spec/support/test/enum.proto +34 -0
- data/spec/support/test/extended.pb.rb +22 -0
- data/spec/support/test/extended.proto +10 -0
- data/spec/support/test/extreme_values.data.bin +0 -0
- data/spec/support/test/google_unittest.pb.rb +543 -0
- data/spec/support/test/google_unittest.proto +713 -0
- data/spec/support/test/google_unittest_import.pb.rb +37 -0
- data/spec/support/test/google_unittest_import.proto +64 -0
- data/spec/support/test/google_unittest_import_public.pb.rb +8 -0
- data/spec/support/test/google_unittest_import_public.proto +38 -0
- data/spec/support/test/multi_field_extensions.pb.rb +56 -0
- data/spec/support/test/multi_field_extensions.proto +33 -0
- data/spec/support/test/resource.pb.rb +117 -0
- data/spec/support/test/resource.proto +94 -0
- data/spec/support/test/resource_service.rb +26 -0
- data/spec/support/test_app_file.rb +2 -0
- data/spec/support/tolerance_matcher.rb +40 -0
- metadata +367 -0
@@ -0,0 +1,280 @@
|
|
1
|
+
require 'protobuf/wire_type'
|
2
|
+
require 'protobuf/field/field_array'
|
3
|
+
|
4
|
+
module Protobuf
|
5
|
+
module Field
|
6
|
+
class BaseField
|
7
|
+
|
8
|
+
##
|
9
|
+
# Constants
|
10
|
+
#
|
11
|
+
|
12
|
+
PACKED_TYPES = [
|
13
|
+
::Protobuf::WireType::VARINT,
|
14
|
+
::Protobuf::WireType::FIXED32,
|
15
|
+
::Protobuf::WireType::FIXED64
|
16
|
+
].freeze
|
17
|
+
|
18
|
+
##
|
19
|
+
# Attributes
|
20
|
+
#
|
21
|
+
|
22
|
+
attr_reader :default, :default_value, :deprecated, :extension,
|
23
|
+
:getter_method_name, :message_class, :name, :optional,
|
24
|
+
:packed, :repeated, :required, :rule, :setter_method_name,
|
25
|
+
:tag, :type_class
|
26
|
+
|
27
|
+
##
|
28
|
+
# Class Methods
|
29
|
+
#
|
30
|
+
|
31
|
+
def self.default
|
32
|
+
nil
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# Constructor
|
37
|
+
#
|
38
|
+
|
39
|
+
def initialize(message_class, rule, type_class, name, tag, options)
|
40
|
+
@message_class, @rule, @type_class, @name, @tag = \
|
41
|
+
message_class, rule, type_class, name, tag
|
42
|
+
|
43
|
+
set_rule_predicates
|
44
|
+
|
45
|
+
@getter_method_name = name
|
46
|
+
@setter_method_name = "#{name}="
|
47
|
+
@default = options.delete(:default)
|
48
|
+
@extension = options.delete(:extension)
|
49
|
+
@packed = repeated? && options.delete(:packed)
|
50
|
+
@deprecated = options.delete(:deprecated)
|
51
|
+
|
52
|
+
set_default_value
|
53
|
+
warn_excess_options(options) unless options.empty?
|
54
|
+
validate_packed_field if packed?
|
55
|
+
define_accessor
|
56
|
+
end
|
57
|
+
|
58
|
+
##
|
59
|
+
# Public Instance Methods
|
60
|
+
#
|
61
|
+
|
62
|
+
def acceptable?(value)
|
63
|
+
true
|
64
|
+
end
|
65
|
+
|
66
|
+
def enum?
|
67
|
+
false
|
68
|
+
end
|
69
|
+
|
70
|
+
def message?
|
71
|
+
false
|
72
|
+
end
|
73
|
+
|
74
|
+
# Decode +bytes+ and pass to +message_instance+.
|
75
|
+
def set(message_instance, bytes)
|
76
|
+
if packed?
|
77
|
+
array = message_instance.__send__(getter_method_name)
|
78
|
+
method = \
|
79
|
+
case wire_type
|
80
|
+
when ::Protobuf::WireType::FIXED32 then :read_fixed32
|
81
|
+
when ::Protobuf::WireType::FIXED64 then :read_fixed64
|
82
|
+
when ::Protobuf::WireType::VARINT then :read_varint
|
83
|
+
end
|
84
|
+
stream = StringIO.new(bytes)
|
85
|
+
|
86
|
+
until stream.eof?
|
87
|
+
array << decode(::Protobuf::Decoder.__send__(method, stream))
|
88
|
+
end
|
89
|
+
else
|
90
|
+
value = decode(bytes)
|
91
|
+
if repeated?
|
92
|
+
message_instance.__send__(getter_method_name) << value
|
93
|
+
else
|
94
|
+
message_instance.__send__(setter_method_name, value)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
# Decode +bytes+ and return a field value.
|
100
|
+
def decode(bytes)
|
101
|
+
raise NotImplementedError, "#{self.class.name}\#decode"
|
102
|
+
end
|
103
|
+
|
104
|
+
# Encode +value+ and return a byte string.
|
105
|
+
def encode(value)
|
106
|
+
raise NotImplementedError, "#{self.class.name}\#encode"
|
107
|
+
end
|
108
|
+
|
109
|
+
def extension?
|
110
|
+
!! extension
|
111
|
+
end
|
112
|
+
|
113
|
+
# Is this a repeated field?
|
114
|
+
def repeated?
|
115
|
+
!! repeated
|
116
|
+
end
|
117
|
+
|
118
|
+
# Is this a repeated message field?
|
119
|
+
def repeated_message?
|
120
|
+
repeated? && message?
|
121
|
+
end
|
122
|
+
|
123
|
+
# Is this a required field?
|
124
|
+
def required?
|
125
|
+
!! required
|
126
|
+
end
|
127
|
+
|
128
|
+
# Is this a optional field?
|
129
|
+
def optional?
|
130
|
+
!! optional
|
131
|
+
end
|
132
|
+
|
133
|
+
# Is this a deprecated field?
|
134
|
+
def deprecated?
|
135
|
+
!! deprecated
|
136
|
+
end
|
137
|
+
|
138
|
+
# Is this a packed repeated field?
|
139
|
+
def packed?
|
140
|
+
!! packed
|
141
|
+
end
|
142
|
+
|
143
|
+
def to_s
|
144
|
+
"#{rule} #{type_class} #{name} = #{tag} #{default ? "[default=#{default.inspect}]" : ''}"
|
145
|
+
end
|
146
|
+
|
147
|
+
def type
|
148
|
+
$stderr.puts("[DEPRECATED] #{self.class.name}#type usage is deprecated.\nPlease use #type_class instead.")
|
149
|
+
type_class
|
150
|
+
end
|
151
|
+
|
152
|
+
def warn_if_deprecated
|
153
|
+
if ::Protobuf.print_deprecation_warnings? && deprecated?
|
154
|
+
$stderr.puts("[WARNING] #{message_class.name}##{name} field usage is deprecated.")
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
private
|
159
|
+
|
160
|
+
##
|
161
|
+
# Private Instance Methods
|
162
|
+
#
|
163
|
+
|
164
|
+
def define_accessor
|
165
|
+
if repeated?
|
166
|
+
define_array_getter
|
167
|
+
define_array_setter
|
168
|
+
else
|
169
|
+
define_getter
|
170
|
+
define_setter
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
def define_array_getter
|
175
|
+
field = self
|
176
|
+
message_class.class_eval do
|
177
|
+
define_method(field.getter_method_name) do
|
178
|
+
field.warn_if_deprecated
|
179
|
+
@values[field.name] ||= ::Protobuf::Field::FieldArray.new(field)
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
def define_array_setter
|
185
|
+
field = self
|
186
|
+
message_class.class_eval do
|
187
|
+
define_method(field.setter_method_name) do |val|
|
188
|
+
field.warn_if_deprecated
|
189
|
+
|
190
|
+
if val.is_a?(Array)
|
191
|
+
val = val.dup
|
192
|
+
val.compact!
|
193
|
+
else
|
194
|
+
raise TypeError, <<-TYPE_ERROR
|
195
|
+
Expected repeated value of type '#{field.type_class}'
|
196
|
+
Got '#{val.class}' for repeated protobuf field #{field.name}
|
197
|
+
TYPE_ERROR
|
198
|
+
end
|
199
|
+
|
200
|
+
if val.nil? || (val.respond_to?(:empty?) && val.empty?)
|
201
|
+
@values.delete(field.name)
|
202
|
+
else
|
203
|
+
@values[field.name] ||= ::Protobuf::Field::FieldArray.new(field)
|
204
|
+
@values[field.name].replace(val)
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
def define_getter
|
211
|
+
field = self
|
212
|
+
message_class.class_eval do
|
213
|
+
define_method(field.getter_method_name) do
|
214
|
+
field.warn_if_deprecated
|
215
|
+
@values.fetch(field.name, field.default_value)
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
def define_setter
|
221
|
+
field = self
|
222
|
+
message_class.class_eval do
|
223
|
+
define_method(field.setter_method_name) do |val|
|
224
|
+
field.warn_if_deprecated
|
225
|
+
|
226
|
+
if val.nil? || (val.respond_to?(:empty?) && val.empty?)
|
227
|
+
@values.delete(field.name)
|
228
|
+
elsif field.acceptable?(val)
|
229
|
+
@values[field.name] = val
|
230
|
+
else
|
231
|
+
raise TypeError, "Unacceptable value #{val} for field #{field.name} of type #{field.type_class}"
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
def set_default_value
|
238
|
+
@default_value = case
|
239
|
+
when repeated? then ::Protobuf::Field::FieldArray.new(self).freeze
|
240
|
+
when required? then nil
|
241
|
+
when optional? then typed_default_value
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
def set_rule_predicates
|
246
|
+
case rule
|
247
|
+
when :repeated then
|
248
|
+
@required = @optional = false
|
249
|
+
@repeated = true
|
250
|
+
when :required then
|
251
|
+
@repeated = @optional = false
|
252
|
+
@required = true
|
253
|
+
when :optional then
|
254
|
+
@repeated = @required = false
|
255
|
+
@optional = true
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
def typed_default_value
|
260
|
+
if default.nil?
|
261
|
+
self.class.default
|
262
|
+
else
|
263
|
+
default
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
def validate_packed_field
|
268
|
+
if packed? && ! ::Protobuf::Field::BaseField::PACKED_TYPES.include?(wire_type)
|
269
|
+
raise "Can't use packed encoding for '#{type_class}' type"
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
273
|
+
def warn_excess_options(options)
|
274
|
+
warn "WARNING: Invalid options: #{options.inspect} (in #{message_class.name}##{name})"
|
275
|
+
end
|
276
|
+
|
277
|
+
end
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'protobuf/field/varint_field'
|
2
|
+
|
3
|
+
module Protobuf
|
4
|
+
module Field
|
5
|
+
class BoolField < VarintField
|
6
|
+
|
7
|
+
##
|
8
|
+
# Class Methods
|
9
|
+
#
|
10
|
+
|
11
|
+
def self.default
|
12
|
+
false
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
##
|
17
|
+
# Public Instance Methods
|
18
|
+
# #
|
19
|
+
|
20
|
+
def acceptable?(val)
|
21
|
+
[true, false].include?(val)
|
22
|
+
end
|
23
|
+
|
24
|
+
def decode(value)
|
25
|
+
value == 1
|
26
|
+
end
|
27
|
+
|
28
|
+
def encode(value)
|
29
|
+
[value ? 1 : 0].pack('C')
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
##
|
35
|
+
# Private Instance Methods
|
36
|
+
#
|
37
|
+
|
38
|
+
def define_getter
|
39
|
+
super
|
40
|
+
|
41
|
+
field = self
|
42
|
+
message_class.class_eval do
|
43
|
+
define_method("#{field.getter_method_name}?") do
|
44
|
+
field.warn_if_deprecated
|
45
|
+
@values.fetch(field.name, field.default_value)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'protobuf/wire_type'
|
2
|
+
|
3
|
+
module Protobuf
|
4
|
+
module Field
|
5
|
+
class BytesField < BaseField
|
6
|
+
|
7
|
+
##
|
8
|
+
# Constants
|
9
|
+
#
|
10
|
+
|
11
|
+
BYTES_ENCODING = "ASCII-8BIT".freeze
|
12
|
+
|
13
|
+
##
|
14
|
+
# Class Methods
|
15
|
+
#
|
16
|
+
|
17
|
+
def self.default
|
18
|
+
''
|
19
|
+
end
|
20
|
+
|
21
|
+
##
|
22
|
+
# Public Instance Methods
|
23
|
+
#
|
24
|
+
|
25
|
+
def acceptable?(val)
|
26
|
+
val.nil? || val.is_a?(String) || val.is_a?(Symbol) || val.is_a?(::Protobuf::Message)
|
27
|
+
end
|
28
|
+
|
29
|
+
def decode(bytes)
|
30
|
+
bytes_to_decode = bytes.dup
|
31
|
+
bytes_to_decode.force_encoding(::Protobuf::Field::BytesField::BYTES_ENCODING)
|
32
|
+
bytes_to_decode
|
33
|
+
end
|
34
|
+
|
35
|
+
def encode(value)
|
36
|
+
value_to_encode = value.dup
|
37
|
+
value_to_encode = value.encode if value.is_a?(::Protobuf::Message)
|
38
|
+
value_to_encode.force_encoding(::Protobuf::Field::BytesField::BYTES_ENCODING)
|
39
|
+
|
40
|
+
string_size = ::Protobuf::Field::VarintField.encode(value_to_encode.size)
|
41
|
+
string_size << value_to_encode
|
42
|
+
end
|
43
|
+
|
44
|
+
def wire_type
|
45
|
+
::Protobuf::WireType::LENGTH_DELIMITED
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
##
|
51
|
+
# Private Instance Methods
|
52
|
+
#
|
53
|
+
|
54
|
+
def define_setter
|
55
|
+
field = self
|
56
|
+
message_class.class_eval do
|
57
|
+
define_method(field.setter_method_name) do |val|
|
58
|
+
begin
|
59
|
+
field.warn_if_deprecated
|
60
|
+
val = "#{val}" if val.is_a?(Symbol)
|
61
|
+
|
62
|
+
if val.nil?
|
63
|
+
@values.delete(field.name)
|
64
|
+
elsif field.acceptable?(val)
|
65
|
+
@values[field.name] = val.dup
|
66
|
+
else
|
67
|
+
raise TypeError, "Unacceptable value #{val} for field #{field.name} of type #{field.type_class}"
|
68
|
+
end
|
69
|
+
rescue NoMethodError => ex
|
70
|
+
::Protobuf::Logger.error { ex.message }
|
71
|
+
::Protobuf::Logger.error { ex.backtrace.join("\n") }
|
72
|
+
raise TypeError, "Got NoMethodError attempting to set #{val} for field #{field.name} of type #{field.type_class}: #{ex.message}"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'protobuf/field/float_field'
|
2
|
+
|
3
|
+
module Protobuf
|
4
|
+
module Field
|
5
|
+
class DoubleField < FloatField
|
6
|
+
|
7
|
+
##
|
8
|
+
# Public Instance Methods
|
9
|
+
#
|
10
|
+
|
11
|
+
def decode(bytes)
|
12
|
+
bytes.unpack('E').first
|
13
|
+
end
|
14
|
+
|
15
|
+
def encode(value)
|
16
|
+
[value].pack('E')
|
17
|
+
end
|
18
|
+
|
19
|
+
def wire_type
|
20
|
+
WireType::FIXED64
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'protobuf/field/varint_field'
|
2
|
+
|
3
|
+
module Protobuf
|
4
|
+
module Field
|
5
|
+
class EnumField < VarintField
|
6
|
+
|
7
|
+
##
|
8
|
+
# Public Instance Methods
|
9
|
+
#
|
10
|
+
|
11
|
+
def acceptable?(val)
|
12
|
+
! type_class.fetch(val).nil?
|
13
|
+
end
|
14
|
+
|
15
|
+
def encode(value)
|
16
|
+
super(value.to_i)
|
17
|
+
end
|
18
|
+
|
19
|
+
def enum?
|
20
|
+
true
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
##
|
26
|
+
# Private Instance Methods
|
27
|
+
#
|
28
|
+
|
29
|
+
def define_setter
|
30
|
+
field = self
|
31
|
+
message_class.class_eval do
|
32
|
+
define_method("#{field.name}=") do |value|
|
33
|
+
orig_value = value
|
34
|
+
if value.nil?
|
35
|
+
@values.delete(field.name)
|
36
|
+
else
|
37
|
+
value = field.type_class.fetch(value)
|
38
|
+
raise TypeError, "Invalid Enum value: #{orig_value.inspect} for #{field.name}" unless value
|
39
|
+
|
40
|
+
@values[field.name] = value
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def typed_default_value
|
47
|
+
if default.is_a?(Symbol)
|
48
|
+
type_class.const_get(default)
|
49
|
+
else
|
50
|
+
self.class.default
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|