-
Notifications
You must be signed in to change notification settings - Fork 37
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
refactor: add class that wraps CloudEvent into CloudEventInterface #113
Changes from all commits
20d6068
c3635fa
4c6da21
05abd02
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2020 Google LLC. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Google\CloudFunctions; | ||
|
||
use BadMethodCallException; | ||
use JsonSerializable; | ||
use CloudEvents\V1\CloudEventInterface; | ||
use DateTimeImmutable; | ||
use DateTimeInterface; | ||
|
||
/** | ||
* @internal | ||
* Wraps a Google\CloudFunctions\CloudEvent to comply with | ||
* CloudEvents\V1\CloudEventInterface. | ||
*/ | ||
class CloudEventSdkCompliant implements JsonSerializable, CloudEventInterface | ||
{ | ||
private $cloudevent; | ||
|
||
public function __construct( | ||
CloudEvent $cloudevent | ||
) { | ||
$this->cloudevent = $cloudevent; | ||
} | ||
|
||
public function getId(): string | ||
{ | ||
return $this->cloudevent->getId(); | ||
} | ||
public function getSource(): string | ||
{ | ||
return $this->cloudevent->getSource(); | ||
} | ||
public function getSpecVersion(): string | ||
{ | ||
return $this->cloudevent->getSpecVersion(); | ||
} | ||
public function getType(): string | ||
{ | ||
return $this->cloudevent->getType(); | ||
} | ||
public function getDataContentType(): ?string | ||
{ | ||
return $this->cloudevent->getDataContentType(); | ||
} | ||
public function getDataSchema(): ?string | ||
{ | ||
return $this->cloudevent->getDataSchema(); | ||
} | ||
public function getSubject(): ?string | ||
{ | ||
return $this->cloudevent->getSubject(); | ||
} | ||
public function getTime(): ?DateTimeImmutable | ||
{ | ||
return DateTimeImmutable::createFromFormat(DateTimeInterface::RFC3339_EXTENDED, $this->cloudevent->getTime()); | ||
} | ||
public function getExtension(string $attribute) | ||
{ | ||
throw new BadMethodCallException('getExtension() is not currently supported by Functions Framework PHP'); | ||
} | ||
public function getExtensions(): array | ||
{ | ||
throw new BadMethodCallException('getExtensions() is not currently supported by Functions Framework PHP'); | ||
} | ||
/** | ||
* @return mixed | ||
*/ | ||
public function getData() | ||
{ | ||
return $this->cloudevent->getData(); | ||
} | ||
|
||
public function jsonSerialize() | ||
{ | ||
return $this->cloudevent->jsonSerialize(); | ||
} | ||
|
||
public function __toString() | ||
{ | ||
return $this->cloudevent->__toString(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2021 Google LLC. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Google\CloudFunctions\Tests; | ||
|
||
use BadMethodCallException; | ||
use Google\CloudFunctions\CloudEvent; | ||
use Google\CloudFunctions\CloudEventSdkCompliant; | ||
use PHPUnit\Framework\TestCase; | ||
use DateTimeImmutable; | ||
use DateTimeInterface; | ||
|
||
/** | ||
* @group gcf-framework | ||
*/ | ||
class CloudEventSdkCompliantTest extends TestCase | ||
{ | ||
private CloudEvent $cloudevent; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->cloudevent = new CloudEvent( | ||
'1413058901901494', | ||
'//pubsub.googleapis.com/projects/MY-PROJECT/topics/MY-TOPIC', | ||
'1.0', | ||
'com.google.cloud.pubsub.topic.publish', | ||
'application/json', | ||
'type.googleapis.com/google.logging.v2.LogEntry', | ||
'My Subject', | ||
'2020-12-08T20:03:19.162Z', | ||
[ | ||
"message" => [ | ||
"data" => "SGVsbG8gdGhlcmU=", | ||
"messageId" => "1408577928008405", | ||
"publishTime" => "2020-08-06T22:31:14.536Z" | ||
], | ||
"subscription" => "projects/MY-PROJECT/subscriptions/MY-SUB" | ||
] | ||
); | ||
Comment on lines
+37
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than using the constructor, which is pretty fragile given the number of arguments (and is not copy-pastable), could we deserialized a JSON payload?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The CloudEvents SDK can't parse this payload due to a bug. I filed a bug on them: cloudevents/sdk-php#35 Until they fix it, the best we can do is provide the same interface. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add a TODO, or do you think using this constructor is fine? |
||
} | ||
|
||
public function testJsonSerialize(): void | ||
{ | ||
$wrappedEvent = new CloudEventSdkCompliant($this->cloudevent); | ||
|
||
$want = '{ | ||
"id": "1413058901901494", | ||
"source": "\/\/pubsub.googleapis.com\/projects\/MY-PROJECT\/topics\/MY-TOPIC", | ||
"specversion": "1.0", | ||
"type": "com.google.cloud.pubsub.topic.publish", | ||
"datacontenttype": "application\/json", | ||
"dataschema": "type.googleapis.com\/google.logging.v2.LogEntry", | ||
"subject": "My Subject", | ||
"time": "2020-12-08T20:03:19.162Z", | ||
"data": { | ||
"message": { | ||
"data": "SGVsbG8gdGhlcmU=", | ||
"messageId": "1408577928008405", | ||
"publishTime": "2020-08-06T22:31:14.536Z" | ||
}, | ||
"subscription": "projects\\/MY-PROJECT\\/subscriptions\\/MY-SUB" | ||
} | ||
}'; | ||
|
||
$this->assertSame($want, json_encode($wrappedEvent, JSON_PRETTY_PRINT)); | ||
} | ||
|
||
public function testWrapsCloudEvent(): void | ||
{ | ||
$wrappedEvent = new CloudEventSdkCompliant($this->cloudevent); | ||
|
||
$this->assertSame($this->cloudevent->getId(), $wrappedEvent->getId()); | ||
$this->assertSame($this->cloudevent->getSource(), $wrappedEvent->getSource()); | ||
$this->assertSame($this->cloudevent->getType(), $wrappedEvent->getType()); | ||
$this->assertSame($this->cloudevent->getData(), $wrappedEvent->getData()); | ||
$this->assertSame($this->cloudevent->getDataContentType(), $wrappedEvent->getDataContentType()); | ||
$this->assertSame($this->cloudevent->getDataSchema(), $wrappedEvent->getDataSchema()); | ||
$this->assertSame($this->cloudevent->getSubject(), $wrappedEvent->getSubject()); | ||
$this->assertEquals(DateTimeImmutable::createFromFormat(DateTimeInterface::RFC3339_EXTENDED, $this->cloudevent->getTime()), $wrappedEvent->getTime()); | ||
} | ||
|
||
public function testUnimplementedGetExtensionThrowsError(): void | ||
{ | ||
$wrappedEvent = new CloudEventSdkCompliant($this->cloudevent); | ||
$this->expectException(BadMethodCallException::class); | ||
|
||
$wrappedEvent->getExtension('attribute'); | ||
} | ||
|
||
public function testUnimplementedGetExtensionsThrowsError(): void | ||
{ | ||
$wrappedEvent = new CloudEventSdkCompliant($this->cloudevent); | ||
$this->expectException(BadMethodCallException::class); | ||
|
||
$wrappedEvent->getExtensions(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: newline spacing is inconsistent with these functions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how this is going to be used.
Do we want to implement
CloudEventImmutable
in addition toCloudEventInterface
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's the getters don't have newlines in between them, but other functions do.
CloudEventInterface
is going to be what's passed to CloudEvent Functions with the new declarative function signatures. This is similar to howServerRequestInterface
is passed to HTTP functions today.CloudEventImmutable
is not an interface, so we can't implement it. I don't think we would benefit from inheriting or using it within this class, since we are not using any other part of the CloudEvents SDK today that can serialize/deserialize toCloudEvent
orCloudEventImmutable
.Using the
CloudEventInterface
will put us a step in the right direction, because it means we can swap our implementation to use the official CloudEvent SDK in the future, without rewiring everything all at once.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's the next PR queue'd up which makes use of this new class, you can see the new function stub in this test:
https://github.com/anniefu/functions-framework-php/pull/1/files#diff-479b688739465688c5b60552045cb322a656e9bd47a2b9b12a05f8c4f76d3bd6R74