Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
25 changes: 24 additions & 1 deletion source/nodejs/adaptivecards-templating/src/template-engine.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as AEL from "adaptive-expressions";
const pkg = require('./../package.json');

class EvaluationContext {
private static readonly _reservedFields = ["$data", "$when", "$root", "$index", "$host"];
private static readonly _reservedFields = ["$data", "$when", "$root", "$index", "$host", "$_acTemplateVersion"];

private _stateStack: Array<{ $data: any, $index: any }> = [];
private _$data: any;

$root: any;
$host: any;
$index: number;
$_acTemplateVersion: any;

constructor(context?: IEvaluationContext) {
if (context !== undefined) {
this.$_acTemplateVersion = this.generateVersionJson();

this.$root = context.$root;
this.$host = context.$host;
}
Expand Down Expand Up @@ -49,6 +53,23 @@ class EvaluationContext {
set $data(value: any) {
this._$data = value;
}

generateVersionJson() {
Comment thread
anna-dingler marked this conversation as resolved.
Comment thread
licanhua marked this conversation as resolved.
const version = pkg.version;
Comment thread
anna-dingler marked this conversation as resolved.
const versionSplit = version.split('.');

let patchSplit = [];
Comment thread
anna-dingler marked this conversation as resolved.
if (versionSplit[2]) {
Comment thread
jwoo-msft marked this conversation as resolved.
Outdated
patchSplit = versionSplit[2].split('-');
Comment thread
anna-dingler marked this conversation as resolved.
Outdated
Comment thread
beervoley marked this conversation as resolved.
Outdated
}

return {
"major": parseInt(versionSplit[0]),
Comment thread
jwoo-msft marked this conversation as resolved.
"minor": parseInt(versionSplit[1]),
"patch": parseInt(patchSplit[0]),
"suffix": patchSplit[1] || "",
}
}
}

class TemplateObjectMemory implements AEL.MemoryInterface {
Expand All @@ -58,6 +79,7 @@ class TemplateObjectMemory implements AEL.MemoryInterface {
$data: any;
$index: any;
$host: any;
$_acTemplateVersion: any;

constructor() {
this._memory = new AEL.SimpleObjectMemory(this);
Expand Down Expand Up @@ -166,6 +188,7 @@ export class Template {
memory.$data = context.$data;
memory.$index = context.$index;
memory.$host = context.$host;
memory.$_acTemplateVersion = context.$_acTemplateVersion;

let options: AEL.Options | undefined = undefined;

Expand Down
5 changes: 5 additions & 0 deletions source/nodejs/tests/unit-tests/src/unit-tests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ describe("Test Templating Library", () => {
loadFile("template-test-resources/complex-template-host.data.json"),
loadFile("template-test-resources/complex-template-host.host.json"));
});

it("TemplatingVersion", () => {
runTest(loadFile("template-test-resources/version-template.json"),
loadFile("template-test-resources/version-template.output.json"));
});
});

function runTest(templatePayload: any, expectedOutput: any, data?: any, host?: any) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"$when": "${$_acTemplateVersion.major >= 2}",
"type": "TextBlock",
"text": "$_acTemplateVersion.major >= 2"
},
{
"$when": "${$_acTemplateVersion.minor >= 0}",
"type": "TextBlock",
"text": "$_acTemplateVersion.minor >= 0"
},
{
"$when": "${$_acTemplateVersion.patch >= 0}",
"type": "TextBlock",
"text": "$_acTemplateVersion.patch >= 0"
}
Comment thread
beervoley marked this conversation as resolved.
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "TextBlock",
"text": "$_acTemplateVersion.major >= 2"
},
{
"type": "TextBlock",
"text": "$_acTemplateVersion.minor >= 0"
},
{
"type": "TextBlock",
"text": "$_acTemplateVersion.patch >= 0"
}
]
}