Skip to content

Commit

Permalink
Allow proto files to import descriptor.proto (protocolbuffers#2995)
Browse files Browse the repository at this point in the history
descriptor.proto uses proto2 syntax, which is not ready for external
usage. However, some proto3 files import descriptor.proto and cannot be
used. In this PR, all references (We cheated by only removing
extensions, which is enough for now. User should avoid using messages
defined in descriptor.proto as field type.) to content in
descriptor.proto are removed from generated files. Those that import
descriptor.proto can be used like other proto files.
  • Loading branch information
TeBoring authored Apr 20, 2017
1 parent 4c57e84 commit 4523c9c
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 5 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ php_EXTRA_DIST= \
php/tests/map_field_test.php \
php/tests/memory_leak_test.php \
php/tests/php_implementation_test.php \
php/tests/proto/test_import_descriptor_proto.proto \
php/tests/proto/test_include.proto \
php/tests/proto/test.proto \
php/tests/proto/test_prefix.proto \
Expand Down
1 change: 0 additions & 1 deletion php/src/Google/Protobuf/Internal/MapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ function checkKey($key_type, &$key)
GPBUtil::checkString($key, true);
break;
default:
var_dump($key_type);
trigger_error(
"Given type cannot be map key.",
E_USER_ERROR);
Expand Down
1 change: 0 additions & 1 deletion php/src/Google/Protobuf/Internal/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public function __construct($desc = NULL)
return;
}
$pool = DescriptorPool::getGeneratedPool();
var_dump(get_class($this));
$this->desc = $pool->getDescriptorByClassName(get_class($this));
foreach ($this->desc->getField() as $field) {
$setter = $field->getSetter();
Expand Down
2 changes: 1 addition & 1 deletion php/tests/gdb_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which
# phpunit` --bootstrap autoload.php tmp_test.php
#
gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which phpunit` --bootstrap autoload.php encode_decode_test.php
gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which phpunit` --bootstrap autoload.php well_known_test.php
#
# gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so memory_leak_test.php
#
Expand Down
14 changes: 14 additions & 0 deletions php/tests/proto/test_import_descriptor_proto.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
syntax = "proto3";

import "google/protobuf/descriptor.proto";

message TestImportDescriptorProto {
extend google.protobuf.MethodOptions {
int32 a = 72295727;
}
}

extend google.protobuf.MethodOptions {
int32 a = 72295728;
}

10 changes: 8 additions & 2 deletions php/tests/well_known_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@

class WellKnownTest extends PHPUnit_Framework_TestCase {

public function testNone() {
$msg = new GPBEmpty();
public function testNone()
{
$msg = new GPBEmpty();
}

public function testImportDescriptorProto()
{
$msg = new TestImportDescriptorProto();
}

}
26 changes: 26 additions & 0 deletions src/google/protobuf/compiler/php/php_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,12 @@ void GenerateAddFileToPool(const FileDescriptor* file, bool is_descriptor,
} else {
for (int i = 0; i < file->dependency_count(); i++) {
const std::string& name = file->dependency(i)->name();
// Currently, descriptor.proto is not ready for external usage. Skip to
// import it for now, so that its dependencies can still work as long as
// they don't use protos defined in descriptor.proto.
if (name == kDescriptorFile) {
continue;
}
std::string dependency_filename =
GeneratedMetadataFileName(name, is_descriptor);
printer->Print(
Expand All @@ -685,6 +691,26 @@ void GenerateAddFileToPool(const FileDescriptor* file, bool is_descriptor,
FileDescriptorSet files;
FileDescriptorProto* file_proto = files.add_file();
file->CopyTo(file_proto);

// Filter out descriptor.proto as it cannot be depended on for now.
RepeatedPtrField<string>* dependency = file_proto->mutable_dependency();
for (RepeatedPtrField<string>::iterator it = dependency->begin();
it != dependency->end(); ++it) {
if (*it != kDescriptorFile) {
dependency->erase(it);
break;
}
}

// Filter out all extensions, since we do not support extension yet.
file_proto->clear_extension();
RepeatedPtrField<DescriptorProto>* message_type =
file_proto->mutable_message_type();
for (RepeatedPtrField<DescriptorProto>::iterator it = message_type->begin();
it != message_type->end(); ++it) {
it->clear_extension();
}

string files_data;
files.SerializeToString(&files_data);

Expand Down
1 change: 1 addition & 0 deletions tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ generate_php_test_proto() {
../../src/protoc --php_out=generated proto/test.proto proto/test_include.proto proto/test_no_namespace.proto proto/test_prefix.proto
pushd ../../src
./protoc --php_out=../php/tests/generated google/protobuf/empty.proto
./protoc --php_out=../php/tests/generated -I../php/tests -I. ../php/tests/proto/test_import_descriptor_proto.proto
popd
popd
}
Expand Down

0 comments on commit 4523c9c

Please sign in to comment.