From 713111b3c329449bb8360690f73406fbff529791 Mon Sep 17 00:00:00 2001 From: zhixzhan Date: Sat, 9 May 2020 15:54:05 +0800 Subject: [PATCH] update --- .../__tests__/store/reducer/reducer.test.js | 10 ++++- .../client/src/store/reducer/index.ts | 3 +- .../lib/indexers/src/utils/dialogCheckUtil.ts | 42 ++++++++----------- .../server/src/models/bot/botProject.ts | 4 +- 4 files changed, 29 insertions(+), 30 deletions(-) diff --git a/Composer/packages/client/__tests__/store/reducer/reducer.test.js b/Composer/packages/client/__tests__/store/reducer/reducer.test.js index 256eec8bfc..e45750d69e 100644 --- a/Composer/packages/client/__tests__/store/reducer/reducer.test.js +++ b/Composer/packages/client/__tests__/store/reducer/reducer.test.js @@ -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'); @@ -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); diff --git a/Composer/packages/client/src/store/reducer/index.ts b/Composer/packages/client/src/store/reducer/index.ts index af51bf7ce3..74339c7eb2 100644 --- a/Composer/packages/client/src/store/reducer/index.ts +++ b/Composer/packages/client/src/store/reducer/index.ts @@ -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, diff --git a/Composer/packages/lib/indexers/src/utils/dialogCheckUtil.ts b/Composer/packages/lib/indexers/src/utils/dialogCheckUtil.ts index 789f890fbc..711413e864 100644 --- a/Composer/packages/lib/indexers/src/utils/dialogCheckUtil.ts +++ b/Composer/packages/lib/indexers/src/utils/dialogCheckUtil.ts @@ -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; } diff --git a/Composer/packages/server/src/models/bot/botProject.ts b/Composer/packages/server/src/models/bot/botProject.ts index 57d188f4c2..3635f4b7a8 100644 --- a/Composer/packages/server/src/models/bot/botProject.ts +++ b/Composer/packages/server/src/models/bot/botProject.ts @@ -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 = [ @@ -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),