Skip to content

Commit

Permalink
Added support for getRealContainingOneof() and getContainingOneof()
Browse files Browse the repository at this point in the history
  • Loading branch information
fiboknacky committed Jun 3, 2022
1 parent b27dd18 commit 238331b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
22 changes: 20 additions & 2 deletions php/src/Google/Protobuf/FieldDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,30 @@ public function getOneofIndex()
return $this->internal_desc->getOneofIndex();
}

/**
* @return OneofDescriptor
*/
public function getContainingOneof()
{
return $this->getPublicDescriptor($this->internal_desc->getContainingOneof());
}

/**
* Gets the field's containing oneof, only if non-synthetic.
*
* @return null|OneofDescriptor
*/
public function getRealContainingOneof()
{
return $this->getPublicDescriptor($this->internal_desc->getRealContainingOneof());
}

/**
* @return boolean
*/
public function getProto3Optional()
public function hasOptionalKeyword()
{
return $this->internal_desc->getProto3Optional();
return $this->internal_desc->hasOptionalKeyword();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion php/src/Google/Protobuf/Internal/Descriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public static function buildFromProto($proto, $file_proto, $containing)
$desc->setOptions($proto->getOptions());

foreach ($proto->getField() as $field_proto) {
$desc->addField(FieldDescriptor::buildFromProto($field_proto));
$desc->addField(FieldDescriptor::buildFromProto($field_proto, $desc));
}

// Handle nested types.
Expand Down
33 changes: 29 additions & 4 deletions php/src/Google/Protobuf/Internal/FieldDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class FieldDescriptor
private $oneof_index = -1;
private $proto3_optional;

/** @var Descriptor $containing_type */
private $containing_type;
/** @var OneofDescriptor $containing_oneof */
private $containing_oneof;

public function __construct()
{
$this->public_desc = new \Google\Protobuf\FieldDescriptor($this);
Expand Down Expand Up @@ -179,6 +184,17 @@ public function setProto3Optional($proto3_optional)
$this->proto3_optional = $proto3_optional;
}

public function getContainingOneof()
{
return $this->containing_oneof;
}

public function getRealContainingOneof()
{
return !is_null($this->containing_oneof) && !$this->containing_oneof->isSynthetic()
? $this->containing_oneof : null;
}

public function isPackable()
{
return $this->isRepeated() && self::isTypePackable($this->type);
Expand Down Expand Up @@ -226,9 +242,10 @@ private static function isTypePackable($field_type)

/**
* @param FieldDescriptorProto $proto
* @param Descriptor $parent_desc
* @return FieldDescriptor
*/
public static function getFieldDescriptor($proto)
public static function getFieldDescriptor($proto, $parent_desc)
{
$type_name = null;
$type = $proto->getType();
Expand All @@ -242,7 +259,13 @@ public static function getFieldDescriptor($proto)
break;
}

$oneof_index = $proto->hasOneofIndex() ? $proto->getOneofIndex() : -1;
if ($proto->hasOneofIndex()) {
$oneof_index = $proto->getOneofIndex();
$containing_oneof = $parent_desc->getOneofDecl()[$oneof_index];
} else {
$containing_oneof = null;
$oneof_index = -1;
}
// TODO: once proto2 is supported, this default should be false
// for proto2.
if ($proto->getLabel() === GPBLabel::REPEATED &&
Expand All @@ -261,6 +284,8 @@ public static function getFieldDescriptor($proto)

$field = new FieldDescriptor();
$field->setName($proto->getName());
$field->containing_type = $parent_desc;
$field->containing_oneof = $containing_oneof;

$json_name = $proto->hasJsonName() ? $proto->getJsonName() :
lcfirst(implode('', array_map('ucwords', explode('_', $proto->getName()))));
Expand Down Expand Up @@ -302,8 +327,8 @@ public static function getFieldDescriptor($proto)
return $field;
}

public static function buildFromProto($proto)
public static function buildFromProto($proto, $parent_desc)
{
return FieldDescriptor::getFieldDescriptor($proto);
return FieldDescriptor::getFieldDescriptor($proto, $parent_desc);
}
}

0 comments on commit 238331b

Please sign in to comment.