ruby-protocol-buffers 0.8.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/.document +3 -0
  2. data/.gitignore +11 -0
  3. data/LICENSE +22 -0
  4. data/README +82 -0
  5. data/Rakefile +56 -0
  6. data/VERSION +1 -0
  7. data/bin/ruby-protoc +55 -0
  8. data/debian/changelog +105 -0
  9. data/debian/compatability +1 -0
  10. data/debian/control +12 -0
  11. data/debian/libprotobuf-ruby1.8.install +7 -0
  12. data/debian/protocol_buffers.rb +3 -0
  13. data/debian/rules +13 -0
  14. data/examples/json_protobuf.rb +90 -0
  15. data/ext/extconf.disabled.rb +3 -0
  16. data/ext/varint.c +65 -0
  17. data/lib/protocol_buffers.rb +5 -0
  18. data/lib/protocol_buffers/compiler.rb +48 -0
  19. data/lib/protocol_buffers/compiler/descriptor.pb.rb +236 -0
  20. data/lib/protocol_buffers/compiler/descriptor.proto +393 -0
  21. data/lib/protocol_buffers/compiler/file_descriptor_to_d.rb +195 -0
  22. data/lib/protocol_buffers/compiler/file_descriptor_to_ruby.rb +173 -0
  23. data/lib/protocol_buffers/limited_io.rb +33 -0
  24. data/lib/protocol_buffers/runtime/decoder.rb +65 -0
  25. data/lib/protocol_buffers/runtime/encoder.rb +53 -0
  26. data/lib/protocol_buffers/runtime/enum.rb +4 -0
  27. data/lib/protocol_buffers/runtime/extend.rb +1 -0
  28. data/lib/protocol_buffers/runtime/field.rb +550 -0
  29. data/lib/protocol_buffers/runtime/message.rb +494 -0
  30. data/lib/protocol_buffers/runtime/service.rb +1 -0
  31. data/lib/protocol_buffers/runtime/varint.rb +62 -0
  32. data/spec/compiler_spec.rb +19 -0
  33. data/spec/fields_spec.rb +49 -0
  34. data/spec/proto_files/depends.proto +5 -0
  35. data/spec/proto_files/featureful.proto +54 -0
  36. data/spec/proto_files/no_package.proto +10 -0
  37. data/spec/proto_files/simple.proto +5 -0
  38. data/spec/runtime_spec.rb +430 -0
  39. data/spec/spec.opts +1 -0
  40. data/spec/spec_helper.rb +8 -0
  41. metadata +128 -0
data/.document ADDED
@@ -0,0 +1,3 @@
1
+ README
2
+ lib/**/*.rb
3
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /*.pb.rb
2
+ /*.d
3
+ /*.proto
4
+ coverage
5
+ rdoc
6
+ pkg
7
+ prof
8
+ tests
9
+ d/*
10
+ *.gemspec
11
+ tmp
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2009 Mozy, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,82 @@
1
+ = Ruby Protocol Buffers
2
+
3
+ Protocol Buffers are a way of encoding structured data in an efficient yet
4
+ extensible format. Google uses Protocol Buffers for almost all of its internal
5
+ RPC protocols and file formats.
6
+
7
+ This library has two components: a compiler to turn <tt>.proto</tt> definitions
8
+ into Ruby modules (extension <tt>.pb.rb</tt>), and a runtime to use protocol
9
+ buffers defined by these modules. The compiler relies on Google's C++ based
10
+ compiler (+protoc+) for much of the heavy lifting -- this has huge advantages in
11
+ ensuring compatibility and correctness.
12
+
13
+ This library is heavily optimized for encoding and decoding speed. There is a
14
+ small C extension available in the ext/ dir that will improve performance even
15
+ further, but it is currently disabled to avoid rubygems having to compile a
16
+ native extension. TODO: anybody know if we can make this optional?
17
+
18
+ Because this is a tool for generating code, the RDoc documentation is a bit
19
+ unusual. See the text in the ProtocolBuffers::Message class for details on what
20
+ code is generated.
21
+
22
+ == Features
23
+
24
+ === Supported Features
25
+
26
+ * messages, enums, field types, all basic protobuf features
27
+ * packages
28
+ * imports
29
+ * nested types
30
+ * passing on unknown fields when re-serializing a message
31
+
32
+ === Unsupported Features
33
+
34
+ * extensions
35
+ * packed option (could be useful)
36
+ * accessing custom options
37
+
38
+ === Probably Never to be Supported
39
+
40
+ * RPC stubbing
41
+ * deprecated protocol features (e.g. groups)
42
+ * the unsupported options (java_*, optimize_for, message_set_wire_format, deprecated)
43
+
44
+ === Known Issues
45
+
46
+ * not tested on Windows
47
+ * lots of polish still needed in APIs, docs, calling out to +protoc+, etc
48
+
49
+ == Simple Usage Example
50
+
51
+ $ echo "package Test; message MyMessage { optional string myField = 1; }" > test.proto
52
+ $ bin/ruby-protoc test.proto
53
+ $ irb -I./lib -rtest.pb
54
+ > msg = Test::MyMessage.new(:myField => 'zomgkittenz')
55
+ => #<Test::MyMessage myField="zomgkittenz">
56
+ > Test::MyMessage.parse(msg.to_s) == msg
57
+ => true
58
+
59
+ == Authors
60
+
61
+ Brian Palmer (http://github.com/codekitchen)
62
+
63
+ == Installation
64
+
65
+ You'll need protoc version >= 2.2 (the Google Protocol Buffer compiler)
66
+ installed in the environment where you will be compiling protocol buffers. This
67
+ is only needed for compiling, you don't need it installed to use the generated
68
+ <tt>.pb.rb</tt> files. You do need this Ruby library installed to use the
69
+ <tt>.pb.rb</tt> files, for the runtime support.
70
+
71
+ If you use RubyGems, you can install via:
72
+
73
+ $ sudo gem install ruby-protocol-buffers
74
+
75
+ == Source
76
+
77
+ http://github.com/mozy/ruby-protocol-buffers
78
+
79
+ == License
80
+
81
+ See the LICENSE file included with the distribution for licensing and
82
+ copyright details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'spec'
4
+
5
+ begin
6
+ require 'metric_fu'
7
+ rescue LoadError
8
+ end
9
+
10
+ begin
11
+ require 'jeweler'
12
+ Jeweler::Tasks.new do |gem|
13
+ gem.name = "ruby-protocol-buffers"
14
+ gem.summary = %Q{Ruby compiler and runtime for the google protocol buffers library.}
15
+ gem.homepage = "http://github.com/mozy/ruby-protocol-buffers"
16
+ gem.authors = ["Brian Palmer"]
17
+ gem.version = File.read('VERSION')
18
+ gem.add_development_dependency "rspec", ">= 1.2.9"
19
+ gem.required_ruby_version = ">=1.8.6"
20
+ gem.require_path = 'lib'
21
+ # disabled to avoid needing to compile a C extension just to boost
22
+ # performance. TODO: is there a way to tell gems to make the extension
23
+ # optional?
24
+ # s.extensions << 'ext/extconf.rb'
25
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
26
+ end
27
+ Jeweler::GemcutterTasks.new
28
+ rescue LoadError
29
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
30
+ end
31
+
32
+ require 'spec/rake/spectask'
33
+ Spec::Rake::SpecTask.new(:spec) do |spec|
34
+ spec.libs << 'lib' << 'spec'
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ end
37
+
38
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
39
+ spec.libs << 'lib' << 'spec'
40
+ spec.pattern = 'spec/**/*_spec.rb'
41
+ spec.rcov = true
42
+ end
43
+
44
+ task :spec => :check_dependencies
45
+
46
+ task :default => :spec
47
+
48
+ require 'rake/rdoctask'
49
+ Rake::RDocTask.new do |rdoc|
50
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "ruby-protocol-buffers #{version}"
54
+ rdoc.rdoc_files.include('README*', 'LICENSE')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.8.4
data/bin/ruby-protoc ADDED
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ begin
4
+ require 'rubygems'
5
+ rescue LoadError
6
+ end
7
+
8
+ require 'optparse'
9
+ require 'tempfile'
10
+
11
+ ruby_out = "."
12
+ include_dirs = []
13
+
14
+ opts = OptionParser.new
15
+ opts.banner = <<BANNER
16
+ Usage: #{opts.program_name} [OPTION] PROTO_FILES
17
+ Parse PROTO_FILES and generate output based on the options given:
18
+ BANNER
19
+
20
+ opts.on("-o", "--ruby_out", "=OUT_DIR",
21
+ "generates ruby code in OUT_DIR", "[defaults to pwd]",
22
+ String) { |val| ruby_out = val }
23
+ opts.on("-I", "--proto_path", "=PATH",
24
+ "Specify a directory to search for includes/imports.") { |val| include_dirs << val }
25
+ opts.on_tail("-h", "--help") { puts opts; exit }
26
+
27
+ rest = opts.parse(ARGV)
28
+ filenames = rest
29
+
30
+ (puts "Missing input file.\n\n"; puts opts; exit) if filenames.empty?
31
+
32
+ require 'protocol_buffers'
33
+ require 'protocol_buffers/compiler'
34
+
35
+ protocfile = Tempfile.new("ruby-protoc")
36
+ ProtocolBuffers::Compiler.compile(protocfile.path, filenames, :include_dirs => include_dirs)
37
+ descriptor_set = FileDescriptorSet.parse(protocfile)
38
+ protocfile.close(true)
39
+
40
+ if ruby_out
41
+ require 'protocol_buffers/compiler/file_descriptor_to_ruby'
42
+ end
43
+
44
+ descriptor_set.file.each do |file_descriptor|
45
+
46
+ if ruby_out
47
+ path = File.join(ruby_out,
48
+ File.basename(file_descriptor.name, ".proto") + ".pb.rb")
49
+ File.open(path, "wb") do |file|
50
+ dumper = FileDescriptorToRuby.new(file_descriptor)
51
+ dumper.write(file)
52
+ end
53
+ end
54
+
55
+ end
data/debian/changelog ADDED
@@ -0,0 +1,105 @@
1
+ protobuf-ruby (0.8.4) etch; urgency=low
2
+
3
+ * Fix decoding of varints into signed int32/int64 fields
4
+
5
+ -- Brian Palmer <brian@mozy.com> Mon, 17 May 2010 22:10:42 +0000
6
+
7
+ protobuf-ruby (0.8.3) etch; urgency=low
8
+
9
+ * fix for ruby 1.8 <-> 1.9 compat
10
+
11
+ -- Brian Palmer <brian@mozy.com> Thu, 4 Feb 2010 00:02:23 +0000
12
+
13
+ protobuf-ruby (0.8.2) etch; urgency=low
14
+
15
+ * Fix deb package for real. I think. Supporting both deb and gem is
16
+ tricky.
17
+
18
+ -- Brian Palmer <brian@mozy.com> Tue, 2 Feb 2010 22:11:43 +0000
19
+
20
+ protobuf-ruby (0.8.1) etch; urgency=low
21
+
22
+ * whoops fix deb install
23
+
24
+ -- Brian Palmer <brian@mozy.com> Tue, 2 Feb 2010 21:35:43 +0000
25
+
26
+ protobuf-ruby (0.8.0) etch; urgency=low
27
+
28
+ * fix whitespace
29
+ * Remove the pure-ruby experimental compiler.
30
+ * Updated bootstrapping step, and refactored enum support for easier
31
+ include.
32
+ * WIP D protobuf compiler
33
+ * Update README, add empty LICENSE, update Rakefile
34
+ * Remove support for compiling D protobufs from binary (no runtime
35
+ yet)
36
+ * gitignore update
37
+ * new name: s/protobuf/protocol_buffers and s/Protobuf/ProtocolBuffers
38
+ * update README with feature list
39
+ * add compiler helper methods and starting on some tests
40
+ * Add RDoc documentation to the Message class.
41
+ * More tests
42
+ * more tests
43
+ * More field and runtime tests
44
+ * Handle .proto files without a package specified
45
+ * update readme example
46
+ * more documentation
47
+ * Example: JSON-to-Protobuf and back
48
+ * test for unknown fields
49
+ * Drop InvalidFieldValue exception class, separate type checking/value
50
+ checking.
51
+ * validate required fields and improve validation on deserialization
52
+ * Refactor serialization/deserialization to not be so weird.
53
+ * dir rename: s/message/runtime
54
+ * Fix sub-message initialization to work like python.
55
+ * Type check repeated fields using an Array subclass as a proxy.
56
+ * make sure we always give protoc a -I option for the input file
57
+ dir(s)
58
+ * Version bump to 0.0.0
59
+ * clean up gem/rake/rdoc/rspec infrastructure using the Jeweler
60
+ library
61
+ * improved rcov test coverage, and prune a bit of unused code
62
+ * store and pass on unknown fields when re-serializing a message
63
+ * README update
64
+ * fixing deb dependency for ubuntu
65
+ * fix deb build
66
+
67
+ -- Brian Palmer <brian@mozy.com> Fri, 29 Jan 2010 17:05:06 +0000
68
+
69
+ protobuf-ruby (0.1.10) etch; urgency=low
70
+
71
+ * Support for imports.
72
+ * Merge branch 'master' of git@labs.dechocorp.com:ruby-protobufs
73
+ * maintenance work
74
+
75
+ -- Brian Palmer <brianp@decho.com> Fri, 25 Sep 2009 21:26:05 +0000
76
+
77
+ protobuf-ruby (0.1.9) unstable; urgency=low
78
+
79
+ * fix code generation outputting empty case statement (not allowed in ruby)
80
+
81
+ -- Brian Palmer <brianp@decho.com> Fri, 11 Sep 2009 10:58:17 -0600
82
+
83
+ protobuf-ruby (0.1.8) unstable; urgency=low
84
+
85
+ * support for float and double fields
86
+
87
+ -- Brian Palmer <brianp@decho.com> Wed, 29 July 2009 16:36:15 -0600
88
+
89
+ protobuf-ruby (0.1.7) unstable; urgency=low
90
+
91
+ * faster runtime and some code fixes
92
+
93
+ -- Brian Palmer <brianp@decho.com> Wed, 29 July 2009 16:04:03 -0600
94
+
95
+ protobuf-ruby (0.1.5) unstable; urgency=low
96
+
97
+ * deb fixes and generated file missing require fix
98
+
99
+ -- Brian Palmer <brian@cerebus.bds> Sat, 30 May 2009 22:44:14 -0600
100
+
101
+ protobuf-ruby (0.1.0) unstable; urgency=low
102
+
103
+ * First stab at packaging.
104
+
105
+ -- Brian Palmer <brian@mozy.com> Sat, 30 May 2009 21:24:00 -0700
@@ -0,0 +1 @@
1
+ 4
data/debian/control ADDED
@@ -0,0 +1,12 @@
1
+ Source: protobuf-ruby
2
+ Section: non-free/net
3
+ Maintainer: Brian Palmer <brian@mozy.com>
4
+ Priority: optional
5
+ Architecture: all
6
+ Build-Depends: debhelper (>= 4), ruby1.8, ruby1.8-dev
7
+
8
+ Package: libprotobuf-ruby1.8
9
+ Architecture: any
10
+ Depends: ruby1.8, protobuf | protobuf-compiler
11
+ Description: Ruby implementation of a compiler and runtime for Google Protocol
12
+ Buffers.
@@ -0,0 +1,7 @@
1
+ debian/protocol_buffers.rb /usr/lib/ruby/1.8/
2
+ bin/ruby-protoc /usr/bin
3
+ VERSION /usr/lib/ruby/1.8/protocol_buffers
4
+ LICENSE /usr/lib/ruby/1.8/protocol_buffers
5
+ README /usr/lib/ruby/1.8/protocol_buffers
6
+ examples /usr/lib/ruby/1.8/protocol_buffers
7
+ lib /usr/lib/ruby/1.8/protocol_buffers
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), "protocol_buffers", "lib")
2
+
3
+ load(File.join(File.dirname(__FILE__), "protocol_buffers", "lib", "protocol_buffers.rb"))
data/debian/rules ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/make -f
2
+
3
+ include /usr/share/cdbs/1/rules/debhelper.mk
4
+
5
+ build/libprotobuf-ruby1.8::
6
+ -mv ext/extconf.disabled.rb ext/extconf.rb
7
+ cd ext && ruby1.8 extconf.rb --prefix=$(CURDIR)/debian/libprotobuf-ruby1.8/usr
8
+
9
+ cleanbuilddir/libprotobuf-ruby1.8::
10
+ -cd ext && make clean && rm Makefile
11
+
12
+ install/libprotobuf-ruby1.8::
13
+ cd ext && DESTDIR=$(CURDIR)/debian/libprotobuf-ruby1.8 make install
@@ -0,0 +1,90 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Example JSON/Protocol Buffer conversion using reflection.
4
+ # Requires the json gem.
5
+
6
+ begin
7
+ require 'rubygems'
8
+ rescue LoadError; end
9
+
10
+ require 'json'
11
+ $LOAD_PATH << File.dirname(__FILE__) + '/../lib'
12
+ require 'protocol_buffers'
13
+ require 'protocol_buffers/compiler'
14
+
15
+ class ProtocolBuffers::Message
16
+
17
+ def to_json(*args)
18
+ hash = {'json_class' => self.class.name}
19
+
20
+ # simpler version, includes all fields in the output, using the default
21
+ # values if unset. also includes empty repeated fields as empty arrays.
22
+ # fields.each do |tag, field|
23
+ # hash[field.name] = value_for_tag(field.tag)
24
+ # end
25
+
26
+ # prettier output, only includes non-empty repeated fields and set fields
27
+ fields.each do |tag, field|
28
+ if field.repeated?
29
+ value = value_for_tag(field.tag)
30
+ hash[field.name] = value unless value.empty?
31
+ else
32
+ hash[field.name] = value_for_tag(field.tag) if value_for_tag?(field.tag)
33
+ end
34
+ end
35
+ hash.to_json(*args)
36
+ end
37
+
38
+ def self.json_create(hash)
39
+ hash.delete('json_class')
40
+
41
+ # initialize takes a hash of { attribute_name => value } so you can just
42
+ # pass the hash into the constructor. but we're supposed to be showing off
43
+ # reflection, here. plus, that raises an exception if there is an unknown
44
+ # key in the hash.
45
+ # new(hash)
46
+
47
+ message = new
48
+ fields.each do |tag, field|
49
+ if value = hash[field.name.to_s]
50
+ message.set_value_for_tag(field.tag, value)
51
+ end
52
+ end
53
+ message
54
+ end
55
+ end
56
+
57
+ # and that's it... all the rest of this code is just to show it off
58
+
59
+ ProtocolBuffers::Compiler.compile_and_load_string <<-EOS
60
+ message Foo {
61
+ enum Bar {
62
+ A = 1;
63
+ B = 2;
64
+ C = 3;
65
+ };
66
+
67
+ message Baz {
68
+ optional string subby = 1;
69
+ };
70
+
71
+ repeated Bar bars = 1;
72
+ optional string name = 2;
73
+ repeated string bazzesses = 3;
74
+ optional Baz subbaz = 4;
75
+ };
76
+ EOS
77
+
78
+ foo = Foo.new(:name => 'foo1')
79
+ foo.bars += [Foo::Bar::A, Foo::Bar::A, Foo::Bar::C]
80
+ foo.subbaz = Foo::Baz.new(:subby => "subby!")
81
+
82
+ puts "Input protobuf:", foo.inspect, ""
83
+
84
+ json = JSON.pretty_generate(foo)
85
+ puts "JSON representation:", json, ""
86
+
87
+ foo2 = JSON.parse(json)
88
+ puts "Parsed from JSON:", foo2.inspect, ""
89
+
90
+ puts "Are they equal: #{foo == foo2}"