Fixed bugs of OneofDescriptor.

This commit is contained in:
Thanet (Knack) Praneenararat 2022-06-09 16:53:27 +09:00
parent 594f2a9b0d
commit 0f1574aae2
4 changed files with 21 additions and 19 deletions

View File

@ -193,6 +193,10 @@ class Descriptor
$desc->setLegacyClass($legacy_classname);
$desc->setOptions($proto->getOptions());
foreach ($proto->getField() as $field_proto) {
$desc->addField(FieldDescriptor::buildFromProto($field_proto));
}
// Handle nested types.
foreach ($proto->getNestedType() as $nested_proto) {
$desc->addNestedType(Descriptor::buildFromProto(
@ -213,11 +217,6 @@ class Descriptor
$index++;
}
// Pass the descriptor to build FieldDescriptor after the OneofDescriptors are populated.
foreach ($proto->getField() as $field_proto) {
$desc->addField(FieldDescriptor::buildFromProto($field_proto, $desc));
}
return $desc;
}
}

View File

@ -187,6 +187,11 @@ class FieldDescriptor
return $this->containing_oneof;
}
public function setContainingOneof($containing_oneof)
{
$this->containing_oneof = $containing_oneof;
}
public function getRealContainingOneof()
{
return !is_null($this->containing_oneof) && !$this->containing_oneof->isSynthetic()
@ -240,10 +245,9 @@ class FieldDescriptor
/**
* @param FieldDescriptorProto $proto
* @param Descriptor $desc
* @return FieldDescriptor
*/
public static function getFieldDescriptor($proto, $desc)
public static function getFieldDescriptor($proto)
{
$type_name = null;
$type = $proto->getType();
@ -257,13 +261,7 @@ class FieldDescriptor
break;
}
if ($proto->hasOneofIndex()) {
$oneof_index = $proto->getOneofIndex();
$containing_oneof = $desc->getOneofDecl()[$oneof_index];
} else {
$oneof_index = -1;
$containing_oneof = null;
}
$oneof_index = $proto->hasOneofIndex() ? $proto->getOneofIndex() : -1;
// TODO: once proto2 is supported, this default should be false
// for proto2.
if ($proto->getLabel() === GPBLabel::REPEATED &&
@ -282,10 +280,7 @@ class FieldDescriptor
$field = new FieldDescriptor();
$field->setName($proto->getName());
$field->containing_oneof = $containing_oneof;
$json_name = $proto->hasJsonName() ? $proto->getJsonName() :
lcfirst(implode('', array_map('ucwords', explode('_', $proto->getName()))));
if ($proto->hasJsonName()) {
$json_name = $proto->getJsonName();
} else {
@ -324,8 +319,8 @@ class FieldDescriptor
return $field;
}
public static function buildFromProto($proto, $parent_desc)
public static function buildFromProto($proto)
{
return FieldDescriptor::getFieldDescriptor($proto, $parent_desc);
return FieldDescriptor::getFieldDescriptor($proto);
}
}

View File

@ -76,8 +76,10 @@ class OneofDescriptor
$oneof = new OneofDescriptor();
$oneof->setName($oneof_proto->getName());
foreach ($desc->getField() as $field) {
/** @var FieldDescriptor $field */
if ($field->getOneofIndex() == $index) {
$oneof->addField($field);
$field->setContainingOneof($oneof);
}
}
return $oneof;

View File

@ -63,6 +63,12 @@ class OneofDescriptor
*/
public function getField($index)
{
if (
is_null($this->internal_desc->getFields())
|| !isset($this->internal_desc->getFields()[$index])
) {
return null;
}
return $this->getPublicDescriptor($this->internal_desc->getFields()[$index]);
}