ruby-protocol-buffers 1.5.1 → 1.6.0
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.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/.travis.yml +4 -8
- data/CHANGELOG.md +80 -0
- data/Changelog.md +73 -9
- data/README.md +31 -32
- data/Rakefile +8 -0
- data/bin/protoc-gen-ruby +2 -1
- data/lib/protocol_buffers.rb +14 -3
- data/lib/protocol_buffers/runtime/encoder.rb +1 -1
- data/lib/protocol_buffers/runtime/field.rb +44 -0
- data/lib/protocol_buffers/runtime/message.rb +63 -0
- data/lib/protocol_buffers/runtime/text_formatter.rb +116 -0
- data/lib/protocol_buffers/runtime/text_parser.ry +136 -0
- data/lib/protocol_buffers/runtime/text_scanner.rb +94 -0
- data/lib/protocol_buffers/version.rb +1 -1
- data/ruby-protocol-buffers.gemspec +2 -1
- data/spec/message_spec.rb +22 -0
- data/spec/negative_int32_spec.rb +10 -10
- data/spec/nil_bugs_spec.rb +1 -1
- data/spec/runtime_spec.rb +2 -2
- data/spec/text_format_spec.rb +679 -0
- data/spec/unicode_string_spec.rb +2 -2
- metadata +46 -48
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b8b90d97ac7aaee3386bb7610307da62ca99b7e4
|
4
|
+
data.tar.gz: b79d0e1cf4b047363dffef09fdc2560cb91d5a86
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 330587548c9be9afb403acd7ad1ea86b1d5689056180808ad2b10d3ced970e261c0cfc7c91174fd65e58df238c7c054b99b86695597f57d81260f54c59777e78
|
7
|
+
data.tar.gz: 62361ed0fdd8f8926243610d23aef2618f8cc97196076b5fa6e66f306b26244daab81bc86d7ff893c8762cac1cb8795e889277654d5b576c89bc79fa0d4e2bea
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -4,20 +4,16 @@ gemfile:
|
|
4
4
|
# i can't find a way to use the same Gemfile and just specify --without varint
|
5
5
|
- Gemfile.no_varint
|
6
6
|
rvm:
|
7
|
-
- "1.8.7"
|
8
7
|
- "1.9.3"
|
9
8
|
- "2.0.0"
|
10
|
-
-
|
9
|
+
- "2.1.0"
|
11
10
|
- jruby-19mode # JRuby in 1.9 mode
|
12
|
-
- rbx-
|
13
|
-
- rbx-19mode
|
11
|
+
- rbx-2.1.1
|
14
12
|
matrix:
|
15
13
|
exclude:
|
16
14
|
# can't use the varint extension in jruby runs
|
17
|
-
- rvm: jruby-18mode
|
18
|
-
gemfile: Gemfile
|
19
15
|
- rvm: jruby-19mode
|
20
16
|
gemfile: Gemfile
|
21
17
|
allow_failures:
|
22
|
-
|
23
|
-
|
18
|
+
- rvm: rbx-2.1.1
|
19
|
+
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# Change Log
|
2
|
+
All notable changes to this project will be documented in this file.
|
3
|
+
|
4
|
+
## 1.6.0 - 2014-09-22
|
5
|
+
### Added
|
6
|
+
- More metaprogramming support via `get_expected_field`, `get_optional_field`.
|
7
|
+
- Ruby 2.1 support.
|
8
|
+
- Protocol Buffers text format support.
|
9
|
+
|
10
|
+
### Changed
|
11
|
+
- Fixed Windows line ending bug in encoder.
|
12
|
+
- README syntax highlighting.
|
13
|
+
|
14
|
+
### Removed
|
15
|
+
- Dropped Ruby 1.8.7 support.
|
16
|
+
|
17
|
+
## 1.5.1 - 2013-10-28
|
18
|
+
### Added
|
19
|
+
- Better value semantics for `Message`, including `==`, `eql?`, and `hash`.
|
20
|
+
|
21
|
+
## 1.5.0 - 2013-09-19
|
22
|
+
### Added
|
23
|
+
- `Message#to_hash`
|
24
|
+
- Service and RPC classes, as a common interface for other libraries to build on.
|
25
|
+
|
26
|
+
### Changed
|
27
|
+
- Fix for repeated fields being set to itself.
|
28
|
+
|
29
|
+
## 1.4.1 - 2013-07-19
|
30
|
+
### Changed
|
31
|
+
- Fix for frozen strings as input.
|
32
|
+
|
33
|
+
## 1.4.0 - 2013-06-18
|
34
|
+
### Added
|
35
|
+
- Support protobuf groups.
|
36
|
+
- Add new executable protoc-gen-ruby, for the new protoc plugin support.
|
37
|
+
|
38
|
+
## 1.3.3 - 2013-03-22
|
39
|
+
### Added
|
40
|
+
- Validate UTF8 while encoding.
|
41
|
+
- Support packed fields.
|
42
|
+
|
43
|
+
## 1.3.1 - 2013-02-17
|
44
|
+
### Added
|
45
|
+
- Move deactivated varint c extension to a separate gem.
|
46
|
+
- Use the varint gem, if it has been loaded.
|
47
|
+
|
48
|
+
## 1.3.0 - 2013-01-17
|
49
|
+
### Added
|
50
|
+
- Improved documentation and README.
|
51
|
+
|
52
|
+
## 1.2.3.beta2 - 2012-12-18
|
53
|
+
### Added
|
54
|
+
- Better error reporting while parsing invalid messages.
|
55
|
+
- UTF-8 validation of string fields in ruby 1.9+.
|
56
|
+
|
57
|
+
### Changed
|
58
|
+
- Fixed 32-bit negative numbers.
|
59
|
+
- Fix encoding of strings in parsed messages.
|
60
|
+
- Fix for recursive message types.
|
61
|
+
|
62
|
+
## 1.2.1 - 2011-10-01
|
63
|
+
### Added
|
64
|
+
- Translate package names to submodules.
|
65
|
+
- Mirror packages as directory structure.
|
66
|
+
|
67
|
+
## 1.1.0 - 2011-09-19
|
68
|
+
### Removed
|
69
|
+
- Remove package unloading support.
|
70
|
+
|
71
|
+
## 1.0.1 - 2011-03-20
|
72
|
+
### Added
|
73
|
+
- Treat unknown enum values as unknown fields.
|
74
|
+
|
75
|
+
## 1.0.0
|
76
|
+
|
77
|
+
## 0.8.5
|
78
|
+
### Added
|
79
|
+
- Add support for Microsoft Windows (tested on Windows 7)
|
80
|
+
- Fix StringIO encoding issues on Ruby 1.9.2 by explicitly setting binary encoding.
|
data/Changelog.md
CHANGED
@@ -1,16 +1,80 @@
|
|
1
|
-
#
|
1
|
+
# Change Log
|
2
|
+
All notable changes to this project will be documented in this file.
|
2
3
|
|
3
|
-
|
4
|
+
## 1.6.0 - 2014-09-22
|
5
|
+
### Added
|
6
|
+
- More metaprogramming support via `get_expected_field`, `get_optional_field`.
|
7
|
+
- Ruby 2.1 support.
|
8
|
+
- Protocol Buffers text format support.
|
4
9
|
|
5
|
-
|
10
|
+
### Changed
|
11
|
+
- Fixed Windows line ending bug in encoder.
|
12
|
+
- README syntax highlighting.
|
6
13
|
|
7
|
-
|
14
|
+
### Removed
|
15
|
+
- Dropped Ruby 1.8.7 support.
|
8
16
|
|
9
|
-
|
17
|
+
## 1.5.1 - 2013-10-28
|
18
|
+
### Added
|
19
|
+
- Better value semantics for `Message`, including `==`, `eql?`, and `hash`.
|
10
20
|
|
11
|
-
|
12
|
-
|
21
|
+
## 1.5.0 - 2013-09-19
|
22
|
+
### Added
|
23
|
+
- `Message#to_hash`
|
24
|
+
- Service and RPC classes, as a common interface for other libraries to build on.
|
13
25
|
|
14
|
-
|
26
|
+
### Changed
|
27
|
+
- Fix for repeated fields being set to itself.
|
15
28
|
|
16
|
-
|
29
|
+
## 1.4.1 - 2013-07-19
|
30
|
+
### Changed
|
31
|
+
- Fix for frozen strings as input.
|
32
|
+
|
33
|
+
## 1.4.0 - 2013-06-18
|
34
|
+
### Added
|
35
|
+
- Support protobuf groups.
|
36
|
+
- Add new executable protoc-gen-ruby, for the new protoc plugin support.
|
37
|
+
|
38
|
+
## 1.3.3 - 2013-03-22
|
39
|
+
### Added
|
40
|
+
- Validate UTF8 while encoding.
|
41
|
+
- Support packed fields.
|
42
|
+
|
43
|
+
## 1.3.1 - 2013-02-17
|
44
|
+
### Added
|
45
|
+
- Move deactivated varint c extension to a separate gem.
|
46
|
+
- Use the varint gem, if it has been loaded.
|
47
|
+
|
48
|
+
## 1.3.0 - 2013-01-17
|
49
|
+
### Added
|
50
|
+
- Improved documentation and README.
|
51
|
+
|
52
|
+
## 1.2.3.beta2 - 2012-12-18
|
53
|
+
### Added
|
54
|
+
- Better error reporting while parsing invalid messages.
|
55
|
+
- UTF-8 validation of string fields in ruby 1.9+.
|
56
|
+
|
57
|
+
### Changed
|
58
|
+
- Fixed 32-bit negative numbers.
|
59
|
+
- Fix encoding of strings in parsed messages.
|
60
|
+
- Fix for recursive message types.
|
61
|
+
|
62
|
+
## 1.2.1 - 2011-10-01
|
63
|
+
### Added
|
64
|
+
- Translate package names to submodules.
|
65
|
+
- Mirror packages as directory structure.
|
66
|
+
|
67
|
+
## 1.1.0 - 2011-09-19
|
68
|
+
### Removed
|
69
|
+
- Remove package unloading support.
|
70
|
+
|
71
|
+
## 1.0.1 - 2011-03-20
|
72
|
+
### Added
|
73
|
+
- Treat unknown enum values as unknown fields.
|
74
|
+
|
75
|
+
## 1.0.0
|
76
|
+
|
77
|
+
## 0.8.5
|
78
|
+
### Added
|
79
|
+
- Add support for Microsoft Windows (tested on Windows 7)
|
80
|
+
- Fix StringIO encoding issues on Ruby 1.9.2 by explicitly setting binary encoding.
|
data/README.md
CHANGED
@@ -3,36 +3,36 @@
|
|
3
3
|
[](https://travis-ci.org/codekitchen/ruby-protocol-buffers)
|
4
4
|
|
5
5
|
Protocol Buffers are a way of encoding structured data in an efficient yet
|
6
|
-
extensible format. Google uses Protocol Buffers for almost all of its
|
7
|
-
RPC protocols and file formats.
|
6
|
+
extensible format. Google uses Protocol Buffers for almost all of its
|
7
|
+
internal RPC protocols and file formats.
|
8
8
|
|
9
9
|
This library has two components: a compiler to turn `.proto` definitions
|
10
10
|
into Ruby modules (extension `.pb.rb`), and a runtime to use protocol
|
11
11
|
buffers defined by these modules.
|
12
12
|
|
13
|
-
The compiler relies on Google's C++ based compiler (`protoc`) for much
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
Directly" below.
|
13
|
+
The compiler relies on Google's C++ based compiler (`protoc`) for much of
|
14
|
+
the heavy lifting -- this has huge advantages in ensuring compatibility and
|
15
|
+
correctness. If you don't need cross-language interoperability you can
|
16
|
+
create Message classes directly in ruby, in which case `protoc` is not
|
17
|
+
needed. See "Writing Message Classes Directly" below.
|
19
18
|
|
20
19
|
This library is heavily optimized for encoding and decoding speed.
|
21
20
|
|
22
21
|
Because this is a tool for generating code, the RDoc documentation is a bit
|
23
|
-
unusual. See the text in the ProtocolBuffers::Message class for details on
|
24
|
-
code is generated.
|
22
|
+
unusual. See the text in the ProtocolBuffers::Message class for details on
|
23
|
+
what code is generated.
|
25
24
|
|
26
25
|
## Installation
|
27
26
|
|
28
27
|
$ gem install ruby-protocol-buffers
|
29
28
|
|
30
|
-
If you want to compile .proto files to ruby, you'll need `protoc` version >=
|
31
|
-
installed in the environment where
|
32
|
-
You do not need `protoc` installed to use the
|
29
|
+
If you want to compile .proto files to ruby, you'll need `protoc` version >=
|
30
|
+
2.2 (the Google Protocol Buffer compiler) installed in the environment where
|
31
|
+
you will be compiling them. You do not need `protoc` installed to use the
|
32
|
+
generated `.pb.rb` files.
|
33
33
|
|
34
|
-
For greater performance, consider installing the varint gem as well.
|
35
|
-
|
34
|
+
For greater performance, consider installing the varint gem as well. This
|
35
|
+
optional gem builds a small C extension to make parsing protocol buffers
|
36
36
|
faster. If your application uses a Gemfile, add varint to the Gemfile
|
37
37
|
alongside ruby-protocol-buffers.
|
38
38
|
|
@@ -40,11 +40,10 @@ alongside ruby-protocol-buffers.
|
|
40
40
|
|
41
41
|
Given the file test.proto:
|
42
42
|
|
43
|
-
```
|
43
|
+
```protobuf
|
44
44
|
package Test;
|
45
45
|
|
46
|
-
message MyMessage
|
47
|
-
{
|
46
|
+
message MyMessage {
|
48
47
|
optional string myField = 1;
|
49
48
|
}
|
50
49
|
```
|
@@ -67,11 +66,11 @@ Test::MyMessage.parse(encoded) == msg # true
|
|
67
66
|
|
68
67
|
## Writing Message Classes Directly
|
69
68
|
|
70
|
-
Protocol Buffer definitions are often shared between applications
|
71
|
-
|
72
|
-
|
69
|
+
Protocol Buffer definitions are often shared between applications written in
|
70
|
+
different programming languages, and so are normally defined in .proto files
|
71
|
+
and translated to ruby using the `ruby-protoc` binary.
|
73
72
|
|
74
|
-
However, it's quite simple to write ProtocolBuffers::Message classes
|
73
|
+
However, it's quite simple to write `ProtocolBuffers::Message` classes
|
75
74
|
directly when a .proto file isn't needed.
|
76
75
|
|
77
76
|
```ruby
|
@@ -97,19 +96,17 @@ class Group < ProtocolBuffers::Message
|
|
97
96
|
end
|
98
97
|
```
|
99
98
|
|
100
|
-
This code is essentially equivalent to the code `ruby-protoc` will
|
101
|
-
|
99
|
+
This code is essentially equivalent to the code `ruby-protoc` will generate
|
100
|
+
if given this .proto file:
|
102
101
|
|
103
|
-
```
|
104
|
-
message User
|
105
|
-
{
|
102
|
+
```protobuf
|
103
|
+
message User {
|
106
104
|
required string name = 1;
|
107
105
|
required string email = 2;
|
108
106
|
optional int32 logins = 3;
|
109
107
|
}
|
110
108
|
|
111
|
-
message Group
|
112
|
-
{
|
109
|
+
message Group {
|
113
110
|
repeated User users = 1;
|
114
111
|
repeated Group subgroups = 2;
|
115
112
|
|
@@ -123,8 +120,8 @@ message Group
|
|
123
120
|
|
124
121
|
```
|
125
122
|
|
126
|
-
Using a hand-written Message subclass is the same as using a Message
|
127
|
-
|
123
|
+
Using a hand-written Message subclass is the same as using a Message class
|
124
|
+
generated by `ruby-protoc`.
|
128
125
|
|
129
126
|
```ruby
|
130
127
|
group = Group.new(:group_type => Group::GroupType::Play)
|
@@ -145,6 +142,7 @@ end
|
|
145
142
|
* passing on unknown fields when re-serializing a message
|
146
143
|
* groups
|
147
144
|
* RPC stubbing
|
145
|
+
* formatting to and parsing from text format
|
148
146
|
|
149
147
|
### Unsupported Features
|
150
148
|
|
@@ -154,7 +152,8 @@ end
|
|
154
152
|
|
155
153
|
### Probably Never to be Supported
|
156
154
|
|
157
|
-
* the unsupported options (java_*, optimize_for, message_set_wire_format,
|
155
|
+
* the unsupported options (java_*, optimize_for, message_set_wire_format,
|
156
|
+
deprecated)
|
158
157
|
|
159
158
|
## Authors
|
160
159
|
|
data/Rakefile
CHANGED
@@ -3,3 +3,11 @@ require "bundler/gem_tasks"
|
|
3
3
|
Dir['tasks/**/*.rake'].each { |t| load t }
|
4
4
|
|
5
5
|
task :default => [:spec]
|
6
|
+
|
7
|
+
file 'lib/protocol_buffers/runtime/text_parser.rb' => 'lib/protocol_buffers/runtime/text_parser.ry' do |t|
|
8
|
+
sh 'racc', '-o', t.name, *t.prerequisites
|
9
|
+
end
|
10
|
+
|
11
|
+
task :text_parser => 'lib/protocol_buffers/runtime/text_parser.rb'
|
12
|
+
task :spec => :text_parser
|
13
|
+
task :build => :text_parser
|
data/bin/protoc-gen-ruby
CHANGED
data/lib/protocol_buffers.rb
CHANGED
@@ -1,12 +1,23 @@
|
|
1
1
|
require 'stringio'
|
2
2
|
|
3
|
+
if RUBY_VERSION < "1.9"
|
4
|
+
raise("This version of ruby-protocol-buffers no longer supports Ruby 1.8.7")
|
5
|
+
end
|
6
|
+
|
3
7
|
module ProtocolBuffers
|
4
|
-
# for 1.9.2 compatibility
|
5
8
|
def self.bin_sio(*args)
|
6
9
|
sio = StringIO.new(*args)
|
7
|
-
sio.set_encoding(
|
10
|
+
sio.set_encoding(Encoding::BINARY) if
|
11
|
+
sio.respond_to?(:set_encoding) and
|
12
|
+
sio.external_encoding != Encoding::BINARY
|
13
|
+
sio
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.utf8_sio(*args)
|
17
|
+
sio = StringIO.new(*args)
|
18
|
+
sio.set_encoding('utf-8') if
|
8
19
|
sio.respond_to?(:set_encoding) and
|
9
|
-
sio.external_encoding != Encoding::
|
20
|
+
sio.external_encoding != Encoding::UTF_8
|
10
21
|
sio
|
11
22
|
end
|
12
23
|
end
|
@@ -26,7 +26,7 @@ module ProtocolBuffers
|
|
26
26
|
# encode packed field in a LENGTH_DELIMITED wire
|
27
27
|
wire_type = 2
|
28
28
|
tag = (field.tag << 3) | wire_type
|
29
|
-
buf =
|
29
|
+
buf = ProtocolBuffers.bin_sio
|
30
30
|
value.each { |i| serialize_field_value(buf, field.wire_type, field.serialize(i)) }
|
31
31
|
Varint.encode(io, tag)
|
32
32
|
Varint.encode(io, buf.size)
|
@@ -242,6 +242,10 @@ module ProtocolBuffers
|
|
242
242
|
value
|
243
243
|
end
|
244
244
|
|
245
|
+
def text_format(io, value, options = nil)
|
246
|
+
io.write value.to_s
|
247
|
+
end
|
248
|
+
|
245
249
|
module WireFormats
|
246
250
|
module LENGTH_DELIMITED
|
247
251
|
def wire_type
|
@@ -304,6 +308,11 @@ module ProtocolBuffers
|
|
304
308
|
def deserialize(value)
|
305
309
|
value.read
|
306
310
|
end
|
311
|
+
|
312
|
+
def text_format(io, value, options = nil)
|
313
|
+
value = value.unpack("C*").map { |b| "\\x%02x" % b }.join(nil)
|
314
|
+
io.write "\"#{value}\""
|
315
|
+
end
|
307
316
|
end
|
308
317
|
|
309
318
|
class StringField < BytesField
|
@@ -332,6 +341,14 @@ module ProtocolBuffers
|
|
332
341
|
read_value
|
333
342
|
end
|
334
343
|
end
|
344
|
+
|
345
|
+
def text_format(io, value, options = nil)
|
346
|
+
if HAS_ENCODING
|
347
|
+
io.write value.dup.force_encoding(Encoding::ASCII_8BIT).dump
|
348
|
+
else
|
349
|
+
io.write value.dump
|
350
|
+
end
|
351
|
+
end
|
335
352
|
end
|
336
353
|
|
337
354
|
class NumericField < Field
|
@@ -583,9 +600,18 @@ module ProtocolBuffers
|
|
583
600
|
@opts[:default] || @valid_values.first
|
584
601
|
end
|
585
602
|
|
603
|
+
def value_from_name(name)
|
604
|
+
@proxy_enum.name_to_value_map[name.to_sym]
|
605
|
+
end
|
606
|
+
|
586
607
|
def inspect_value(value)
|
587
608
|
"#{@value_to_name[value]}(#{value})"
|
588
609
|
end
|
610
|
+
|
611
|
+
def text_format(io, value, options = nil)
|
612
|
+
formatted = @value_to_name[value] || value.to_s
|
613
|
+
io.write formatted
|
614
|
+
end
|
589
615
|
end
|
590
616
|
|
591
617
|
class AggregateField < Field
|
@@ -614,6 +640,24 @@ module ProtocolBuffers
|
|
614
640
|
def deserialize(io)
|
615
641
|
@proxy_class.parse(io)
|
616
642
|
end
|
643
|
+
|
644
|
+
def text_format(io, value, options = nil)
|
645
|
+
options = options.dup
|
646
|
+
options[:nest] ||= 0
|
647
|
+
if options[:short]
|
648
|
+
indent = ""
|
649
|
+
newline = " "
|
650
|
+
else
|
651
|
+
indent = " " * options[:nest]
|
652
|
+
newline = "\n"
|
653
|
+
end
|
654
|
+
options[:nest] += 1
|
655
|
+
|
656
|
+
io.write "{#{newline}"
|
657
|
+
value.text_format(io, options)
|
658
|
+
io.write " " if options[:short]
|
659
|
+
io.write "#{indent}}"
|
660
|
+
end
|
617
661
|
end
|
618
662
|
|
619
663
|
class MessageField < AggregateField
|