Skip to content

Commit

Permalink
add isTemplate function (#1735)
Browse files Browse the repository at this point in the history
  • Loading branch information
Danieladu authored Feb 19, 2020
1 parent 5491bbf commit c0c3f8e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
21 changes: 21 additions & 0 deletions libraries/botbuilder-lg/src/evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGFilePa
public static readonly activityAttachmentFunctionName = 'ActivityAttachment';
public static readonly fromFileFunctionName = 'fromFile';
public static readonly templateFunctionName = 'template';
public static readonly isTemplateFunctionName = 'isTemplate';

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

if (name === Evaluator.isTemplateFunctionName) {
return new ExpressionEvaluator(Evaluator.isTemplateFunctionName, BuiltInFunctions.apply(this.isTemplate()), ReturnType.Boolean, this.validateIsTemplate);
}

return baseLookup(name);
}

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

private readonly isTemplate = (): any => (args: readonly any[]): boolean => {
const templateName = args[0].toString();
return templateName in this.templateMap;
}

private readonly validateIsTemplate = (expression: Expression): void => {
if (expression.children.length !== 1) {
throw new Error(`isTemplate should have one parameter`);
}

const children0: Expression = expression.children[0];
if (children0.returnType !== ReturnType.Object && children0.returnType !== ReturnType.String) {
throw new Error(`${ children0 } can't be used as a template name, must be a string value`);
}
}

private readonly fromFile = (): any => (args: readonly any[]): any => {
const filePath: string = path.normalize(ImportResolver.normalizePath(args[0].toString()));
const resourcePath: string = this.getResourcePath(filePath);
Expand Down
13 changes: 13 additions & 0 deletions libraries/botbuilder-lg/tests/lg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -786,4 +786,17 @@ describe('LG', function() {
var evaled = engine.evaluateTemplate('template', {list:[{}], obj : {a : 'a'}});
assert.strictEqual(evaled, 'list and obj are both not empty.');
});

it('TestIsTemplateFunction', function() {
var engine = new TemplateEngine().addFile(GetExampleFilePath('IsTemplate.lg'));

var evaled = engine.evaluateTemplate('template2', {templateName:'template1'});
assert.strictEqual(evaled, 'template template1 exists');

var evaled = engine.evaluateTemplate('template2', {templateName:'wPhrase'});
assert.strictEqual(evaled, 'template wPhrase exists');

var evaled = engine.evaluateTemplate('template2', {templateName:'xxx'});
assert.strictEqual(evaled, 'template xxx does not exist');
});
});
10 changes: 10 additions & 0 deletions libraries/botbuilder-lg/tests/testData/examples/IsTemplate.lg
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[import](2.lg)

# template1
- hi

# template2(templateName)
- IF: ${isTemplate(templateName)}
- template ${templateName} exists
- ELSE:
- template ${templateName} does not exist

0 comments on commit c0c3f8e

Please sign in to comment.