Skip to content

Commit 01a7429

Browse files
FabioBatSilvacopybara-github
authored andcommitted
[php][compiler][ext] - Add missing has* methods and other well known types (#20636)
Should fix : #20036 Closes #20636 COPYBARA_INTEGRATE_REVIEW=#20636 from ChessCom:php-wellknown-types-predicate-methods 65aece9 PiperOrigin-RevId: 756414066
1 parent e5d1834 commit 01a7429

File tree

3 files changed

+149
-6
lines changed

3 files changed

+149
-6
lines changed

php/ext/google/protobuf/wkt.inc

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,13 @@ static PHP_METHOD(google_protobuf_Api, setSourceContext) {
319319
RETURN_COPY(getThis());
320320
}
321321

322+
static PHP_METHOD(google_protobuf_Api, hasSourceContext) {
323+
Message* intern = (Message*)Z_OBJ_P(getThis());
324+
const upb_FieldDef *f = upb_MessageDef_FindFieldByName(
325+
intern->desc->msgdef, "source_context");
326+
RETVAL_BOOL(upb_Message_HasFieldByDef(intern->msg, f));
327+
}
328+
322329
static PHP_METHOD(google_protobuf_Api, getMixins) {
323330
Message* intern = (Message*)Z_OBJ_P(getThis());
324331
const upb_FieldDef *f = upb_MessageDef_FindFieldByName(
@@ -375,6 +382,7 @@ static zend_function_entry google_protobuf_Api_phpmethods[] = {
375382
PHP_ME(google_protobuf_Api, setVersion, arginfo_setter, ZEND_ACC_PUBLIC)
376383
PHP_ME(google_protobuf_Api, getSourceContext, arginfo_void, ZEND_ACC_PUBLIC)
377384
PHP_ME(google_protobuf_Api, setSourceContext, arginfo_setter, ZEND_ACC_PUBLIC)
385+
PHP_ME(google_protobuf_Api, hasSourceContext, arginfo_void, ZEND_ACC_PUBLIC)
378386
PHP_ME(google_protobuf_Api, getMixins, arginfo_void, ZEND_ACC_PUBLIC)
379387
PHP_ME(google_protobuf_Api, setMixins, arginfo_setter, ZEND_ACC_PUBLIC)
380388
PHP_ME(google_protobuf_Api, getSyntax, arginfo_void, ZEND_ACC_PUBLIC)
@@ -1183,12 +1191,20 @@ static PHP_METHOD(google_protobuf_Struct_FieldsEntry, setValue) {
11831191
RETURN_COPY(getThis());
11841192
}
11851193

1194+
static PHP_METHOD(google_protobuf_Struct_FieldsEntry, hasValue) {
1195+
Message* intern = (Message*)Z_OBJ_P(getThis());
1196+
const upb_FieldDef *f = upb_MessageDef_FindFieldByName(
1197+
intern->desc->msgdef, "value");
1198+
RETVAL_BOOL(upb_Message_HasFieldByDef(intern->msg, f));
1199+
}
1200+
11861201
static zend_function_entry google_protobuf_Struct_FieldsEntry_phpmethods[] = {
11871202
PHP_ME(google_protobuf_Struct_FieldsEntry, __construct, arginfo_construct, ZEND_ACC_PUBLIC)
11881203
PHP_ME(google_protobuf_Struct_FieldsEntry, getKey, arginfo_void, ZEND_ACC_PUBLIC)
11891204
PHP_ME(google_protobuf_Struct_FieldsEntry, setKey, arginfo_setter, ZEND_ACC_PUBLIC)
11901205
PHP_ME(google_protobuf_Struct_FieldsEntry, getValue, arginfo_void, ZEND_ACC_PUBLIC)
11911206
PHP_ME(google_protobuf_Struct_FieldsEntry, setValue, arginfo_setter, ZEND_ACC_PUBLIC)
1207+
PHP_ME(google_protobuf_Struct_FieldsEntry, hasValue, arginfo_void, ZEND_ACC_PUBLIC)
11921208
ZEND_FE_END
11931209
};
11941210

@@ -1235,6 +1251,13 @@ static PHP_METHOD(google_protobuf_Value, setNullValue) {
12351251
RETURN_COPY(getThis());
12361252
}
12371253

1254+
static PHP_METHOD(google_protobuf_Value, hasNullValue) {
1255+
Message* intern = (Message*)Z_OBJ_P(getThis());
1256+
const upb_FieldDef *f = upb_MessageDef_FindFieldByName(
1257+
intern->desc->msgdef, "null_value");
1258+
RETVAL_BOOL(upb_Message_HasFieldByDef(intern->msg, f));
1259+
}
1260+
12381261
static PHP_METHOD(google_protobuf_Value, getNumberValue) {
12391262
Message* intern = (Message*)Z_OBJ_P(getThis());
12401263
const upb_FieldDef *f = upb_MessageDef_FindFieldByName(
@@ -1257,6 +1280,13 @@ static PHP_METHOD(google_protobuf_Value, setNumberValue) {
12571280
RETURN_COPY(getThis());
12581281
}
12591282

1283+
static PHP_METHOD(google_protobuf_Value, hasNumberValue) {
1284+
Message* intern = (Message*)Z_OBJ_P(getThis());
1285+
const upb_FieldDef *f = upb_MessageDef_FindFieldByName(
1286+
intern->desc->msgdef, "number_value");
1287+
RETVAL_BOOL(upb_Message_HasFieldByDef(intern->msg, f));
1288+
}
1289+
12601290
static PHP_METHOD(google_protobuf_Value, getStringValue) {
12611291
Message* intern = (Message*)Z_OBJ_P(getThis());
12621292
const upb_FieldDef *f = upb_MessageDef_FindFieldByName(
@@ -1279,6 +1309,13 @@ static PHP_METHOD(google_protobuf_Value, setStringValue) {
12791309
RETURN_COPY(getThis());
12801310
}
12811311

1312+
static PHP_METHOD(google_protobuf_Value, hasStringValue) {
1313+
Message* intern = (Message*)Z_OBJ_P(getThis());
1314+
const upb_FieldDef *f = upb_MessageDef_FindFieldByName(
1315+
intern->desc->msgdef, "string_value");
1316+
RETVAL_BOOL(upb_Message_HasFieldByDef(intern->msg, f));
1317+
}
1318+
12821319
static PHP_METHOD(google_protobuf_Value, getBoolValue) {
12831320
Message* intern = (Message*)Z_OBJ_P(getThis());
12841321
const upb_FieldDef *f = upb_MessageDef_FindFieldByName(
@@ -1301,6 +1338,13 @@ static PHP_METHOD(google_protobuf_Value, setBoolValue) {
13011338
RETURN_COPY(getThis());
13021339
}
13031340

1341+
static PHP_METHOD(google_protobuf_Value, hasBoolValue) {
1342+
Message* intern = (Message*)Z_OBJ_P(getThis());
1343+
const upb_FieldDef *f = upb_MessageDef_FindFieldByName(
1344+
intern->desc->msgdef, "bool_value");
1345+
RETVAL_BOOL(upb_Message_HasFieldByDef(intern->msg, f));
1346+
}
1347+
13041348
static PHP_METHOD(google_protobuf_Value, getStructValue) {
13051349
Message* intern = (Message*)Z_OBJ_P(getThis());
13061350
const upb_FieldDef *f = upb_MessageDef_FindFieldByName(
@@ -1323,6 +1367,13 @@ static PHP_METHOD(google_protobuf_Value, setStructValue) {
13231367
RETURN_COPY(getThis());
13241368
}
13251369

1370+
static PHP_METHOD(google_protobuf_Value, hasStructValue) {
1371+
Message* intern = (Message*)Z_OBJ_P(getThis());
1372+
const upb_FieldDef *f = upb_MessageDef_FindFieldByName(
1373+
intern->desc->msgdef, "struct_value");
1374+
RETVAL_BOOL(upb_Message_HasFieldByDef(intern->msg, f));
1375+
}
1376+
13261377
static PHP_METHOD(google_protobuf_Value, getListValue) {
13271378
Message* intern = (Message*)Z_OBJ_P(getThis());
13281379
const upb_FieldDef *f = upb_MessageDef_FindFieldByName(
@@ -1345,6 +1396,13 @@ static PHP_METHOD(google_protobuf_Value, setListValue) {
13451396
RETURN_COPY(getThis());
13461397
}
13471398

1399+
static PHP_METHOD(google_protobuf_Value, hasListValue) {
1400+
Message* intern = (Message*)Z_OBJ_P(getThis());
1401+
const upb_FieldDef *f = upb_MessageDef_FindFieldByName(
1402+
intern->desc->msgdef, "list_value");
1403+
RETVAL_BOOL(upb_Message_HasFieldByDef(intern->msg, f));
1404+
}
1405+
13481406
static PHP_METHOD(google_protobuf_Value, getKind) {
13491407
Message* intern = (Message*)Z_OBJ_P(getThis());
13501408
const upb_OneofDef *oneof = upb_MessageDef_FindOneofByName(
@@ -1357,16 +1415,22 @@ static zend_function_entry google_protobuf_Value_phpmethods[] = {
13571415
PHP_ME(google_protobuf_Value, __construct, arginfo_construct, ZEND_ACC_PUBLIC)
13581416
PHP_ME(google_protobuf_Value, getNullValue, arginfo_void, ZEND_ACC_PUBLIC)
13591417
PHP_ME(google_protobuf_Value, setNullValue, arginfo_setter, ZEND_ACC_PUBLIC)
1418+
PHP_ME(google_protobuf_Value, hasNullValue, arginfo_void, ZEND_ACC_PUBLIC)
13601419
PHP_ME(google_protobuf_Value, getNumberValue, arginfo_void, ZEND_ACC_PUBLIC)
13611420
PHP_ME(google_protobuf_Value, setNumberValue, arginfo_setter, ZEND_ACC_PUBLIC)
1421+
PHP_ME(google_protobuf_Value, hasNumberValue, arginfo_void, ZEND_ACC_PUBLIC)
13621422
PHP_ME(google_protobuf_Value, getStringValue, arginfo_void, ZEND_ACC_PUBLIC)
13631423
PHP_ME(google_protobuf_Value, setStringValue, arginfo_setter, ZEND_ACC_PUBLIC)
1424+
PHP_ME(google_protobuf_Value, hasStringValue, arginfo_void, ZEND_ACC_PUBLIC)
13641425
PHP_ME(google_protobuf_Value, getBoolValue, arginfo_void, ZEND_ACC_PUBLIC)
13651426
PHP_ME(google_protobuf_Value, setBoolValue, arginfo_setter, ZEND_ACC_PUBLIC)
1427+
PHP_ME(google_protobuf_Value, hasBoolValue, arginfo_void, ZEND_ACC_PUBLIC)
13661428
PHP_ME(google_protobuf_Value, getStructValue, arginfo_void, ZEND_ACC_PUBLIC)
13671429
PHP_ME(google_protobuf_Value, setStructValue, arginfo_setter, ZEND_ACC_PUBLIC)
1430+
PHP_ME(google_protobuf_Value, hasStructValue, arginfo_void, ZEND_ACC_PUBLIC)
13681431
PHP_ME(google_protobuf_Value, getListValue, arginfo_void, ZEND_ACC_PUBLIC)
13691432
PHP_ME(google_protobuf_Value, setListValue, arginfo_setter, ZEND_ACC_PUBLIC)
1433+
PHP_ME(google_protobuf_Value, hasListValue, arginfo_void, ZEND_ACC_PUBLIC)
13701434
PHP_ME(google_protobuf_Value, getKind, arginfo_void, ZEND_ACC_PUBLIC)
13711435
ZEND_FE_END
13721436
};
@@ -1836,6 +1900,13 @@ static PHP_METHOD(google_protobuf_Type, setSourceContext) {
18361900
RETURN_COPY(getThis());
18371901
}
18381902

1903+
static PHP_METHOD(google_protobuf_Type, hasSourceContext) {
1904+
Message* intern = (Message*)Z_OBJ_P(getThis());
1905+
const upb_FieldDef *f = upb_MessageDef_FindFieldByName(
1906+
intern->desc->msgdef, "source_context");
1907+
RETVAL_BOOL(upb_Message_HasFieldByDef(intern->msg, f));
1908+
}
1909+
18391910
static PHP_METHOD(google_protobuf_Type, getSyntax) {
18401911
Message* intern = (Message*)Z_OBJ_P(getThis());
18411912
const upb_FieldDef *f = upb_MessageDef_FindFieldByName(
@@ -1892,6 +1963,7 @@ static zend_function_entry google_protobuf_Type_phpmethods[] = {
18921963
PHP_ME(google_protobuf_Type, setOptions, arginfo_setter, ZEND_ACC_PUBLIC)
18931964
PHP_ME(google_protobuf_Type, getSourceContext, arginfo_void, ZEND_ACC_PUBLIC)
18941965
PHP_ME(google_protobuf_Type, setSourceContext, arginfo_setter, ZEND_ACC_PUBLIC)
1966+
PHP_ME(google_protobuf_Type, hasSourceContext, arginfo_void, ZEND_ACC_PUBLIC)
18951967
PHP_ME(google_protobuf_Type, getSyntax, arginfo_void, ZEND_ACC_PUBLIC)
18961968
PHP_ME(google_protobuf_Type, setSyntax, arginfo_setter, ZEND_ACC_PUBLIC)
18971969
PHP_ME(google_protobuf_Type, getEdition, arginfo_void, ZEND_ACC_PUBLIC)
@@ -2444,6 +2516,13 @@ static PHP_METHOD(google_protobuf_Enum, setSourceContext) {
24442516
RETURN_COPY(getThis());
24452517
}
24462518

2519+
static PHP_METHOD(google_protobuf_Enum, hasSourceContext) {
2520+
Message* intern = (Message*)Z_OBJ_P(getThis());
2521+
const upb_FieldDef *f = upb_MessageDef_FindFieldByName(
2522+
intern->desc->msgdef, "source_context");
2523+
RETVAL_BOOL(upb_Message_HasFieldByDef(intern->msg, f));
2524+
}
2525+
24472526
static PHP_METHOD(google_protobuf_Enum, getSyntax) {
24482527
Message* intern = (Message*)Z_OBJ_P(getThis());
24492528
const upb_FieldDef *f = upb_MessageDef_FindFieldByName(
@@ -2498,6 +2577,7 @@ static zend_function_entry google_protobuf_Enum_phpmethods[] = {
24982577
PHP_ME(google_protobuf_Enum, setOptions, arginfo_setter, ZEND_ACC_PUBLIC)
24992578
PHP_ME(google_protobuf_Enum, getSourceContext, arginfo_void, ZEND_ACC_PUBLIC)
25002579
PHP_ME(google_protobuf_Enum, setSourceContext, arginfo_setter, ZEND_ACC_PUBLIC)
2580+
PHP_ME(google_protobuf_Enum, hasSourceContext, arginfo_void, ZEND_ACC_PUBLIC)
25012581
PHP_ME(google_protobuf_Enum, getSyntax, arginfo_void, ZEND_ACC_PUBLIC)
25022582
PHP_ME(google_protobuf_Enum, setSyntax, arginfo_setter, ZEND_ACC_PUBLIC)
25032583
PHP_ME(google_protobuf_Enum, getEdition, arginfo_void, ZEND_ACC_PUBLIC)
@@ -2668,12 +2748,20 @@ static PHP_METHOD(google_protobuf_Option, setValue) {
26682748
RETURN_COPY(getThis());
26692749
}
26702750

2751+
static PHP_METHOD(google_protobuf_Option, hasValue) {
2752+
Message* intern = (Message*)Z_OBJ_P(getThis());
2753+
const upb_FieldDef *f = upb_MessageDef_FindFieldByName(
2754+
intern->desc->msgdef, "value");
2755+
RETVAL_BOOL(upb_Message_HasFieldByDef(intern->msg, f));
2756+
}
2757+
26712758
static zend_function_entry google_protobuf_Option_phpmethods[] = {
26722759
PHP_ME(google_protobuf_Option, __construct, arginfo_construct, ZEND_ACC_PUBLIC)
26732760
PHP_ME(google_protobuf_Option, getName, arginfo_void, ZEND_ACC_PUBLIC)
26742761
PHP_ME(google_protobuf_Option, setName, arginfo_setter, ZEND_ACC_PUBLIC)
26752762
PHP_ME(google_protobuf_Option, getValue, arginfo_void, ZEND_ACC_PUBLIC)
26762763
PHP_ME(google_protobuf_Option, setValue, arginfo_setter, ZEND_ACC_PUBLIC)
2764+
PHP_ME(google_protobuf_Option, hasValue, arginfo_void, ZEND_ACC_PUBLIC)
26772765
ZEND_FE_END
26782766
};
26792767

php/tests/WellKnownTest.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,29 +270,63 @@ public function testStruct()
270270

271271
$m = new Value();
272272

273-
$this->assertNull($m->getStructValue());
273+
# null_value
274+
$this->assertFalse($m->hasNullValue());
274275

275276
$m->setNullValue(NullValue::NULL_VALUE);
276-
$this->assertSame(NullValue::NULL_VALUE, $m->getNullValue());
277+
278+
$this->assertTrue($m->hasNullValue());
277279
$this->assertSame("null_value", $m->getKind());
280+
$this->assertSame(NullValue::NULL_VALUE, $m->getNullValue());
281+
282+
# number_value
283+
$this->assertFalse($m->hasNumberValue());
278284

279285
$m->setNumberValue(1.0);
286+
287+
$this->assertTrue($m->hasNumberValue());
280288
$this->assertSame(1.0, $m->getNumberValue());
281289
$this->assertSame("number_value", $m->getKind());
282290

291+
# string_value
292+
$this->assertFalse($m->hasStringValue());
293+
283294
$m->setStringValue("a");
295+
296+
$this->assertTrue($m->hasStringValue());
297+
284298
$this->assertSame("a", $m->getStringValue());
285299
$this->assertSame("string_value", $m->getKind());
286300

301+
# bool_value
302+
$this->assertFalse($m->hasBoolValue());
303+
287304
$m->setBoolValue(true);
305+
306+
$this->assertTrue($m->hasBoolValue());
307+
288308
$this->assertSame(true, $m->getBoolValue());
289309
$this->assertSame("bool_value", $m->getKind());
290310

311+
312+
# struct_value
313+
$this->assertNull($m->getStructValue());
314+
$this->assertFalse($m->hasStructValue());
315+
291316
$m->setStructValue(new Struct());
317+
318+
$this->assertTrue($m->hasStructValue());
319+
292320
$this->assertFalse(is_null($m->getStructValue()));
293321
$this->assertSame("struct_value", $m->getKind());
294322

323+
# list_value
324+
$this->assertFalse($m->hasListValue());
325+
295326
$m->setListValue(new ListValue());
327+
328+
$this->assertTrue($m->hasListValue());
329+
296330
$this->assertFalse(is_null($m->getListValue()));
297331
$this->assertSame("list_value", $m->getKind());
298332

src/google/protobuf/compiler/php/php_generator.cc

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,6 +1767,8 @@ void GenerateCMessage(const Descriptor* message, io::Printer* printer) {
17671767

17681768
for (int i = 0; i < message->field_count(); i++) {
17691769
auto field = message->field(i);
1770+
auto camel_name = UnderscoresToCamelCase(field->name(), true);
1771+
17701772
printer->Print(
17711773
"static PHP_METHOD($c_name$, get$camel_name$) {\n"
17721774
" Message* intern = (Message*)Z_OBJ_P(getThis());\n"
@@ -1790,8 +1792,19 @@ void GenerateCMessage(const Descriptor* message, io::Printer* printer) {
17901792
" RETURN_COPY(getThis());\n"
17911793
"}\n"
17921794
"\n",
1793-
"c_name", c_name, "name", field->name(), "camel_name",
1794-
UnderscoresToCamelCase(field->name(), true));
1795+
"c_name", c_name, "name", field->name(), "camel_name", camel_name);
1796+
1797+
if (field->has_presence()) {
1798+
printer->Print(
1799+
"static PHP_METHOD($c_name$, has$camel_name$) {\n"
1800+
" Message* intern = (Message*)Z_OBJ_P(getThis());\n"
1801+
" const upb_FieldDef *f = upb_MessageDef_FindFieldByName(\n"
1802+
" intern->desc->msgdef, \"$name$\");\n"
1803+
" RETVAL_BOOL(upb_Message_HasFieldByDef(intern->msg, f));\n"
1804+
"}\n"
1805+
"\n",
1806+
"c_name", c_name, "name", field->name(), "camel_name", camel_name);
1807+
}
17951808
}
17961809

17971810
for (int i = 0; i < message->real_oneof_decl_count(); i++) {
@@ -1835,12 +1848,20 @@ void GenerateCMessage(const Descriptor* message, io::Printer* printer) {
18351848

18361849
for (int i = 0; i < message->field_count(); i++) {
18371850
auto field = message->field(i);
1851+
auto camel_name = UnderscoresToCamelCase(field->name(), true);
1852+
18381853
printer->Print(
18391854
" PHP_ME($c_name$, get$camel_name$, arginfo_void, ZEND_ACC_PUBLIC)\n"
18401855
" PHP_ME($c_name$, set$camel_name$, arginfo_setter, "
18411856
"ZEND_ACC_PUBLIC)\n",
1842-
"c_name", c_name, "camel_name",
1843-
UnderscoresToCamelCase(field->name(), true));
1857+
"c_name", c_name, "camel_name", camel_name);
1858+
1859+
if (field->has_presence()) {
1860+
printer->Print(
1861+
" PHP_ME($c_name$, has$camel_name$, arginfo_void, "
1862+
"ZEND_ACC_PUBLIC)\n",
1863+
"c_name", c_name, "camel_name", camel_name);
1864+
}
18441865
}
18451866

18461867
for (int i = 0; i < message->real_oneof_decl_count(); i++) {

0 commit comments

Comments
 (0)