Skip to content

Commit c0c3f8e

Browse files
authored
add isTemplate function (#1735)
1 parent 5491bbf commit c0c3f8e

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

libraries/botbuilder-lg/src/evaluator.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGFilePa
3838
public static readonly activityAttachmentFunctionName = 'ActivityAttachment';
3939
public static readonly fromFileFunctionName = 'fromFile';
4040
public static readonly templateFunctionName = 'template';
41+
public static readonly isTemplateFunctionName = 'isTemplate';
4142

4243
public constructor(templates: LGTemplate[], expressionEngine: ExpressionEngine) {
4344
super();
@@ -399,6 +400,10 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGFilePa
399400
return new ExpressionEvaluator(Evaluator.activityAttachmentFunctionName, BuiltInFunctions.apply(this.activityAttachment()), ReturnType.Object, this.validateActivityAttachment);
400401
}
401402

403+
if (name === Evaluator.isTemplateFunctionName) {
404+
return new ExpressionEvaluator(Evaluator.isTemplateFunctionName, BuiltInFunctions.apply(this.isTemplate()), ReturnType.Boolean, this.validateIsTemplate);
405+
}
406+
402407
return baseLookup(name);
403408
}
404409

@@ -419,6 +424,22 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGFilePa
419424
});
420425
}
421426

427+
private readonly isTemplate = (): any => (args: readonly any[]): boolean => {
428+
const templateName = args[0].toString();
429+
return templateName in this.templateMap;
430+
}
431+
432+
private readonly validateIsTemplate = (expression: Expression): void => {
433+
if (expression.children.length !== 1) {
434+
throw new Error(`isTemplate should have one parameter`);
435+
}
436+
437+
const children0: Expression = expression.children[0];
438+
if (children0.returnType !== ReturnType.Object && children0.returnType !== ReturnType.String) {
439+
throw new Error(`${ children0 } can't be used as a template name, must be a string value`);
440+
}
441+
}
442+
422443
private readonly fromFile = (): any => (args: readonly any[]): any => {
423444
const filePath: string = path.normalize(ImportResolver.normalizePath(args[0].toString()));
424445
const resourcePath: string = this.getResourcePath(filePath);

libraries/botbuilder-lg/tests/lg.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,4 +786,17 @@ describe('LG', function() {
786786
var evaled = engine.evaluateTemplate('template', {list:[{}], obj : {a : 'a'}});
787787
assert.strictEqual(evaled, 'list and obj are both not empty.');
788788
});
789+
790+
it('TestIsTemplateFunction', function() {
791+
var engine = new TemplateEngine().addFile(GetExampleFilePath('IsTemplate.lg'));
792+
793+
var evaled = engine.evaluateTemplate('template2', {templateName:'template1'});
794+
assert.strictEqual(evaled, 'template template1 exists');
795+
796+
var evaled = engine.evaluateTemplate('template2', {templateName:'wPhrase'});
797+
assert.strictEqual(evaled, 'template wPhrase exists');
798+
799+
var evaled = engine.evaluateTemplate('template2', {templateName:'xxx'});
800+
assert.strictEqual(evaled, 'template xxx does not exist');
801+
});
789802
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[import](2.lg)
2+
3+
# template1
4+
- hi
5+
6+
# template2(templateName)
7+
- IF: ${isTemplate(templateName)}
8+
- template ${templateName} exists
9+
- ELSE:
10+
- template ${templateName} does not exist

0 commit comments

Comments
 (0)