Skip to content
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

add isTemplate function #1735

Merged
merged 1 commit into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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