ruby_protobuf 0.4.1 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,5 +1,9 @@
1
- === 0.4.1 / 2010-04-30
1
+ === 0.4.4 / 2010-06-04
2
2
 
3
+ * 0.4.4
4
+ * Bug fix. (rprotoc)
5
+ * 0.4.3
6
+ * Bug fixes.
3
7
  * 0.4.1
4
8
  * Switched to 'jeweler'.
5
9
  * 0.4.0 alpha
data/Manifest.txt CHANGED
@@ -38,7 +38,6 @@ lib/protobuf/message/service.rb
38
38
  lib/protobuf/rpc/client.rb
39
39
  lib/protobuf/rpc/handler.rb
40
40
  lib/protobuf/rpc/server.rb
41
- lib/ruby_protobuf.rb
42
41
  script/mk_parser
43
42
  test/check_unbuild.rb
44
43
  test/data/data.bin
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.1
1
+ 0.4.4
data/bin/rprotoc CHANGED
@@ -8,7 +8,6 @@ else
8
8
  require 'rubygems'
9
9
  gem 'ruby_protobuf'
10
10
  end
11
- require 'ruby_protobuf'
12
11
  require 'protobuf/compiler/compiler'
13
12
 
14
13
 
@@ -22,8 +21,6 @@ opts.on('-o', '--out <OUT_DIR>', 'Specify the directory in which Ruby source fil
22
21
  opts.on_tail('-v', '--version', 'Show version.'){ puts(opts.ver); exit }
23
22
  opts.on_tail('-h', '--help', 'Show this message.'){ puts(opts.help); exit }
24
23
 
25
- ::Version = RubyProtobuf::VERSION
26
-
27
24
  begin
28
25
  opts.order!
29
26
  rescue OptionParser::ParseError
@@ -5,5 +5,13 @@ module Protobuf
5
5
  def camelize(name)
6
6
  name.to_s.gsub(/(?:\A|_)(\w)/) { $1.upcase }
7
7
  end
8
+
9
+ def modulize(name)
10
+ name.to_s.sub(/\A[a-z]/) {|c| c.upcase }
11
+ end
12
+
13
+ def underscore(name)
14
+ name.to_s.gsub(/\B[A-Z]/, '_\&').downcase
15
+ end
8
16
  end
9
17
  end
@@ -107,9 +107,7 @@ require 'protobuf/message/extend'
107
107
  end
108
108
 
109
109
  def accept_message_visitor(visitor)
110
- class_name = @name.to_s
111
- class_name.gsub!(/\A[a-z]/) {|c| c.upcase}
112
- visitor.write("class #{class_name} < ::Protobuf::Message")
110
+ visitor.write("class #{Util.modulize(@name)} < ::Protobuf::Message")
113
111
  visitor.in_context(self.class) do
114
112
  define_in_the_file(visitor)
115
113
  @children.each {|child| child.accept_message_visitor(visitor) }
@@ -133,7 +131,7 @@ require 'protobuf/message/extend'
133
131
 
134
132
  def accept_message_visitor(visitor)
135
133
  name = @name.is_a?(Array) ? @name.join : name.to_s
136
- visitor.write("class #{name} < ::Protobuf::Message")
134
+ visitor.write("class #{Util.modulize(name)} < ::Protobuf::Message")
137
135
  visitor.in_context(self.class) do
138
136
  define_in_the_file(visitor)
139
137
  @children.each {|child| child.accept_message_visitor(visitor) }
@@ -152,7 +150,7 @@ require 'protobuf/message/extend'
152
150
  end
153
151
 
154
152
  def accept_message_visitor(visitor)
155
- visitor.write("class #{@name} < ::Protobuf::Enum")
153
+ visitor.write("class #{Util.modulize(@name)} < ::Protobuf::Enum")
156
154
  visitor.in_context(self.class) do
157
155
  define_in_the_file(visitor)
158
156
  @children.each {|child| child.accept_message_visitor(visitor) }
@@ -175,7 +173,7 @@ require 'protobuf/message/extend'
175
173
  end
176
174
 
177
175
  def accept_message_visitor(visitor)
178
- visitor.write("define :#{@name}, #{@value}")
176
+ visitor.write("#{Util.modulize(@name)} = value(:#{@name}, #{@value})")
179
177
  end
180
178
 
181
179
  def accept_descriptor_visitor(visitor)
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
2
  require '<%= underscored_name %>'
3
3
 
4
- <%= module_name %>::<%= service_name %>.new(:Port => <%= default_port %>).start
4
+ <%= module_name %>::<%= Util.modulize(service_name) %>.new(:Port => <%= default_port %>).start
@@ -3,15 +3,15 @@ require 'protobuf/rpc/client'
3
3
  require '<%= required_file %>'
4
4
 
5
5
  # build request
6
- request = <%= message_module %>::<%= request %>.new
6
+ request = <%= message_module %>::<%= Util.modulize(request) %>.new
7
7
  # TODO: setup a request
8
8
  raise StandardError, 'setup a request'
9
9
 
10
- # create blunk response
11
- response = <%= message_module %>::<%= response %>.new
10
+ # create blank response
11
+ response = <%= message_module %>::<%= Util.modulize(response) %>.new
12
12
 
13
13
  # execute rpc
14
- Protobuf::Rpc::Client.new('localhost', <%= default_port %>).call :<%= underscore name %>, request, response
14
+ Protobuf::Rpc::Client.new('localhost', <%= default_port %>).call :<%= Util.underscore(name) %>, request, response
15
15
 
16
16
  # show response
17
17
  puts response
@@ -3,9 +3,9 @@ require 'protobuf/rpc/handler'
3
3
  require '<%= required_file %>'
4
4
 
5
5
  <%- rpcs.each do |name, request, response| -%>
6
- class <%= module_name %>::<%= name %>Handler < Protobuf::Rpc::Handler
7
- request <%= module_name %>::<%= request %>
8
- response <%= module_name %>::<%= response %>
6
+ class <%= module_name %>::<%= Util.modulize(name) %>Handler < Protobuf::Rpc::Handler
7
+ request <%= module_name %>::<%= Util.modulize(request) %>
8
+ response <%= module_name %>::<%= Util.modulize(response) %>
9
9
 
10
10
  def self.process_request(request, response)
11
11
  # TODO: edit this method
@@ -13,11 +13,11 @@ class <%= module_name %>::<%= name %>Handler < Protobuf::Rpc::Handler
13
13
  end
14
14
 
15
15
  <%- end -%>
16
- class <%= module_name %>::<%= service_name %> < Protobuf::Rpc::Server
16
+ class <%= module_name %>::<%= Util.modulize(service_name) %> < Protobuf::Rpc::Server
17
17
  def setup_handlers
18
18
  @handlers = {
19
19
  <%- rpcs.each do |name, | -%>
20
- :<%= underscore name %> => <%= module_name %>::<%= name %>Handler,
20
+ :<%= Util.underscore(name) %> => <%= module_name %>::<%= Util.modulize(name) %>Handler,
21
21
  <%- end -%>
22
22
  }
23
23
  end
@@ -1,5 +1,6 @@
1
1
  require 'erb'
2
2
  require 'fileutils'
3
+ require 'protobuf/common/util'
3
4
  require 'protobuf/descriptor/descriptor_proto'
4
5
 
5
6
  module Protobuf
@@ -107,9 +108,9 @@ module Protobuf
107
108
 
108
109
  def create_files(filename, out_dir, file_create)
109
110
  begin
110
- Class.new.class_eval(to_s) # check the message
111
- rescue LoadError => e
112
- puts "Warning, couldn't test load proto file because of imports"
111
+ eval(to_s, TOPLEVEL_BINDING) # check the message
112
+ rescue LoadError
113
+ $stderr.puts "Warning, couldn't test load proto file because of imports"
113
114
  end
114
115
  if file_create
115
116
  log_writing(filename)
@@ -143,8 +144,8 @@ module Protobuf
143
144
  @create_file = create_file
144
145
  default_port = 9999
145
146
  @services.each do |service_name, rpcs|
146
- underscored_name = underscore service_name.to_s
147
- message_module = package.map{|p| p.to_s.capitalize}.join('::')
147
+ underscored_name = Util.underscore(service_name.to_s)
148
+ message_module = package.map{|p| Util.camelize(p.to_s)}.join('::')
148
149
  required_file = message_file.sub(/^\.\//, '').sub(/\.rb$/, '')
149
150
 
150
151
  create_bin(out_dir, underscored_name, message_module, service_name, default_port)
@@ -165,17 +166,15 @@ module Protobuf
165
166
  @file_contents[bin_filename] = bin_contents
166
167
  end
167
168
 
168
- def create_service(message_file, out_dir, underscored_name, module_name, service_name,
169
- default_port, rpcs, required_file)
169
+ def create_service(message_file, out_dir, underscored_name, module_name, service_name, default_port, rpcs, required_file)
170
170
  service_filename = "#{out_dir}/#{underscored_name}.rb"
171
171
  service_contents = template_erb('rpc_service').result(binding)
172
172
  create_file_with_backup(service_filename, service_contents) if @create_file
173
173
  @file_contents[service_filename] = service_contents
174
174
  end
175
175
 
176
- def create_client(out_dir, underscored_name, default_port, name, request, response,
177
- message_module, required_file)
178
- client_filename = "#{out_dir}/client_#{underscore name}.rb"
176
+ def create_client(out_dir, underscored_name, default_port, name, request, response, message_module, required_file)
177
+ client_filename = "#{out_dir}/client_#{Util.underscore(name)}.rb"
179
178
  client_contents = template_erb('rpc_client').result binding
180
179
  create_file_with_backup(client_filename, client_contents, true) if @create_file
181
180
  @file_contents[client_filename] = client_contents
@@ -183,10 +182,6 @@ module Protobuf
183
182
 
184
183
  private
185
184
 
186
- def underscore(str)
187
- str.to_s.gsub(/\B[A-Z]/, '_\&').downcase
188
- end
189
-
190
185
  def template_erb(template)
191
186
  ERB.new(File.read("#{File.dirname(__FILE__)}/template/#{template}.erb"), nil, '-')
192
187
  end
@@ -26,6 +26,7 @@ module Protobuf
26
26
  def unbuild(parent_proto)
27
27
  message_proto = Google::Protobuf::DescriptorProto.new
28
28
  message_proto.name = @message_class.to_s.split('::').last
29
+ @message_class.new # XXX: Setup field proxies.
29
30
  @message_class.fields.each do |tag, field|
30
31
  field.descriptor.unbuild(message_proto)
31
32
  end
@@ -1,4 +1,5 @@
1
1
  require 'delegate'
2
+ require 'protobuf/common/util'
2
3
  require 'protobuf/descriptor/enum_descriptor'
3
4
  require 'protobuf/message/protoable'
4
5
 
@@ -13,7 +14,7 @@ module Protobuf
13
14
  if not defined?(@values)
14
15
  constants.find {|c| const_get(c) == value} # for compatibility
15
16
  else
16
- @values_index ||= @values.inject({}) {|hash, (n, v)| hash[v.value] = n; hash }
17
+ @values_index ||= @values.inject({}) {|hash, (n, v)| hash[v.value.to_i] = n; hash }
17
18
  @values_index[value]
18
19
  end
19
20
  end
@@ -30,11 +31,17 @@ module Protobuf
30
31
 
31
32
  private
32
33
 
34
+ # for compatibility
33
35
  def define(name, value)
34
36
  enum_value = EnumValue.new(self, name, value)
35
- const_set(name, enum_value)
36
- @values ||= {}
37
- @values[name] = enum_value
37
+ const_set(Util.modulize(name), enum_value)
38
+ (@values ||= {})[name] = enum_value
39
+ end
40
+
41
+ def value(name, val)
42
+ enum_value = EnumValue.new(self, name, val)
43
+ (@values ||= {})[name] = enum_value
44
+ enum_value
38
45
  end
39
46
  end
40
47
  end
@@ -47,9 +47,9 @@ module Tutorial
47
47
  optional :string, :email, 3
48
48
  class PhoneType < ::Protobuf::Enum
49
49
  defined_in __FILE__
50
- define :MOBILE, 0
51
- define :HOME, 1
52
- define :WORK, 2
50
+ MOBILE = value(:MOBILE, 0)
51
+ HOME = value(:HOME, 1)
52
+ WORK = value(:WORK, 2)
53
53
  end
54
54
  class PhoneNumber < ::Protobuf::Message
55
55
  defined_in __FILE__
@@ -40,9 +40,9 @@ module TutorialExt
40
40
  optional :string, :email, 3
41
41
  class PhoneType < ::Protobuf::Enum
42
42
  defined_in __FILE__
43
- define :MOBILE, 0
44
- define :HOME, 1
45
- define :WORK, 2
43
+ MOBILE = value(:MOBILE, 0)
44
+ HOME = value(:HOME, 1)
45
+ WORK = value(:WORK, 2)
46
46
  end
47
47
  class PhoneNumber < ::Protobuf::Message
48
48
  defined_in __FILE__
@@ -1,14 +1,24 @@
1
1
  ### Generated by rprotoc. DO NOT EDIT!
2
2
  ### <proto file: test/proto/lowercase.proto>
3
- # package test.lowercase;
3
+ # package test.lower_case.lowerCamelCase;
4
4
  #
5
5
  # message foo {
6
6
  # message bar {
7
+ # message fooBar {
8
+ # }
7
9
  # }
8
10
  # }
9
11
  # message baaz {
10
12
  # required foo.bar x = 1;
11
13
  # }
14
+ # enum baaaz {
15
+ # abc = 0;
16
+ # def = 1;
17
+ # }
18
+ #
19
+ # service qux {
20
+ # rpc quuuux(baaz) returns (foo);
21
+ # }
12
22
 
13
23
  require 'protobuf/message/message'
14
24
  require 'protobuf/message/enum'
@@ -16,16 +26,26 @@ require 'protobuf/message/service'
16
26
  require 'protobuf/message/extend'
17
27
 
18
28
  module Test
19
- module Lowercase
20
- class Foo < ::Protobuf::Message
21
- defined_in __FILE__
22
- class Bar < ::Protobuf::Message
29
+ module LowerCase
30
+ module LowerCamelCase
31
+ class Foo < ::Protobuf::Message
23
32
  defined_in __FILE__
33
+ class Bar < ::Protobuf::Message
34
+ defined_in __FILE__
35
+ class FooBar < ::Protobuf::Message
36
+ defined_in __FILE__
37
+ end
38
+ end
39
+ end
40
+ class Baaz < ::Protobuf::Message
41
+ defined_in __FILE__
42
+ required :'foo::bar', :x, 1
43
+ end
44
+ class Baaaz < ::Protobuf::Enum
45
+ defined_in __FILE__
46
+ define :abc, 0
47
+ define :def, 1
24
48
  end
25
- end
26
- class Baaz < ::Protobuf::Message
27
- defined_in __FILE__
28
- required :'foo::bar', :x, 1
29
49
  end
30
50
  end
31
51
  end
@@ -1,9 +1,19 @@
1
- package test.lowercase;
1
+ package test.lower_case.lowerCamelCase;
2
2
 
3
3
  message foo {
4
4
  message bar {
5
+ message fooBar {
6
+ }
5
7
  }
6
8
  }
7
9
  message baaz {
8
10
  required foo.bar x = 1;
9
11
  }
12
+ enum baaaz {
13
+ abc = 0;
14
+ def = 1;
15
+ }
16
+
17
+ service qux {
18
+ rpc quuuux(baaz) returns (foo);
19
+ }
@@ -24,8 +24,8 @@ module Test
24
24
  defined_in __FILE__
25
25
  class Enum < ::Protobuf::Enum
26
26
  defined_in __FILE__
27
- define :A, 1
28
- define :B, 2
27
+ A = value(:A, 1)
28
+ B = value(:B, 2)
29
29
  end
30
30
  optional :uint32, :number, 1, :default => 20
31
31
  optional :string, :text, 2, :default => "default string"
@@ -2,6 +2,20 @@ require 'test/unit'
2
2
  require 'protobuf/compiler/compiler'
3
3
 
4
4
  class CompilerTest < Test::Unit::TestCase
5
+ def save_const(parent_class, const_name)
6
+ if parent_class.const_defined?(const_name)
7
+ saved = parent_class.const_get(const_name)
8
+ parent_class.__send__(:remove_const, const_name)
9
+ end
10
+
11
+ yield
12
+
13
+ if saved
14
+ parent_class.__send__(:remove_const, const_name) if parent_class.const_defined?(const_name)
15
+ parent_class.const_set(const_name, saved)
16
+ end
17
+ end
18
+
5
19
  # Issue 12: Parse error on float default value
6
20
  def test_compile_float_default
7
21
  assert_compile_proto(<<-EOS, 'test/proto/float_default.proto')
@@ -37,7 +51,8 @@ end
37
51
  end
38
52
 
39
53
  def test_create_message
40
- assert_compile_proto(<<-EOS, 'test/proto/addressbook.proto')
54
+ save_const(Object, :Tutorial) do
55
+ assert_compile_proto(<<-EOS, 'test/proto/addressbook.proto')
41
56
  ### Generated by rprotoc. DO NOT EDIT!
42
57
  ### <proto file: test/proto/addressbook.proto>
43
58
  # package tutorial;
@@ -88,9 +103,9 @@ module Tutorial
88
103
 
89
104
  class PhoneType < ::Protobuf::Enum
90
105
  defined_in __FILE__
91
- define :MOBILE, 0
92
- define :HOME, 1
93
- define :WORK, 2
106
+ MOBILE = value(:MOBILE, 0)
107
+ HOME = value(:HOME, 1)
108
+ WORK = value(:WORK, 2)
94
109
  end
95
110
 
96
111
  class PhoneNumber < ::Protobuf::Message
@@ -110,11 +125,13 @@ module Tutorial
110
125
  repeated :Person, :person, 1
111
126
  end
112
127
  end
113
- EOS
128
+ EOS
129
+ end
114
130
  end
115
131
 
116
132
  def test_create_nested_message
117
- assert_compile_proto(<<-EOS, 'test/proto/nested.proto')
133
+ save_const(Test, :Nested) do
134
+ assert_compile_proto(<<-EOS, 'test/proto/nested.proto')
118
135
  ### Generated by rprotoc. DO NOT EDIT!
119
136
  ### <proto file: test/proto/nested.proto>
120
137
  # package test.nested;
@@ -146,14 +163,16 @@ module Test
146
163
  end
147
164
  end
148
165
  end
149
- EOS
166
+ EOS
167
+ end
150
168
  end
151
169
 
152
170
  def test_nested_message
153
- file_contents = Protobuf::Compiler.new.create_message('test/proto/nested.proto', '.', '.', false)
154
- assert_nothing_raised {Object.class_eval file_contents}
155
- assert_raise(TypeError) {Test::Nested::Baaz.new.x = 1}
156
- assert_nothing_raised {Test::Nested::Baaz.new.x = Test::Nested::Foo::Bar.new}
171
+ save_const(Test, :Nested) do
172
+ file_contents = Protobuf::Compiler.new.create_message('test/proto/nested.proto', '.', '.', false)
173
+ assert_raise(TypeError) {Test::Nested::Baaz.new.x = 1}
174
+ assert_nothing_raised {Test::Nested::Baaz.new.x = Test::Nested::Foo::Bar.new}
175
+ end
157
176
  end
158
177
 
159
178
  def test_create_rpc
@@ -209,7 +228,7 @@ request = Tutorial::Person.new
209
228
  # TODO: setup a request
210
229
  raise StandardError, 'setup a request'
211
230
 
212
- # create blunk response
231
+ # create blank response
213
232
  response = Tutorial::AddressBook.new
214
233
 
215
234
  # execute rpc
@@ -229,7 +248,7 @@ request = Tutorial::Person.new
229
248
  # TODO: setup a request
230
249
  raise StandardError, 'setup a request'
231
250
 
232
- # create blunk response
251
+ # create blank response
233
252
  response = Tutorial::Person.new
234
253
 
235
254
  # execute rpc
@@ -4,8 +4,16 @@ require 'test/proto/lowercase.pb'
4
4
  class LowercaseTest < Test::Unit::TestCase
5
5
  def test_lowercase
6
6
  message = nil
7
- assert_nothing_raised { message = Test::Lowercase::Baaz.new }
8
- assert_nothing_raised { message.x = Test::Lowercase::Foo::Bar.new }
9
- assert_equal(Test::Lowercase::Foo::Bar, message.get_field_by_name(:x).type)
7
+ assert_nothing_raised { message = Test::LowerCase::LowerCamelCase::Baaz.new }
8
+ assert_nothing_raised { message.x = Test::LowerCase::LowerCamelCase::Foo::Bar.new }
9
+ assert_equal(Test::LowerCase::LowerCamelCase::Foo::Bar, message.get_field_by_name(:x).type)
10
+ end
11
+
12
+ def test_lowercased_enum_members
13
+ klass = Test::LowerCase::LowerCamelCase::Baaaz
14
+ assert_equal(0, klass::Abc)
15
+ assert_equal(1, klass::Def)
16
+ assert_equal(0, klass.values[:abc])
17
+ assert_equal(1, klass.values[:def])
10
18
  end
11
19
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 4
8
- - 1
9
- version: 0.4.1
8
+ - 4
9
+ version: 0.4.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - MATSUYAMA Kengo
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-30 00:00:00 +09:00
17
+ date: 2010-06-04 00:00:00 +09:00
18
18
  default_executable: rprotoc
19
19
  dependencies: []
20
20
 
@@ -69,7 +69,6 @@ files:
69
69
  - lib/protobuf/rpc/client.rb
70
70
  - lib/protobuf/rpc/handler.rb
71
71
  - lib/protobuf/rpc/server.rb
72
- - lib/ruby_protobuf.rb
73
72
  - script/mk_parser
74
73
  - test/check_unbuild.rb
75
74
  - test/data/data.bin
data/lib/ruby_protobuf.rb DELETED
@@ -1,3 +0,0 @@
1
- class RubyProtobuf
2
- VERSION = '0.4.1'
3
- end