Skip to content

Commit

Permalink
[LG] Redo the default fallback of namespace (#2882)
Browse files Browse the repository at this point in the history
  • Loading branch information
Danieladu authored Oct 9, 2020
1 parent eef341f commit 1b0a7d2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
13 changes: 6 additions & 7 deletions libraries/botbuilder-lg/src/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { AnalyzerResult } from './analyzerResult';
import { TemplateErrors } from './templateErrors';
import { TemplateExtensions } from './templateExtensions';
import { EvaluationOptions, LGLineBreakStyle } from './evaluationOptions';
import { isAbsolute, basename } from 'path';
import { basename } from 'path';
import { StaticChecker } from './staticChecker';
import { LGResource } from './lgResource';
import { CustomizedMemory } from './customizedMemory';
Expand Down Expand Up @@ -549,7 +549,10 @@ export class Templates implements Iterable<Template> {
const globalFuncs = curTemplates.getGlobalFunctionTable(curTemplates.options);
for (const templateName of globalFuncs) {
if (curTemplates.items.find((u) => u.name === templateName) !== undefined) {
const newGlobalName = `${curTemplates.namespace}.${templateName}`;
const prefix =
!curTemplates.namespace || !curTemplates.namespace.trim() ? '' : curTemplates.namespace + '.';

const newGlobalName = prefix + templateName;
Expression.functions.add(
newGlobalName,
new ExpressionEvaluator(
Expand Down Expand Up @@ -606,11 +609,7 @@ export class Templates implements Iterable<Template> {
private extractNamespace(options: string[]): string {
let result = this.extractOptionByKey(this.namespaceKey, options);
if (!result) {
if (isAbsolute(this.source)) {
result = basename(this.source).split('.')[0];
} else {
throw new Error('namespace is required or the id should be an absoulte path!"');
}
result = basename(this.id || '').split('.')[0];
}

return result;
Expand Down
27 changes: 27 additions & 0 deletions libraries/botbuilder-lg/tests/lg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ describe('LG', function() {
MemoryAccess: Templates.parseFile(GetExampleFilePath('MemoryAccess.lg')),
ReExecute: Templates.parseFile(GetExampleFilePath('ReExecute.lg')),
inject: Templates.parseFile(GetExampleFilePath('./injectionTest/inject.lg')),
injectWithoutNamespace: Templates.parseFile(GetExampleFilePath('./injectionTest/injectWithoutNamespace.lg')),
StrictModeTrue: Templates.parseFile(GetExampleFilePath('./EvaluationOptions/StrictModeTrue.lg')),
a1: Templates.parseFile(GetExampleFilePath('./EvaluationOptions/a1.lg')),
a2: Templates.parseFile(GetExampleFilePath('./EvaluationOptions/a2.lg')),
Expand Down Expand Up @@ -1413,4 +1414,30 @@ describe('LG', function() {
var {value: evaled, error} = Expression.parse('common.sumFourNumbers(i, j, k, l)').tryEvaluate(scope2);
assert.strictEqual(10, evaled);
});

it('TestInjectLGWithoutNamespace', function() {
const lgPath = GetExampleFilePath('./injectionTest/injectWithoutNamespace.lg');
let resource = new LGResource('myId', lgPath, fs.readFileSync(lgPath, 'utf-8'));
Templates.parseResource(resource);

var { value: evaled, error } = Expression.parse('myId.greeting()').tryEvaluate({ name: 'Alice' });
assert(error === undefined);
assert.strictEqual('hi Alice', evaled);

// using the fuileName parsed from Id as the namespace
resource = new LGResource('./path/myNewId.lg', lgPath, fs.readFileSync(lgPath, 'utf-8'));
Templates.parseResource(resource);

var { value: evaled, error } = Expression.parse('myNewId.greeting()').tryEvaluate({ name: 'Alice' });
assert(error === undefined);
assert.strictEqual('hi Alice', evaled);

// With empty id
resource = new LGResource('', lgPath, fs.readFileSync(lgPath, 'utf-8'));
Templates.parseResource(resource);

var { value: evaled, error } = Expression.parse('greeting()').tryEvaluate({ name: 'Alice' });
assert(error === undefined);
assert.strictEqual('hi Alice', evaled);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
> !# @Exports = greeting

# greeting
- hi ${name}

0 comments on commit 1b0a7d2

Please sign in to comment.