Skip to content

Commit

Permalink
fix(parser/args): convert values to strings
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Sep 8, 2019
1 parent fa35259 commit 5dea140
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
4 changes: 1 addition & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,12 @@ function mainModules() {
return modules;
}

declare function __non_webpack_require__(name: string): { [name: string]: ModuleCtor };

async function loadModules(config: BotDefinition, logger: Logger) {
const modules: Array<Module> = [];

for (const p of config.data.modules) {
try {
const nodeModule = __non_webpack_require__(/* webpackIgnore: true */ p.require);
const nodeModule = require(p.require);
const moduleType = nodeModule[p.export];

// TODO: verify this is a module constructor before instantiating
Expand Down
4 changes: 2 additions & 2 deletions src/parser/ArgsParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Fragment } from '../entity/Fragment';
import { Message } from '../entity/Message';
import { MimeTypeError } from '../error/MimeTypeError';
import { mustExist } from '../utils';
import { dictValuesToArrays, makeMap, pushMergeMap } from '../utils/Map';
import { makeMap, normalizeMap, pushMergeMap } from '../utils/Map';
import { TYPE_TEXT } from '../utils/Mime';
import { BaseParser } from './BaseParser';

Expand Down Expand Up @@ -63,7 +63,7 @@ export class ArgsParser extends BaseParser<ArgsParserData> implements Parser {

protected async decodeBody(body: string): Promise<ParserOutput> {
return {
data: dictValuesToArrays<string>(yargs(body, this.data.args)),
data: normalizeMap(yargs(body, this.data.args)),
};
}

Expand Down
17 changes: 16 additions & 1 deletion src/utils/Map.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isMap, isNil } from 'lodash';
import { isMap, isNil, isObject, isString } from 'lodash';

import { doesExist, mergeList, mustExist } from '.';
import { NotFoundError } from '../error/NotFoundError';
Expand Down Expand Up @@ -146,3 +146,18 @@ export function dictValuesToArrays<TVal>(map: MapLike<TVal>): Dict<Array<TVal>>

return data;
}

export function normalizeMap(map: MapLike<any>): Dict<Array<string>> {
const data: Dict<Array<string>> = {};
for (const [key, value] of makeMap(map)) {
if (Array.isArray(value)) {
data[key] = value;
} else if (isString(value)) {
data[key] = [value];
} else if (isObject(value)) {
data[key] = [value.toString()];
}
}

return data;
}
9 changes: 5 additions & 4 deletions test/parser/TestArgsParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { createService, createServiceContainer } from '../helpers/container';
const TEST_CONFIG = {
args: {
required: ['foo', 'bar'],
string: ['foo', 'bar'],
},
defaultCommand: {
data: {},
Expand Down Expand Up @@ -64,8 +65,8 @@ describeAsync('args parser', async () => {
}));
expect(data.data).to.deep.equal({
_: [],
bar: [2],
foo: [1],
bar: ['2'],
foo: ['1'],
});
});

Expand Down Expand Up @@ -95,7 +96,7 @@ describeAsync('args parser', async () => {

const [cmd] = commands;
expect(cmd.noun).to.equal(NOUN_FRAGMENT);
expect(cmd.getHead('foo')).to.equal(1);
expect(cmd.getHead('foo')).to.equal('1');
});

itAsync('should complete a fragment', async () => {
Expand Down Expand Up @@ -129,7 +130,7 @@ describeAsync('args parser', async () => {
const [cmd] = commands;
expect(cmd.noun).to.equal('test');
expect(cmd.getHead('foo')).to.equal('1');
expect(cmd.getHead('bar')).to.equal(2); // TODO: should convert to string
expect(cmd.getHead('bar')).to.equal('2');
});

itAsync('should reject messages with other types', async () => {
Expand Down

0 comments on commit 5dea140

Please sign in to comment.