ruby_protobuf 0.3.2 → 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 -2
- data/Manifest.txt +74 -0
- data/README.txt +1 -1
- data/Rakefile +5 -5
- data/bin/rprotoc +25 -10
- data/{test/proto → examples}/addressbook.pb.rb +1 -14
- data/examples/addressbook.proto +24 -0
- data/examples/reading_a_message.rb +32 -0
- data/examples/writing_a_message.rb +46 -0
- data/lib/protobuf/compiler/compiler.rb +9 -11
- data/lib/protobuf/compiler/nodes.rb +15 -7
- data/lib/protobuf/compiler/proto.y +56 -36
- data/lib/protobuf/compiler/proto_parser.rb +373 -236
- data/lib/protobuf/compiler/visitors.rb +5 -1
- data/lib/protobuf/descriptor/enum_descriptor.rb +1 -1
- data/lib/protobuf/message/decoder.rb +12 -19
- data/lib/protobuf/message/encoder.rb +5 -2
- data/lib/protobuf/message/enum.rb +1 -1
- data/lib/protobuf/message/field.rb +302 -298
- data/lib/protobuf/message/message.rb +63 -35
- data/lib/ruby_protobuf.rb +1 -1
- data/{bin → script}/mk_parser +0 -0
- data/test/merge.rb +1 -1
- data/test/proto/types.proto +20 -0
- data/test/test_addressbook.rb +1 -0
- data/test/test_compiler.rb +21 -7
- data/test/test_message.rb +19 -0
- data/test/test_optional_field.rb +80 -0
- data/test/test_repeated_types.rb +106 -0
- data/test/test_serialize.rb +34 -0
- data/test/test_standard_message.rb +10 -0
- data/test/test_types.rb +49 -4
- data/test/types.rb +23 -2
- metadata +22 -20
- data/lib/protobuf/compiler/compiler_old.rb +0 -123
- data/test/proto/addressbook.rb +0 -69
- data/test/proto/bool_default.pb.rb +0 -16
- data/test/proto/bool_default.proto +0 -3
- data/test/proto/float_default.proto +0 -2
@@ -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
@@ -1,3 +1,4 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
require 'test/unit'
|
2
3
|
require 'test/addressbook'
|
3
4
|
|
@@ -24,4 +25,37 @@ class SerializeTest < Test::Unit::TestCase
|
|
24
25
|
assert_equal '555-4321', person2.phone[0].number
|
25
26
|
assert_equal Tutorial::Person::PhoneType::HOME, person2.phone[0].type
|
26
27
|
end
|
28
|
+
|
29
|
+
def test_serialize_utf8string
|
30
|
+
# serialize to string
|
31
|
+
person = Tutorial::Person.new
|
32
|
+
person.id = 1234
|
33
|
+
person.name = '山田 太郎' # kanji characters
|
34
|
+
serialized_string = person.serialize_to_string
|
35
|
+
|
36
|
+
# parse the serialized string
|
37
|
+
person2 = Tutorial::Person.new
|
38
|
+
person2.parse_from_string serialized_string
|
39
|
+
assert_equal 1234, person2.id
|
40
|
+
assert_equal '山田 太郎', person2.name
|
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
|
27
61
|
end
|
@@ -11,6 +11,12 @@ class StandardMessageTest < Test::Unit::TestCase
|
|
11
11
|
assert !person.initialized?
|
12
12
|
person.id = 12
|
13
13
|
assert person.initialized?
|
14
|
+
|
15
|
+
# repeated field
|
16
|
+
person.phone << Tutorial::Person::PhoneNumber.new
|
17
|
+
assert !person.initialized?
|
18
|
+
person.phone.last.number = '123-456'
|
19
|
+
assert person.initialized?
|
14
20
|
end
|
15
21
|
|
16
22
|
def test_clear
|
@@ -25,6 +31,10 @@ class StandardMessageTest < Test::Unit::TestCase
|
|
25
31
|
assert_nil person.id
|
26
32
|
assert_equal '', person.email
|
27
33
|
assert_equal 0, person.phone.size
|
34
|
+
|
35
|
+
assert !person.has_field?(:name)
|
36
|
+
assert !person.has_field?(:id)
|
37
|
+
assert !person.has_field?(:email)
|
28
38
|
end
|
29
39
|
|
30
40
|
def test_dup
|
data/test/test_types.rb
CHANGED
@@ -18,6 +18,8 @@ class TypesTest < Test::Unit::TestCase
|
|
18
18
|
types.type12 = 'hello all types'
|
19
19
|
image_bin = File.open('test/data/unk.png', 'r+b'){|f| f.read}
|
20
20
|
types.type13 = image_bin
|
21
|
+
types.type14 = -100
|
22
|
+
types.type15 = -1000
|
21
23
|
|
22
24
|
serialized_string = types.serialize_to_string
|
23
25
|
|
@@ -37,6 +39,49 @@ class TypesTest < Test::Unit::TestCase
|
|
37
39
|
assert_equal 'hello all types', types2.type12
|
38
40
|
assert_equal 10938, types2.type13.size
|
39
41
|
assert_equal image_bin, types2.type13
|
42
|
+
assert_equal(-100, types2.type14)
|
43
|
+
assert_equal(-1000, types2.type15)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_serialize2
|
47
|
+
types = Test::Types::TestTypes.new
|
48
|
+
types.type1 = 1.0/0 # double (Inf)
|
49
|
+
types.type2 = -1.0/0 # float (-Inf)
|
50
|
+
types.type3 = -1 # int32
|
51
|
+
types.type4 = -10 # int64
|
52
|
+
types.type5 = 100 # uint32
|
53
|
+
types.type6 = 1000 # uint64
|
54
|
+
types.type7 = -1000 # sint32
|
55
|
+
types.type8 = -10000 # sint64
|
56
|
+
types.type9 = 10000 # fixed32
|
57
|
+
types.type10 = 100000 # fixed64
|
58
|
+
types.type11 = true
|
59
|
+
types.type12 = 'hello all types'
|
60
|
+
image_bin = File.open('test/data/unk.png', 'r+b'){|f| f.read}
|
61
|
+
types.type13 = image_bin
|
62
|
+
types.type14 = -2_000_000_000 # sfixed32
|
63
|
+
types.type15 = -8_000_000_000_000_000_000 # sfixed64
|
64
|
+
|
65
|
+
serialized_string = types.serialize_to_string
|
66
|
+
|
67
|
+
types2 = Test::Types::TestTypes.new
|
68
|
+
types2.parse_from_string serialized_string
|
69
|
+
assert_equal(1.0/0.0, types2.type1)
|
70
|
+
assert_equal(-1.0/0.0, types2.type2)
|
71
|
+
assert_equal(-1, types2.type3)
|
72
|
+
assert_equal(-10, types2.type4)
|
73
|
+
assert_equal(100, types2.type5)
|
74
|
+
assert_equal(1000, types2.type6)
|
75
|
+
assert_equal(-1000, types2.type7)
|
76
|
+
assert_equal(-10000, types2.type8)
|
77
|
+
assert_equal(10000, types2.type9)
|
78
|
+
assert_equal(100000, types2.type10)
|
79
|
+
assert types2.type11
|
80
|
+
assert_equal('hello all types', types2.type12)
|
81
|
+
assert_equal(10938, types2.type13.size)
|
82
|
+
assert_equal(image_bin, types2.type13)
|
83
|
+
assert_equal(-2_000_000_000, types2.type14)
|
84
|
+
assert_equal(-8_000_000_000_000_000_000, types2.type15)
|
40
85
|
end
|
41
86
|
|
42
87
|
def test_parse
|
@@ -64,9 +109,7 @@ class TypesTest < Test::Unit::TestCase
|
|
64
109
|
assert_nothing_raised do types.type1 = 1.0 end
|
65
110
|
assert_raise TypeError do types.type1 = '' end
|
66
111
|
assert_nothing_raised do types.type1 = Protobuf::Field::DoubleField.max end
|
67
|
-
assert_raise RangeError do types.type1 = Protobuf::Field::DoubleField.max * 2 end
|
68
112
|
assert_nothing_raised do types.type1 = Protobuf::Field::DoubleField.min end
|
69
|
-
assert_raise RangeError do types.type1 = Protobuf::Field::DoubleField.min * 2 end
|
70
113
|
end
|
71
114
|
|
72
115
|
def test_float
|
@@ -76,9 +119,7 @@ class TypesTest < Test::Unit::TestCase
|
|
76
119
|
assert_nothing_raised do types.type2 = 1.0 end
|
77
120
|
assert_raise TypeError do types.type2 = '' end
|
78
121
|
assert_nothing_raised do types.type2 = Protobuf::Field::FloatField.max end
|
79
|
-
assert_raise RangeError do types.type2 = Protobuf::Field::FloatField.max * 2 end
|
80
122
|
assert_nothing_raised do types.type2 = Protobuf::Field::FloatField.min end
|
81
|
-
assert_raise RangeError do types.type2 = Protobuf::Field::FloatField.min * 2 end
|
82
123
|
end
|
83
124
|
|
84
125
|
def test_int32
|
@@ -178,4 +219,8 @@ class TypesTest < Test::Unit::TestCase
|
|
178
219
|
assert_raise TypeError do types.type13 = true end
|
179
220
|
assert_raise TypeError do types.type13 = [] end
|
180
221
|
end
|
222
|
+
|
223
|
+
def test_varint_getbytes
|
224
|
+
assert_equal "\xac\x02", Protobuf::Field::VarintField.encode(300)
|
225
|
+
end
|
181
226
|
end
|
data/test/types.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
require 'protobuf/message/message'
|
2
|
+
require 'protobuf/message/enum'
|
2
3
|
|
3
4
|
module Test
|
4
5
|
module Types
|
5
|
-
|
6
|
-
|
6
|
+
class TestTypes < ::Protobuf::Message
|
7
|
+
defined_in __FILE__
|
7
8
|
required :double, :type1, 1
|
8
9
|
required :float, :type2, 2
|
9
10
|
required :int32, :type3, 3
|
@@ -17,6 +18,26 @@ module Test
|
|
17
18
|
required :bool, :type11, 11
|
18
19
|
required :string, :type12, 12
|
19
20
|
required :bytes, :type13, 13
|
21
|
+
required :sfixed32, :type14, 14
|
22
|
+
required :sfixed64, :type15, 15
|
23
|
+
end
|
24
|
+
class RepeatedTypes < ::Protobuf::Message
|
25
|
+
defined_in __FILE__
|
26
|
+
repeated :double, :type1, 1
|
27
|
+
repeated :float, :type2, 2
|
28
|
+
repeated :int32, :type3, 3
|
29
|
+
repeated :int64, :type4, 4
|
30
|
+
repeated :uint32, :type5, 5
|
31
|
+
repeated :uint64, :type6, 6
|
32
|
+
repeated :sint32, :type7, 7
|
33
|
+
repeated :sint64, :type8, 8
|
34
|
+
repeated :fixed32, :type9, 9
|
35
|
+
repeated :fixed64, :type10, 10
|
36
|
+
repeated :bool, :type11, 11
|
37
|
+
repeated :string, :type12, 12
|
38
|
+
repeated :bytes, :type13, 13
|
39
|
+
repeated :sfixed32, :type14, 14
|
40
|
+
repeated :sfixed64, :type15, 15
|
20
41
|
end
|
21
42
|
end
|
22
43
|
end
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: 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
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2009-07-10 00:00:00 +09:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -20,27 +20,32 @@ dependencies:
|
|
20
20
|
requirements:
|
21
21
|
- - ">="
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version:
|
23
|
+
version: 2.2.0
|
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
|
-
- mk_parser
|
29
29
|
- rprotoc
|
30
30
|
extensions: []
|
31
31
|
|
32
32
|
extra_rdoc_files:
|
33
33
|
- History.txt
|
34
|
+
- Manifest.txt
|
34
35
|
- README.txt
|
35
36
|
files:
|
36
37
|
- History.txt
|
38
|
+
- Manifest.txt
|
37
39
|
- README.txt
|
38
40
|
- Rakefile
|
39
|
-
-
|
41
|
+
- script/mk_parser
|
40
42
|
- bin/rprotoc
|
43
|
+
- examples/addressbook.proto
|
44
|
+
- examples/addressbook.pb.rb
|
45
|
+
- examples/reading_a_message.rb
|
46
|
+
- examples/writing_a_message.rb
|
41
47
|
- lib/protobuf/common/wire_type.rb
|
42
48
|
- lib/protobuf/compiler/compiler.rb
|
43
|
-
- lib/protobuf/compiler/compiler_old.rb
|
44
49
|
- lib/protobuf/compiler/nodes.rb
|
45
50
|
- lib/protobuf/compiler/proto.y
|
46
51
|
- lib/protobuf/compiler/proto2.ebnf
|
@@ -82,17 +87,12 @@ files:
|
|
82
87
|
- test/ext_range.rb
|
83
88
|
- test/merge.rb
|
84
89
|
- test/nested.rb
|
85
|
-
- test/proto/addressbook.pb.rb
|
86
90
|
- test/proto/addressbook.proto
|
87
|
-
- test/proto/addressbook.rb
|
88
91
|
- test/proto/addressbook_base.proto
|
89
92
|
- test/proto/addressbook_ext.proto
|
90
|
-
- test/proto/bool_default.pb.rb
|
91
|
-
- test/proto/bool_default.proto
|
92
93
|
- test/proto/collision.proto
|
93
94
|
- test/proto/ext_collision.proto
|
94
95
|
- test/proto/ext_range.proto
|
95
|
-
- test/proto/float_default.proto
|
96
96
|
- test/proto/merge.proto
|
97
97
|
- test/proto/nested.proto
|
98
98
|
- test/proto/rpc.proto
|
@@ -109,7 +109,7 @@ files:
|
|
109
109
|
- test/test_types.rb
|
110
110
|
- test/types.rb
|
111
111
|
has_rdoc: true
|
112
|
-
homepage:
|
112
|
+
homepage: http://code.google.com/p/ruby-protobuf
|
113
113
|
post_install_message:
|
114
114
|
rdoc_options:
|
115
115
|
- --main
|
@@ -131,18 +131,20 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
131
131
|
requirements: []
|
132
132
|
|
133
133
|
rubyforge_project: ruby-protobuf
|
134
|
-
rubygems_version: 1.
|
134
|
+
rubygems_version: 1.3.1
|
135
135
|
signing_key:
|
136
136
|
specification_version: 2
|
137
137
|
summary: Protocol Buffers for Ruby
|
138
138
|
test_files:
|
139
139
|
- test/test_descriptor.rb
|
140
|
-
- test/
|
141
|
-
- test/
|
140
|
+
- test/test_optional_field.rb
|
141
|
+
- test/test_repeated_types.rb
|
142
|
+
- test/test_types.rb
|
142
143
|
- test/test_extension.rb
|
143
|
-
- test/test_parse.rb
|
144
144
|
- test/test_serialize.rb
|
145
|
-
- test/
|
145
|
+
- test/test_standard_message.rb
|
146
146
|
- test/test_compiler.rb
|
147
|
+
- test/test_message.rb
|
148
|
+
- test/test_parse.rb
|
147
149
|
- test/test_addressbook.rb
|
148
|
-
- test/
|
150
|
+
- test/test_ruby_protobuf.rb
|
@@ -1,123 +0,0 @@
|
|
1
|
-
# This is a quite temporary implementation.
|
2
|
-
# I'll create a compiler class using Racc.
|
3
|
-
|
4
|
-
require 'fileutils'
|
5
|
-
|
6
|
-
module Protobuf
|
7
|
-
class Compiler
|
8
|
-
INDENT_UNIT = ' '
|
9
|
-
|
10
|
-
def self.compile(proto_file, proto_dir='.', out_dir='.', file_create=true)
|
11
|
-
self.new.compile proto_file, proto_dir, out_dir, file_create
|
12
|
-
end
|
13
|
-
|
14
|
-
def initialize
|
15
|
-
@indent_level = 0
|
16
|
-
@ret = <<-eos
|
17
|
-
require 'protobuf/message/message'
|
18
|
-
require 'protobuf/message/enum'
|
19
|
-
require 'protobuf/message/service'
|
20
|
-
require 'protobuf/message/extend'
|
21
|
-
|
22
|
-
eos
|
23
|
-
end
|
24
|
-
|
25
|
-
def indent
|
26
|
-
INDENT_UNIT * @indent_level
|
27
|
-
end
|
28
|
-
|
29
|
-
def puts_with_indent(string)
|
30
|
-
#puts "#{indent}#{string}"
|
31
|
-
@ret += "#{indent}#{string}\n"
|
32
|
-
end
|
33
|
-
alias putswi puts_with_indent
|
34
|
-
|
35
|
-
def compile(proto_file, proto_dir='.', out_dir='.', file_create=true)
|
36
|
-
rb_file = "#{out_dir}/#{proto_file.sub(/\.proto$/, '.rb')}"
|
37
|
-
proto_path = validate_existence proto_file, proto_dir
|
38
|
-
File.open proto_path, 'r' do |file|
|
39
|
-
file.each_line do |line|
|
40
|
-
line.sub!(/^(.*)\/\/.*/, '\1')
|
41
|
-
line.strip!
|
42
|
-
case line
|
43
|
-
when /^package\s+(\w+(\.\w+)?)\s*;$/
|
44
|
-
$1.split('.').each do |path|
|
45
|
-
putswi "module #{path.capitalize}"
|
46
|
-
@indent_level += 1
|
47
|
-
end
|
48
|
-
when /^import\s+"((?:[^"\\]+|\\.)*)"\s*;$/
|
49
|
-
putswi "require '#{required_message_from_proto $1, proto_dir, out_dir}'"
|
50
|
-
when /^message\s+(\w+)\s*\{$/
|
51
|
-
putswi "class #{$1} < ::Protobuf::Message"
|
52
|
-
@extension = false
|
53
|
-
@indent_level += 1
|
54
|
-
when /^(required|optional|repeated)\s+(\w+(\.\w+)?)\s+(\w+)\s*=\s*(\d+)\s*(\[\s*default\s*=\s*(\w+)\s*\])?\s*;$/
|
55
|
-
rule, type, name, tag, default = $1, $2, $4, $5, $7
|
56
|
-
if default
|
57
|
-
default = default =~ /\d+(\.\d+)/ \
|
58
|
-
? ", {:default => #{default}}" \
|
59
|
-
: ", {:default => :#{default}}"
|
60
|
-
end
|
61
|
-
extension = @extension ? ', :extension => true' : ''
|
62
|
-
putswi "#{rule} :#{type}, :#{name}, #{tag}#{default}#{extension}"
|
63
|
-
when /^enum\s+(\w+)\s*\{$/
|
64
|
-
putswi "class #{$1} < ::Protobuf::Enum"
|
65
|
-
@indent_level += 1
|
66
|
-
when /^(\w+)\s*=\s*(\w+)\s*;$/
|
67
|
-
putswi "#{$1} = #{$2}"
|
68
|
-
when /^extensions\s+(\w+)\s+to\s+(\w+)\s*;/
|
69
|
-
low, high = $1, $2
|
70
|
-
low = '::Protobuf::Extend.MIN' if low == 'min'
|
71
|
-
high = '::Protobuf::Extend.MAX' if high == 'max'
|
72
|
-
putswi "extensions #{low}..#{high}"
|
73
|
-
when /^extend\s+(\w+)\s*\{/
|
74
|
-
putswi "class #{$1} < ::Protobuf::Message"
|
75
|
-
@extension = true
|
76
|
-
@indent_level += 1
|
77
|
-
when /^service\s+(\w+)\s*\{/
|
78
|
-
putswi "class #{$1} < ::Protobuf::Service"
|
79
|
-
@indent_level += 1
|
80
|
-
when /^rpc\s+(\w+)\s+\(\s*(\w+)\s*\)\s+returns\s+\(\s*(\w+)\s*\)\s*;/
|
81
|
-
putswi "rpc :#{$1} => :#{$2}, :#{$3} => :#{$4}"
|
82
|
-
when /^option\s+(\w+)\s*=\s*(.+)\s*;/
|
83
|
-
putswi "::Protobuf::OPTIONS[:#{$1}] = :#{$2}"
|
84
|
-
when /^\}\s*;?$/
|
85
|
-
@indent_level -= 1
|
86
|
-
putswi "end"
|
87
|
-
when ''
|
88
|
-
putswi ''
|
89
|
-
end
|
90
|
-
end
|
91
|
-
while 0 < @indent_level
|
92
|
-
@indent_level -= 1
|
93
|
-
putswi "end"
|
94
|
-
end
|
95
|
-
end
|
96
|
-
if file_create
|
97
|
-
puts "#{rb_file} writing..."
|
98
|
-
FileUtils.mkpath File.dirname(rb_file)
|
99
|
-
File.open(rb_file, 'w') {|f| f.write @ret}
|
100
|
-
end
|
101
|
-
@ret
|
102
|
-
end
|
103
|
-
|
104
|
-
def validate_existence(path, base_dir)
|
105
|
-
if File.exist? path
|
106
|
-
elsif File.exist?(path = "#{base_dir or '.'}/#{path}")
|
107
|
-
else
|
108
|
-
raise ArgumentError.new("File does not exist: #{path}")
|
109
|
-
end
|
110
|
-
path
|
111
|
-
end
|
112
|
-
|
113
|
-
def required_message_from_proto(proto_file, proto_dir, out_dir)
|
114
|
-
rb_path = proto_file.sub(/\.proto$/, '.rb')
|
115
|
-
proto_dir ||= '.'
|
116
|
-
out_dir ||= '.'
|
117
|
-
unless File.exist?("#{out_dir}/#{rb_path}")
|
118
|
-
Compiler.compile proto_file, proto_dir, out_dir
|
119
|
-
end
|
120
|
-
rb_path
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|