-
Notifications
You must be signed in to change notification settings - Fork 4k
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
aws-cdk-events: jsonTemplate versus textTemplate (take 2) #27
Changes from all commits
0016b96
7a97742
b202880
93aaf3c
77f914a
493bea9
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 |
---|---|---|
|
@@ -18,13 +18,13 @@ const repository = new Repository(stack, 'CodeCommitRepo', { repositoryName: 'fo | |
const project = new BuildProject(stack, 'BuildProject', { source: new CodePipelineSource() }); | ||
|
||
const sourceAction = new CodeCommitSource(sourceStage, 'CodeCommitSource', { artifactName: 'Source', repository }); | ||
new CodeBuildAction(buildStage, 'CodeBuildAction', { source: sourceAction, project }); | ||
new CodeBuildAction(buildStage, 'CodeBuildAction', { inputArtifact: sourceAction.artifact, project }); | ||
|
||
const topic = new Topic(stack, 'MyTopic'); | ||
topic.subscribeEmail('benisrae', '[email protected]'); | ||
|
||
pipeline.onStateChange('OnPipelineStateChange').addTarget(topic, { | ||
template: 'Pipeline <pipeline> changed state to <state>', | ||
textTemplate: 'Pipeline <pipeline> changed state to <state>', | ||
pathsMap: { | ||
pipeline: '$.detail.pipeline', | ||
state: '$.detail.state' | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,38 @@ export interface TargetInputTemplate { | |
* inputPathsMap to customize the data sent to the target. Enclose each | ||
* InputPathsMaps value in brackets: <value> | ||
* | ||
* The value will be serialized as a JSON object (JSON.stringify). | ||
* The value passed here will be double-quoted to indicate it's a string value. | ||
* This option is mutually exclusive with `jsonTemplate`. | ||
* | ||
* @example | ||
* | ||
* { | ||
* textTemplate: 'Build <buildid> started', | ||
* pathsMap: { | ||
* buildid: '$.detail.id' | ||
* } | ||
* } | ||
*/ | ||
textTemplate?: any; | ||
|
||
/** | ||
* Input template where you can use the values of the keys from | ||
* inputPathsMap to customize the data sent to the target. Enclose each | ||
* InputPathsMaps value in brackets: <value> | ||
* | ||
* This option is mutually exclusive with `textTemplate`. | ||
* | ||
* @example | ||
* | ||
* { | ||
* jsonTemplate: '{ "commands": <commandsToRun> }' , | ||
* pathsMap: { | ||
* commandsToRun: '$.detail.commands' | ||
* } | ||
* } | ||
* | ||
*/ | ||
template?: any; | ||
jsonTemplate?: any; | ||
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. Wait, confusing. If But at the same time, why CAN'T I pass in a JSON object? That is so much more user-friendly than a pre-stringified object. 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. Oh I see, it's 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. :sad: tokens are evil (unless maybe we can make them behave like strings... #24) 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. That only helps insofar as that now we maybe could type this field as Let me just leave this notion of |
||
|
||
/** | ||
* Map of JSON paths to be extracted from the event. These are key-value | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { Construct, resolve, Token } from 'aws-cdk'; | ||
import { Construct, FnConcat, Token } from 'aws-cdk'; | ||
import { events } from 'aws-cdk-resources'; | ||
import { EventPattern } from './event-pattern'; | ||
import { TargetInputTemplate } from './input-options'; | ||
|
@@ -107,13 +107,37 @@ export class EventRule extends EventRuleRef { | |
|
||
this.targets.push({ | ||
...target.eventRuleTarget, | ||
inputTransformer: inputOptions && { | ||
inputTemplate: typeof(inputOptions.template) === 'string' | ||
? JSON.stringify(inputOptions.template) | ||
: resolve(inputOptions.template), | ||
inputPathsMap: inputOptions.pathsMap | ||
}, | ||
inputTransformer: renderTransformer(), | ||
}); | ||
|
||
function renderTransformer(): events.RuleResource.InputTransformerProperty | undefined { | ||
if (!inputOptions) { | ||
return undefined; | ||
} | ||
|
||
if (inputOptions.jsonTemplate && inputOptions.textTemplate) { | ||
throw new Error('"jsonTemplate" and "textTemplate" are mutually exclusive'); | ||
} | ||
|
||
if (!inputOptions.jsonTemplate && !inputOptions.textTemplate) { | ||
throw new Error('One of "jsonTemplate" or "textTemplate" are required'); | ||
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. This error message could cover both cases. So simplification (warning, nasty code ahead using if (!!inputOptions.jsonTemplate === !!inputOptions.textTemplate) {
throw new Error('Exactly one of "jsonTemplate" or "textTemplate" is required');
} 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. Is this really a simplification? 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. I mean, meh. Honestly I don't care. But it is one fewer The implementation is moderately more complex, using the "bang-bang operator" (a legitimate JavaScript hack I believe) and the fact that we're comparing two booleans for equality. |
||
} | ||
|
||
let inputTemplate: any; | ||
|
||
if (inputOptions.jsonTemplate) { | ||
inputTemplate = inputOptions.jsonTemplate; | ||
} else if (typeof(inputOptions.textTemplate) === 'string') { | ||
inputTemplate = JSON.stringify(inputOptions.textTemplate); | ||
} else { | ||
inputTemplate = new FnConcat('"', inputOptions.textTemplate, '"'); | ||
} | ||
|
||
return { | ||
inputPathsMap: inputOptions.pathsMap, | ||
inputTemplate | ||
}; | ||
} | ||
} | ||
|
||
/** | ||
|
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.
Out of scope for this PR, but can we model this as well.
For example, how am I to know that the current event source has a "detail" member which has an "id" member?
Should be not that hard to slap together some classes that produce the same string, but in a way that can be
Ctrl-space
autocompleted.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.
The surface area is pretty big given events can potentially have tons of data associated with them and users will potentially want to extract any of it and transform it when they trigger their target. We might be able to provide some helpers with in the source construct (the one that has
onXxx
methods) which make it easier to work with certain fields of events emitted by the construct.At any rate, I've opened #38 to track.