Skip to content

Commit ea0c6fe

Browse files
authored
Merge pull request #10362 from haberman/cherry-pick-php-oneof-descriptor
Cherry-pick: [PHP] Added getContainingOneof and getRealContainingOneof to descriptor.
2 parents 23097c4 + 005f6d1 commit ea0c6fe

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

php/ext/google/protobuf/def.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,44 @@ PHP_METHOD(FieldDescriptor, getEnumType) {
456456
RETURN_COPY_VALUE(&ret);
457457
}
458458

459+
/*
460+
* FieldDescriptor::getContainingOneof()
461+
*
462+
* Returns the OneofDescriptor for this field, or null if it is not inside
463+
* a oneof.
464+
*/
465+
PHP_METHOD(FieldDescriptor, getContainingOneof) {
466+
FieldDescriptor *intern = (FieldDescriptor*)Z_OBJ_P(getThis());
467+
const upb_OneofDef *o = upb_FieldDef_ContainingOneof(intern->fielddef);
468+
zval ret;
469+
470+
if (!o) {
471+
RETURN_NULL();
472+
}
473+
474+
OneofDescriptor_FromOneofDef(&ret, o);
475+
RETURN_COPY_VALUE(&ret);
476+
}
477+
478+
/*
479+
* FieldDescriptor::getRealContainingOneof()
480+
*
481+
* Returns the non-synthetic OneofDescriptor for this field, or null if it is
482+
* not inside a oneof.
483+
*/
484+
PHP_METHOD(FieldDescriptor, getRealContainingOneof) {
485+
FieldDescriptor *intern = (FieldDescriptor*)Z_OBJ_P(getThis());
486+
const upb_OneofDef *o = upb_FieldDef_RealContainingOneof(intern->fielddef);
487+
zval ret;
488+
489+
if (!o) {
490+
RETURN_NULL();
491+
}
492+
493+
OneofDescriptor_FromOneofDef(&ret, o);
494+
RETURN_COPY_VALUE(&ret);
495+
}
496+
459497
/*
460498
* FieldDescriptor::getMessageType()
461499
*
@@ -482,6 +520,8 @@ static zend_function_entry FieldDescriptor_methods[] = {
482520
PHP_ME(FieldDescriptor, getType, arginfo_void, ZEND_ACC_PUBLIC)
483521
PHP_ME(FieldDescriptor, isMap, arginfo_void, ZEND_ACC_PUBLIC)
484522
PHP_ME(FieldDescriptor, getEnumType, arginfo_void, ZEND_ACC_PUBLIC)
523+
PHP_ME(FieldDescriptor, getContainingOneof, arginfo_void, ZEND_ACC_PUBLIC)
524+
PHP_ME(FieldDescriptor, getRealContainingOneof, arginfo_void, ZEND_ACC_PUBLIC)
485525
PHP_ME(FieldDescriptor, getMessageType, arginfo_void, ZEND_ACC_PUBLIC)
486526
ZEND_FE_END
487527
};
@@ -516,6 +556,7 @@ static zend_class_entry *Descriptor_GetGeneratedClass(const upb_MessageDef *m) {
516556
char *classname =
517557
GetPhpClassname(upb_MessageDef_File(m), upb_MessageDef_FullName(m), false);
518558
zend_error(E_ERROR, "Couldn't load generated class %s", classname);
559+
return NULL;
519560
}
520561

521562
void Descriptor_FromMessageDef(zval *val, const upb_MessageDef *m) {

php/tests/DescriptorsTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ public function testDescriptor()
8383
$this->assertSame($class, $desc->getClass());
8484

8585
$this->assertInstanceOf('\Google\Protobuf\FieldDescriptor', $desc->getField(0));
86-
$this->assertSame(7, $desc->getFieldCount());
86+
$this->assertSame(8, $desc->getFieldCount());
8787

8888
$this->assertInstanceOf('\Google\Protobuf\OneofDescriptor', $desc->getOneofDecl(0));
89-
$this->assertSame(1, $desc->getOneofDeclCount());
89+
$this->assertSame(2, $desc->getOneofDeclCount());
9090
}
9191

9292
public function testDescriptorForIncludedMessage()
@@ -180,6 +180,7 @@ public function testFieldDescriptor()
180180
$this->assertSame(self::GPBTYPE_MESSAGE, $fieldDesc->getType());
181181
$this->assertInstanceOf('\Google\Protobuf\Descriptor', $fieldDesc->getMessageType());
182182
$this->assertFalse($fieldDesc->isMap());
183+
$this->assertNull($fieldDesc->getContainingOneof());
183184

184185
// Oneof int field
185186
// Tested further in testOneofDescriptor()
@@ -189,6 +190,21 @@ public function testFieldDescriptor()
189190
$this->assertSame(self::GPBLABEL_OPTIONAL, $fieldDesc->getLabel());
190191
$this->assertSame(self::GPBTYPE_INT32, $fieldDesc->getType());
191192
$this->assertFalse($fieldDesc->isMap());
193+
$this->assertSame($fieldDesc->getContainingOneof(), $fieldDesc->getRealContainingOneof());
194+
195+
$oneofDesc = $fieldDesc->getContainingOneof();
196+
$this->assertSame('my_oneof', $oneofDesc->getName());
197+
198+
// Proto3 optional it field.
199+
// Tested further in testOneofDescriptor()
200+
$fieldDesc = $fieldDescMap[52];
201+
$this->assertSame('proto3_optional_int32', $fieldDesc->getName());
202+
$this->assertSame(52, $fieldDesc->getNumber());
203+
$this->assertSame(self::GPBLABEL_OPTIONAL, $fieldDesc->getLabel());
204+
$this->assertSame(self::GPBTYPE_INT32, $fieldDesc->getType());
205+
$this->assertFalse($fieldDesc->isMap());
206+
$this->assertNull($fieldDesc->getRealContainingOneof());
207+
$this->assertNotNull($fieldDesc->getContainingOneof());
192208

193209
// Map int-enum field
194210
$fieldDesc = $fieldDescMap[71];

php/tests/proto/test_descriptors.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ message TestDescriptorsMessage {
1414
oneof my_oneof {
1515
int32 oneof_int32 = 51;
1616
}
17+
optional int32 proto3_optional_int32 = 52;
1718

1819
map<int32, EnumSub> map_int32_enum = 71;
1920

0 commit comments

Comments
 (0)