Skip to content

Commit

Permalink
Fixed bugs of OneofDescriptor.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiboknacky committed Jun 9, 2022
1 parent 594f2a9 commit 88d57d4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
9 changes: 4 additions & 5 deletions php/src/Google/Protobuf/Internal/Descriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ public static function buildFromProto($proto, $file_proto, $containing)
$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(
Expand All @@ -213,11 +217,6 @@ public static function buildFromProto($proto, $file_proto, $containing)
$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;
}
}
23 changes: 9 additions & 14 deletions php/src/Google/Protobuf/Internal/FieldDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ public function getContainingOneof()
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()
Expand Down Expand Up @@ -240,10 +245,9 @@ private static function isTypePackable($field_type)

/**
* @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();
Expand All @@ -257,13 +261,7 @@ public static function getFieldDescriptor($proto, $desc)
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 &&
Expand All @@ -282,10 +280,7 @@ public static function getFieldDescriptor($proto, $desc)

$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 {
Expand Down Expand Up @@ -324,8 +319,8 @@ public static function getFieldDescriptor($proto, $desc)
return $field;
}

public static function buildFromProto($proto, $parent_desc)
public static function buildFromProto($proto)
{
return FieldDescriptor::getFieldDescriptor($proto, $parent_desc);
return FieldDescriptor::getFieldDescriptor($proto);
}
}
3 changes: 3 additions & 0 deletions php/src/Google/Protobuf/Internal/OneofDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,13 @@ public static function buildFromProto($oneof_proto, $desc, $index)
$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;
}
}
6 changes: 6 additions & 0 deletions php/src/Google/Protobuf/OneofDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public function getName()
*/
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]);
}

Expand Down

0 comments on commit 88d57d4

Please sign in to comment.