forked from microsoft/BotFramework-Composer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Destructor in shared lib for removing LG templates during node…
… deletion (microsoft#1213) (microsoft#1580) * implement adaptive action walker * implement `deleteAction` * add a batch api `deleteActions` * apply new destructor to ObiEditor * fix walkAdaptiveAction && add UT * add test for walkAdaptiveActionList * normalize lg templates before removing * Update jsonTracker.ts * move deleteAction bindings outside jsonTracker to not break origin tests * avoid unnecessary change * remove unused code
- Loading branch information
Showing
11 changed files
with
204 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
Composer/packages/lib/shared/__tests__/deleteUtils/walkAdaptiveAction.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License | ||
|
||
import { walkAdaptiveAction } from '../../src/deleteUtils/walkAdaptiveAction'; | ||
|
||
describe('walkAdaptiveAction', () => { | ||
it('can walk single action', () => { | ||
const action = { | ||
$type: 'Microsoft.SendActivity', | ||
activity: 'hello', | ||
}; | ||
const spy = jest.fn(); | ||
walkAdaptiveAction(action, x => spy(x)); | ||
|
||
expect(spy).toBeCalledTimes(1); | ||
expect(spy).toBeCalledWith(action); | ||
}); | ||
}); |
25 changes: 25 additions & 0 deletions
25
Composer/packages/lib/shared/__tests__/deleteUtils/walkAdaptiveActionList.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License | ||
|
||
import { walkAdaptiveActionList } from '../../src/deleteUtils/walkAdaptiveActionList'; | ||
|
||
describe('walkAdaptiveAction', () => { | ||
it('can walk action list', () => { | ||
const actions = [ | ||
{ | ||
$type: 'Microsoft.SendActivity', | ||
prompt: 'hello', | ||
}, | ||
{ | ||
$type: 'Microsoft.ChoiceInput', | ||
prompt: 'hello', | ||
}, | ||
]; | ||
const spy = jest.fn(); | ||
walkAdaptiveActionList(actions, x => spy(x)); | ||
|
||
expect(spy).toBeCalledTimes(2); | ||
expect(spy).toHaveBeenNthCalledWith(1, actions[0]); | ||
expect(spy).toHaveBeenNthCalledWith(2, actions[1]); | ||
}); | ||
}); |
6 changes: 6 additions & 0 deletions
6
Composer/packages/lib/shared/src/deleteUtils/AdaptiveActionVisitor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License | ||
|
||
import { MicrosoftIDialog } from '../types'; | ||
|
||
export type AdaptiveActionVisitor = (action: MicrosoftIDialog) => void; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License | ||
|
||
import { MicrosoftIDialog, SDKTypes } from '../types'; | ||
|
||
import { walkAdaptiveAction } from './walkAdaptiveAction'; | ||
import { walkAdaptiveActionList } from './walkAdaptiveActionList'; | ||
|
||
const collectLgTemplates = (action: any, outputTemplates: string[]) => { | ||
if (typeof action === 'string') return; | ||
if (!action || !action.$type) return; | ||
|
||
switch (action.$type) { | ||
case SDKTypes.SendActivity: | ||
outputTemplates.push(action.activity); | ||
break; | ||
case SDKTypes.AttachmentInput: | ||
case SDKTypes.ChoiceInput: | ||
case SDKTypes.ConfirmInput: | ||
case SDKTypes.DateTimeInput: | ||
case SDKTypes.NumberInput: | ||
case SDKTypes.TextInput: | ||
outputTemplates.push(action.prompt, action.unrecognizedPrompt, action.invalidPrompt, action.defaultValueResponse); | ||
break; | ||
} | ||
}; | ||
|
||
export const deleteAdaptiveAction = (data: MicrosoftIDialog, deleteLgTemplates: (lgTemplates: string[]) => any) => { | ||
const lgTemplates: string[] = []; | ||
walkAdaptiveAction(data, action => collectLgTemplates(action, lgTemplates)); | ||
|
||
deleteLgTemplates(lgTemplates.filter(activity => !!activity)); | ||
}; | ||
|
||
export const deleteAdaptiveActionList = ( | ||
data: MicrosoftIDialog[], | ||
deleteLgTemplates: (lgTemplates: string[]) => any | ||
) => { | ||
const lgTemplates: string[] = []; | ||
walkAdaptiveActionList(data, action => collectLgTemplates(action, lgTemplates)); | ||
|
||
deleteLgTemplates(lgTemplates.filter(activity => !!activity)); | ||
}; |
11 changes: 11 additions & 0 deletions
11
Composer/packages/lib/shared/src/deleteUtils/walkActionWithChildren.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License | ||
|
||
import { AdaptiveActionVisitor } from './AdaptiveActionVisitor'; | ||
import { walkAdaptiveActionList } from './walkAdaptiveActionList'; | ||
|
||
export const walkActionWithChildren = (input, visitor: AdaptiveActionVisitor) => { | ||
visitor(input); | ||
|
||
walkAdaptiveActionList(input.actions, visitor); | ||
}; |
35 changes: 35 additions & 0 deletions
35
Composer/packages/lib/shared/src/deleteUtils/walkAdaptiveAction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License | ||
|
||
import { MicrosoftIDialog, SDKTypes } from '../types'; | ||
|
||
import { walkIfCondition } from './walkIfCondition'; | ||
import { walkSwitchCondition } from './walkSwitchCondition'; | ||
import { walkActionWithChildren } from './walkActionWithChildren'; | ||
import { AdaptiveActionVisitor } from './AdaptiveActionVisitor'; | ||
|
||
const WalkerMap: { [$type: string]: (input, visitor: AdaptiveActionVisitor) => void } = { | ||
[SDKTypes.IfCondition]: walkIfCondition, | ||
[SDKTypes.SwitchCondition]: walkSwitchCondition, | ||
[SDKTypes.Foreach]: walkActionWithChildren, | ||
[SDKTypes.ForeachPage]: walkActionWithChildren, | ||
[SDKTypes.EditActions]: walkActionWithChildren, | ||
}; | ||
|
||
export const walkAdaptiveAction = (input, visit: (action: MicrosoftIDialog) => void): void => { | ||
if (typeof input === 'string') { | ||
visit(input); | ||
return; | ||
} | ||
|
||
if (!input || !input.$type) { | ||
return; | ||
} | ||
|
||
if (WalkerMap[input.$type]) { | ||
WalkerMap[input.$type](input, visit); | ||
} else { | ||
visit(input); | ||
} | ||
return; | ||
}; |
11 changes: 11 additions & 0 deletions
11
Composer/packages/lib/shared/src/deleteUtils/walkAdaptiveActionList.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License | ||
|
||
import { MicrosoftIDialog } from '../types'; | ||
|
||
import { walkAdaptiveAction } from './walkAdaptiveAction'; | ||
export const walkAdaptiveActionList = (inputs: MicrosoftIDialog[], visit: (action: MicrosoftIDialog) => void): void => { | ||
if (Array.isArray(inputs)) { | ||
inputs.forEach(action => walkAdaptiveAction(action, visit)); | ||
} | ||
}; |
12 changes: 12 additions & 0 deletions
12
Composer/packages/lib/shared/src/deleteUtils/walkIfCondition.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License | ||
|
||
import { AdaptiveActionVisitor } from './AdaptiveActionVisitor'; | ||
import { walkAdaptiveActionList } from './walkAdaptiveActionList'; | ||
|
||
export const walkIfCondition = (input, visitor: AdaptiveActionVisitor) => { | ||
visitor(input); | ||
|
||
walkAdaptiveActionList(input.actions, visitor); | ||
walkAdaptiveActionList(input.elseActions, visitor); | ||
}; |
17 changes: 17 additions & 0 deletions
17
Composer/packages/lib/shared/src/deleteUtils/walkSwitchCondition.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License | ||
|
||
import { AdaptiveActionVisitor } from './AdaptiveActionVisitor'; | ||
import { walkAdaptiveActionList } from './walkAdaptiveActionList'; | ||
|
||
export const walkSwitchCondition = (input, visitor: AdaptiveActionVisitor) => { | ||
visitor(input); | ||
|
||
walkAdaptiveActionList(input.default, visitor); | ||
|
||
if (Array.isArray(input.cases)) { | ||
input.cases.forEach(currentCase => { | ||
walkAdaptiveActionList(currentCase.actions, visitor); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters