Adopt ruby_package in ruby generated code. (#4627)

* Adopt ruby_package in ruby generated code.

* Add test for ruby_package
pull/4675/head
Paul Yang 7 years ago committed by Bo Yang
parent d8483a92d7
commit 5289ee0c21
  1. 1
      .gitignore
  2. 5
      ruby/Rakefile
  3. 2
      ruby/tests/generated_code_test.rb
  4. 7
      ruby/tests/test_ruby_package.proto
  5. 19
      src/google/protobuf/compiler/ruby/ruby_generator.cc

1
.gitignore vendored

@ -174,6 +174,7 @@ js/testproto_libs2.js
ruby/lib/
ruby/tests/generated_code_pb.rb
ruby/tests/test_import_pb.rb
ruby/tests/test_ruby_package_pb.rb
ruby/Gemfile.lock
ruby/compatibility_tests/v3.0.0/protoc
ruby/compatibility_tests/v3.0.0/tests/generated_code_pb.rb

@ -87,6 +87,7 @@ end
# Proto for tests.
genproto_output << "tests/generated_code.rb"
genproto_output << "tests/test_import.rb"
genproto_output << "tests/test_ruby_package.rb"
file "tests/generated_code.rb" => "tests/generated_code.proto" do |file_task|
sh "../src/protoc --ruby_out=. tests/generated_code.proto"
end
@ -95,6 +96,10 @@ file "tests/test_import.rb" => "tests/test_import.proto" do |file_task|
sh "../src/protoc --ruby_out=. tests/test_import.proto"
end
file "tests/test_ruby_package.rb" => "tests/test_ruby_package.proto" do |file_task|
sh "../src/protoc --ruby_out=. tests/test_ruby_package.proto"
end
task :genproto => genproto_output
task :clean do

@ -5,6 +5,7 @@ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
require 'generated_code_pb'
require 'test_import_pb'
require 'test_ruby_package_pb'
require 'test/unit'
class GeneratedCodeTest < Test::Unit::TestCase
@ -15,5 +16,6 @@ class GeneratedCodeTest < Test::Unit::TestCase
# aspect of the extension (basic.rb is for that).
m = A::B::C::TestMessage.new()
m2 = FooBar::TestImportedMessage.new()
m3 = A::B::TestRubyPackageMessage.new()
end
end

@ -0,0 +1,7 @@
syntax = "proto3";
package foo_bar;
option ruby_package = "A.B";
message TestRubyPackageMessage {}

@ -337,9 +337,20 @@ void GenerateEnumAssignment(
}
int GeneratePackageModules(
std::string package_name,
const FileDescriptor* file,
google::protobuf::io::Printer* printer) {
int levels = 0;
bool need_change_to_module;
std::string package_name;
if (file->options().has_ruby_package()) {
package_name = file->options().ruby_package();
need_change_to_module = false;
} else {
package_name = file->package();
need_change_to_module = true;
}
while (!package_name.empty()) {
size_t dot_index = package_name.find(".");
string component;
@ -350,7 +361,9 @@ int GeneratePackageModules(
component = package_name.substr(0, dot_index);
package_name = package_name.substr(dot_index + 1);
}
component = PackageToModule(component);
if (need_change_to_module) {
component = PackageToModule(component);
}
printer->Print(
"module $name$\n",
"name", component);
@ -462,7 +475,7 @@ bool GenerateFile(const FileDescriptor* file, io::Printer* printer,
printer->Print(
"end\n\n");
int levels = GeneratePackageModules(file->package(), printer);
int levels = GeneratePackageModules(file, printer);
for (int i = 0; i < file->message_type_count(); i++) {
GenerateMessageAssignment("", file->message_type(i), printer);
}

Loading…
Cancel
Save