Skip to content

Commit

Permalink
Modify the parsing logic for the Serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Hectorhammett committed Jan 16, 2025
1 parent d58b5eb commit bbedbf6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
12 changes: 6 additions & 6 deletions src/ApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,15 @@ public function getProtobufErrors()
*/
public static function createFromStdClass(stdClass $status)
{
$unserializedErrors = [];
$metadata = property_exists($status, 'metadata') ? $status->metadata : null;
$decodedMessages = Serializer::decodeMetadata((array) $metadata);

return self::create(
$status->details,
$status->code,
$metadata,
$decodedMessages['serialized'],
$decodedMessages['unserialized']
Serializer::decodeMetadata((array) $metadata, $unserializedErrors),
$unserializedErrors
);
}

Expand All @@ -181,13 +181,13 @@ public static function createFromApiResponse(
?array $metadata = null,
?Exception $previous = null
) {
$decodedMessages = Serializer::decodeMetadata((array) $metadata);
$unserializedErrors = [];
return self::create(
$basicMessage,
$rpcCode,
$metadata,
$decodedMessages['serialized'],
$decodedMessages['unserialized'],
Serializer::decodeMetadata((array) $metadata, $unserializedErrors),
$unserializedErrors,
$previous
);
}
Expand Down
31 changes: 12 additions & 19 deletions src/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ class Serializer
'google.rpc.errorinfo-bin' => \Google\Rpc\ErrorInfo::class,
'google.rpc.help-bin' => \Google\Rpc\Help::class,
'google.rpc.localizedmessage-bin' => \Google\Rpc\LocalizedMessage::class,
];

private static $restKnownTypes = [
'type.googleapis.com/google.rpc.RetryInfo' => \Google\Rpc\RetryInfo::class,
'type.googleapis.com/google.rpc.DebugInfo' => \Google\Rpc\DebugInfo::class,
'type.googleapis.com/google.rpc.QuotaFailure' => \Google\Rpc\QuotaFailure::class,
Expand Down Expand Up @@ -156,11 +153,11 @@ public static function encodeMetadataToProtobufErrors(array $metadata): array
$message = null;
$type = $error['@type'];

if (!isset(self::$restKnownTypes[$type])) {
if (!isset(self::$metadataKnownTypes[$type])) {
continue;
}

$class = self::$restKnownTypes[$type];
$class = self::$metadataKnownTypes[$type];
$message = new $class;
$jsonMessage = json_encode(array_diff($error, ['@type' => $error['@type']]));
$message->mergeFromJsonString($jsonMessage);
Expand Down Expand Up @@ -218,31 +215,32 @@ public static function serializeToPhpArray(Message $message)
* Decode metadata received from gRPC status object
*
* @param array $metadata
* @param array $unserializedErrors
* @return array
*/
public static function decodeMetadata(array $metadata)
public static function decodeMetadata(array $metadata, array &$unserializedErrors = null)
{
if (count($metadata) == 0) {
return [];
}
$result = [
'serialized' => [],
'unserialized' => []
];
$result = [];
foreach ($metadata as $key => $values) {
foreach ($values as $value) {
$decodedValue = [
'@type' => $key,
];
/** @var Message $message */
$message = null;
if (self::hasBinaryHeaderSuffix($key)) {
if (isset(self::$metadataKnownTypes[$key])) {
$class = self::$metadataKnownTypes[$key];
/** @var Message $message */
$message = new $class();
try {
$message->mergeFromString($value);
$decodedValue += self::serializeToPhpArray($message);

if (!is_null($unserializedErrors)) {
$unserializedErrors[] = $message;
}
} catch (\Exception $e) {
// We encountered an error trying to deserialize the data
$decodedValue += [
Expand All @@ -251,21 +249,16 @@ public static function decodeMetadata(array $metadata)
}
} else {
// The metadata contains an unexpected binary type
$unknownDataMessage = '<Unknown Binary Data>';
$decodedValue += [
'data' => $unknownDataMessage,
'data' => '<Unknown Binary Data>',
];
$message = null;
}
} else {
$decodedValue += [
'data' => $value,
];
}
$result['serialized'][] = $decodedValue;
if (!is_null($message)) {
$result['unserialized'][] = $message;
}
$result[] = $decodedValue;
}
}
return $result;
Expand Down

0 comments on commit bbedbf6

Please sign in to comment.