ruby_protobuf 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -3
- data/Manifest.txt +20 -11
- data/README.txt +1 -1
- data/lib/protobuf/{wire_type.rb → common/wire_type.rb} +0 -0
- data/lib/protobuf/{compiler.rb → compiler/compiler.rb} +13 -13
- data/lib/protobuf/{parser.y → compiler/parser.y} +0 -0
- data/lib/protobuf/descriptor/descriptor.proto +286 -0
- data/lib/protobuf/descriptor/descriptor.rb +54 -0
- data/lib/protobuf/descriptor/descriptor_builder.rb +144 -0
- data/lib/protobuf/descriptor/descriptor_proto.rb +119 -0
- data/lib/protobuf/descriptor/enum_descriptor.rb +33 -0
- data/lib/protobuf/descriptor/field_descriptor.rb +50 -0
- data/lib/protobuf/descriptor/file_descriptor.rb +38 -0
- data/lib/protobuf/{decoder.rb → message/decoder.rb} +1 -1
- data/lib/protobuf/{encoder.rb → message/encoder.rb} +12 -11
- data/lib/protobuf/message/enum.rb +21 -0
- data/lib/protobuf/{extend.rb → message/extend.rb} +1 -1
- data/lib/protobuf/{field.rb → message/field.rb} +82 -53
- data/lib/protobuf/{message.rb → message/message.rb} +12 -3
- data/lib/protobuf/message/service.rb +9 -0
- data/lib/ruby_protobuf.rb +1 -1
- data/test/addressbook.rb +22 -4
- data/test/check_unbuild.rb +30 -0
- data/test/data/data2.bin +3 -0
- data/test/test_addressbook.rb +2 -2
- data/test/test_compiler.rb +9 -9
- data/test/test_descriptor.rb +122 -0
- data/test/test_message.rb +2 -2
- data/test/types.rb +1 -1
- metadata +23 -14
- data/bin/ruby_protobuf +0 -0
- data/lib/protobuf/enum.rb +0 -13
- data/lib/protobuf/service.rb +0 -7
@@ -1,55 +1,43 @@
|
|
1
|
-
require 'protobuf/wire_type'
|
1
|
+
require 'protobuf/common/wire_type'
|
2
|
+
require 'protobuf/descriptor/field_descriptor'
|
2
3
|
|
3
4
|
module Protobuf
|
4
5
|
module Field
|
5
6
|
def self.build(message_class, rule, type, name, tag, opts={})
|
6
|
-
|
7
|
+
field_class =
|
8
|
+
if [:double, :float, :int32, :int64, :uint32, :uint64,
|
9
|
+
:sint32, :sint64, :fixed32, :fixed64, :sfixed32, :sfixed64,
|
10
|
+
:bool, :string, :bytes].include? type
|
11
|
+
eval "Protobuf::Field::#{type.to_s.capitalize}Field"
|
12
|
+
else
|
13
|
+
Protobuf::Field::FieldProxy
|
14
|
+
end
|
15
|
+
field_class.new message_class, rule, type, name, tag, opts
|
7
16
|
end
|
8
17
|
|
9
18
|
class InvalidRuleError < StandardError; end
|
10
19
|
|
11
|
-
class
|
20
|
+
class BaseField
|
12
21
|
class <<self
|
13
|
-
def
|
14
|
-
|
15
|
-
begin
|
16
|
-
field_class = eval "Protobuf::Field::#{type.to_s.capitalize}Field"
|
17
|
-
rescue NameError
|
18
|
-
type = typename_to_class message_class, type
|
19
|
-
field_class =
|
20
|
-
if type.superclass == Protobuf::Enum
|
21
|
-
Protobuf::Field::Enum
|
22
|
-
elsif type.superclass == Protobuf::Message
|
23
|
-
Protobuf::Field::Message
|
24
|
-
else
|
25
|
-
raise $!
|
26
|
-
end
|
27
|
-
end
|
28
|
-
field_class.new message_class, rule, type, name, tag, opts
|
29
|
-
end
|
30
|
-
|
31
|
-
def typename_to_class(message_class, type)
|
32
|
-
modules = message_class.to_s.split('::')
|
33
|
-
while
|
34
|
-
begin
|
35
|
-
type = eval((modules | [type.to_s]).join('::'))
|
36
|
-
break
|
37
|
-
rescue NameError
|
38
|
-
modules.empty? ? raise($!) : modules.pop
|
39
|
-
end
|
40
|
-
end
|
41
|
-
type
|
22
|
+
def descriptor
|
23
|
+
@descriptor ||= Protobuf::Descriptor::FieldDescriptor.new
|
42
24
|
end
|
43
25
|
end
|
44
26
|
|
45
27
|
attr_accessor :message_class, :rule, :type, :name, :tag, :default
|
46
28
|
|
29
|
+
def descriptor
|
30
|
+
@descriptor ||= Protobuf::Descriptor::FieldDescriptor.new self
|
31
|
+
end
|
32
|
+
|
47
33
|
def initialize(message_class, rule, type, name, tag, opts={})
|
48
34
|
@message_class, @rule, @type, @name, @tag, @default =
|
49
35
|
message_class, rule, type, name, tag, opts[:default]
|
50
36
|
@error_message = 'Type invalid'
|
51
37
|
end
|
52
38
|
|
39
|
+
def ready?; true end
|
40
|
+
|
53
41
|
def default_value
|
54
42
|
case rule
|
55
43
|
when :repeated
|
@@ -150,6 +138,41 @@ module Protobuf
|
|
150
138
|
end
|
151
139
|
end
|
152
140
|
|
141
|
+
class FieldProxy
|
142
|
+
def initialize(message_class, rule, type, name, tag, opts={})
|
143
|
+
@message_class, @rule, @type, @name, @tag, @opts =
|
144
|
+
message_class, rule, type, name, tag, opts
|
145
|
+
end
|
146
|
+
|
147
|
+
def ready?; false end
|
148
|
+
|
149
|
+
def setup
|
150
|
+
type = typename_to_class @message_class, @type
|
151
|
+
field_class =
|
152
|
+
if type.superclass == Protobuf::Enum
|
153
|
+
Protobuf::Field::EnumField
|
154
|
+
elsif type.superclass == Protobuf::Message
|
155
|
+
Protobuf::Field::MessageField
|
156
|
+
else
|
157
|
+
raise $!
|
158
|
+
end
|
159
|
+
field_class.new @message_class, @rule, type, @name, @tag, @opts
|
160
|
+
end
|
161
|
+
|
162
|
+
def typename_to_class(message_class, type)
|
163
|
+
modules = message_class.to_s.split('::')
|
164
|
+
while
|
165
|
+
begin
|
166
|
+
type = eval((modules | [type.to_s]).join('::'))
|
167
|
+
break
|
168
|
+
rescue NameError
|
169
|
+
modules.empty? ? raise($!) : modules.pop
|
170
|
+
end
|
171
|
+
end
|
172
|
+
type
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
153
176
|
class FieldArray < Array
|
154
177
|
def initialize(field)
|
155
178
|
@field = field
|
@@ -184,7 +207,7 @@ module Protobuf
|
|
184
207
|
end
|
185
208
|
end
|
186
209
|
|
187
|
-
class StringField <
|
210
|
+
class StringField < BaseField
|
188
211
|
def wire_type
|
189
212
|
Protobuf::WireType::LENGTH_DELIMITED
|
190
213
|
end
|
@@ -201,16 +224,22 @@ module Protobuf
|
|
201
224
|
def set_bytes(method_instance, bytes)
|
202
225
|
method_instance.send("#{name}=", bytes.pack('U*'))
|
203
226
|
end
|
227
|
+
|
228
|
+
def set_array(method_instance, bytes)
|
229
|
+
message = bytes.pack('U*')
|
230
|
+
arr = method_instance.send name
|
231
|
+
arr << message
|
232
|
+
end
|
204
233
|
|
205
234
|
def get_bytes(value)
|
206
235
|
bytes = value.unpack('U*')
|
207
|
-
string_size =
|
236
|
+
string_size = VarintField.get_bytes bytes.size
|
208
237
|
#(string_size + bytes).pack('C*')
|
209
238
|
string_size + bytes.pack('C*')
|
210
239
|
end
|
211
240
|
end
|
212
241
|
|
213
|
-
class BytesField <
|
242
|
+
class BytesField < BaseField
|
214
243
|
def wire_type
|
215
244
|
Protobuf::WireType::VARINT
|
216
245
|
end
|
@@ -224,12 +253,12 @@ module Protobuf
|
|
224
253
|
end
|
225
254
|
|
226
255
|
def get_bytes(value)
|
227
|
-
string_size =
|
256
|
+
string_size = VarintField.get_bytes value.size
|
228
257
|
string_size + value.pack('C*')
|
229
258
|
end
|
230
259
|
end
|
231
260
|
|
232
|
-
class
|
261
|
+
class VarintField < BaseField
|
233
262
|
def wire_type
|
234
263
|
Protobuf::WireType::VARINT
|
235
264
|
end
|
@@ -278,27 +307,27 @@ module Protobuf
|
|
278
307
|
end
|
279
308
|
end
|
280
309
|
|
281
|
-
class Int32Field <
|
310
|
+
class Int32Field < VarintField
|
282
311
|
def self.max; 1.0/0.0 end
|
283
312
|
def self.min; -1.0/0.0 end
|
284
313
|
end
|
285
314
|
|
286
|
-
class Int64Field <
|
315
|
+
class Int64Field < VarintField
|
287
316
|
def self.max; 1.0/0.0 end
|
288
317
|
def self.min; -1.0/0.0 end
|
289
318
|
end
|
290
319
|
|
291
|
-
class Uint32Field <
|
320
|
+
class Uint32Field < VarintField
|
292
321
|
def self.max; 1.0/0.0 end
|
293
322
|
def self.min; 0 end
|
294
323
|
end
|
295
324
|
|
296
|
-
class Uint64Field <
|
325
|
+
class Uint64Field < VarintField
|
297
326
|
def self.max; 1.0/0.0 end
|
298
327
|
def self.min; 0 end
|
299
328
|
end
|
300
329
|
|
301
|
-
class Sint32Field <
|
330
|
+
class Sint32Field < VarintField
|
302
331
|
def self.max; 1.0/0.0 end
|
303
332
|
def self.min; -1.0/0.0 end
|
304
333
|
|
@@ -320,7 +349,7 @@ module Protobuf
|
|
320
349
|
end
|
321
350
|
end
|
322
351
|
|
323
|
-
class Sint64Field <
|
352
|
+
class Sint64Field < VarintField
|
324
353
|
def self.max; 1.0/0.0 end
|
325
354
|
def self.min; -1.0/0.0 end
|
326
355
|
|
@@ -342,7 +371,7 @@ module Protobuf
|
|
342
371
|
end
|
343
372
|
end
|
344
373
|
|
345
|
-
class DoubleField <
|
374
|
+
class DoubleField < VarintField
|
346
375
|
def wire_type
|
347
376
|
Protobuf::WireType::FIXED64
|
348
377
|
end
|
@@ -372,7 +401,7 @@ module Protobuf
|
|
372
401
|
end
|
373
402
|
end
|
374
403
|
|
375
|
-
class FloatField <
|
404
|
+
class FloatField < VarintField
|
376
405
|
def wire_type
|
377
406
|
Protobuf::WireType::FIXED32
|
378
407
|
end
|
@@ -402,7 +431,7 @@ module Protobuf
|
|
402
431
|
end
|
403
432
|
end
|
404
433
|
|
405
|
-
class Fixed32Field <
|
434
|
+
class Fixed32Field < VarintField
|
406
435
|
def wire_type
|
407
436
|
Protobuf::WireType::FIXED32
|
408
437
|
end
|
@@ -424,7 +453,7 @@ module Protobuf
|
|
424
453
|
end
|
425
454
|
end
|
426
455
|
|
427
|
-
class Fixed64Field <
|
456
|
+
class Fixed64Field < VarintField
|
428
457
|
def wire_type
|
429
458
|
Protobuf::WireType::FIXED64
|
430
459
|
end
|
@@ -446,7 +475,7 @@ module Protobuf
|
|
446
475
|
end
|
447
476
|
end
|
448
477
|
|
449
|
-
class Sfinxed32Field <
|
478
|
+
class Sfinxed32Field < VarintField
|
450
479
|
def wire_type
|
451
480
|
Protobuf::WireType::FIXED32
|
452
481
|
end
|
@@ -460,7 +489,7 @@ module Protobuf
|
|
460
489
|
end
|
461
490
|
end
|
462
491
|
|
463
|
-
class Sfixed64Field <
|
492
|
+
class Sfixed64Field < VarintField
|
464
493
|
def wire_type
|
465
494
|
Protobuf::WireType::FIXED64
|
466
495
|
end
|
@@ -474,7 +503,7 @@ module Protobuf
|
|
474
503
|
end
|
475
504
|
end
|
476
505
|
|
477
|
-
class BoolField <
|
506
|
+
class BoolField < VarintField
|
478
507
|
def typed_default_value(default=nil)
|
479
508
|
default or false
|
480
509
|
end
|
@@ -493,7 +522,7 @@ module Protobuf
|
|
493
522
|
end
|
494
523
|
end
|
495
524
|
|
496
|
-
class
|
525
|
+
class MessageField < BaseField
|
497
526
|
def wire_type
|
498
527
|
Protobuf::WireType::LENGTH_DELIMITED
|
499
528
|
end
|
@@ -530,14 +559,14 @@ module Protobuf
|
|
530
559
|
stringio = StringIO.new ''
|
531
560
|
value.serialize_to stringio
|
532
561
|
bytes = stringio.string.unpack 'C*'
|
533
|
-
string_size =
|
562
|
+
string_size = VarintField.get_bytes bytes.size
|
534
563
|
#(string_size + bytes).pack('C*')
|
535
564
|
#bytes + string_size
|
536
565
|
string_size + bytes.pack('C*')
|
537
566
|
end
|
538
567
|
end
|
539
568
|
|
540
|
-
class
|
569
|
+
class EnumField < VarintField
|
541
570
|
def acceptable?(val)
|
542
571
|
raise TypeError unless type.valid_tag? val
|
543
572
|
true
|
@@ -1,7 +1,8 @@
|
|
1
1
|
require 'stringio'
|
2
|
-
require 'protobuf/decoder'
|
3
|
-
require 'protobuf/encoder'
|
4
|
-
require 'protobuf/field'
|
2
|
+
require 'protobuf/message/decoder'
|
3
|
+
require 'protobuf/message/encoder'
|
4
|
+
require 'protobuf/message/field'
|
5
|
+
require 'protobuf/descriptor/descriptor'
|
5
6
|
|
6
7
|
module Protobuf
|
7
8
|
OPTIONS = {}
|
@@ -48,10 +49,18 @@ module Protobuf
|
|
48
49
|
raise TypeError
|
49
50
|
end
|
50
51
|
end
|
52
|
+
|
53
|
+
def descriptor
|
54
|
+
@descriptor ||= Protobuf::Descriptor::Descriptor.new(self)
|
55
|
+
end
|
51
56
|
end
|
52
57
|
|
53
58
|
def initialize
|
54
59
|
fields.each do |tag, field|
|
60
|
+
unless field.ready?
|
61
|
+
field = field.setup
|
62
|
+
self.class.class_eval {@fields[tag] = field}
|
63
|
+
end
|
55
64
|
field.define_accessor self
|
56
65
|
end
|
57
66
|
end
|
data/lib/ruby_protobuf.rb
CHANGED
data/test/addressbook.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require 'protobuf/message'
|
2
|
-
require 'protobuf/enum'
|
3
|
-
require 'protobuf/service'
|
4
|
-
require 'protobuf/extend'
|
1
|
+
require 'protobuf/message/message'
|
2
|
+
require 'protobuf/message/enum'
|
3
|
+
require 'protobuf/message/service'
|
4
|
+
require 'protobuf/message/extend'
|
5
5
|
|
6
6
|
module Tutorial
|
7
7
|
class Person < Protobuf::Message
|
@@ -39,3 +39,21 @@ module Tutorial
|
|
39
39
|
#Protobuf::OPTIONS[:optimize_for] = :SPEED
|
40
40
|
#Protobuf::OPTIONS[:java_package] = :'com.example.foo'
|
41
41
|
end
|
42
|
+
|
43
|
+
=begin
|
44
|
+
tutorial = Object.const_set :Tutorial, Module.new
|
45
|
+
person = tutorial.const_set :Person, Class.new(Protobuf::Message)
|
46
|
+
person.required :string, :name, 1
|
47
|
+
person.required :int32, :id, 2
|
48
|
+
person.optional :string, :email, 3
|
49
|
+
phone_type = person.const_set :PhoneType, Class.new(Protobuf::Enum)
|
50
|
+
phone_type.const_set :MOBILE, 0
|
51
|
+
phone_type.const_set :HOME, 1
|
52
|
+
phone_type.const_set :WORK, 2
|
53
|
+
phone_number = person.const_set :PhoneNumber, Class.new(Protobuf::Message)
|
54
|
+
phone_number.required :string, :number, 1
|
55
|
+
phone_number.optional :PhoneType, :type, 2, {:default => :HOME}
|
56
|
+
person.repeated :PhoneNumber, :phone, 4
|
57
|
+
address_book = tutorial.const_set :AddressBook, Class.new(Protobuf::Message)
|
58
|
+
address_book.repeated :Person, :person, 1
|
59
|
+
=end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
#!/usr/local/bin/ruby
|
2
|
+
|
3
|
+
$:.push "#{File.dirname(__FILE__)}/../lib"
|
4
|
+
|
5
|
+
require 'test/unit'
|
6
|
+
require 'protobuf/descriptor/descriptor_builder'
|
7
|
+
require 'protobuf/descriptor/descriptor_proto'
|
8
|
+
|
9
|
+
class DescriptorTest < Test::Unit::TestCase
|
10
|
+
def test_unbuild
|
11
|
+
tutorial_proto = Google::Protobuf::FileDescriptorProto.new
|
12
|
+
tutorial_proto.parse_from_file 'person.bin'
|
13
|
+
Protobuf::Descriptor::DescriptorBuilder.build tutorial_proto
|
14
|
+
|
15
|
+
assert_nothing_raised {Tutorial::Person}
|
16
|
+
assert_nothing_raised {Tutorial::Person.new}
|
17
|
+
assert_equal ['email', 'id', 'name', 'phone'],
|
18
|
+
Tutorial::Person.fields.map{|tag, field| field.name}.sort
|
19
|
+
|
20
|
+
assert_nothing_raised {Tutorial::Person::PhoneNumber}
|
21
|
+
assert_nothing_raised {Tutorial::Person::PhoneNumber.new}
|
22
|
+
assert_equal ['number', 'type'],
|
23
|
+
Tutorial::Person::PhoneNumber.fields.map{|tag, field| field.name}.sort
|
24
|
+
|
25
|
+
assert_nothing_raised {Tutorial::Person::PhoneType}
|
26
|
+
assert_equal 0, Tutorial::Person::PhoneType::MOBILE
|
27
|
+
assert_equal 1, Tutorial::Person::PhoneType::HOME
|
28
|
+
assert_equal 2, Tutorial::Person::PhoneType::WORK
|
29
|
+
end
|
30
|
+
end
|
data/test/data/data2.bin
ADDED
data/test/test_addressbook.rb
CHANGED
data/test/test_compiler.rb
CHANGED
@@ -1,28 +1,28 @@
|
|
1
1
|
require 'test/unit'
|
2
|
-
require 'protobuf/compiler'
|
2
|
+
require 'protobuf/compiler/compiler'
|
3
3
|
|
4
4
|
class CompilerTest < Test::Unit::TestCase
|
5
5
|
def test_compile
|
6
6
|
assert_equal <<-eos.strip, Protobuf::Compiler.compile('test/addressbook.proto').strip
|
7
|
-
require 'protobuf/message'
|
8
|
-
require 'protobuf/enum'
|
9
|
-
require 'protobuf/service'
|
10
|
-
require 'protobuf/extend'
|
7
|
+
require 'protobuf/message/message'
|
8
|
+
require 'protobuf/message/enum'
|
9
|
+
require 'protobuf/message/service'
|
10
|
+
require 'protobuf/message/extend'
|
11
11
|
|
12
12
|
module Tutorial
|
13
13
|
|
14
|
-
class Person < Protobuf::Message
|
14
|
+
class Person < ::Protobuf::Message
|
15
15
|
required :string, :name, 1
|
16
16
|
required :int32, :id, 2
|
17
17
|
optional :string, :email, 3
|
18
18
|
|
19
|
-
class PhoneType < Protobuf::Enum
|
19
|
+
class PhoneType < ::Protobuf::Enum
|
20
20
|
MOBILE = 0
|
21
21
|
HOME = 1
|
22
22
|
WORK = 2
|
23
23
|
end
|
24
24
|
|
25
|
-
class PhoneNumber < Protobuf::Message
|
25
|
+
class PhoneNumber < ::Protobuf::Message
|
26
26
|
required :string, :number, 1
|
27
27
|
optional :PhoneType, :type, 2, {:default => :HOME}
|
28
28
|
end
|
@@ -30,7 +30,7 @@ module Tutorial
|
|
30
30
|
repeated :PhoneNumber, :phone, 4
|
31
31
|
end
|
32
32
|
|
33
|
-
class AddressBook < Protobuf::Message
|
33
|
+
class AddressBook < ::Protobuf::Message
|
34
34
|
repeated :Person, :person, 1
|
35
35
|
end
|
36
36
|
end
|