Skip to content

Commit

Permalink
fix(aws-events): jsonTemplate now accepts arbitrary objects
Browse files Browse the repository at this point in the history
They will be JSONified automatically.

Fixes #1198.
  • Loading branch information
rix0rrr committed Jan 18, 2019
1 parent ef52892 commit 74a2900
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
14 changes: 9 additions & 5 deletions packages/@aws-cdk/aws-events/lib/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export class EventRule extends Construct implements IEventRule {
*/
public addTarget(target?: IEventRuleTarget, inputOptions?: TargetInputTemplate) {
if (!target) { return; }
const self = this;

const targetProps = target.asEventRuleTarget(this.ruleArn, this.node.uniqueId);

Expand Down Expand Up @@ -145,12 +146,15 @@ export class EventRule extends Construct implements IEventRule {
let inputTemplate: any;

if (inputOptions.jsonTemplate) {
inputTemplate = inputOptions.jsonTemplate;
} else if (typeof(inputOptions.textTemplate) === 'string') {
// Newline separated list of JSON-encoded strings
inputTemplate = inputOptions.textTemplate.split('\n').map(x => JSON.stringify(x)).join('\n');
inputTemplate = typeof inputOptions.jsonTemplate === 'string'
? inputOptions.jsonTemplate
: self.node.stringifyJson(inputOptions.jsonTemplate);
} else {
inputTemplate = `"${inputOptions.textTemplate}"`;
inputTemplate = typeof(inputOptions.textTemplate) === 'string'
// Newline separated list of JSON-encoded strings
? inputOptions.textTemplate.split('\n').map(x => self.node.stringifyJson(x)).join('\n')
// Some object, stringify it, then stringify the string for proper escaping
: self.node.stringifyJson(self.node.stringifyJson(inputOptions.textTemplate));
}

return {
Expand Down
27 changes: 27 additions & 0 deletions packages/@aws-cdk/aws-events/test/test.rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,33 @@ export = {
test.done();
},

'json template': {
'can just be a JSON object'(test: Test) {
// GIVEN
const stack = new Stack();
const rule = new EventRule(stack, 'Rule', {
scheduleExpression: 'rate(1 minute)'
});

// WHEN
rule.addTarget(new SomeTarget(), {
jsonTemplate: { SomeObject: 'withAValue' },
});

// THEN
expect(stack).to(haveResourceLike('AWS::Events::Rule', {
Targets: [
{
InputTransformer: {
InputTemplate: "{\"SomeObject\":\"withAValue\"}"
},
}
]
}));
test.done();
},
},

'text templates': {
'strings with newlines are serialized to a newline-delimited list of JSON strings'(test: Test) {
// GIVEN
Expand Down

0 comments on commit 74a2900

Please sign in to comment.