Skip to content

Commit 59d5006

Browse files
bshaffercopybara-github
authored andcommitted
feat: better debug info for PHP messages and repeated fields (#12718)
addresses #12714 by dumping more concise debug info for protobuf messages and repeated fields via the `serializeToJsonString` function. Additionally, message types which serialize into something other than an array (e.g. `Google\Protobuf\Value`, `Google\Protobuf\Timestamp`, etc) are handled in a special way to make their output consistent with other messages. ```php $m = new Google\Protobuf\DoubleValue(); $m->setValue(1.5); var_dump($m); ``` will output ``` object(Google\Protobuf\DoubleValue)#12 (1) { ["value"]=> float(1.5) } ``` Closes #12718 COPYBARA_INTEGRATE_REVIEW=#12718 from bshaffer:php-add-debuginfo c40a6f9 PiperOrigin-RevId: 574115431
1 parent d792703 commit 59d5006

File tree

3 files changed

+662
-0
lines changed

3 files changed

+662
-0
lines changed

php/src/Google/Protobuf/Internal/Message.php

+52
Original file line numberDiff line numberDiff line change
@@ -2014,4 +2014,56 @@ public function jsonByteSize()
20142014
}
20152015
return $size;
20162016
}
2017+
2018+
public function __debugInfo()
2019+
{
2020+
if (is_a($this, 'Google\Protobuf\FieldMask')) {
2021+
return ['paths' => $this->getPaths()->__debugInfo()];
2022+
}
2023+
2024+
if (is_a($this, 'Google\Protobuf\Value')) {
2025+
switch ($this->getKind()) {
2026+
case 'null_value':
2027+
return ['nullValue' => $this->getNullValue()];
2028+
case 'number_value':
2029+
return ['numberValue' => $this->getNumberValue()];
2030+
case 'string_value':
2031+
return ['stringValue' => $this->getStringValue()];
2032+
case 'bool_value':
2033+
return ['boolValue' => $this->getBoolValue()];
2034+
case 'struct_value':
2035+
return ['structValue' => $this->getStructValue()->__debugInfo()];
2036+
case 'list_value':
2037+
return ['listValue' => $this->getListValue()->__debugInfo()];
2038+
}
2039+
return [];
2040+
}
2041+
2042+
if (is_a($this, 'Google\Protobuf\BoolValue')
2043+
|| is_a($this, 'Google\Protobuf\BytesValue')
2044+
|| is_a($this, 'Google\Protobuf\DoubleValue')
2045+
|| is_a($this, 'Google\Protobuf\FloatValue')
2046+
|| is_a($this, 'Google\Protobuf\StringValue')
2047+
|| is_a($this, 'Google\Protobuf\Int32Value')
2048+
|| is_a($this, 'Google\Protobuf\Int64Value')
2049+
|| is_a($this, 'Google\Protobuf\UInt32Value')
2050+
|| is_a($this, 'Google\Protobuf\UInt64Value')
2051+
) {
2052+
return [
2053+
'value' => json_decode($this->serializeToJsonString(), true),
2054+
];
2055+
}
2056+
2057+
if (
2058+
is_a($this, 'Google\Protobuf\Duration')
2059+
|| is_a($this, 'Google\Protobuf\Timestamp')
2060+
) {
2061+
return [
2062+
'seconds' => $this->getSeconds(),
2063+
'nanos' => $this->getNanos(),
2064+
];
2065+
}
2066+
2067+
return json_decode($this->serializeToJsonString(), true);
2068+
}
20172069
}

php/src/Google/Protobuf/Internal/RepeatedField.php

+13
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,17 @@ public function count(): int
238238
{
239239
return count($this->container);
240240
}
241+
242+
public function __debugInfo()
243+
{
244+
return array_map(
245+
function ($item) {
246+
if ($item instanceof Message || $item instanceof RepeatedField) {
247+
return $item->__debugInfo();
248+
}
249+
return $item;
250+
},
251+
iterator_to_array($this)
252+
);
253+
}
241254
}

0 commit comments

Comments
 (0)