Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposed more functions in FieldDescriptor and OneofDescriptor. #10102

Merged
merged 4 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions php/src/Google/Protobuf/FieldDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class FieldDescriptor
{
use GetPublicDescriptorTrait;

/** @var \Google\Protobuf\Internal\FieldDescriptor $internal_desc */
private $internal_desc;

/**
Expand Down Expand Up @@ -81,6 +82,22 @@ public function getType()
return $this->internal_desc->getType();
}

/**
* @return int
*/
public function getOneofIndex()
fiboknacky marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->internal_desc->getOneofIndex();
}

/**
* @return boolean
*/
public function getProto3Optional()
fiboknacky marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->internal_desc->getProto3Optional();
}

/**
* @return Descriptor Returns a descriptor for the field type if the field type is a message, otherwise throws \Exception
* @throws \Exception
Expand Down Expand Up @@ -114,12 +131,4 @@ public function isMap()
{
return $this->internal_desc->isMap();
}

/**
* @return boolean
*/
public function hasOptionalKeyword()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should keep this function; it is useful.

You can implement it to match the C++ definition: http://google3/third_party/protobuf/descriptor.h;l=2387-2391;rcl=452188950

Copy link
Contributor Author

@fiboknacky fiboknacky Jun 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the C++ implementation needs to rely on many fields and functions that don't exist here yet. I'll leave this function to you. I may get back here once I implement other functions that I need for my feature.

Having said that, IMHO, it's a broken function (for a while). Leaving it like this doesn't provide any good unless we provide the correct implementation soon.

{
return $this->internal_desc->hasOptionalKeyword();
}
}
17 changes: 16 additions & 1 deletion php/src/Google/Protobuf/Internal/FieldDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class FieldDescriptor
private $message_type;
private $enum_type;
private $packed;
private $is_map;
private $oneof_index = -1;
private $proto3_optional;

public function __construct()
{
Expand Down Expand Up @@ -169,6 +169,16 @@ public function getPacked()
return $this->packed;
}

public function getProto3Optional()
{
return $this->proto3_optional;
}

public function setProto3Optional($proto3_optional)
{
$this->proto3_optional = $proto3_optional;
}

public function isPackable()
{
return $this->isRepeated() && self::isTypePackable($this->type);
Expand Down Expand Up @@ -214,6 +224,10 @@ private static function isTypePackable($field_type)
$field_type !== GPBType::BYTES);
}

/**
* @param FieldDescriptorProto $proto
* @return FieldDescriptor
*/
public static function getFieldDescriptor($proto)
{
$type_name = null;
Expand Down Expand Up @@ -269,6 +283,7 @@ public static function getFieldDescriptor($proto)
$field->setLabel($proto->getLabel());
$field->setPacked($packed);
$field->setOneofIndex($oneof_index);
$field->setProto3Optional($proto->getProto3Optional());

// At this time, the message/enum type may have not been added to pool.
// So we use the type name as place holder and will replace it with the
Expand Down
6 changes: 6 additions & 0 deletions php/src/Google/Protobuf/Internal/OneofDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class OneofDescriptor
use HasPublicDescriptorTrait;

private $name;
/** @var \Google\Protobuf\FieldDescriptor[] $fields */
private $fields;

public function __construct()
Expand Down Expand Up @@ -64,6 +65,11 @@ public function getFields()
return $this->fields;
}

public function isSynthetic()
{
return count($this->fields) === 1 && $this->fields[0]->getProto3Optional();
}

public static function buildFromProto($oneof_proto, $desc, $index)
{
$oneof = new OneofDescriptor();
Expand Down
3 changes: 2 additions & 1 deletion php/src/Google/Protobuf/OneofDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class OneofDescriptor
{
use GetPublicDescriptorTrait;

/** @var \Google\Protobuf\Internal\OneofDescriptor $internal_desc */
private $internal_desc;

/**
Expand Down Expand Up @@ -75,6 +76,6 @@ public function getFieldCount()

public function isSynthetic()
{
return $this->internal_desc->isSynthetic();
return $this->internal_desc->isSynthetic();
}
}