macks-ruby_protobuf 0.3.2.1

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.
Files changed (76) hide show
  1. data/History.txt +14 -0
  2. data/Manifest.txt +74 -0
  3. data/README.txt +58 -0
  4. data/Rakefile +18 -0
  5. data/bin/mk_parser +2 -0
  6. data/bin/rprotoc +36 -0
  7. data/examples/addressbook.pb.rb +56 -0
  8. data/examples/addressbook.proto +24 -0
  9. data/examples/reading_a_message.rb +32 -0
  10. data/examples/writing_a_message.rb +46 -0
  11. data/lib/protobuf/common/wire_type.rb +10 -0
  12. data/lib/protobuf/compiler/compiler.rb +54 -0
  13. data/lib/protobuf/compiler/nodes.rb +319 -0
  14. data/lib/protobuf/compiler/proto.y +203 -0
  15. data/lib/protobuf/compiler/proto2.ebnf +79 -0
  16. data/lib/protobuf/compiler/proto_parser.rb +1394 -0
  17. data/lib/protobuf/compiler/template/rpc_bin.erb +4 -0
  18. data/lib/protobuf/compiler/template/rpc_client.erb +18 -0
  19. data/lib/protobuf/compiler/template/rpc_service.erb +25 -0
  20. data/lib/protobuf/compiler/visitors.rb +288 -0
  21. data/lib/protobuf/descriptor/descriptor.proto +286 -0
  22. data/lib/protobuf/descriptor/descriptor.rb +54 -0
  23. data/lib/protobuf/descriptor/descriptor_builder.rb +144 -0
  24. data/lib/protobuf/descriptor/descriptor_proto.rb +119 -0
  25. data/lib/protobuf/descriptor/enum_descriptor.rb +33 -0
  26. data/lib/protobuf/descriptor/field_descriptor.rb +50 -0
  27. data/lib/protobuf/descriptor/file_descriptor.rb +38 -0
  28. data/lib/protobuf/message/decoder.rb +93 -0
  29. data/lib/protobuf/message/encoder.rb +37 -0
  30. data/lib/protobuf/message/enum.rb +28 -0
  31. data/lib/protobuf/message/extend.rb +8 -0
  32. data/lib/protobuf/message/field.rb +654 -0
  33. data/lib/protobuf/message/message.rb +308 -0
  34. data/lib/protobuf/message/protoable.rb +37 -0
  35. data/lib/protobuf/message/service.rb +9 -0
  36. data/lib/protobuf/rpc/client.rb +19 -0
  37. data/lib/protobuf/rpc/handler.rb +17 -0
  38. data/lib/protobuf/rpc/server.rb +39 -0
  39. data/lib/ruby_protobuf.rb +3 -0
  40. data/test/addressbook.rb +98 -0
  41. data/test/addressbook_base.rb +62 -0
  42. data/test/addressbook_ext.rb +12 -0
  43. data/test/check_unbuild.rb +30 -0
  44. data/test/collision.rb +18 -0
  45. data/test/data/data.bin +3 -0
  46. data/test/data/data_source.py +14 -0
  47. data/test/data/types.bin +0 -0
  48. data/test/data/types_source.py +22 -0
  49. data/test/data/unk.png +0 -0
  50. data/test/ext_collision.rb +25 -0
  51. data/test/ext_range.rb +23 -0
  52. data/test/merge.rb +40 -0
  53. data/test/nested.rb +25 -0
  54. data/test/proto/addressbook.proto +31 -0
  55. data/test/proto/addressbook_base.proto +26 -0
  56. data/test/proto/addressbook_ext.proto +6 -0
  57. data/test/proto/collision.proto +5 -0
  58. data/test/proto/ext_collision.proto +8 -0
  59. data/test/proto/ext_range.proto +7 -0
  60. data/test/proto/merge.proto +15 -0
  61. data/test/proto/nested.proto +7 -0
  62. data/test/proto/rpc.proto +6 -0
  63. data/test/proto/types.proto +17 -0
  64. data/test/test_addressbook.rb +43 -0
  65. data/test/test_compiler.rb +313 -0
  66. data/test/test_descriptor.rb +122 -0
  67. data/test/test_extension.rb +40 -0
  68. data/test/test_message.rb +106 -0
  69. data/test/test_optional_field.rb +68 -0
  70. data/test/test_parse.rb +15 -0
  71. data/test/test_ruby_protobuf.rb +1 -0
  72. data/test/test_serialize.rb +42 -0
  73. data/test/test_standard_message.rb +96 -0
  74. data/test/test_types.rb +181 -0
  75. data/test/types.rb +22 -0
  76. metadata +150 -0
data/History.txt ADDED
@@ -0,0 +1,14 @@
1
+ === 0.3.1 / 2008-08-22
2
+
3
+ * 0.3.1 fixed bugs concerning RPC (rev.146)
4
+ * 0.3.0 RPC is available (rev.124)
5
+ * RPC is available
6
+ * New compiler using racc (ruby version of yacc)
7
+ * Rename `sample' directory to `example'
8
+ * 0.2.1 extensions and import are available (rev.82)
9
+ * 0.2.0 introduce descriptor (rev.75)
10
+ * Message structures can be restored from encoded data by means a descriptor object
11
+ * 0.1.0 fixed a small bug
12
+ * 0.0.1 major enhancement
13
+ * Birthday!
14
+
data/Manifest.txt ADDED
@@ -0,0 +1,74 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ bin/mk_parser
6
+ bin/rprotoc
7
+ examples/addressbook.proto
8
+ examples/addressbook.pb.rb
9
+ examples/reading_a_message.rb
10
+ examples/writing_a_message.rb
11
+ lib/protobuf/common/wire_type.rb
12
+ lib/protobuf/compiler/compiler.rb
13
+ lib/protobuf/compiler/nodes.rb
14
+ lib/protobuf/compiler/proto.y
15
+ lib/protobuf/compiler/proto2.ebnf
16
+ lib/protobuf/compiler/proto_parser.rb
17
+ lib/protobuf/compiler/template/rpc_bin.erb
18
+ lib/protobuf/compiler/template/rpc_client.erb
19
+ lib/protobuf/compiler/template/rpc_service.erb
20
+ lib/protobuf/compiler/visitors.rb
21
+ lib/protobuf/descriptor/descriptor.proto
22
+ lib/protobuf/descriptor/descriptor.rb
23
+ lib/protobuf/descriptor/descriptor_builder.rb
24
+ lib/protobuf/descriptor/descriptor_proto.rb
25
+ lib/protobuf/descriptor/enum_descriptor.rb
26
+ lib/protobuf/descriptor/field_descriptor.rb
27
+ lib/protobuf/descriptor/file_descriptor.rb
28
+ lib/protobuf/message/decoder.rb
29
+ lib/protobuf/message/encoder.rb
30
+ lib/protobuf/message/enum.rb
31
+ lib/protobuf/message/extend.rb
32
+ lib/protobuf/message/field.rb
33
+ lib/protobuf/message/message.rb
34
+ lib/protobuf/message/protoable.rb
35
+ lib/protobuf/message/service.rb
36
+ lib/protobuf/rpc/client.rb
37
+ lib/protobuf/rpc/handler.rb
38
+ lib/protobuf/rpc/server.rb
39
+ lib/ruby_protobuf.rb
40
+ test/addressbook.rb
41
+ test/addressbook_base.rb
42
+ test/addressbook_ext.rb
43
+ test/check_unbuild.rb
44
+ test/collision.rb
45
+ test/data/data.bin
46
+ test/data/data_source.py
47
+ test/data/types.bin
48
+ test/data/types_source.py
49
+ test/data/unk.png
50
+ test/ext_collision.rb
51
+ test/ext_range.rb
52
+ test/merge.rb
53
+ test/nested.rb
54
+ test/proto/addressbook.proto
55
+ test/proto/addressbook_base.proto
56
+ test/proto/addressbook_ext.proto
57
+ test/proto/collision.proto
58
+ test/proto/ext_collision.proto
59
+ test/proto/ext_range.proto
60
+ test/proto/merge.proto
61
+ test/proto/nested.proto
62
+ test/proto/rpc.proto
63
+ test/proto/types.proto
64
+ test/test_addressbook.rb
65
+ test/test_compiler.rb
66
+ test/test_descriptor.rb
67
+ test/test_extension.rb
68
+ test/test_message.rb
69
+ test/test_parse.rb
70
+ test/test_ruby_protobuf.rb
71
+ test/test_serialize.rb
72
+ test/test_standard_message.rb
73
+ test/test_types.rb
74
+ test/types.rb
data/README.txt ADDED
@@ -0,0 +1,58 @@
1
+ = RubyProtobuf
2
+
3
+ * http://github.com/macks/ruby-protobuf
4
+ * http://code.google.com/p/ruby-protobuf
5
+
6
+ == DESCRIPTION:
7
+
8
+ Protocol Buffers for Ruby.
9
+
10
+ == FEATURES/PROBLEMS:
11
+
12
+ * Compile .proto file to ruby script
13
+ * Parse the binary wire format for protocol buffer
14
+ * Serialize data to the binary wire format for protocol buffer
15
+
16
+ == SYNOPSIS:
17
+
18
+ rprotoc examples/addressbook.proto
19
+
20
+ == REQUIREMENTS:
21
+
22
+ * All you need are included.
23
+
24
+ == INSTALL:
25
+
26
+ * rake gem; sudo gem install pkg/ruby_protobuf-*.gem
27
+
28
+ == AUTHOR:
29
+
30
+ MATSUYAMA Kengo <macksx@gmail.com>
31
+
32
+ Original Author:
33
+ ANDO Yasushi <andyjpn@gmail.com>
34
+
35
+ == LICENSE:
36
+
37
+ (The MIT License)
38
+
39
+ Copyright (c) 2008 FIX
40
+
41
+ Permission is hereby granted, free of charge, to any person obtaining
42
+ a copy of this software and associated documentation files (the
43
+ 'Software'), to deal in the Software without restriction, including
44
+ without limitation the rights to use, copy, modify, merge, publish,
45
+ distribute, sublicense, and/or sell copies of the Software, and to
46
+ permit persons to whom the Software is furnished to do so, subject to
47
+ the following conditions:
48
+
49
+ The above copyright notice and this permission notice shall be
50
+ included in all copies or substantial portions of the Software.
51
+
52
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
53
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
54
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
55
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
56
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
57
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
58
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ # -*- ruby -*-
2
+
3
+ $:.push(File.dirname(__FILE__) + '/lib')
4
+ require 'rubygems'
5
+ require 'hoe'
6
+ require 'ruby_protobuf'
7
+
8
+ Hoe.new('ruby_protobuf', RubyProtobuf::VERSION) do |p|
9
+ p.rubyforge_name = nil
10
+ p.author = 'MATSUYAMA Kengo'
11
+ p.email = 'macksx@gmail.com'
12
+ p.summary = 'Protocol Buffers for Ruby'
13
+ p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
14
+ p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
15
+ p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
16
+ end
17
+
18
+ # vim: syntax=ruby
data/bin/mk_parser ADDED
@@ -0,0 +1,2 @@
1
+ #!/bin/sh
2
+ racc -E lib/protobuf/compiler/proto.y -o lib/protobuf/compiler/proto_parser.rb
data/bin/rprotoc ADDED
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+ require 'optparse'
3
+ if File.exist? "#{File.dirname(__FILE__)}/../lib"
4
+ $: << "#{File.dirname(__FILE__)}/../lib"
5
+ else
6
+ require 'rubygems'
7
+ gem 'ruby_protobuf'
8
+ end
9
+ require 'ruby_protobuf'
10
+ require 'protobuf/compiler/compiler'
11
+
12
+
13
+ OPT = {}
14
+ opts = OptionParser.new("#{$0} [OPTIONS] PROTO_FILE")
15
+ opts.on('-p', '--proto_path <PATH>', 'Specify the directory in which to search for imports. The current directory is default.'){|v| OPT[:proto_path] = v}
16
+ opts.on('-o', '--out <OUT_DIR>', 'Specify the directory in which Ruby source file is generated. The current directory is default.'){|v| OPT[:out] = v}
17
+ opts.on_tail('-v', '--version', 'Show version.'){puts(opts.ver); exit}
18
+ opts.on_tail('-h', '--help', 'Show this message.'){puts(opts.help); exit}
19
+
20
+ ::Version = RubyProtobuf::VERSION
21
+
22
+ begin
23
+ opts.order! ARGV
24
+ rescue OptionParser::ParseError
25
+ $stderr.puts $!.to_s
26
+ exit 1
27
+ end
28
+
29
+ PROTO_FILE = ARGV.shift
30
+
31
+ unless PROTO_FILE
32
+ puts opts
33
+ exit
34
+ end
35
+
36
+ Protobuf::Compiler.compile(PROTO_FILE, (OPT[:proto_path] or '.'), (OPT[:out] or '.'))
@@ -0,0 +1,56 @@
1
+ ### Generated by rprotoc. DO NOT EDIT!
2
+ ### <proto file: examples/addressbook.proto>
3
+ # package tutorial;
4
+ #
5
+ # message Person {
6
+ # required string name = 1;
7
+ # required int32 id = 2;
8
+ # optional string email = 3;
9
+ #
10
+ # enum PhoneType {
11
+ # MOBILE = 0;
12
+ # HOME = 1;
13
+ # WORK = 2;
14
+ # }
15
+ #
16
+ # message PhoneNumber {
17
+ # required string number = 1;
18
+ # optional PhoneType type = 2 [default = HOME];
19
+ # }
20
+ #
21
+ # repeated PhoneNumber phone = 4;
22
+ # }
23
+ #
24
+ # message AddressBook {
25
+ # repeated Person person = 1;
26
+ # }
27
+
28
+ require 'protobuf/message/message'
29
+ require 'protobuf/message/enum'
30
+ require 'protobuf/message/service'
31
+ require 'protobuf/message/extend'
32
+
33
+ module Tutorial
34
+ class Person < ::Protobuf::Message
35
+ defined_in __FILE__
36
+ required :string, :name, 1
37
+ required :int32, :id, 2
38
+ optional :string, :email, 3
39
+ class PhoneType < ::Protobuf::Enum
40
+ defined_in __FILE__
41
+ MOBILE = 0
42
+ HOME = 1
43
+ WORK = 2
44
+ end
45
+ class PhoneNumber < ::Protobuf::Message
46
+ defined_in __FILE__
47
+ required :string, :number, 1
48
+ optional :PhoneType, :type, 2, :default => :HOME
49
+ end
50
+ repeated :PhoneNumber, :phone, 4
51
+ end
52
+ class AddressBook < ::Protobuf::Message
53
+ defined_in __FILE__
54
+ repeated :Person, :person, 1
55
+ end
56
+ end
@@ -0,0 +1,24 @@
1
+ package tutorial;
2
+
3
+ message Person {
4
+ required string name = 1;
5
+ required int32 id = 2;
6
+ optional string email = 3;
7
+
8
+ enum PhoneType {
9
+ MOBILE = 0;
10
+ HOME = 1;
11
+ WORK = 2;
12
+ }
13
+
14
+ message PhoneNumber {
15
+ required string number = 1;
16
+ optional PhoneType type = 2 [default = HOME];
17
+ }
18
+
19
+ repeated PhoneNumber phone = 4;
20
+ }
21
+
22
+ message AddressBook {
23
+ repeated Person person = 1;
24
+ }
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'addressbook.pb'
4
+
5
+ def list_people(address_book)
6
+ address_book.person.each do |person|
7
+ puts "Person ID: #{person.id}"
8
+ puts " Name: #{person.name}"
9
+ puts " E-mail: #{person.email}" unless person.email.empty?
10
+ person.phone.each do |phone_number|
11
+ print(case phone_number.type
12
+ when Tutorial::Person::PhoneType::MOBILE
13
+ ' Mobile phone #: '
14
+ when Tutorial::Person::PhoneType::HOME
15
+ ' Home phone #: '
16
+ when Tutorial::Person::PhoneType::WORK
17
+ ' Work phone #: '
18
+ end)
19
+ puts phone_number.number
20
+ end
21
+ end
22
+ end
23
+
24
+ unless ARGV.size == 1
25
+ puts "Usage: #{$0} ADDRESS_BOOK_FILE"
26
+ exit
27
+ end
28
+
29
+ address_book = Tutorial::AddressBook.new
30
+ address_book.parse_from_file ARGV[0]
31
+
32
+ list_people address_book
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'addressbook.pb'
4
+
5
+ def prompt_for_address(person)
6
+ print 'Enter person ID number: '
7
+ person.id = STDIN.gets.strip.to_i
8
+ print 'Enter name: '
9
+ person.name = STDIN.gets.strip
10
+ print 'Enter email address (blank for none): '
11
+ email = STDIN.gets.strip
12
+ person.email = email unless email.empty?
13
+
14
+ loop do
15
+ print 'Enter a phone number (or leave blank to finish): '
16
+ break if (number = STDIN.gets.strip).empty?
17
+
18
+ person.phone << Tutorial::Person::PhoneNumber.new
19
+ person.phone.last.number = number
20
+
21
+ print 'Is this a mobile, home, or work phone? '
22
+ person.phone.last.type =
23
+ case type = STDIN.gets.strip
24
+ when 'mobile'
25
+ Tutorial::Person::PhoneType::MOBILE
26
+ when 'home'
27
+ Tutorial::Person::PhoneType::HOME
28
+ when 'work'
29
+ Tutorial::Person::PhoneType::WORK
30
+ else
31
+ puts 'Unknown phone type; leaving as default value.'
32
+ nil
33
+ end
34
+ end
35
+ end
36
+
37
+ unless ARGV.size == 1
38
+ puts "Usage: #{$0} ADDRESS_BOOK_FILE"
39
+ exit
40
+ end
41
+
42
+ address_book = Tutorial::AddressBook.new
43
+ address_book.parse_from_file ARGV[0] if File.exist? ARGV[0]
44
+ address_book.person << Tutorial::Person.new
45
+ prompt_for_address address_book.person.last
46
+ address_book.serialize_to_file ARGV[0]
@@ -0,0 +1,10 @@
1
+ module Protobuf
2
+ class WireType
3
+ VARINT = 0
4
+ FIXED64 = 1
5
+ LENGTH_DELIMITED = 2
6
+ START_GROUP = 3
7
+ END_GROUP = 4
8
+ FIXED32 = 5
9
+ end
10
+ end
@@ -0,0 +1,54 @@
1
+ require 'fileutils'
2
+ require 'protobuf/compiler/proto_parser'
3
+ require 'protobuf/compiler/nodes'
4
+ require 'protobuf/compiler/visitors'
5
+
6
+ module Protobuf
7
+ class Compiler
8
+ def self.compile(proto_file, proto_dir='.', out_dir='.', file_create=true)
9
+ self.new.compile proto_file, proto_dir, out_dir, file_create
10
+ end
11
+
12
+ def compile(proto_file, proto_dir='.', out_dir='.', file_create=true)
13
+ create_message proto_file, proto_dir, out_dir, file_create
14
+ create_rpc proto_file, proto_dir, out_dir, file_create
15
+ end
16
+
17
+ def create_message(proto_file, proto_dir='.', out_dir='.', file_create=true)
18
+ out_dir.sub! %r{/$}, ''
19
+ proto_dir.sub! %r{/$}, ''
20
+ rb_file =
21
+ if proto_file =~ %r{^/}
22
+ then "#{out_dir}/#{proto_file.split('/').last.sub(/\.proto$/, '')}.pb.rb"
23
+ else "#{out_dir}/#{proto_file.sub(/\.proto$/, '')}.pb.rb" end
24
+ proto_path = validate_existence proto_file, proto_dir
25
+
26
+ message_visitor = Protobuf::Visitor::CreateMessageVisitor.new proto_file, proto_dir, out_dir
27
+ File.open proto_path, 'r' do |file|
28
+ message_visitor.visit Protobuf::ProtoParser.new.parse(file)
29
+ end
30
+ message_visitor.create_files rb_file, out_dir, file_create
31
+ end
32
+
33
+ def create_rpc(proto_file, proto_dir='.', out_dir='.', file_create=true)
34
+ message_file = "#{out_dir}/#{proto_file.sub(/\.proto$/, '')}.pb.rb"
35
+ out_dir = "#{out_dir}/#{File.dirname proto_file}"
36
+ proto_path = validate_existence proto_file, proto_dir
37
+
38
+ rpc_visitor = Protobuf::Visitor::CreateRpcVisitor.new
39
+ File.open proto_path, 'r' do |file|
40
+ rpc_visitor.visit Protobuf::ProtoParser.new.parse(file)
41
+ end
42
+ rpc_visitor.create_files message_file, out_dir, file_create
43
+ end
44
+
45
+ def validate_existence(path, base_dir)
46
+ if File.exist? path
47
+ elsif File.exist?(path = "#{base_dir or '.'}/#{path}")
48
+ else
49
+ raise ArgumentError.new("File does not exist: #{path}")
50
+ end
51
+ path
52
+ end
53
+ end
54
+ end