protobuf 1.4.2 → 2.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Rakefile +7 -7
- data/bin/rpc_server +2 -188
- data/bin/rprotoc +44 -38
- data/examples/addressbook.pb.rb +2 -2
- data/ext/Makefile +11 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/code_generator.h +142 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/command_line_interface.h +318 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum.h +99 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum_field.h +103 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_extension.h +85 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_field.h +167 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_file.h +98 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_generator.h +72 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_helpers.h +159 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message.h +170 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message_field.h +102 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_primitive_field.h +103 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_service.h +118 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_string_field.h +104 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h +2721 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/importer.h +303 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum.h +84 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum_field.h +121 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_extension.h +77 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_field.h +108 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_file.h +101 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_generator.h +72 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_helpers.h +213 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message.h +109 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message_field.h +134 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_primitive_field.h +121 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_service.h +113 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_string_field.h +120 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/mock_code_generator.h +113 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/package_info.h +64 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/parser.h +434 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.h +73 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.pb.h +790 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/python/python_generator.h +156 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/subprocess.h +108 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/zip_writer.h +93 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.h +1367 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.pb.h +5223 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/descriptor_database.h +366 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/dynamic_message.h +136 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/extension_set.h +904 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_reflection.h +424 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_util.h +82 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream.h +1102 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream_inl.h +64 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/gzip_stream.h +207 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/package_info.h +54 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/printer.h +136 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/tokenizer.h +313 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream.h +238 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl.h +357 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl_lite.h +340 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/message.h +692 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/message_lite.h +239 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/package_info.h +64 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/reflection_ops.h +80 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/repeated_field.h +1295 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/service.h +291 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/common.h +1211 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/hash.h +220 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/map-util.h +119 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/once.h +123 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/stl_util-inl.h +121 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/strutil.h +457 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/substitute.h +170 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/test_util.h +174 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/test_util_lite.h +101 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/testing/file.h +83 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/testing/googletest.h +98 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/text_format.h +285 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest.pb.h +11915 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_custom_options.pb.h +2895 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_embed_optimize_for.pb.h +211 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_empty.pb.h +56 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import.pb.h +188 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import_lite.pb.h +151 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite.pb.h +4752 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite_imports_nonlite.pb.h +150 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_mset.pb.h +816 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_no_generic_services.pb.h +197 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_optimize_for.pb.h +403 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unknown_field_set.h +268 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/wire_format.h +304 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite.h +620 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite_inl.h +774 -0
- data/ext/ruby_generator/RubyGenerator.cpp +435 -0
- data/ext/ruby_generator/RubyGenerator.h +180 -0
- data/ext/ruby_generator/extconf.rb +24 -0
- data/lib/protobuf.rb +55 -33
- data/lib/protobuf/cli.rb +176 -0
- data/lib/protobuf/common/logger.rb +11 -11
- data/lib/protobuf/{message/enum.rb → enum.rb} +0 -10
- data/lib/protobuf/evented.rb +22 -0
- data/lib/protobuf/field.rb +57 -0
- data/lib/protobuf/field/base_field.rb +210 -0
- data/lib/protobuf/field/bool_field.rb +24 -0
- data/lib/protobuf/field/bytes_field.rb +38 -0
- data/lib/protobuf/field/double_field.rb +19 -0
- data/lib/protobuf/field/enum_field.rb +58 -0
- data/lib/protobuf/field/extension_fields.rb +24 -0
- data/lib/protobuf/field/field_array.rb +55 -0
- data/lib/protobuf/field/fixed32_field.rb +19 -0
- data/lib/protobuf/field/fixed64_field.rb +22 -0
- data/lib/protobuf/field/float_field.rb +29 -0
- data/lib/protobuf/field/int32_field.rb +10 -0
- data/lib/protobuf/field/int64_field.rb +10 -0
- data/lib/protobuf/field/integer_field.rb +19 -0
- data/lib/protobuf/field/message_field.rb +51 -0
- data/lib/protobuf/field/sfixed32_field.rb +21 -0
- data/lib/protobuf/field/sfixed64_field.rb +24 -0
- data/lib/protobuf/field/signed_integer_field.rb +23 -0
- data/lib/protobuf/field/sint32_field.rb +10 -0
- data/lib/protobuf/field/sint64_field.rb +10 -0
- data/lib/protobuf/field/string_field.rb +14 -0
- data/lib/protobuf/field/uint32_field.rb +10 -0
- data/lib/protobuf/field/uint64_field.rb +10 -0
- data/lib/protobuf/field/varint_field.rb +63 -0
- data/lib/protobuf/message.rb +376 -0
- data/lib/protobuf/message/message.rb +1 -475
- data/lib/protobuf/rpc/buffer.rb +1 -1
- data/lib/protobuf/rpc/client.rb +26 -26
- data/lib/protobuf/rpc/connector.rb +15 -14
- data/lib/protobuf/rpc/connectors/eventmachine.rb +13 -13
- data/lib/protobuf/rpc/rpc.pb.rb +39 -28
- data/lib/protobuf/rpc/server.rb +7 -28
- data/lib/protobuf/rpc/servers/evented_runner.rb +15 -15
- data/lib/protobuf/rpc/servers/socket/server.rb +2 -1
- data/lib/protobuf/rpc/servers/socket_runner.rb +5 -6
- data/lib/protobuf/rpc/servers/zmq_runner.rb +4 -5
- data/lib/protobuf/socket.rb +22 -0
- data/lib/protobuf/version.rb +2 -1
- data/lib/protobuf/zmq.rb +21 -0
- data/proto/rpc.pb.rb +48 -0
- data/protobuf.gemspec +14 -6
- data/spec/benchmark/tasks.rb +42 -23
- data/spec/functional/evented_server_spec.rb +2 -0
- data/spec/functional/socket_server_spec.rb +22 -26
- data/spec/functional/zmq_server_spec.rb +22 -26
- data/spec/lib/protobuf/cli_spec.rb +182 -0
- data/spec/{unit → lib/protobuf}/common/logger_spec.rb +0 -0
- data/spec/{unit → lib/protobuf/message}/enum_spec.rb +0 -0
- data/spec/{unit → lib/protobuf/message}/message_spec.rb +0 -0
- data/spec/{unit → lib/protobuf}/rpc/client_spec.rb +6 -4
- data/spec/lib/protobuf/rpc/connector_spec.rb +41 -0
- data/spec/{unit → lib/protobuf}/rpc/connectors/base_spec.rb +18 -18
- data/spec/{unit → lib/protobuf}/rpc/connectors/common_spec.rb +20 -20
- data/spec/{unit → lib/protobuf}/rpc/connectors/socket_spec.rb +9 -8
- data/spec/{unit → lib/protobuf}/rpc/connectors/zmq_spec.rb +1 -1
- data/spec/{unit → lib/protobuf}/rpc/servers/evented_server_spec.rb +4 -4
- data/spec/{unit → lib/protobuf}/rpc/servers/socket_server_spec.rb +11 -23
- data/spec/{unit → lib/protobuf}/rpc/servers/zmq/broker_spec.rb +5 -0
- data/spec/{unit → lib/protobuf}/rpc/servers/zmq/server_spec.rb +5 -0
- data/spec/{unit → lib/protobuf}/rpc/servers/zmq/util_spec.rb +5 -0
- data/spec/{unit → lib/protobuf}/rpc/servers/zmq/worker_spec.rb +5 -0
- data/spec/{unit → lib/protobuf}/rpc/service_spec.rb +0 -0
- data/spec/proto/test.pb.rb +48 -25
- data/spec/spec_helper.rb +9 -15
- data/spec/support/all.rb +7 -0
- data/spec/{helper → support}/server.rb +5 -7
- data/spec/{helper → support}/silent_constants.rb +4 -0
- data/spec/support/test_app_file.rb +2 -0
- data/spec/{helper → support}/tolerance_matcher.rb +0 -0
- data/test/proto/types.pb.rb +1 -1
- data/test/test_addressbook.rb +1 -1
- data/test/test_enum_value.rb +1 -1
- data/test/test_standard_message.rb +1 -1
- metadata +243 -107
- data/lib/protobuf/common/util.rb +0 -59
- data/lib/protobuf/compiler/compiler.rb +0 -52
- data/lib/protobuf/compiler/nodes.rb +0 -323
- data/lib/protobuf/compiler/proto.y +0 -216
- data/lib/protobuf/compiler/proto2.ebnf +0 -79
- data/lib/protobuf/compiler/proto_parser.rb +0 -1425
- data/lib/protobuf/compiler/template/rpc_service_implementation.erb +0 -42
- data/lib/protobuf/compiler/visitors.rb +0 -282
- data/lib/protobuf/descriptor/descriptor.proto +0 -286
- data/lib/protobuf/descriptor/descriptor.rb +0 -55
- data/lib/protobuf/descriptor/descriptor_builder.rb +0 -143
- data/lib/protobuf/descriptor/descriptor_proto.rb +0 -138
- data/lib/protobuf/descriptor/enum_descriptor.rb +0 -33
- data/lib/protobuf/descriptor/field_descriptor.rb +0 -49
- data/lib/protobuf/descriptor/file_descriptor.rb +0 -37
- data/lib/protobuf/message/field.rb +0 -684
- data/lib/protobuf/message/protoable.rb +0 -38
- data/spec/helper/all.rb +0 -7
- data/spec/unit/common/util_spec.rb +0 -17
- data/spec/unit/rpc/connector_spec.rb +0 -31
- data/spec/unit/rpc/connectors/eventmachine_client_spec.rb +0 -32
- data/spec/unit/rpc/connectors/eventmachine_spec.rb +0 -0
- data/test/check_unbuild.rb +0 -30
- data/test/test_compiler.rb +0 -325
- data/test/test_descriptor.rb +0 -122
@@ -0,0 +1,210 @@
|
|
1
|
+
require 'protobuf/common/wire_type'
|
2
|
+
require 'protobuf/field/field_array'
|
3
|
+
|
4
|
+
module Protobuf
|
5
|
+
module Field
|
6
|
+
class BaseField
|
7
|
+
##
|
8
|
+
# Attributes
|
9
|
+
#
|
10
|
+
attr_reader :message_class, :rule, :type, :name, :tag, :default, :default_value, :setter_method_name, :getter_method_name
|
11
|
+
|
12
|
+
##
|
13
|
+
# Class Methods
|
14
|
+
#
|
15
|
+
def self.default
|
16
|
+
nil
|
17
|
+
end
|
18
|
+
|
19
|
+
##
|
20
|
+
# Constructor
|
21
|
+
#
|
22
|
+
def initialize(message_class, rule, type, name, tag, options)
|
23
|
+
@message_class, @rule, @type, @name, @tag = \
|
24
|
+
message_class, rule, type, name, tag
|
25
|
+
|
26
|
+
@getter_method_name = name
|
27
|
+
@setter_method_name = "#{name}=".to_sym
|
28
|
+
@default = options.delete(:default)
|
29
|
+
@extension = options.delete(:extension)
|
30
|
+
@packed = repeated? && options.delete(:packed)
|
31
|
+
unless options.empty?
|
32
|
+
warn "WARNING: Invalid options: #{options.inspect} (in #{@message_class.name.split('::').last}.#{@name})"
|
33
|
+
end
|
34
|
+
|
35
|
+
if packed? && ! [WireType::VARINT, WireType::FIXED32, WireType::FIXED64].include?(wire_type)
|
36
|
+
raise "Can't use packed encoding for `#{@type}' type"
|
37
|
+
end
|
38
|
+
|
39
|
+
@default_value = \
|
40
|
+
case @rule
|
41
|
+
when :repeated then
|
42
|
+
FieldArray.new(self).freeze
|
43
|
+
when :required then
|
44
|
+
nil
|
45
|
+
when :optional then
|
46
|
+
typed_default_value
|
47
|
+
end
|
48
|
+
|
49
|
+
define_accessor
|
50
|
+
end
|
51
|
+
|
52
|
+
##
|
53
|
+
# Public Instance Methods
|
54
|
+
#
|
55
|
+
def acceptable?(value)
|
56
|
+
true
|
57
|
+
end
|
58
|
+
|
59
|
+
def enum?
|
60
|
+
return @_enum unless @_enum.nil?
|
61
|
+
@_enum = (self.class < ::Protobuf::Field::EnumField)
|
62
|
+
end
|
63
|
+
|
64
|
+
def message?
|
65
|
+
return @_message unless @_message.nil?
|
66
|
+
@_message = (self.class < ::Protobuf::Field::MessageField)
|
67
|
+
end
|
68
|
+
|
69
|
+
def initialized?(message_instance)
|
70
|
+
value = message_instance.__send__(getter_method_name)
|
71
|
+
case @rule
|
72
|
+
when :required then
|
73
|
+
! value.nil? && (! kind_of?(MessageField) || value.initialized?)
|
74
|
+
when :repeated then
|
75
|
+
value.all? {|msg| ! kind_of?(MessageField) || msg.initialized? }
|
76
|
+
when :optional then
|
77
|
+
value.nil? || ! kind_of?(MessageField) || value.initialized?
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# Decode +bytes+ and pass to +message_instance+.
|
82
|
+
def set(message_instance, bytes)
|
83
|
+
if packed?
|
84
|
+
array = message_instance.__send__(getter_method_name)
|
85
|
+
method = \
|
86
|
+
case wire_type
|
87
|
+
when WireType::FIXED32 then :read_fixed32
|
88
|
+
when WireType::FIXED64 then :read_fixed64
|
89
|
+
when WireType::VARINT then :read_varint
|
90
|
+
end
|
91
|
+
stream = StringIO.new(bytes)
|
92
|
+
until stream.eof?
|
93
|
+
array << decode(Decoder.__send__(method, stream))
|
94
|
+
end
|
95
|
+
else
|
96
|
+
value = decode(bytes)
|
97
|
+
if repeated?
|
98
|
+
message_instance.__send__(getter_method_name) << value
|
99
|
+
else
|
100
|
+
message_instance.__send__(setter_method_name, value)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
# Decode +bytes+ and return a field value.
|
106
|
+
def decode(bytes)
|
107
|
+
raise NotImplementedError, "#{self.class.name}\#decode"
|
108
|
+
end
|
109
|
+
|
110
|
+
# Encode +value+ and return a byte string.
|
111
|
+
def encode(value)
|
112
|
+
raise NotImplementedError, "#{self.class.name}\#encode"
|
113
|
+
end
|
114
|
+
|
115
|
+
def extension?
|
116
|
+
!!@extension
|
117
|
+
end
|
118
|
+
|
119
|
+
# Is this a repeated field?
|
120
|
+
def repeated?
|
121
|
+
return @_repeated unless @_repeated.nil?
|
122
|
+
@_repeated = (@rule == :repeated)
|
123
|
+
end
|
124
|
+
|
125
|
+
# Is this a required field?
|
126
|
+
def required?
|
127
|
+
return @_required unless @_required.nil?
|
128
|
+
@_required = (@rule == :required)
|
129
|
+
end
|
130
|
+
|
131
|
+
# Is this a optional field?
|
132
|
+
def optional?
|
133
|
+
return @_optional unless @_optional.nil?
|
134
|
+
@_optional = (@rule == :optional)
|
135
|
+
end
|
136
|
+
|
137
|
+
# Is this a packed repeated field?
|
138
|
+
def packed?
|
139
|
+
!!@packed
|
140
|
+
end
|
141
|
+
|
142
|
+
# Upper limit for this field.
|
143
|
+
def max
|
144
|
+
self.class.max
|
145
|
+
end
|
146
|
+
|
147
|
+
# Lower limit for this field.
|
148
|
+
def min
|
149
|
+
self.class.min
|
150
|
+
end
|
151
|
+
|
152
|
+
def to_s
|
153
|
+
"#{@rule} #{@type} #{@name} = #{@tag} #{@default ? "[default=#{@default.inspect}]" : ''}"
|
154
|
+
end
|
155
|
+
|
156
|
+
private
|
157
|
+
|
158
|
+
##
|
159
|
+
# Private Instance Methods
|
160
|
+
#
|
161
|
+
def define_accessor
|
162
|
+
define_getter
|
163
|
+
if repeated?
|
164
|
+
define_array_setter
|
165
|
+
else
|
166
|
+
define_setter
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
def define_array_setter
|
171
|
+
field = self
|
172
|
+
@message_class.class_eval do
|
173
|
+
define_method(field.setter_method_name) do |val|
|
174
|
+
@values[field.name].replace(val)
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
def define_getter
|
180
|
+
field = self
|
181
|
+
@message_class.class_eval do
|
182
|
+
define_method(field.getter_method_name) do
|
183
|
+
@values.fetch(field.name, field.default_value)
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
def define_setter
|
189
|
+
field = self
|
190
|
+
@message_class.class_eval do
|
191
|
+
define_method(field.setter_method_name) do |val|
|
192
|
+
if val.nil?
|
193
|
+
@values.delete(field.name)
|
194
|
+
elsif field.acceptable?(val)
|
195
|
+
@values[field.name] = val
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
def typed_default_value
|
202
|
+
if @default.nil?
|
203
|
+
self.class.default
|
204
|
+
else
|
205
|
+
@default
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'protobuf/field/varint_field'
|
2
|
+
|
3
|
+
module Protobuf
|
4
|
+
module Field
|
5
|
+
class BoolField < VarintField
|
6
|
+
def self.default
|
7
|
+
false
|
8
|
+
end
|
9
|
+
|
10
|
+
def acceptable?(val)
|
11
|
+
raise TypeError unless [true, false].include?(val)
|
12
|
+
true
|
13
|
+
end
|
14
|
+
|
15
|
+
def decode(value)
|
16
|
+
value == 1
|
17
|
+
end
|
18
|
+
|
19
|
+
def encode(value)
|
20
|
+
[value ? 1 : 0].pack('C')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Protobuf
|
2
|
+
module Field
|
3
|
+
class BytesField < BaseField
|
4
|
+
def self.default
|
5
|
+
''
|
6
|
+
end
|
7
|
+
|
8
|
+
def wire_type
|
9
|
+
WireType::LENGTH_DELIMITED
|
10
|
+
end
|
11
|
+
|
12
|
+
def acceptable?(val)
|
13
|
+
if val.is_a?(::Protobuf::Message) || val.instance_of?(String)
|
14
|
+
return true
|
15
|
+
end
|
16
|
+
|
17
|
+
raise TypeError
|
18
|
+
end
|
19
|
+
|
20
|
+
def decode(bytes)
|
21
|
+
bytes.force_encoding(::Protobuf::Message::STRING_ENCODING) if bytes.respond_to?(:force_encoding)
|
22
|
+
bytes
|
23
|
+
end
|
24
|
+
|
25
|
+
def encode(value)
|
26
|
+
if value.is_a?(::Protobuf::Message)
|
27
|
+
value = value.serialize_to_string
|
28
|
+
else
|
29
|
+
value = value.dup
|
30
|
+
value.force_encoding(::Protobuf::Message::STRING_ENCODING) if value.respond_to?(:force_encoding)
|
31
|
+
end
|
32
|
+
|
33
|
+
string_size = VarintField.encode(value.size)
|
34
|
+
string_size << value
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'protobuf/field/float_field'
|
2
|
+
|
3
|
+
module Protobuf
|
4
|
+
module Field
|
5
|
+
class DoubleField < FloatField
|
6
|
+
def wire_type
|
7
|
+
WireType::FIXED64
|
8
|
+
end
|
9
|
+
|
10
|
+
def decode(bytes)
|
11
|
+
bytes.unpack('E').first
|
12
|
+
end
|
13
|
+
|
14
|
+
def encode(value)
|
15
|
+
[value].pack('E')
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'protobuf/field/varint_field'
|
2
|
+
|
3
|
+
module Protobuf
|
4
|
+
module Field
|
5
|
+
class EnumField < VarintField
|
6
|
+
def acceptable?(val)
|
7
|
+
case val
|
8
|
+
when Symbol then
|
9
|
+
raise TypeError unless @type.const_defined?(val)
|
10
|
+
when EnumValue then
|
11
|
+
raise TypeError if val.parent_class != @type
|
12
|
+
else
|
13
|
+
raise TypeError unless @type.valid_tag?(val)
|
14
|
+
end
|
15
|
+
true
|
16
|
+
end
|
17
|
+
|
18
|
+
def encode(value)
|
19
|
+
super(value.to_i)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def typed_default_value
|
25
|
+
if @default.is_a?(Symbol)
|
26
|
+
@type.const_get(@default)
|
27
|
+
else
|
28
|
+
self.class.default
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def define_setter
|
33
|
+
field = self
|
34
|
+
@message_class.class_eval do
|
35
|
+
define_method("#{field.name}=") do |val|
|
36
|
+
if val.nil?
|
37
|
+
@values.delete(field.name)
|
38
|
+
else
|
39
|
+
val = \
|
40
|
+
case val
|
41
|
+
when Symbol then
|
42
|
+
field.type.const_get(val) rescue nil
|
43
|
+
when Integer then
|
44
|
+
field.type.const_get(field.type.name_by_value(val)) rescue nil
|
45
|
+
when EnumValue then
|
46
|
+
raise TypeError, "Invalid value: #{val.inspect} for #{field.name}" if val.parent_class != field.type
|
47
|
+
val
|
48
|
+
end
|
49
|
+
raise TypeError, "Invalid value: #{val.inspect} for #{field.name}" unless val
|
50
|
+
|
51
|
+
@values[field.name] = val
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Protobuf
|
2
|
+
module Field
|
3
|
+
class ExtensionFields < Hash
|
4
|
+
##
|
5
|
+
# Constructor
|
6
|
+
#
|
7
|
+
def initialize(key_range=0..-1)
|
8
|
+
@key_range = key_range
|
9
|
+
end
|
10
|
+
|
11
|
+
##
|
12
|
+
# Public Instance Methods
|
13
|
+
#
|
14
|
+
def []=(key, value)
|
15
|
+
raise RangeError, "#{key} is not in #{@key_range}" unless @key_range.include?(key)
|
16
|
+
super
|
17
|
+
end
|
18
|
+
|
19
|
+
def include_tag?(tag)
|
20
|
+
@key_range.include?(tag)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module Protobuf
|
2
|
+
module Field
|
3
|
+
class FieldArray < Array
|
4
|
+
##
|
5
|
+
# Constructor
|
6
|
+
#
|
7
|
+
def initialize(field)
|
8
|
+
@field = field
|
9
|
+
end
|
10
|
+
|
11
|
+
##
|
12
|
+
# Public Instance Methods
|
13
|
+
#
|
14
|
+
def []=(nth, val)
|
15
|
+
super(nth, normalize(val))
|
16
|
+
end
|
17
|
+
|
18
|
+
def <<(val)
|
19
|
+
super(normalize(val))
|
20
|
+
end
|
21
|
+
|
22
|
+
def push(val)
|
23
|
+
super(normalize(val))
|
24
|
+
end
|
25
|
+
|
26
|
+
def unshift(val)
|
27
|
+
super(normalize(val))
|
28
|
+
end
|
29
|
+
|
30
|
+
def replace(val)
|
31
|
+
raise TypeError unless val.is_a?(Array)
|
32
|
+
val = val.map {|v| normalize(v)}
|
33
|
+
super(val)
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_s
|
37
|
+
"[#{@field.name}]"
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
##
|
43
|
+
# Private Instance Methods
|
44
|
+
#
|
45
|
+
def normalize(val)
|
46
|
+
raise TypeError unless @field.acceptable?(val)
|
47
|
+
if @field.is_a?(::Protobuf::Field::MessageField) && val.is_a?(Hash)
|
48
|
+
@field.type.new(val)
|
49
|
+
else
|
50
|
+
val
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'protobuf/field/uint32_field.rb'
|
2
|
+
|
3
|
+
module Protobuf
|
4
|
+
module Field
|
5
|
+
class Fixed32Field < Uint32Field
|
6
|
+
def wire_type
|
7
|
+
::Protobuf::WireType::FIXED32
|
8
|
+
end
|
9
|
+
|
10
|
+
def decode(bytes)
|
11
|
+
bytes.unpack('V').first
|
12
|
+
end
|
13
|
+
|
14
|
+
def encode(value)
|
15
|
+
[value].pack('V')
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|