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

deserialize sqs message attributes #901

Merged
merged 1 commit into from
Jun 14, 2019
Merged
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
4 changes: 4 additions & 0 deletions pkg/sqs/SqsConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ protected function convertMessage(array $sqsMessage): SqsMessage
$message->setBody($sqsMessage['Body']);
$message->setReceiptHandle($sqsMessage['ReceiptHandle']);

if (isset($sqsMessage['Attributes'])) {
$message->setAttributes($sqsMessage['Attributes']);
}

if (isset($sqsMessage['Attributes']['ApproximateReceiveCount'])) {
$message->setRedelivered(((int) $sqsMessage['Attributes']['ApproximateReceiveCount']) > 1);
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/sqs/SqsMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class SqsMessage implements Message
*/
private $headers;

/**
* @var array
*/
private $attributes;

/**
* @var bool
*/
Expand Down Expand Up @@ -58,6 +63,7 @@ public function __construct(string $body = '', array $properties = [], array $he
$this->body = $body;
$this->properties = $properties;
$this->headers = $headers;
$this->attributes = [];
$this->redelivered = false;
$this->delaySeconds = 0;
$this->requeueVisibilityTimeout = 0;
Expand Down Expand Up @@ -113,6 +119,21 @@ public function getHeader(string $name, $default = null)
return array_key_exists($name, $this->headers) ? $this->headers[$name] : $default;
}

public function setAttributes(array $attributes): void
{
$this->attributes = $attributes;
}

public function getAttributes(): array
{
return $this->attributes;
}

public function getAttribute(string $name, $default = null)
{
return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default;
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't isset be sufficient? It is said it's a lot faster than array_key_exists with the difference that it will report null values as not set.

https://ilia.ws/archives/247-Performance-Analysis-of-isset-vs-array_key_exists.html
http://thinkofdev.com/php-fast-way-to-determine-a-key-elements-existance-in-an-array/

At least it was in 2012, but I don't think this changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure there are better ways now, return $this->attributes[$name] ?? $default, but i was copying the current style of the code base.

}

public function isRedelivered(): bool
{
return $this->redelivered;
Expand Down
11 changes: 10 additions & 1 deletion pkg/sqs/Tests/SqsConsumerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,10 @@ public function testShouldReceiveMessage()
'Body' => 'The Body',
'ReceiptHandle' => 'The Receipt',
'Attributes' => [
'ApproximateReceiveCount' => 3,
'SenderId' => 'AROAX5IAWYILCTYIS3OZ5:[email protected]',
'ApproximateFirstReceiveTimestamp' => '1560512269481',
'ApproximateReceiveCount' => '3',
'SentTimestamp' => '1560512260079',
],
'MessageAttributes' => [
'Headers' => [
Expand Down Expand Up @@ -336,6 +339,12 @@ public function testShouldReceiveMessage()
$this->assertEquals('The Body', $result->getBody());
$this->assertEquals(['hkey' => 'hvalue'], $result->getHeaders());
$this->assertEquals(['key' => 'value'], $result->getProperties());
$this->assertEquals([
'SenderId' => 'AROAX5IAWYILCTYIS3OZ5:[email protected]',
'ApproximateFirstReceiveTimestamp' => '1560512269481',
'ApproximateReceiveCount' => '3',
'SentTimestamp' => '1560512260079',
], $result->getAttributes());
$this->assertTrue($result->isRedelivered());
$this->assertEquals('The Receipt', $result->getReceiptHandle());
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/sqs/Tests/SqsMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function testCouldBeConstructedWithoutArguments()
$this->assertSame('', $message->getBody());
$this->assertSame([], $message->getProperties());
$this->assertSame([], $message->getHeaders());
$this->assertSame([], $message->getAttributes());
}

public function testCouldBeConstructedWithOptionalArguments()
Expand Down Expand Up @@ -90,4 +91,18 @@ public function testShouldAllowGetReceiptHandle()

$this->assertSame('theId', $message->getReceiptHandle());
}

public function testShouldAllowSettingAndGettingAttributes()
{
$message = new SqsMessage();
$message->setAttributes($attributes = [
'SenderId' => 'AROAX5IAWYILCTYIS3OZ5:[email protected]',
'ApproximateFirstReceiveTimestamp' => '1560512269481',
'ApproximateReceiveCount' => '2',
'SentTimestamp' => '1560512260079',
]);

$this->assertSame($attributes, $message->getAttributes());
$this->assertSame($attributes['SenderId'], $message->getAttribute('SenderId'));
}
}