add support of persistable context to template node

This commit is contained in:
Hiroyasu Nishiyama
2018-07-14 00:11:59 +09:00
parent afe6afca36
commit c72961a52a
3 changed files with 252 additions and 41 deletions

View File

@@ -77,7 +77,7 @@
}</pre>
<p>The resulting property will be:
<pre>Hello Fred. Today is Monday</pre>
<p>It is possible to use a property from the flow context or global context. Just use <code>{{flow.name}}</code> or <code>{{global.name}}</code>.
<p>It is possible to use a property from the flow context or global context. Just use <code>{{flow.name}}</code> or <code>{{global.name}}</code>, or for persistable store <code>store</code> use <code>{{flow[store].name}}</code> or <code>{{flobal[store].name}}</code>.
<p><b>Note: </b>By default, <i>mustache</i> will escape any HTML entities in the values it substitutes.
To prevent this, use <code>{{{triple}}}</code> braces.
</script>

View File

@@ -19,20 +19,39 @@ module.exports = function(RED) {
var mustache = require("mustache");
var yaml = require("js-yaml");
function parseContext(key) {
var match = /^(flow|global)(\[(\w+)\])?\.(.+)/.exec(key);
if (match) {
var parts = {};
parts.type = match[1];
parts.store = (match[3] === '') ? "default" : match[3];
parts.field = match[4];
return parts;
}
return undefined;
}
/**
* Custom Mustache Context capable to resolve message property and node
* Custom Mustache Context capable to collect message property and node
* flow and global context
*/
function NodeContext(msg, nodeContext, parent, escapeStrings) {
function NodeContext(msg, nodeContext, parent, escapeStrings, promises, results) {
this.msgContext = new mustache.Context(msg,parent);
this.nodeContext = nodeContext;
this.escapeStrings = escapeStrings;
this.promises = promises;
this.results = results;
}
NodeContext.prototype = new mustache.Context();
NodeContext.prototype.lookup = function (name) {
var results = this.results;
if (results) {
var val = results.shift();
return val;
}
// try message first:
try {
var value = this.msgContext.lookup(name);
@@ -45,23 +64,40 @@ module.exports = function(RED) {
value = value.replace(/\f/g, "\\f");
value = value.replace(/[\b]/g, "\\b");
}
this.promises.push(Promise.resolve(value));
return value;
}
// try node context:
var dot = name.indexOf(".");
/* istanbul ignore else */
if (dot > 0) {
var contextName = name.substr(0, dot);
var variableName = name.substr(dot + 1);
if (contextName === "flow" && this.nodeContext.flow) {
return this.nodeContext.flow.get(variableName);
// try flow/global context:
var context = parseContext(name);
if (context) {
var type = context.type;
var store = context.store;
var field = context.field;
var target = this.nodeContext[type];
if (target) {
var promise = new Promise((resolve, reject) => {
var callback = (err, val) => {
if (err) {
reject(err);
} else {
resolve(val);
}
};
target.get(field, store, callback);
});
this.promises.push(promise);
return '';
}
else if (contextName === "global" && this.nodeContext.global) {
return this.nodeContext.global.get(variableName);
else {
this.promises.push(Promise.resolve(''));
return '';
}
}
else {
this.promises.push(Promise.resolve(''));
return '';
}
}
catch(err) {
throw err;
@@ -69,7 +105,7 @@ module.exports = function(RED) {
}
NodeContext.prototype.push = function push (view) {
return new NodeContext(view, this.nodeContext,this.msgContext);
return new NodeContext(view, this.nodeContext, this.msgContext, undefined, this.promises, this.results);
};
function TemplateNode(n) {
@@ -83,8 +119,33 @@ module.exports = function(RED) {
var node = this;
node.on("input", function(msg) {
function output(value) {
/* istanbul ignore else */
if (node.outputFormat === "json") {
value = JSON.parse(value);
}
/* istanbul ignore else */
if (node.outputFormat === "yaml") {
value = yaml.load(value);
}
if (node.fieldType === 'msg') {
RED.util.setMessageProperty(msg, node.field, value);
node.send(msg);
} else if ((node.fieldType === 'flow') ||
(node.fieldType === 'global')) {
var context = RED.util.parseContextStore(node.field);
var target = node.context()[node.fieldType];
target.set(context.key, value, context.store, function (err) {
if (err) {
node.error(err, msg);
} else {
node.send(msg);
}
});
}
}
try {
var value;
/***
* Allow template contents to be defined externally
* through inbound msg.template IFF node.template empty
@@ -97,31 +158,18 @@ module.exports = function(RED) {
}
if (node.syntax === "mustache") {
if (node.outputFormat === "json") {
value = mustache.render(template,new NodeContext(msg, node.context(), null, true));
} else {
value = mustache.render(template,new NodeContext(msg, node.context(), null, false));
}
var is_json = (node.outputFormat === "json");
var promises = [];
mustache.render(template, new NodeContext(msg, node.context(), null, is_json, promises, null));
Promise.all(promises).then(function (values) {
var value = mustache.render(template, new NodeContext(msg, node.context(), null, is_json, null, values));
output(value);
}).catch(function (err) {
node.error(err.message);
});
} else {
value = template;
output(template);
}
/* istanbul ignore else */
if (node.outputFormat === "json") {
value = JSON.parse(value);
}
/* istanbul ignore else */
if (node.outputFormat === "yaml") {
value = yaml.load(value);
}
if (node.fieldType === 'msg') {
RED.util.setMessageProperty(msg,node.field,value);
} else if (node.fieldType === 'flow') {
node.context().flow.set(node.field,value);
} else if (node.fieldType === 'global') {
node.context().global.set(node.field,value);
}
node.send(msg);
}
catch(err) {
node.error(err.message);