Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhixzhan committed May 9, 2020
1 parent b3870e1 commit 713111b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 30 deletions.
10 changes: 8 additions & 2 deletions Composer/packages/client/__tests__/store/reducer/reducer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('test all reducer handlers', () => {
luFiles: [],
schemas: { sdk: { content: {} } },
},
{ type: ActionTypes.CREATE_DIALOG, payload: { id: '3', content: '' } }
{ type: ActionTypes.CREATE_DIALOG, payload: { id: '3', content: {} } }
);
expect(result.dialogs.length).toBe(3);
expect(result.dialogs[2].id).toBe('3');
Expand All @@ -94,7 +94,13 @@ describe('test all reducer handlers', () => {

it('update dialog file', () => {
const result = reducer(
{ dialogs: [{ id: '1', content: 'old' }, { id: '2' }], schemas: { sdk: { content: {} } } },
{
dialogs: [{ id: '1', content: 'old' }, { id: '2' }],
locale: 'en-us',
lgFiles: [],
luFiles: [],
schemas: { sdk: { content: {} } },
},
{ type: ActionTypes.UPDATE_DIALOG, payload: { id: '1', content: 'new' } }
);
expect(result.dialogs.length).toBe(2);
Expand Down
3 changes: 1 addition & 2 deletions Composer/packages/client/src/store/reducer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,7 @@ const createDialogCancel: ReducerFunc = state => {
};

const createDialog: ReducerFunc = (state, { id, content }) => {
const fixedContent = autofixReferInDialog(id, content);
const dialogJson = JSON.parse(fixedContent);
const dialogJson = autofixReferInDialog(id, content);
const dialog = {
isRoot: false,
displayName: id,
Expand Down
42 changes: 18 additions & 24 deletions Composer/packages/lib/indexers/src/utils/dialogCheckUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,26 @@ import { VisitorFunc, JsonWalk } from './jsonWalk';
* - "dialog": 'AddTodos'
* + "dialog": 'addtodos'
*/
export function autofixReferInDialog(dialogId: string, content: string): string {
try {
const dialogJson = JSON.parse(content);

// fix dialog referrence
const visitor: VisitorFunc = (_path: string, value: any) => {
if (has(value, '$type') && value.$type === SDKKinds.BeginDialog) {
const dialogName = value.dialog;
value.dialog = dialogName.toLowerCase();
}
return false;
};
export function autofixReferInDialog(dialogId: string, dialog: { [key: string]: any }): { [key: string]: any } {
const dialogJson = { ...dialog };
// fix dialog referrence
const visitor: VisitorFunc = (_path: string, value: any) => {
if (has(value, '$type') && value.$type === SDKKinds.BeginDialog) {
const dialogName = value.dialog;
value.dialog = dialogName.toLowerCase();
}
return false;
};

JsonWalk('/', dialogJson, visitor);
JsonWalk('/', dialogJson, visitor);

// fix lg referrence
dialogJson.generator = `${dialogId}.lg`;
// fix lg referrence
dialogJson.generator = `${dialogId}.lg`;

// fix lu referrence
if (typeof dialogJson.recognizer === 'string') {
dialogJson.recognizer = `${dialogId}.lu`;
}

return JSON.stringify(dialogJson, null, 2);
} catch (_error) {
// pass, content may be empty
return content;
// fix lu referrence
if (typeof dialogJson.recognizer === 'string') {
dialogJson.recognizer = `${dialogId}.lu`;
}

return dialogJson;
}
4 changes: 2 additions & 2 deletions Composer/packages/server/src/models/bot/botProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export class BotProject {
newDesigner = getNewDesigner(name, description);
}
content.$designer = newDesigner;
const updatedContent = autofixReferInDialog(entryDialogId, JSON.stringify(content, null, 2));
const updatedContent = JSON.stringify(autofixReferInDialog(entryDialogId, content));
await this._updateFile(relativePath, updatedContent);
// when create/saveAs bot, serialize entry dialog/lg/lu
const entryPatterns = [
Expand Down Expand Up @@ -548,7 +548,7 @@ export class BotProject {
TemplateVariables.DIALOGNAME = dialogName;

if (fileType === '.dialog') {
content = autofixReferInDialog(dialogName, content);
content = JSON.stringify(autofixReferInDialog(dialogName, JSON.parse(content)));

targetRelativePath = templateInterpolate(
Path.join(pathEndPoint, BotStructureTemplate.dialogs.entry),
Expand Down

0 comments on commit 713111b

Please sign in to comment.