macks-ruby_protobuf 0.3.2.3 → 0.3.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.
- data/History.txt +7 -1
- data/Manifest.txt +1 -1
- data/Rakefile +5 -5
- data/bin/rprotoc +6 -6
- data/lib/protobuf/compiler/compiler.rb +9 -11
- data/lib/protobuf/compiler/nodes.rb +3 -1
- data/lib/protobuf/compiler/proto.y +3 -3
- data/lib/protobuf/compiler/proto_parser.rb +7 -7
- data/lib/protobuf/message/field.rb +54 -14
- data/lib/protobuf/message/message.rb +14 -16
- data/lib/ruby_protobuf.rb +1 -1
- data/test/test_compiler.rb +20 -6
- data/test/test_message.rb +16 -0
- data/test/test_optional_field.rb +12 -0
- data/test/test_repeated_types.rb +106 -0
- data/test/test_serialize.rb +19 -0
- metadata +10 -7
data/History.txt
CHANGED
@@ -1,5 +1,11 @@
|
|
1
|
-
=== 0.3.
|
1
|
+
=== 0.3.3 / 2009-07-10
|
2
2
|
|
3
|
+
* 0.3.3 (rev.218)
|
4
|
+
* Support C-like comment.
|
5
|
+
* Optimize for speed.
|
6
|
+
* Bug fixes.
|
7
|
+
* 0.3.2 (rev.185)
|
8
|
+
* Bug fixes.
|
3
9
|
* 0.3.1 fixed bugs concerning RPC (rev.146)
|
4
10
|
* 0.3.0 RPC is available (rev.124)
|
5
11
|
* RPC is available
|
data/Manifest.txt
CHANGED
@@ -2,6 +2,7 @@ History.txt
|
|
2
2
|
Manifest.txt
|
3
3
|
README.txt
|
4
4
|
Rakefile
|
5
|
+
script/mk_parser
|
5
6
|
bin/rprotoc
|
6
7
|
examples/addressbook.proto
|
7
8
|
examples/addressbook.pb.rb
|
@@ -36,7 +37,6 @@ lib/protobuf/rpc/client.rb
|
|
36
37
|
lib/protobuf/rpc/handler.rb
|
37
38
|
lib/protobuf/rpc/server.rb
|
38
39
|
lib/ruby_protobuf.rb
|
39
|
-
script/mk_parser
|
40
40
|
test/addressbook.rb
|
41
41
|
test/addressbook_base.rb
|
42
42
|
test/addressbook_ext.rb
|
data/Rakefile
CHANGED
@@ -5,13 +5,13 @@ require 'rubygems'
|
|
5
5
|
require 'hoe'
|
6
6
|
require 'ruby_protobuf'
|
7
7
|
|
8
|
-
Hoe.
|
9
|
-
p.
|
10
|
-
p.
|
11
|
-
p.
|
8
|
+
Hoe.spec('ruby_protobuf') do |p|
|
9
|
+
p.version = RubyProtobuf::VERSION
|
10
|
+
p.rubyforge_name = 'ruby-protobuf'
|
11
|
+
p.developer('MATSUYAMA Kengo', 'macksx@gmail.com')
|
12
12
|
p.summary = 'Protocol Buffers for Ruby'
|
13
13
|
p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
|
14
|
-
p.url = 'http://
|
14
|
+
p.url = 'http://code.google.com/p/ruby-protobuf'
|
15
15
|
p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
|
16
16
|
end
|
17
17
|
|
data/bin/rprotoc
CHANGED
@@ -10,10 +10,10 @@ require 'ruby_protobuf'
|
|
10
10
|
require 'protobuf/compiler/compiler'
|
11
11
|
|
12
12
|
|
13
|
-
|
13
|
+
options = {}
|
14
14
|
opts = OptionParser.new("#{$0} [OPTIONS] PROTO_FILE")
|
15
|
-
opts.on('-p', '--proto_path <PATH>', 'Specify the directory in which to search for imports. The current directory is default.'){|v|
|
16
|
-
opts.on('-o', '--out <OUT_DIR>', 'Specify the directory in which Ruby source file is generated. The current directory is default.'){|v|
|
15
|
+
opts.on('-p', '--proto_path <PATH>', 'Specify the directory in which to search for imports. The current directory is default.'){|v| options[:proto_path] = v}
|
16
|
+
opts.on('-o', '--out <OUT_DIR>', 'Specify the directory in which Ruby source file is generated. The current directory is default.'){|v| options[:out] = v}
|
17
17
|
opts.on_tail('-v', '--version', 'Show version.'){puts(opts.ver); exit}
|
18
18
|
opts.on_tail('-h', '--help', 'Show this message.'){puts(opts.help); exit}
|
19
19
|
|
@@ -26,11 +26,11 @@ rescue OptionParser::ParseError
|
|
26
26
|
exit 1
|
27
27
|
end
|
28
28
|
|
29
|
-
|
29
|
+
proto_file = ARGV.shift
|
30
30
|
|
31
|
-
unless
|
31
|
+
unless proto_file
|
32
32
|
puts opts
|
33
33
|
exit
|
34
34
|
end
|
35
35
|
|
36
|
-
Protobuf::Compiler.compile(
|
36
|
+
Protobuf::Compiler.compile(proto_file, (options[:proto_path] or '.'), (options[:out] or '.'))
|
@@ -15,12 +15,7 @@ module Protobuf
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def create_message(proto_file, proto_dir='.', out_dir='.', file_create=true)
|
18
|
-
out_dir.sub
|
19
|
-
proto_dir.sub! %r{/$}, ''
|
20
|
-
rb_file =
|
21
|
-
if proto_file =~ %r{^/}
|
22
|
-
then "#{out_dir}/#{proto_file.split('/').last.sub(/\.proto$/, '')}.pb.rb"
|
23
|
-
else "#{out_dir}/#{proto_file.sub(/\.proto$/, '')}.pb.rb" end
|
18
|
+
rb_file = File.join(out_dir, File.basename(proto_file).sub(/\.[^\0]*\z/, '') + '.pb.rb')
|
24
19
|
proto_path = validate_existence proto_file, proto_dir
|
25
20
|
|
26
21
|
message_visitor = Protobuf::Visitor::CreateMessageVisitor.new proto_file, proto_dir, out_dir
|
@@ -31,8 +26,7 @@ module Protobuf
|
|
31
26
|
end
|
32
27
|
|
33
28
|
def create_rpc(proto_file, proto_dir='.', out_dir='.', file_create=true)
|
34
|
-
message_file =
|
35
|
-
out_dir = "#{out_dir}/#{File.dirname proto_file}"
|
29
|
+
message_file = File.join(out_dir, File.basename(proto_file).sub(/\.[^\0]*\z/, '') + '.pb.rb')
|
36
30
|
proto_path = validate_existence proto_file, proto_dir
|
37
31
|
|
38
32
|
rpc_visitor = Protobuf::Visitor::CreateRpcVisitor.new
|
@@ -44,11 +38,15 @@ module Protobuf
|
|
44
38
|
|
45
39
|
def validate_existence(path, base_dir)
|
46
40
|
if File.exist? path
|
47
|
-
|
41
|
+
path
|
48
42
|
else
|
49
|
-
|
43
|
+
newpath = File.join(base_dir, path)
|
44
|
+
if File.exist? newpath
|
45
|
+
newpath
|
46
|
+
else
|
47
|
+
raise ArgumentError.new("File does not exist: #{path}")
|
48
|
+
end
|
50
49
|
end
|
51
|
-
path
|
52
50
|
end
|
53
51
|
end
|
54
52
|
end
|
@@ -106,7 +106,9 @@ require 'protobuf/message/extend'
|
|
106
106
|
end
|
107
107
|
|
108
108
|
def accept_message_visitor(visitor)
|
109
|
-
|
109
|
+
class_name = @name.to_s
|
110
|
+
class_name.gsub!(/\A[a-z]/) {|c| c.upcase}
|
111
|
+
visitor.write "class #{class_name} < ::Protobuf::Message"
|
110
112
|
visitor.in_context self.class do
|
111
113
|
define_in_the_file visitor
|
112
114
|
#@children.each {|child| child.accept_message_visitor visitor}
|
@@ -185,14 +185,14 @@ end
|
|
185
185
|
raise 'EOF inside block comment' until @scanner.scan_until(/\*\//)
|
186
186
|
when match(/(?:required|optional|repeated|import|package|option|message|extend|enum|service|rpc|returns|group|default|extensions|to|max|double|float|int32|int64|uint32|uint64|sint32|sint64|fixed32|fixed64|sfixed32|sfixed64|bool|string|bytes)\b/)
|
187
187
|
yield [@token, @token.to_sym]
|
188
|
-
when match(/[
|
188
|
+
when match(/[+-]?\d*\.\d+([Ee][\+-]?\d+)?/)
|
189
|
+
yield [:FLOAT_LITERAL, @token.to_f]
|
190
|
+
when match(/[+-]?[1-9]\d*(?!\.)/, /0(?![.xX0-9])/)
|
189
191
|
yield [:DEC_INTEGER, @token.to_i]
|
190
192
|
when match(/0[xX]([A-Fa-f0-9])+/)
|
191
193
|
yield [:HEX_INTEGER, @token.to_i(0)]
|
192
194
|
when match(/0[0-7]+/)
|
193
195
|
yield [:OCT_INTEGER, @token.to_i(0)]
|
194
|
-
when match(/\d+(\.\d+)?([Ee][\+-]?\d+)?/)
|
195
|
-
yield [:FLOAT_LITERAL, @token.to_f]
|
196
196
|
when match(/(true|false)\b/)
|
197
197
|
yield [:BOOLEAN_LITERAL, @token == 'true']
|
198
198
|
when match(/"(?:[^"\\]+|\\.)*"/, /'(?:[^'\\]+|\\.)*'/)
|
@@ -10,7 +10,7 @@
|
|
10
10
|
unless $".index 'racc/parser.rb'
|
11
11
|
$".push 'racc/parser.rb'
|
12
12
|
|
13
|
-
self.class.module_eval <<'..end racc/parser.rb modeval..
|
13
|
+
self.class.module_eval <<'..end racc/parser.rb modeval..id521073ac1f', 'racc/parser.rb', 1
|
14
14
|
#
|
15
15
|
# $Id: parser.rb,v 1.7 2005/11/20 17:31:32 aamine Exp $
|
16
16
|
#
|
@@ -453,7 +453,7 @@ module Racc
|
|
453
453
|
end
|
454
454
|
|
455
455
|
end
|
456
|
-
..end racc/parser.rb modeval..
|
456
|
+
..end racc/parser.rb modeval..id521073ac1f
|
457
457
|
end
|
458
458
|
###### racc/parser.rb end
|
459
459
|
|
@@ -462,7 +462,7 @@ module Protobuf
|
|
462
462
|
|
463
463
|
class ProtoParser < Racc::Parser
|
464
464
|
|
465
|
-
module_eval <<'..end lib/protobuf/compiler/proto.y modeval..
|
465
|
+
module_eval <<'..end lib/protobuf/compiler/proto.y modeval..id7865f341f1', 'lib/protobuf/compiler/proto.y', 163
|
466
466
|
|
467
467
|
require 'strscan'
|
468
468
|
|
@@ -488,14 +488,14 @@ module_eval <<'..end lib/protobuf/compiler/proto.y modeval..idbff1afc93c', 'lib/
|
|
488
488
|
raise 'EOF inside block comment' until @scanner.scan_until(/\*\//)
|
489
489
|
when match(/(?:required|optional|repeated|import|package|option|message|extend|enum|service|rpc|returns|group|default|extensions|to|max|double|float|int32|int64|uint32|uint64|sint32|sint64|fixed32|fixed64|sfixed32|sfixed64|bool|string|bytes)\b/)
|
490
490
|
yield [@token, @token.to_sym]
|
491
|
-
when match(/[
|
491
|
+
when match(/[+-]?\d*\.\d+([Ee][\+-]?\d+)?/)
|
492
|
+
yield [:FLOAT_LITERAL, @token.to_f]
|
493
|
+
when match(/[+-]?[1-9]\d*(?!\.)/, /0(?![.xX0-9])/)
|
492
494
|
yield [:DEC_INTEGER, @token.to_i]
|
493
495
|
when match(/0[xX]([A-Fa-f0-9])+/)
|
494
496
|
yield [:HEX_INTEGER, @token.to_i(0)]
|
495
497
|
when match(/0[0-7]+/)
|
496
498
|
yield [:OCT_INTEGER, @token.to_i(0)]
|
497
|
-
when match(/\d+(\.\d+)?([Ee][\+-]?\d+)?/)
|
498
|
-
yield [:FLOAT_LITERAL, @token.to_f]
|
499
499
|
when match(/(true|false)\b/)
|
500
500
|
yield [:BOOLEAN_LITERAL, @token == 'true']
|
501
501
|
when match(/"(?:[^"\\]+|\\.)*"/, /'(?:[^'\\]+|\\.)*'/)
|
@@ -522,7 +522,7 @@ module_eval <<'..end lib/protobuf/compiler/proto.y modeval..idbff1afc93c', 'lib/
|
|
522
522
|
end
|
523
523
|
false
|
524
524
|
end
|
525
|
-
..end lib/protobuf/compiler/proto.y modeval..
|
525
|
+
..end lib/protobuf/compiler/proto.y modeval..id7865f341f1
|
526
526
|
|
527
527
|
##### racc 1.4.5 generates ###
|
528
528
|
|
@@ -85,7 +85,11 @@ module Protobuf
|
|
85
85
|
|
86
86
|
def define_accessor
|
87
87
|
define_getter
|
88
|
-
|
88
|
+
if rule == :repeated
|
89
|
+
define_array_setter
|
90
|
+
else
|
91
|
+
define_setter
|
92
|
+
end
|
89
93
|
end
|
90
94
|
|
91
95
|
def define_getter
|
@@ -114,6 +118,15 @@ module Protobuf
|
|
114
118
|
end
|
115
119
|
end
|
116
120
|
|
121
|
+
def define_array_setter
|
122
|
+
field = self
|
123
|
+
@message_class.class_eval do
|
124
|
+
define_method("#{field.name}=") do |val|
|
125
|
+
@values[field.name].replace(val)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
117
130
|
public
|
118
131
|
|
119
132
|
# encoder/decoder related methods
|
@@ -224,32 +237,41 @@ module Protobuf
|
|
224
237
|
end
|
225
238
|
|
226
239
|
def []=(nth, val)
|
227
|
-
|
228
|
-
super
|
229
|
-
end
|
240
|
+
super(normalize(val))
|
230
241
|
end
|
231
242
|
|
232
243
|
def <<(val)
|
233
|
-
|
234
|
-
super
|
235
|
-
end
|
244
|
+
super(normalize(val))
|
236
245
|
end
|
237
246
|
|
238
247
|
def push(val)
|
239
|
-
|
240
|
-
super
|
241
|
-
end
|
248
|
+
super(normalize(val))
|
242
249
|
end
|
243
250
|
|
244
251
|
def unshift(val)
|
245
|
-
|
246
|
-
|
247
|
-
|
252
|
+
super(normalize(val))
|
253
|
+
end
|
254
|
+
|
255
|
+
def replace(val)
|
256
|
+
raise TypeError unless val.is_a?(Array)
|
257
|
+
val = val.map {|v| normalize(v)}
|
258
|
+
super(val)
|
248
259
|
end
|
249
260
|
|
250
261
|
def to_s
|
251
262
|
"[#{@field.name}]"
|
252
263
|
end
|
264
|
+
|
265
|
+
private
|
266
|
+
|
267
|
+
def normalize(val)
|
268
|
+
raise TypeError unless @field.acceptable?(val)
|
269
|
+
if @field.is_a?(MessageField) && val.is_a?(Hash)
|
270
|
+
@field.type.new(val)
|
271
|
+
else
|
272
|
+
val
|
273
|
+
end
|
274
|
+
end
|
253
275
|
end
|
254
276
|
|
255
277
|
class BytesField < BaseField
|
@@ -529,6 +551,24 @@ module Protobuf
|
|
529
551
|
def default; nil end
|
530
552
|
end
|
531
553
|
|
554
|
+
def define_setter
|
555
|
+
field = self
|
556
|
+
@message_class.class_eval do
|
557
|
+
define_method("#{field.name}=") do |val|
|
558
|
+
case val
|
559
|
+
when nil
|
560
|
+
@values.delete(field.name)
|
561
|
+
when Hash
|
562
|
+
@values[field.name] = field.type.new(val)
|
563
|
+
when field.type
|
564
|
+
@values[field.name] = val
|
565
|
+
else
|
566
|
+
raise TypeError
|
567
|
+
end
|
568
|
+
end
|
569
|
+
end
|
570
|
+
end
|
571
|
+
|
532
572
|
def wire_type
|
533
573
|
Protobuf::WireType::LENGTH_DELIMITED
|
534
574
|
end
|
@@ -542,7 +582,7 @@ module Protobuf
|
|
542
582
|
end
|
543
583
|
|
544
584
|
def acceptable?(val)
|
545
|
-
raise TypeError unless val.instance_of?
|
585
|
+
raise TypeError unless val.instance_of?(type) or val.instance_of?(Hash)
|
546
586
|
true
|
547
587
|
end
|
548
588
|
|
@@ -188,9 +188,9 @@ module Protobuf
|
|
188
188
|
"#{i}#{field.name} {\n#{value.inspect(indent + 1)}#{i}}\n"
|
189
189
|
end
|
190
190
|
elsif field.is_a? Protobuf::Field::EnumField
|
191
|
-
|
192
|
-
|
193
|
-
|
191
|
+
if field.optional? and not has_field?(field.name)
|
192
|
+
''
|
193
|
+
else
|
194
194
|
"#{i}#{field.name}: #{field.type.name_by_value(value)}\n"
|
195
195
|
end
|
196
196
|
else
|
@@ -225,7 +225,7 @@ module Protobuf
|
|
225
225
|
if filename.is_a? File
|
226
226
|
parse_from filename
|
227
227
|
else
|
228
|
-
File.open(filename, '
|
228
|
+
File.open(filename, 'rb') do |f|
|
229
229
|
parse_from f
|
230
230
|
end
|
231
231
|
end
|
@@ -238,7 +238,9 @@ module Protobuf
|
|
238
238
|
def serialize_to_string(string='')
|
239
239
|
io = StringIO.new string
|
240
240
|
serialize_to io
|
241
|
-
io.string
|
241
|
+
result = io.string
|
242
|
+
result.force_encoding('ASCII-8BIT') if result.respond_to?(:force_encoding)
|
243
|
+
result
|
242
244
|
end
|
243
245
|
alias to_s serialize_to_string
|
244
246
|
|
@@ -246,7 +248,7 @@ module Protobuf
|
|
246
248
|
if filename.is_a? File
|
247
249
|
serialize_to filename
|
248
250
|
else
|
249
|
-
File.open(filename, '
|
251
|
+
File.open(filename, 'wb') do |f|
|
250
252
|
serialize_to f
|
251
253
|
end
|
252
254
|
end
|
@@ -263,8 +265,8 @@ module Protobuf
|
|
263
265
|
end
|
264
266
|
|
265
267
|
def set_field(tag, bytes)
|
266
|
-
|
267
|
-
|
268
|
+
field = (get_field_by_tag(tag) or get_ext_field_by_tag(tag))
|
269
|
+
field.set self, bytes if field
|
268
270
|
end
|
269
271
|
|
270
272
|
def merge_field(tag, value)
|
@@ -273,22 +275,18 @@ module Protobuf
|
|
273
275
|
end
|
274
276
|
|
275
277
|
def [](tag_or_name)
|
276
|
-
if field = get_field(tag_or_name)
|
277
|
-
send field.name
|
278
|
-
elsif field = get_ext_field(tag_or_name)
|
278
|
+
if field = get_field(tag_or_name) || get_ext_field(tag_or_name)
|
279
279
|
send field.name
|
280
280
|
else
|
281
|
-
raise NoMethodError.new("No such
|
281
|
+
raise NoMethodError.new("No such field: #{tag_or_name.inspect}")
|
282
282
|
end
|
283
283
|
end
|
284
284
|
|
285
285
|
def []=(tag_or_name, value)
|
286
|
-
if field = get_field(tag_or_name)
|
287
|
-
send "#{field.name}=", value
|
288
|
-
elsif field = get_ext_field(tag_or_name) and not field.repeated?
|
286
|
+
if field = get_field(tag_or_name) || get_ext_field(tag_or_name)
|
289
287
|
send "#{field.name}=", value
|
290
288
|
else
|
291
|
-
raise NoMethodError.new("No such
|
289
|
+
raise NoMethodError.new("No such field: #{tag_or_name.inspect}")
|
292
290
|
end
|
293
291
|
end
|
294
292
|
|
data/lib/ruby_protobuf.rb
CHANGED
data/test/test_compiler.rb
CHANGED
@@ -8,7 +8,15 @@ class CompilerTest < Test::Unit::TestCase
|
|
8
8
|
assert_compile_proto <<-eos, 'test/proto/float_default.proto'
|
9
9
|
### Generated by rprotoc. DO NOT EDIT!
|
10
10
|
### <proto file: test/proto/float_default.proto>
|
11
|
-
# message M {
|
11
|
+
# message M {
|
12
|
+
# optional float f = 1 [default = 4.2];
|
13
|
+
# optional float g = 2 [default = -4.2];
|
14
|
+
# optional float h = 3 [default = 4352];
|
15
|
+
# optional float i = 4 [default = 23145.2 ];
|
16
|
+
# optional float j = 5 [default = -5 ];
|
17
|
+
# optional float k = 6 [default = +23 ];
|
18
|
+
# optional float l = 7 [default = +23.42 ];
|
19
|
+
# }
|
12
20
|
#
|
13
21
|
|
14
22
|
require 'protobuf/message/message'
|
@@ -19,6 +27,12 @@ require 'protobuf/message/extend'
|
|
19
27
|
class M < ::Protobuf::Message
|
20
28
|
defined_in __FILE__
|
21
29
|
optional :float, :f, 1, :default => 4.2
|
30
|
+
optional :float, :g, 2, :default => -4.2
|
31
|
+
optional :float, :h, 3, :default => 4352
|
32
|
+
optional :float, :i, 4, :default => 23145.2
|
33
|
+
optional :float, :j, 5, :default => -5
|
34
|
+
optional :float, :k, 6, :default => 23
|
35
|
+
optional :float, :l, 7, :default => 23.42
|
22
36
|
end
|
23
37
|
eos
|
24
38
|
end
|
@@ -142,9 +156,9 @@ end
|
|
142
156
|
end
|
143
157
|
|
144
158
|
def test_create_rpc
|
145
|
-
file_contents = Protobuf::Compiler.new.create_rpc('test/proto/rpc.proto', '.', '
|
159
|
+
file_contents = Protobuf::Compiler.new.create_rpc('test/proto/rpc.proto', '.', 'test/proto', false)
|
146
160
|
|
147
|
-
assert_source <<-eos, file_contents['
|
161
|
+
assert_source <<-eos, file_contents['test/proto/address_book_service.rb']
|
148
162
|
require 'protobuf/rpc/server'
|
149
163
|
require 'protobuf/rpc/handler'
|
150
164
|
require 'test/proto/rpc.pb'
|
@@ -177,14 +191,14 @@ class Tutorial::AddressBookService < Protobuf::Rpc::Server
|
|
177
191
|
end
|
178
192
|
eos
|
179
193
|
|
180
|
-
assert_source <<-eos, file_contents['
|
194
|
+
assert_source <<-eos, file_contents['test/proto/start_address_book_service']
|
181
195
|
#!/usr/bin/env ruby
|
182
196
|
require 'address_book_service'
|
183
197
|
|
184
198
|
Tutorial::AddressBookService.new(:port => 9999).start
|
185
199
|
eos
|
186
200
|
|
187
|
-
assert_source <<-eos, file_contents['
|
201
|
+
assert_source <<-eos, file_contents['test/proto/client_search.rb']
|
188
202
|
#!/usr/bin/env ruby
|
189
203
|
require 'protobuf/rpc/client'
|
190
204
|
require 'test/proto/rpc.pb'
|
@@ -204,7 +218,7 @@ Protobuf::Rpc::Client.new('localhost', 9999).call :search, request, response
|
|
204
218
|
puts response
|
205
219
|
eos
|
206
220
|
|
207
|
-
assert_source <<-eos, file_contents['
|
221
|
+
assert_source <<-eos, file_contents['test/proto/client_add.rb']
|
208
222
|
#!/usr/bin/env ruby
|
209
223
|
require 'protobuf/rpc/client'
|
210
224
|
require 'test/proto/rpc.pb'
|
data/test/test_message.rb
CHANGED
@@ -36,6 +36,22 @@ class MessageTest < Test::Unit::TestCase
|
|
36
36
|
assert_equal 'Jiro', person.name
|
37
37
|
assert_equal 300, person.id
|
38
38
|
assert_equal 'jiro@ema.il', person.email
|
39
|
+
|
40
|
+
# initialize with array of hash
|
41
|
+
person = Tutorial::Person.new(:phone => [{:number => 'phone1'}, {:number => 'phone2'}])
|
42
|
+
assert_equal 'phone1', person.phone[0].number
|
43
|
+
assert_equal 'phone2', person.phone[1].number
|
44
|
+
|
45
|
+
# initalize with hash in hash
|
46
|
+
message = Test::MergeMessage.new(:require_message => { :name => 'name1', :repeate_message => [{:name => 'name2'}] })
|
47
|
+
assert_equal 'name1', message.require_message.name
|
48
|
+
assert_equal 'name2', message.require_message.repeate_message[0].name
|
49
|
+
|
50
|
+
message.require_message = { :name => 'name21' }
|
51
|
+
message.require_message.repeate_message = [ {:name => 'name22'} ]
|
52
|
+
assert_equal 'name21', message.require_message.name
|
53
|
+
assert_equal 'name22', message.require_message.repeate_message[0].name
|
54
|
+
assert_equal 1, message.require_message.repeate_message.size
|
39
55
|
end
|
40
56
|
|
41
57
|
def test_defined_filenames
|
data/test/test_optional_field.rb
CHANGED
@@ -15,6 +15,9 @@ class OptionalFieldTest < Test::Unit::TestCase
|
|
15
15
|
assert !message.has_field?(:enum)
|
16
16
|
assert_equal 2, message.enum
|
17
17
|
|
18
|
+
assert !message.has_field?(:signed)
|
19
|
+
assert_equal(-100, message.signed)
|
20
|
+
|
18
21
|
# assign values
|
19
22
|
assert_nothing_raised { message.number = 100 }
|
20
23
|
assert message.has_field?(:number)
|
@@ -27,6 +30,10 @@ class OptionalFieldTest < Test::Unit::TestCase
|
|
27
30
|
assert_nothing_raised { message.enum = Test::Optional_field::Message::Enum::A }
|
28
31
|
assert message.has_field?(:enum)
|
29
32
|
assert_equal 1, message.enum
|
33
|
+
|
34
|
+
assert_nothing_raised { message.signed = -20 }
|
35
|
+
assert message.has_field?(:signed)
|
36
|
+
assert_equal(-20, message.signed)
|
30
37
|
end
|
31
38
|
|
32
39
|
def test_serialize
|
@@ -40,15 +47,18 @@ class OptionalFieldTest < Test::Unit::TestCase
|
|
40
47
|
assert_equal message1.number, message2.number
|
41
48
|
assert_equal message1.text, message2.text
|
42
49
|
assert_equal message1.enum, message2.enum
|
50
|
+
assert_equal message1.signed, message2.signed
|
43
51
|
assert !message2.has_field?(:number)
|
44
52
|
assert !message2.has_field?(:text)
|
45
53
|
assert !message2.has_field?(:enum)
|
54
|
+
assert !message2.has_field?(:signed)
|
46
55
|
|
47
56
|
# assign the value whith is equal to default value
|
48
57
|
message1 = Test::Optional_field::Message.new
|
49
58
|
message1.number = message1.number
|
50
59
|
message1.text = message1.text
|
51
60
|
message1.enum = message1.enum
|
61
|
+
message1.signed = message1.signed
|
52
62
|
serialized_string = message1.to_s
|
53
63
|
assert !serialized_string.empty?
|
54
64
|
|
@@ -61,8 +71,10 @@ class OptionalFieldTest < Test::Unit::TestCase
|
|
61
71
|
assert_equal message1.number, message2.number
|
62
72
|
assert_equal message1.text, message2.text
|
63
73
|
assert_equal message1.enum, message2.enum
|
74
|
+
assert_equal message1.signed, message2.signed
|
64
75
|
assert message2.has_field?(:number)
|
65
76
|
assert message2.has_field?(:text)
|
66
77
|
assert !message2.has_field?(:enum)
|
78
|
+
assert !message2.has_field?(:signed)
|
67
79
|
end
|
68
80
|
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'test/types'
|
3
|
+
|
4
|
+
class RepeatedTypesTest < Test::Unit::TestCase
|
5
|
+
def test_serialize
|
6
|
+
# empty set
|
7
|
+
types = Test::Types::RepeatedTypes.new
|
8
|
+
assert_equal('', types.to_s)
|
9
|
+
|
10
|
+
# has 1 member
|
11
|
+
types = Test::Types::RepeatedTypes.new
|
12
|
+
types.type1 << 0.01
|
13
|
+
types.type2 << 0.1
|
14
|
+
types.type3 << 1
|
15
|
+
types.type4 << 10
|
16
|
+
types.type5 << 100
|
17
|
+
types.type6 << 1000
|
18
|
+
types.type7 << -1
|
19
|
+
types.type8 << -10
|
20
|
+
types.type9 << 10000
|
21
|
+
types.type10 << 100000
|
22
|
+
types.type11 << false
|
23
|
+
types.type12 << 'hello all types'
|
24
|
+
image_bin = File.open('test/data/unk.png', 'r+b'){|f| f.read}
|
25
|
+
types.type13 << image_bin
|
26
|
+
types.type14 << -100
|
27
|
+
types.type15 << -1000
|
28
|
+
|
29
|
+
serialized_string = types.serialize_to_string
|
30
|
+
|
31
|
+
types2 = Test::Types::RepeatedTypes.new
|
32
|
+
types2.parse_from_string serialized_string
|
33
|
+
assert_in_delta(0.01, types2.type1[0], 0.00001)
|
34
|
+
assert_in_delta(0.1, types2.type2[0], 0.00001)
|
35
|
+
assert_equal(1, types2.type3[0])
|
36
|
+
assert_equal(10, types2.type4[0])
|
37
|
+
assert_equal(100, types2.type5[0])
|
38
|
+
assert_equal(1000, types2.type6[0])
|
39
|
+
assert_equal(-1, types2.type7[0])
|
40
|
+
assert_equal(-10, types2.type8[0])
|
41
|
+
assert_equal(10000, types2.type9[0])
|
42
|
+
assert_equal(100000, types2.type10[0])
|
43
|
+
assert(!types2.type11[0])
|
44
|
+
assert_equal('hello all types', types2.type12[0])
|
45
|
+
assert_equal(10938, types2.type13[0].size)
|
46
|
+
assert_equal(image_bin, types2.type13[0])
|
47
|
+
assert_equal(-100, types2.type14[0])
|
48
|
+
assert_equal(-1000, types2.type15[0])
|
49
|
+
|
50
|
+
# has 2 members
|
51
|
+
types.type1 << 1.0/0 # double (Inf)
|
52
|
+
types.type2 << -1.0/0 # float (-Inf)
|
53
|
+
types.type3 << -1 # int32
|
54
|
+
types.type4 << -10 # int64
|
55
|
+
types.type5 << 100 # uint32
|
56
|
+
types.type6 << 1000 # uint64
|
57
|
+
types.type7 << -1000 # sint32
|
58
|
+
types.type8 << -10000 # sint64
|
59
|
+
types.type9 << 10000 # fixed32
|
60
|
+
types.type10 << 100000 # fixed64
|
61
|
+
types.type11 << true
|
62
|
+
types.type12 << 'hello all types'
|
63
|
+
image_bin = File.open('test/data/unk.png', 'r+b'){|f| f.read}
|
64
|
+
types.type13 << image_bin
|
65
|
+
types.type14 << -2_000_000_000 # sfixed32
|
66
|
+
types.type15 << -8_000_000_000_000_000_000 # sfixed64
|
67
|
+
|
68
|
+
serialized_string = types.serialize_to_string
|
69
|
+
|
70
|
+
types2 = Test::Types::RepeatedTypes.new
|
71
|
+
types2.parse_from_string serialized_string
|
72
|
+
assert_in_delta(0.01, types2.type1[0], 0.00001)
|
73
|
+
assert_in_delta(0.1, types2.type2[0], 0.00001)
|
74
|
+
assert_equal(1, types2.type3[0])
|
75
|
+
assert_equal(10, types2.type4[0])
|
76
|
+
assert_equal(100, types2.type5[0])
|
77
|
+
assert_equal(1000, types2.type6[0])
|
78
|
+
assert_equal(-1, types2.type7[0])
|
79
|
+
assert_equal(-10, types2.type8[0])
|
80
|
+
assert_equal(10000, types2.type9[0])
|
81
|
+
assert_equal(100000, types2.type10[0])
|
82
|
+
assert(!types2.type11[0])
|
83
|
+
assert_equal('hello all types', types2.type12[0])
|
84
|
+
assert_equal(10938, types2.type13[0].size)
|
85
|
+
assert_equal(image_bin, types2.type13[0])
|
86
|
+
assert_equal(-100, types2.type14[0])
|
87
|
+
assert_equal(-1000, types2.type15[0])
|
88
|
+
|
89
|
+
assert_equal(1.0/0.0, types2.type1[1])
|
90
|
+
assert_equal(-1.0/0.0, types2.type2[1])
|
91
|
+
assert_equal(-1, types2.type3[1])
|
92
|
+
assert_equal(-10, types2.type4[1])
|
93
|
+
assert_equal(100, types2.type5[1])
|
94
|
+
assert_equal(1000, types2.type6[1])
|
95
|
+
assert_equal(-1000, types2.type7[1])
|
96
|
+
assert_equal(-10000, types2.type8[1])
|
97
|
+
assert_equal(10000, types2.type9[1])
|
98
|
+
assert_equal(100000, types2.type10[1])
|
99
|
+
assert types2.type11[1]
|
100
|
+
assert_equal('hello all types', types2.type12[1])
|
101
|
+
assert_equal(10938, types2.type13[1].size)
|
102
|
+
assert_equal(image_bin, types2.type13[1])
|
103
|
+
assert_equal(-2_000_000_000, types2.type14[1])
|
104
|
+
assert_equal(-8_000_000_000_000_000_000, types2.type15[1])
|
105
|
+
end
|
106
|
+
end
|
data/test/test_serialize.rb
CHANGED
@@ -39,4 +39,23 @@ class SerializeTest < Test::Unit::TestCase
|
|
39
39
|
assert_equal 1234, person2.id
|
40
40
|
assert_equal '山田 太郎', person2.name
|
41
41
|
end
|
42
|
+
|
43
|
+
def test_unknown_field
|
44
|
+
person = Tutorial::Person.new
|
45
|
+
person.id = 1234
|
46
|
+
person.name = 'a b c'
|
47
|
+
serialized_string = person.serialize_to_string
|
48
|
+
|
49
|
+
# add invalid field
|
50
|
+
tag = 1000
|
51
|
+
wire_type = Protobuf::WireType::VARINT
|
52
|
+
serialized_string << Protobuf::Field::VarintField.encode((tag << 3) | wire_type)
|
53
|
+
serialized_string << Protobuf::Field::VarintField.encode(100)
|
54
|
+
|
55
|
+
# decode
|
56
|
+
person2 = Tutorial::Person.new
|
57
|
+
person2.parse_from_string serialized_string
|
58
|
+
|
59
|
+
assert_equal(person, person2)
|
60
|
+
end
|
42
61
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: macks-ruby_protobuf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- MATSUYAMA Kengo
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-07-16 00:00:00 -07:00
|
13
13
|
default_executable: rprotoc
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -20,10 +20,11 @@ dependencies:
|
|
20
20
|
requirements:
|
21
21
|
- - ">="
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version:
|
23
|
+
version: 2.3.2
|
24
24
|
version:
|
25
25
|
description: "== DESCRIPTION: Protocol Buffers for Ruby. == FEATURES/PROBLEMS: * Compile .proto file to ruby script * Parse the binary wire format for protocol buffer * Serialize data to the binary wire format for protocol buffer"
|
26
|
-
email:
|
26
|
+
email:
|
27
|
+
- macksx@gmail.com
|
27
28
|
executables:
|
28
29
|
- rprotoc
|
29
30
|
extensions: []
|
@@ -37,6 +38,7 @@ files:
|
|
37
38
|
- Manifest.txt
|
38
39
|
- README.txt
|
39
40
|
- Rakefile
|
41
|
+
- script/mk_parser
|
40
42
|
- bin/rprotoc
|
41
43
|
- examples/addressbook.proto
|
42
44
|
- examples/addressbook.pb.rb
|
@@ -71,7 +73,6 @@ files:
|
|
71
73
|
- lib/protobuf/rpc/handler.rb
|
72
74
|
- lib/protobuf/rpc/server.rb
|
73
75
|
- lib/ruby_protobuf.rb
|
74
|
-
- script/mk_parser
|
75
76
|
- test/addressbook.rb
|
76
77
|
- test/addressbook_base.rb
|
77
78
|
- test/addressbook_ext.rb
|
@@ -108,8 +109,9 @@ files:
|
|
108
109
|
- test/test_types.rb
|
109
110
|
- test/types.rb
|
110
111
|
- test/test_optional_field.rb
|
112
|
+
- test/test_repeated_types.rb
|
111
113
|
has_rdoc: true
|
112
|
-
homepage: http://
|
114
|
+
homepage: http://code.google.com/p/ruby-protobuf
|
113
115
|
post_install_message:
|
114
116
|
rdoc_options:
|
115
117
|
- --main
|
@@ -130,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
130
132
|
version:
|
131
133
|
requirements: []
|
132
134
|
|
133
|
-
rubyforge_project:
|
135
|
+
rubyforge_project: ruby-protobuf
|
134
136
|
rubygems_version: 1.2.0
|
135
137
|
signing_key:
|
136
138
|
specification_version: 2
|
@@ -140,6 +142,7 @@ test_files:
|
|
140
142
|
- test/test_ruby_protobuf.rb
|
141
143
|
- test/test_message.rb
|
142
144
|
- test/test_optional_field.rb
|
145
|
+
- test/test_repeated_types.rb
|
143
146
|
- test/test_extension.rb
|
144
147
|
- test/test_addressbook.rb
|
145
148
|
- test/test_types.rb
|