Skip to content

Commit

Permalink
Adopt ruby_package in ruby generated code. (protocolbuffers#4627)
Browse files Browse the repository at this point in the history
* Adopt ruby_package in ruby generated code.

* Add test for ruby_package
  • Loading branch information
TeBoring authored May 18, 2018
1 parent 036947f commit 9ccc3e5
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions ruby/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions ruby/tests/generated_code_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

require 'generated_code_pb'
require 'test_import_pb'
require 'test_ruby_package_pb'
require 'test/unit'

class GeneratedCodeTest < Test::Unit::TestCase
Expand All @@ -15,5 +16,6 @@ def test_generated_msg
# 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
7 changes: 7 additions & 0 deletions ruby/tests/test_ruby_package.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
syntax = "proto3";

package foo_bar;

option ruby_package = "A.B";

message TestRubyPackageMessage {}
19 changes: 16 additions & 3 deletions src/google/protobuf/compiler/ruby/ruby_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 9ccc3e5

Please sign in to comment.