Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1229 Allow return null in kafka Serializer (delete messages aka Tombstones) #1230

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions pkg/rdkafka/JsonSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,30 @@

class JsonSerializer implements Serializer
{
public function toString(RdKafkaMessage $message): string
public function toString(RdKafkaMessage $message): ?string
Copy link
Contributor

@Steveb-p Steveb-p Jan 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not true, and it would not match with the Stringable interface that is part of PHP8.

See https://www.php.net/manual/en/class.stringable.php.

Suggested change
public function toString(RdKafkaMessage $message): ?string
public function toString(RdKafkaMessage $message): string

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But Serializer does not extend Stringable?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And Stringable defines interface public __toString(): string method with two underscores in name, not toString

{
$json = json_encode([
'body' => $message->getBody(),
'properties' => $message->getProperties(),
'headers' => $message->getHeaders(),
]);

if (JSON_ERROR_NONE !== json_last_error()) {
throw new \InvalidArgumentException(sprintf(
'The malformed json given. Error %s and message %s',
json_last_error(),
json_last_error_msg()
));
if (\JSON_ERROR_NONE !== json_last_error()) {
throw new \InvalidArgumentException(sprintf('The malformed json given. Error %s and message %s', json_last_error(), json_last_error_msg()));
}

return $json;
}

public function toMessage(string $string): RdKafkaMessage
public function toMessage(?string $string): RdKafkaMessage
{
if (null === $string) {
return new RdKafkaMessage(null, null, null);
}

$data = json_decode($string, true);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new \InvalidArgumentException(sprintf(
'The malformed json given. Error %s and message %s',
json_last_error(),
json_last_error_msg()
));
if (\JSON_ERROR_NONE !== json_last_error()) {
throw new \InvalidArgumentException(sprintf('The malformed json given. Error %s and message %s', json_last_error(), json_last_error_msg()));
}

return new RdKafkaMessage($data['body'], $data['properties'], $data['headers']);
Expand Down
4 changes: 2 additions & 2 deletions pkg/rdkafka/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

interface Serializer
{
public function toString(RdKafkaMessage $message): string;
public function toString(RdKafkaMessage $message): ?string;
Steveb-p marked this conversation as resolved.
Show resolved Hide resolved

public function toMessage(string $string): RdKafkaMessage;
public function toMessage(?string $string): RdKafkaMessage;
}