Allow proto files to import descriptor.proto (#2995)

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.
pull/3000/head
Paul Yang 8 years ago committed by GitHub
parent 4c57e8475f
commit 4523c9c233
  1. 1
      Makefile.am
  2. 1
      php/src/Google/Protobuf/Internal/MapField.php
  3. 1
      php/src/Google/Protobuf/Internal/Message.php
  4. 2
      php/tests/gdb_test.sh
  5. 14
      php/tests/proto/test_import_descriptor_proto.proto
  6. 10
      php/tests/well_known_test.php
  7. 26
      src/google/protobuf/compiler/php/php_generator.cc
  8. 1
      tests.sh

@ -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 \

@ -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);

@ -71,7 +71,6 @@ class Message
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();

@ -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
#

@ -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;
}

@ -4,8 +4,14 @@ use Google\Protobuf\GPBEmpty;
class WellKnownTest extends PHPUnit_Framework_TestCase {
public function testNone() {
$msg = new GPBEmpty();
public function testNone()
{
$msg = new GPBEmpty();
}
public function testImportDescriptorProto()
{
$msg = new TestImportDescriptorProto();
}
}

@ -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(
@ -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);

@ -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
}

Loading…
Cancel
Save