-
Notifications
You must be signed in to change notification settings - Fork 171
feat: implement CloudEvent down Conversion & Adds Tests #262
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
Conversation
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
Should be ready to review @mtraver. Pardon the large PR. |
.github/workflows/conformance.yml
Outdated
with: | ||
functionType: 'http' | ||
useBuildpacks: false |
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.
It seems like this should be false
by default, no? Any reason to add it in this PR?
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.
Yeah, it's not needed. We can remove.
package.json
Outdated
"check": "gts check", | ||
"clean": "gts clean", | ||
"compile": "tsc -p .", | ||
"watch": "tsc -p . -w", |
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.
maybe DRY this up with:
"watch": "npm run compile -- --watch",
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.
Thanks!
return ce; | ||
} else { | ||
// Structured event. Just return the event, which is stored in the HTTP body. | ||
return req.body; |
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.
how does this pass the typechecker right now?
* @return True if the request is a CloudEvents event in structured content mode, | ||
* false otherwise. | ||
*/ | ||
export function isStructuredCloudEvent(req: express.Request): boolean { |
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.
Is there an opportunity to use a type predicate here? Something like:
interface StructuredCloudEventRequest extends express.Request {
body: CloudEventsContext;
}
export function isStructuredCloudEvent(req: express.Request) req is StructuredCloudEventRequest
@@ -0,0 +1,113 @@ | |||
// Copyright 2021 Google LLC |
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.
as above: test/integration/legacyEvents.ts
?
// TEST_CLOUDEVENT_STRUCTURED, | ||
// } from './data/testHTTPData'; | ||
|
||
describe('EventConverter: CE -> Legacy', () => { |
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.
as below, if you programatically skip, you don't have comment out the imports:
describe.skip('EventConverter: CE -> Legacy', () => {
const ce: CloudEventsContext = { | ||
specversion: req.header('ce-specversion'), | ||
type: req.header('ce-type'), | ||
source: req.header('ce-source'), | ||
subject: req.header('ce-subject'), | ||
id: req.header('ce-id'), | ||
time: req.header('ce-time'), | ||
datacontenttype: req.header('ce-datacontenttype'), | ||
data: req.body, | ||
}; | ||
// Remove undefined keys from CE object | ||
Object.keys(ce).forEach((key: string) => | ||
ce[key] === undefined ? delete ce[key] : {} | ||
); | ||
return ce; |
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 might write this a bit differently. Rather than going through and deleting undefined keys, just don't set them in the first place:
const ce: CloudEventsContext = {
data: req.body,
};
[
'specversion',
'type',
'source',
'subject',
'id',
'time',
'datacontenttype',
].forEach((key) => {
if (req.header(`ce-${key}`) !== undefined) {
ce[key] = req.header(`ce-${key}`);
}
}
even better would be to move that array to a constant somewhere
/** | ||
* Returns a CloudEvents context from the given CloudEvents request. Context | ||
* attributes are retrieved from request headers. | ||
* | ||
* @param req Express request object. | ||
* @return CloudEvents context. | ||
*/ | ||
export function getBinaryCloudEventContext( | ||
export function getStructuredCloudEventContext( |
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 am a bit confused about how this function is different from convertRequestToStructuredCE
// Set data and context normally | ||
data = req.body.data; | ||
context = req.body.context; |
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.
Looks like these variables are already initialized to exactly these values on line 186/187. Can we just remove the else block here or remove the initial values above?
} else { | ||
// If a really legacy event, convert it to a normal event. | ||
// See the tests for more details. | ||
if (!context) { |
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: flatten to } else if (!context) {
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.
Formally requesting changes because I am sure if you saw my review.
Thanks. I'll have to apply the comments and likely break this PR into some smaller PRs. |
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
I've pulled out a few changes into separate PRs. I think it will make things cleaner and safer: |
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
acb2053
to
b378ccf
Compare
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
Going to close this PR. Other PRs will be able to complete the conversions. Feel free to ping me if there are questions. |
Refactors event conversion and implements partial event conversion.
Test Output
(Reference) Previous Test Output