diff options
author | michaelbausor <michaelbausor@google.com> | 2017-08-04 16:35:22 -0700 |
---|---|---|
committer | Paul Yang <TeBoring@users.noreply.github.com> | 2017-08-04 16:35:22 -0700 |
commit | 21b0e5587c01948927ede9be789671ff116b7ad4 (patch) | |
tree | 8bb156c68677804739e65c3b4ee6cd9f891797f2 /php/src/Google/Protobuf/Internal | |
parent | 1ab5adbdec4d6487c8e6f230138092d27d6a0818 (diff) | |
download | protobuf-21b0e5587c01948927ede9be789671ff116b7ad4.tar.gz protobuf-21b0e5587c01948927ede9be789671ff116b7ad4.tar.bz2 protobuf-21b0e5587c01948927ede9be789671ff116b7ad4.zip |
Update PHP descriptors (#3391)
* Add descriptors test
* Update descriptors tests
* Add public descriptors
* Add test_desriptors.proto to test script
* Update composer files
* Remove references to GPBType, update tests to be compatible with c
* Update for c extension compatibility
* Remove nested enums for descriptor, update tests
* Strip leading '.' from descriptor name
* Update tests with test for getClass, fix OneofDescriptor
* Add new files to Makefile.am
Diffstat (limited to 'php/src/Google/Protobuf/Internal')
-rw-r--r-- | php/src/Google/Protobuf/Internal/Descriptor.php | 21 | ||||
-rw-r--r-- | php/src/Google/Protobuf/Internal/EnumBuilderContext.php | 4 | ||||
-rw-r--r-- | php/src/Google/Protobuf/Internal/EnumDescriptor.php | 20 | ||||
-rw-r--r-- | php/src/Google/Protobuf/Internal/FieldDescriptor.php | 6 | ||||
-rw-r--r-- | php/src/Google/Protobuf/Internal/GetPublicDescriptorTrait.php | 41 | ||||
-rw-r--r-- | php/src/Google/Protobuf/Internal/HasPublicDescriptorTrait.php (renamed from php/src/Google/Protobuf/Internal/EnumValueDescriptor.php) | 24 | ||||
-rw-r--r-- | php/src/Google/Protobuf/Internal/OneofDescriptor.php | 15 |
7 files changed, 106 insertions, 25 deletions
diff --git a/php/src/Google/Protobuf/Internal/Descriptor.php b/php/src/Google/Protobuf/Internal/Descriptor.php index 44225ad2..ee3a8bde 100644 --- a/php/src/Google/Protobuf/Internal/Descriptor.php +++ b/php/src/Google/Protobuf/Internal/Descriptor.php @@ -34,17 +34,24 @@ namespace Google\Protobuf\Internal; class Descriptor { + use HasPublicDescriptorTrait; private $full_name; private $field = []; private $json_to_field = []; private $name_to_field = []; + private $index_to_field = []; private $nested_type = []; private $enum_type = []; private $klass; private $options; private $oneof_decl = []; + public function __construct() + { + $this->public_desc = new \Google\Protobuf\Descriptor($this); + } + public function addOneofDecl($oneof) { $this->oneof_decl[] = $oneof; @@ -70,6 +77,7 @@ class Descriptor $this->field[$field->getNumber()] = $field; $this->json_to_field[$field->getJsonName()] = $field; $this->name_to_field[$field->getName()] = $field; + $this->index_to_field[] = $field; } public function getField() @@ -124,6 +132,15 @@ class Descriptor } } + public function getFieldByIndex($index) + { + if (count($this->index_to_field) <= $index) { + return NULL; + } else { + return $this->index_to_field[$index]; + } + } + public function setClass($klass) { $this->klass = $klass; @@ -179,9 +196,11 @@ class Descriptor } // Handle oneof fields. + $index = 0; foreach ($proto->getOneofDecl() as $oneof_proto) { $desc->addOneofDecl( - OneofDescriptor::buildFromProto($oneof_proto, $desc)); + OneofDescriptor::buildFromProto($oneof_proto, $desc, $index)); + $index++; } return $desc; diff --git a/php/src/Google/Protobuf/Internal/EnumBuilderContext.php b/php/src/Google/Protobuf/Internal/EnumBuilderContext.php index c1dac24d..08397284 100644 --- a/php/src/Google/Protobuf/Internal/EnumBuilderContext.php +++ b/php/src/Google/Protobuf/Internal/EnumBuilderContext.php @@ -33,7 +33,7 @@ namespace Google\Protobuf\Internal; use Google\Protobuf\Internal\EnumDescriptor; -use Google\Protobuf\Internal\EnumValueDescriptor; +use Google\Protobuf\EnumValueDescriptor; class EnumBuilderContext { @@ -51,7 +51,7 @@ class EnumBuilderContext public function value($name, $number) { - $value = new EnumValueDescriptor(); + $value = new EnumValueDescriptor($name, $number); $this->descriptor->addValue($number, $value); return $this; } diff --git a/php/src/Google/Protobuf/Internal/EnumDescriptor.php b/php/src/Google/Protobuf/Internal/EnumDescriptor.php index 33a55a4a..01649fec 100644 --- a/php/src/Google/Protobuf/Internal/EnumDescriptor.php +++ b/php/src/Google/Protobuf/Internal/EnumDescriptor.php @@ -2,13 +2,22 @@ namespace Google\Protobuf\Internal; +use Google\Protobuf\EnumValueDescriptor; + class EnumDescriptor { + use HasPublicDescriptorTrait; private $klass; private $full_name; private $value; private $name_to_value; + private $value_descriptor = []; + + public function __construct() + { + $this->public_desc = new \Google\Protobuf\EnumDescriptor($this); + } public function setFullName($full_name) { @@ -24,6 +33,7 @@ class EnumDescriptor { $this->value[$number] = $value; $this->name_to_value[$value->getName()] = $value; + $this->value_descriptor[] = new EnumValueDescriptor($value->getName(), $number); } public function getValueByNumber($number) @@ -36,6 +46,16 @@ class EnumDescriptor return $this->name_to_value[$name]; } + public function getValueDescriptorByIndex($index) + { + return $this->value_descriptor[$index]; + } + + public function getValueCount() + { + return count($this->value); + } + public function setClass($klass) { $this->klass = $klass; diff --git a/php/src/Google/Protobuf/Internal/FieldDescriptor.php b/php/src/Google/Protobuf/Internal/FieldDescriptor.php index f18bf810..1443c6fd 100644 --- a/php/src/Google/Protobuf/Internal/FieldDescriptor.php +++ b/php/src/Google/Protobuf/Internal/FieldDescriptor.php @@ -34,6 +34,7 @@ namespace Google\Protobuf\Internal; class FieldDescriptor { + use HasPublicDescriptorTrait; private $name; private $json_name; @@ -48,6 +49,11 @@ class FieldDescriptor private $is_map; private $oneof_index = -1; + public function __construct() + { + $this->public_desc = new \Google\Protobuf\FieldDescriptor($this); + } + public function setOneofIndex($index) { $this->oneof_index = $index; diff --git a/php/src/Google/Protobuf/Internal/GetPublicDescriptorTrait.php b/php/src/Google/Protobuf/Internal/GetPublicDescriptorTrait.php new file mode 100644 index 00000000..d22bc305 --- /dev/null +++ b/php/src/Google/Protobuf/Internal/GetPublicDescriptorTrait.php @@ -0,0 +1,41 @@ +<?php + +// Protocol Buffers - Google's data interchange format +// Copyright 2017 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +namespace Google\Protobuf\Internal; + +trait GetPublicDescriptorTrait +{ + private function getPublicDescriptor($desc) + { + return is_null($desc) ? null : $desc->getPublicDescriptor(); + } +} diff --git a/php/src/Google/Protobuf/Internal/EnumValueDescriptor.php b/php/src/Google/Protobuf/Internal/HasPublicDescriptorTrait.php index 549766e3..ed5d1660 100644 --- a/php/src/Google/Protobuf/Internal/EnumValueDescriptor.php +++ b/php/src/Google/Protobuf/Internal/HasPublicDescriptorTrait.php @@ -32,28 +32,12 @@ namespace Google\Protobuf\Internal; -class EnumValueDescriptor +trait HasPublicDescriptorTrait { - private $name; - private $number; + private $public_desc; - public function setName($name) + public function getPublicDescriptor() { - $this->name = $name; - } - - public function getName() - { - return $this->name; - } - - public function setNumber($number) - { - $this->number = $number; - } - - public function getNumber() - { - return $this->number; + return $this->public_desc; } } diff --git a/php/src/Google/Protobuf/Internal/OneofDescriptor.php b/php/src/Google/Protobuf/Internal/OneofDescriptor.php index 57961f39..67b107f6 100644 --- a/php/src/Google/Protobuf/Internal/OneofDescriptor.php +++ b/php/src/Google/Protobuf/Internal/OneofDescriptor.php @@ -34,10 +34,16 @@ namespace Google\Protobuf\Internal; class OneofDescriptor { + use HasPublicDescriptorTrait; private $name; private $fields; + public function __construct() + { + $this->public_desc = new \Google\Protobuf\OneofDescriptor($this); + } + public function setName($name) { $this->name = $name; @@ -48,7 +54,7 @@ class OneofDescriptor return $this->name; } - public function addField(Descriptor $field) + public function addField(FieldDescriptor $field) { $this->fields[] = $field; } @@ -58,10 +64,15 @@ class OneofDescriptor return $this->fields; } - public static function buildFromProto($oneof_proto) + public static function buildFromProto($oneof_proto, $desc, $index) { $oneof = new OneofDescriptor(); $oneof->setName($oneof_proto->getName()); + foreach ($desc->getField() as $field) { + if ($field->getOneofIndex() == $index) { + $oneof->addField($field); + } + } return $oneof; } } |