Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit 028a4ec

Browse files
author
Shuai Wang
authored
Fix lg:expand cannot retrieve the value from scope with certain paths (#994)
* fix issue in access scope * simplify a for loop * add test for access index in path * fix some extra code * rename a constant
1 parent 845f2e1 commit 028a4ec

File tree

8 files changed

+141
-4
lines changed

8 files changed

+141
-4
lines changed

.vscode/launch.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,28 @@
337337
],
338338
"internalConsoleOptions": "openOnSessionStart",
339339
"cwd": "${workspaceFolder}/../botbuilder-dotnet/tests/Microsoft.Bot.Builder.Dialogs.Adaptive.Tests/tests/LUISRecognizerTests"
340+
},
341+
{
342+
"type": "node",
343+
"request": "launch",
344+
"name": "LG Expand Test",
345+
"preLaunchTask": "${defaultBuildTask}",
346+
"program": "${workspaceFolder}/packages/lg/bin/run",
347+
"outputCapture": "std",
348+
"outFiles": [
349+
"./packages/lg/lib/**"
350+
],
351+
"args": [
352+
"lg:expand",
353+
"--in",
354+
"accessScope.lg",
355+
"--testInput",
356+
"data.json",
357+
"--template",
358+
"GetIndex"
359+
],
360+
"internalConsoleOptions": "openOnSessionStart",
361+
"cwd": "${workspaceFolder}/packages/lg/test/fixtures/testcase"
340362
}
341363
]
342364
}

packages/lg/src/commands/lg/expand.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import * as txtfile from 'read-text-file'
1313
import * as path from 'path'
1414
import * as fs from 'fs-extra'
1515
import * as readlineSync from 'readline-sync'
16+
import * as lodash from 'lodash'
1617

1718
export default class ExpandCommand extends Command {
1819
static description = 'Expand one or all templates in .lg file(s). Expand an inline expression.'
@@ -230,8 +231,9 @@ export default class ExpandCommand extends Command {
230231

231232
if (expectedVariables !== undefined) {
232233
for (const variable of expectedVariables) {
233-
if (variablesObj !== undefined && variablesObj[variable] !== undefined) {
234-
result.set(variable, variablesObj[variable])
234+
const evalPathResult = lodash.get(variablesObj, variable)
235+
if (variablesObj !== undefined && evalPathResult !== undefined) {
236+
result.set(variable, evalPathResult)
235237
} else if (userInputValues !== undefined && userInputValues.has(variable)) {
236238
result.set(variable, userInputValues.get(variable))
237239
} else {
@@ -247,7 +249,7 @@ export default class ExpandCommand extends Command {
247249
const result: any = {}
248250
if (variablesValue !== undefined) {
249251
for (const variable of variablesValue) {
250-
result[variable[0]] = variable[1]
252+
lodash.set(result, variable[0], variable[1])
251253
}
252254
}
253255

packages/lg/test/commands/lg/expand.test.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,71 @@ describe('lg:expand lg template', async () => {
9393
await TestUtil.compareFiles(path.join(generatedFolderPath, outputFileName), path.join(verifiedFolderPath, '4.testinput.expand.lg'))
9494
})
9595
})
96+
97+
describe('lg:expand lg template with scope', async () => {
98+
after(async function () {
99+
await fs.remove(generatedFolder)
100+
})
101+
102+
before(async function () {
103+
await fs.remove(generatedFolder)
104+
await fs.mkdirp(generatedFolder)
105+
})
106+
107+
const inputFileName = 'accessScope.lg'
108+
let outputFileName = 'accessScope.expand.lg'
109+
110+
let testInputTemplate = 'welcomeUser'
111+
// test access nested path
112+
test
113+
.command(['lg:expand',
114+
'--in',
115+
path.join(__dirname, testcaseFolderPath, inputFileName),
116+
'--out',
117+
generatedFolder,
118+
'--template',
119+
testInputTemplate,
120+
'--testInput',
121+
path.join(__dirname, testcaseFolderPath, 'data.json'),
122+
'-r',
123+
'-f'])
124+
.it('', async () => {
125+
await TestUtil.compareFiles(path.join(generatedFolderPath, outputFileName), path.join(verifiedFolderPath, 'accessScope.testinput.expand.lg'))
126+
})
127+
128+
testInputTemplate = 'AddItemReadBack'
129+
// test multiple nested path access
130+
test
131+
.command(['lg:expand',
132+
'--in',
133+
path.join(__dirname, testcaseFolderPath, inputFileName),
134+
'--out',
135+
generatedFolder,
136+
'--template',
137+
testInputTemplate,
138+
'--testInput',
139+
path.join(__dirname, testcaseFolderPath, 'data.json'),
140+
'-r',
141+
'-f'])
142+
.it('', async () => {
143+
await TestUtil.compareFiles(path.join(generatedFolderPath, outputFileName), path.join(verifiedFolderPath, 'accessScope2.testinput.expand.lg'))
144+
})
145+
146+
testInputTemplate = 'GetIndex'
147+
// test multiple nested path access
148+
test
149+
.command(['lg:expand',
150+
'--in',
151+
path.join(__dirname, testcaseFolderPath, inputFileName),
152+
'--out',
153+
generatedFolder,
154+
'--template',
155+
testInputTemplate,
156+
'--testInput',
157+
path.join(__dirname, testcaseFolderPath, 'data.json'),
158+
'-r',
159+
'-f'])
160+
.it('', async () => {
161+
await TestUtil.compareFiles(path.join(generatedFolderPath, outputFileName), path.join(verifiedFolderPath, 'accessScope3.testinput.expand.lg'))
162+
})
163+
})
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#welcomeUser(age)
2+
- hi ${user.name} at ${age}
3+
- hello ${user.name} at ${age}
4+
5+
# AddItemReadBack
6+
[Activity
7+
Text = ${HelpPrefix()}, I have added "**${dialog.itemTitle}**" to your **${dialog.listType}** list. You have **${count(user.lists[dialog.listType])}** items in your ${dialog.listType} list.
8+
${WelcomeActions()}
9+
]
10+
11+
# HelpPrefix
12+
- Sure
13+
- You bet
14+
- Absolutely
15+
16+
# WelcomeActions
17+
[Activity
18+
SuggestedActions = Add item | View lists | Remove item | Profile | Cancel | Help
19+
]
20+
21+
#GetIndex
22+
- ${user.lists.reminder[1]}
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
{
2-
"time":"morning"
2+
"time":"morning",
3+
"user": {
4+
"name": "cosmic",
5+
"lists": {
6+
"reminder": ["car washing", "laundry", "cooking"]
7+
}
8+
},
9+
"age": 18,
10+
"dialog": {
11+
"itemTitle": "Sunday ToDo",
12+
"listType": "reminder"
13+
}
314
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# welcomeUser
2+
- hi cosmic at 18
3+
- hello cosmic at 18
4+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# AddItemReadBack
2+
- {"lgType":"Activity","text":"Sure, I have added "**Sunday ToDo**" to your **reminder** list. You have **3** items in your reminder list.","suggestedactions":["Add item ","View lists ","Remove item ","Profile ","Cancel ","Help"]}
3+
- {"lgType":"Activity","text":"You bet, I have added "**Sunday ToDo**" to your **reminder** list. You have **3** items in your reminder list.","suggestedactions":["Add item ","View lists ","Remove item ","Profile ","Cancel ","Help"]}
4+
- {"lgType":"Activity","text":"Absolutely, I have added "**Sunday ToDo**" to your **reminder** list. You have **3** items in your reminder list.","suggestedactions":["Add item ","View lists ","Remove item ","Profile ","Cancel ","Help"]}
5+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# GetIndex
2+
- laundry
3+

0 commit comments

Comments
 (0)