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.
@@ -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
@@ -12,3 +12,4 @@ prof
12
12
  rdoc
13
13
  tests
14
14
  tmp
15
+ /lib/protocol_buffers/runtime/text_parser.rb
@@ -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
- - jruby-18mode # JRuby in 1.8 mode
9
+ - "2.1.0"
11
10
  - jruby-19mode # JRuby in 1.9 mode
12
- - rbx-18mode
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
- # https://github.com/rubinius/rubinius/issues/2215
23
- - rvm: rbx-19mode
18
+ - rvm: rbx-2.1.1
19
+
@@ -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.
@@ -1,16 +1,80 @@
1
- # ruby-protocol-buffers 1.0.1
1
+ # Change Log
2
+ All notable changes to this project will be documented in this file.
2
3
 
3
- * Treat unknown enum values as unknown fields
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
- # ruby-protocol-buffers 1.0.0
10
+ ### Changed
11
+ - Fixed Windows line ending bug in encoder.
12
+ - README syntax highlighting.
6
13
 
7
- * The library has reached to 1.0 status, hurrah
14
+ ### Removed
15
+ - Dropped Ruby 1.8.7 support.
8
16
 
9
- # ruby-protocol-buffers 0.8.5
17
+ ## 1.5.1 - 2013-10-28
18
+ ### Added
19
+ - Better value semantics for `Message`, including `==`, `eql?`, and `hash`.
10
20
 
11
- * Add support for Microsoft Windows (tested on Windows 7)
12
- * Fix StringIO encoding issues on Ruby 1.9.2 by explicitly setting binary encoding
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
- # ruby-protocol-buffers 0.8.4
26
+ ### Changed
27
+ - Fix for repeated fields being set to itself.
15
28
 
16
- * First gem release
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
  [![Build Status](https://travis-ci.org/codekitchen/ruby-protocol-buffers.png?branch=master)](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 internal
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
- of the heavy lifting -- this has huge advantages in ensuring
15
- compatibility and correctness. If you don't need cross-language
16
- interoperability you can create Message classes directly in ruby,
17
- in which case `protoc` is not needed. See "Writing Message Classes
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 what
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 >= 2.2 (the Google Protocol Buffer compiler)
31
- installed in the environment where you will be compiling them.
32
- You do not need `protoc` installed to use the generated `.pb.rb` files.
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
- This optional gem builds a small C extension to make parsing protocol buffers
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
- written in different programming languages, and so are normally defined
72
- in .proto files and translated to ruby using the `ruby-protoc` binary.
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
- generate if given this .proto file:
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
- class generated by `ruby-protoc`.
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, deprecated)
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
@@ -26,5 +26,6 @@ rescue => error
26
26
  ]
27
27
  $stderr.puts response.error
28
28
  else
29
- print response.to_s
29
+ $stdout.binmode
30
+ $stdout.print response.to_s
30
31
  end
@@ -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('binary') if
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::ASCII_8BIT
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 = StringIO.new
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