Skip to content

Commit

Permalink
fix(transform): check final transforms return type
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Oct 20, 2019
1 parent bc8fbd1 commit 1cf1d3b
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 19 deletions.
16 changes: 16 additions & 0 deletions docs/controller/echo-controller.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
metadata:
kind: echo-controller
name: test-echo-controller
data:
defaultTarget:
kind: slack-listener
name: slack-isolex

filters: []
transforms:
- metadata:
kind: template-transform
name: test-echo-template
data:
templates:
body: "{{ json this }}"
1 change: 1 addition & 0 deletions docs/isolex.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ data:
- !include ../docs/controller/command-controller.yml
- !include ../docs/controller/completion-controller.yml
- !include ../docs/controller/dice-controller.yml
- !include ../docs/controller/echo-controller.yml
- !include ../docs/controller/github/pr-controller.yml
- !include ../docs/controller/gitlab/ci-controller.yml
- !include ../docs/controller/gitlab/endpoint/push-controller.yml
Expand Down
12 changes: 4 additions & 8 deletions src/controller/BaseController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNil, isString } from 'lodash';
import { isNil } from 'lodash';
import { Inject, MissingValueError } from 'noicejs';

import { Controller, ControllerData, getHandlerOptions, HandlerOptions } from '.';
Expand All @@ -12,7 +12,7 @@ import { Listener } from '../listener';
import { Locale, TranslateOptions } from '../locale';
import { ServiceModule } from '../module/ServiceModule';
import { ServiceDefinition } from '../Service';
import { applyTransforms, Transform, TransformData } from '../transform';
import { applyTransforms, extractBody, Transform, TransformData } from '../transform';
import { doesExist, mustCoalesce, mustExist } from '../utils';
import { TYPE_JSON, TYPE_TEXT } from '../utils/Mime';
import { getMethods } from '../utils/Reflect';
Expand Down Expand Up @@ -151,12 +151,8 @@ export abstract class BaseController<TData extends ControllerData> extends BotSe
const result = await applyTransforms(this.transforms, cmd, TYPE_JSON, data);
this.logger.debug({ result }, 'transformed json results');

const [body] = result.body;
if (isString(body)) {
return this.reply(mustCoalesce(ctx, cmd.context), body);
} else {
this.logger.error({ body }, 'final transform did not return a string');
}
const body = extractBody(result);
return this.reply(mustCoalesce(ctx, cmd.context), body);
}

protected async errorReply(ctx: Context, errCode: ErrorReplyType, msg?: string): Promise<void> {
Expand Down
13 changes: 4 additions & 9 deletions src/generator/MessageGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { isString } from 'lodash';
import { BaseError } from 'noicejs';

import { GeneratorData } from '.';
import { Context } from '../entity/Context';
import { Message, MessageEntityOptions } from '../entity/Message';
import { Tick } from '../entity/Tick';
import { NotInitializedError } from '../error/NotInitializedError';
import { ServiceDefinition } from '../Service';
import { applyTransforms, Transform, TransformData } from '../transform';
import { applyTransforms, extractBody, Transform, TransformData } from '../transform';
import { BaseGenerator, BaseIntervalOptions } from './BaseGenerator';

export interface MessageGeneratorData extends GeneratorData {
Expand Down Expand Up @@ -54,17 +51,15 @@ export class MessageGenerator extends BaseGenerator<MessageGeneratorData> {
context,
});

const body = await applyTransforms(this.transforms, initial, this.data.defaultMessage.type, {
const result = await applyTransforms(this.transforms, initial, this.data.defaultMessage.type, {
last,
next,
});
if (!isString(body.body)) {
throw new BaseError('final transform did not return a string');
}

const body = extractBody(result);
await this.bot.sendMessage(new Message({
...initial,
body: body.body,
body,
}));
return 0;
}
Expand Down
6 changes: 5 additions & 1 deletion src/transform/TemplateTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ export class TemplateTransform extends BaseTransform<TemplateTransformData> impl
this.logger.debug({ key, scope }, 'rendering template with scope');
const result = template.render(scope);
this.logger.debug({ key, result }, 'rendered template with scope');
out.set(key, result);
if (Array.isArray(result)) {
out.set(key, result);
} else {
out.set(key, [result]);
}
}
return makeDict(out);
}
Expand Down
16 changes: 16 additions & 0 deletions src/transform/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { isString } from 'lodash';
import { BaseError } from 'noicejs';

import { BotServiceData } from '../BotService';
import { FilterData, FilterValue } from '../filter';
import { Service, ServiceDefinition } from '../Service';
Expand Down Expand Up @@ -41,3 +44,16 @@ export async function applyTransforms(

return makeDict(output);
}

export function extractBody(data: TransformOutput): string {
if (!Array.isArray(data.body)) {
throw new BaseError('final transform did not return body array');
}

const [body] = data.body;
if (!isString(body)) {
throw new BaseError('final transform did not return a string');
}

return body;
}
4 changes: 3 additions & 1 deletion test/transform/TestTemplateTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ describeLeaks('template transform', async () => {
verb: CommandVerb.Get,
}), TYPE_JSON, data);

expect(output).to.deep.equal(templates);
expect(output).to.deep.equal({
body: [templates.body],
});
});
});

0 comments on commit 1cf1d3b

Please sign in to comment.