Skip to content

Commit

Permalink
feat: better debug info for PHP messages and repeated fields (#12718)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bshaffer authored and copybara-github committed Oct 17, 2023
1 parent d792703 commit 59d5006
Show file tree
Hide file tree
Showing 3 changed files with 662 additions and 0 deletions.
52 changes: 52 additions & 0 deletions php/src/Google/Protobuf/Internal/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -2014,4 +2014,56 @@ public function jsonByteSize()
}
return $size;
}

public function __debugInfo()
{
if (is_a($this, 'Google\Protobuf\FieldMask')) {
return ['paths' => $this->getPaths()->__debugInfo()];
}

if (is_a($this, 'Google\Protobuf\Value')) {
switch ($this->getKind()) {
case 'null_value':
return ['nullValue' => $this->getNullValue()];
case 'number_value':
return ['numberValue' => $this->getNumberValue()];
case 'string_value':
return ['stringValue' => $this->getStringValue()];
case 'bool_value':
return ['boolValue' => $this->getBoolValue()];
case 'struct_value':
return ['structValue' => $this->getStructValue()->__debugInfo()];
case 'list_value':
return ['listValue' => $this->getListValue()->__debugInfo()];
}
return [];
}

if (is_a($this, 'Google\Protobuf\BoolValue')
|| is_a($this, 'Google\Protobuf\BytesValue')
|| is_a($this, 'Google\Protobuf\DoubleValue')
|| is_a($this, 'Google\Protobuf\FloatValue')
|| is_a($this, 'Google\Protobuf\StringValue')
|| is_a($this, 'Google\Protobuf\Int32Value')
|| is_a($this, 'Google\Protobuf\Int64Value')
|| is_a($this, 'Google\Protobuf\UInt32Value')
|| is_a($this, 'Google\Protobuf\UInt64Value')
) {
return [
'value' => json_decode($this->serializeToJsonString(), true),
];
}

if (
is_a($this, 'Google\Protobuf\Duration')
|| is_a($this, 'Google\Protobuf\Timestamp')
) {
return [
'seconds' => $this->getSeconds(),
'nanos' => $this->getNanos(),
];
}

return json_decode($this->serializeToJsonString(), true);
}
}
13 changes: 13 additions & 0 deletions php/src/Google/Protobuf/Internal/RepeatedField.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,17 @@ public function count(): int
{
return count($this->container);
}

public function __debugInfo()
{
return array_map(
function ($item) {
if ($item instanceof Message || $item instanceof RepeatedField) {
return $item->__debugInfo();
}
return $item;
},
iterator_to_array($this)
);
}
}
Loading

0 comments on commit 59d5006

Please sign in to comment.