-
Notifications
You must be signed in to change notification settings - Fork 372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: centralize lg parsing logic to 'shared' lib #1663
Merged
Merged
Changes from all commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
7088206
implement basic lg apis
yeze322 e975f16
expose them on 'shared' level
yeze322 531f317
UT + fix
yeze322 9e7c541
add static `parse()` method to LgTemplateRef and LgMetaData
yeze322 b55f845
loose restriction on lg format
yeze322 aae1d7c
migrate: lgUtils
yeze322 13ce6b3
migrate: visual editor - hooks
yeze322 1e6b5ea
move 'extractLgTemplateRefs' inside 'shared' lib
yeze322 ee68dde
migrate: dialogIndexer
yeze322 236b2e9
migrate: LgEditorWidget
yeze322 6049ebc
migrate: ObiEditor
yeze322 bd7390b
let shared lib built before others
yeze322 4a1c52c
rename: patterns.ts -> lgPatterns.ts
yeze322 c5cfb59
fix ut: use the lg pattern in indexer file
yeze322 e3d30e6
todo: mark up next pr's todo
yeze322 3291366
Merge branch 'master' into lg/organize
yeze322 456d08f
comments: explain lg strings
yeze322 e3a9b69
enhance ut of parseLgTemplateRef
yeze322 c2a69c7
do not export parsers
yeze322 1353a50
clarify the concept of 'LgText'
yeze322 f8b4d66
implement LgField with UT
yeze322 8254870
add two judement method in LgField
yeze322 490cc65
remove 'toLgText' method in LgMetaData
yeze322 fb991d3
purify 'LgMetaData'
yeze322 06a591d
update LgMetaData usages
yeze322 b7daa72
update stringTypes
yeze322 9c31854
remove LgFiled (over abstracted)
yeze322 02d4c15
fix the usage of LgMetaData
yeze322 8eb4f40
rename param name to lgTemplateName
yeze322 67e091a
refactor: apply LgTemplateRef to LgEditorWidget
yeze322 67c064f
set LgRef default parameters to '[]'
yeze322 31d6c99
refactor: simplify 'getInitialTemplate'
yeze322 48b910b
enrich LgTempateRef test cases
yeze322 284e8ce
folder structure
yeze322 216d413
take out string logic into string builders
yeze322 d4738e6
clean: remove parseLgText since it's not referenced
yeze322 644ae1c
extractLgTemplateNames => extractLgTemplateRefs
yeze322 fc22977
fix a negative case in 'parseLgTemplateRef'
yeze322 cd0aa32
merge extractLgTemplateRefs into 'parseLgTemplateRef'
yeze322 6d1648b
edit comments
yeze322 0bda795
extract common code from parseLgTemplateRef
yeze322 c7a289f
clean
yeze322 17f0929
add a single whitespace aftter '-' in LgEditorWIdget
yeze322 5aa6c5f
Merge branch 'master' of https://github.com/Microsoft/BotFramework-Co…
yeze322 bd9930e
Merge branch 'master' into lg/organize
yeze322 7926a22
Merge branch 'master' into lg/organize
cwhitten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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 |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
import { jsx } from '@emotion/core'; | ||
import { useContext, FC, useEffect, useState, useRef } from 'react'; | ||
import { MarqueeSelection, Selection } from 'office-ui-fabric-react/lib/MarqueeSelection'; | ||
import { deleteAction, deleteActions } from '@bfc/shared'; | ||
import { deleteAction, deleteActions, LgTemplateRef, LgMetaData } from '@bfc/shared'; | ||
|
||
import { NodeEventTypes } from '../constants/NodeEventTypes'; | ||
import { KeyboardCommandTypes, KeyboardPrimaryTypes } from '../constants/KeyboardCommandTypes'; | ||
|
@@ -49,12 +49,10 @@ export const ObiEditor: FC<ObiEditorProps> = ({ | |
); | ||
|
||
const deleteLgTemplates = (lgTemplates: string[]) => { | ||
const lgPattern = /\[(bfd\w+-\d+)\]/; | ||
const normalizedLgTemplates = lgTemplates | ||
.map(x => { | ||
const matches = lgPattern.exec(x); | ||
if (matches && matches.length === 2) return matches[1]; | ||
return ''; | ||
const lgTemplateRef = LgTemplateRef.parse(x); | ||
return lgTemplateRef ? lgTemplateRef.name : ''; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can leverage the 'optional chaining' feature in ts 3.7. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
}) | ||
.filter(x => !!x); | ||
return removeLgTemplates('common', normalizedLgTemplates); | ||
|
@@ -89,16 +87,19 @@ export const ObiEditor: FC<ObiEditorProps> = ({ | |
if (eventData.$type === 'PASTE') { | ||
handler = e => { | ||
// TODO: clean this along with node deletion. | ||
const copyLgTemplateToNewNode = async (lgTemplateName: string, newNodeId: string) => { | ||
const matches = /\[(bfd\w+-(\d+))\]/.exec(lgTemplateName); | ||
if (Array.isArray(matches) && matches.length === 3) { | ||
const originLgId = matches[1]; | ||
const originNodeId = matches[2]; | ||
const newLgId = originLgId.replace(originNodeId, newNodeId); | ||
await copyLgTemplate('common', originLgId, newLgId); | ||
return `[${newLgId}]`; | ||
} | ||
return lgTemplateName; | ||
const copyLgTemplateToNewNode = async (lgText: string, newNodeId: string) => { | ||
const inputLgRef = LgTemplateRef.parse(lgText); | ||
if (!inputLgRef) return lgText; | ||
|
||
const inputLgMetaData = LgMetaData.parse(inputLgRef.name); | ||
if (!inputLgMetaData) return lgText; | ||
|
||
inputLgMetaData.designerId = newNodeId; | ||
const newLgName = inputLgMetaData.toString(); | ||
const newLgTemplateRefString = new LgTemplateRef(newLgName).toString(); | ||
|
||
await copyLgTemplate('common', inputLgRef.name, newLgName); | ||
return newLgTemplateRefString; | ||
}; | ||
pasteNodes(data, e.id, e.position, clipboardActions, copyLgTemplateToNewNode).then(dialog => { | ||
onChange(dialog); | ||
|
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
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
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
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
24 changes: 24 additions & 0 deletions
24
Composer/packages/lib/shared/__tests__/lgUtils/models/LgMetaData.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,24 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License | ||
|
||
import { LgMetaData } from '../../../src'; | ||
|
||
describe('LgMetaData', () => { | ||
it('can construct an instance via constructor', () => { | ||
const instance = new LgMetaData('activity', '123456'); | ||
|
||
expect(instance.type).toEqual('activity'); | ||
expect(instance.designerId).toEqual('123456'); | ||
expect(instance.toString).toBeDefined(); | ||
}); | ||
|
||
it('can generate correct output strings', () => { | ||
const instance = new LgMetaData('activity', '123456'); | ||
|
||
expect(instance.toString()).toEqual('bfdactivity-123456'); | ||
}); | ||
|
||
it('can construct instance via `parse()` method', () => { | ||
expect(LgMetaData.parse('bfdactivity-123456')).toBeInstanceOf(LgMetaData); | ||
}); | ||
}); |
50 changes: 50 additions & 0 deletions
50
Composer/packages/lib/shared/__tests__/lgUtils/models/LgTemplateRef.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,50 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License | ||
|
||
import { LgTemplateRef } from '../../../src'; | ||
|
||
describe('LgTemplateRef#', () => { | ||
it('can construct an instance via constructor', () => { | ||
const a = new LgTemplateRef('a', undefined); | ||
expect(a.name).toEqual('a'); | ||
expect(a.parameters).toEqual([]); | ||
|
||
const b = new LgTemplateRef('b', []); | ||
expect(b.name).toEqual('b'); | ||
expect(b.parameters).toEqual([]); | ||
|
||
const c = new LgTemplateRef('c', ['1', '2']); | ||
expect(c.name).toEqual('c'); | ||
expect(c.parameters).toEqual(['1', '2']); | ||
}); | ||
|
||
it('can output correct strings', () => { | ||
const a = new LgTemplateRef('a', undefined); | ||
expect(a.toString()).toEqual('[a()]'); | ||
|
||
const b = new LgTemplateRef('b', []); | ||
expect(b.toString()).toEqual('[b()]'); | ||
|
||
const c = new LgTemplateRef('c', ['1', '2']); | ||
expect(c.toString()).toEqual('[c(1,2)]'); | ||
}); | ||
|
||
describe('parse()', () => { | ||
it('should return null when inputs are invalid', () => { | ||
expect(LgTemplateRef.parse('')).toEqual(null); | ||
expect(LgTemplateRef.parse('xxx')).toEqual(null); | ||
expect(LgTemplateRef.parse('[0]')).toEqual(null); | ||
}); | ||
|
||
it('should return LgTemplateRef when inputs are valid', () => { | ||
const a = LgTemplateRef.parse('[bfdactivity-123456]'); | ||
expect(a).toEqual(new LgTemplateRef('bfdactivity-123456')); | ||
|
||
const a2 = LgTemplateRef.parse('[bfdactivity-123456()]'); | ||
expect(a2).toEqual(new LgTemplateRef('bfdactivity-123456')); | ||
|
||
const b = LgTemplateRef.parse('[greeting(1,2)]'); | ||
expect(b).toEqual(new LgTemplateRef('greeting', ['1', '2'])); | ||
}); | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
Composer/packages/lib/shared/__tests__/lgUtils/parsers/parseLgParamString.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,16 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License | ||
|
||
import parseLgParamString from '../../../src/lgUtils/parsers/parseLgParamString'; | ||
|
||
describe('parseLgParamString', () => { | ||
it('should return undefined when no params detected', () => { | ||
expect(parseLgParamString('')).toBeUndefined(); | ||
expect(parseLgParamString('xxx')).toBeUndefined(); | ||
}); | ||
|
||
it('should return params array when input valid strings', () => { | ||
expect(parseLgParamString('()')).toEqual([]); | ||
expect(parseLgParamString('(a,b)')).toEqual(['a', 'b']); | ||
}); | ||
}); |
19 changes: 19 additions & 0 deletions
19
Composer/packages/lib/shared/__tests__/lgUtils/parsers/parseLgTemplateName.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,19 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License | ||
|
||
import { LgMetaData } from '../../../src'; | ||
import parseLgTemplateName from '../../../src/lgUtils/parsers/parseLgTemplateName'; | ||
|
||
describe('parseLgTemplateName', () => { | ||
it('should return null when inputs are invalid', () => { | ||
expect(parseLgTemplateName('')).toEqual(null); | ||
expect(parseLgTemplateName('xxx')).toEqual(null); | ||
}); | ||
|
||
it('should return LgMetaData when inputs are valid', () => { | ||
const result = parseLgTemplateName('bfdactivity-123456'); | ||
expect(result).toBeInstanceOf(LgMetaData); | ||
expect((result as LgMetaData).designerId).toEqual('123456'); | ||
expect((result as LgMetaData).type).toEqual('activity'); | ||
}); | ||
}); |
33 changes: 33 additions & 0 deletions
33
Composer/packages/lib/shared/__tests__/lgUtils/parsers/parseLgTemplateRef.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,33 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License | ||
|
||
import { LgTemplateRef } from '../../../src'; | ||
import parseLgTemplateRef, { extractLgTemplateRefs } from '../../../src/lgUtils/parsers/parseLgTemplateRef'; | ||
|
||
describe('parseLgTemplateRef', () => { | ||
it('should return null when inputs are invalid', () => { | ||
expect(parseLgTemplateRef('')).toEqual(null); | ||
yeze322 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expect(parseLgTemplateRef('xxx')).toEqual(null); | ||
expect(parseLgTemplateRef('[0]')).toEqual(null); | ||
expect(parseLgTemplateRef('hi, [greeting]. [greeting]')).toEqual(null); | ||
}); | ||
|
||
it('should return LgTemplateRef when inputs are valid', () => { | ||
const a = parseLgTemplateRef('[bfdactivity-123456]'); | ||
expect(a).toEqual(new LgTemplateRef('bfdactivity-123456')); | ||
|
||
const b = parseLgTemplateRef('[greeting(1,2)]'); | ||
expect(b).toEqual(new LgTemplateRef('greeting', ['1', '2'])); | ||
}); | ||
}); | ||
|
||
describe('extractLgTemplateRefs', () => { | ||
it('can extract lg refs from input string', () => { | ||
expect(extractLgTemplateRefs('Hi')).toEqual([]); | ||
expect(extractLgTemplateRefs('[bfdactivity-123456]')).toEqual([new LgTemplateRef('bfdactivity-123456')]); | ||
expect(extractLgTemplateRefs(`-[Greeting], I'm a fancy bot, [Bye]`)).toEqual([ | ||
new LgTemplateRef('Greeting'), | ||
new LgTemplateRef('Bye'), | ||
]); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
deleteLgTemplates
will be defined asdeleteLgText
. See my next comments oncopyLgTemplate