mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Merge pull request #1806 from node-red-hitachi/0.19-template-node-for-persistable-context
Add support of persistable context to template node
This commit is contained in:
commit
5b76c91004
@ -77,7 +77,7 @@
|
|||||||
}</pre>
|
}</pre>
|
||||||
<p>The resulting property will be:
|
<p>The resulting property will be:
|
||||||
<pre>Hello Fred. Today is Monday</pre>
|
<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.
|
<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.
|
To prevent this, use <code>{{{triple}}}</code> braces.
|
||||||
</script>
|
</script>
|
||||||
|
@ -19,20 +19,39 @@ module.exports = function(RED) {
|
|||||||
var mustache = require("mustache");
|
var mustache = require("mustache");
|
||||||
var yaml = require("js-yaml");
|
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
|
* 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.msgContext = new mustache.Context(msg,parent);
|
||||||
this.nodeContext = nodeContext;
|
this.nodeContext = nodeContext;
|
||||||
this.escapeStrings = escapeStrings;
|
this.escapeStrings = escapeStrings;
|
||||||
|
this.promises = promises;
|
||||||
|
this.results = results;
|
||||||
}
|
}
|
||||||
|
|
||||||
NodeContext.prototype = new mustache.Context();
|
NodeContext.prototype = new mustache.Context();
|
||||||
|
|
||||||
NodeContext.prototype.lookup = function (name) {
|
NodeContext.prototype.lookup = function (name) {
|
||||||
|
var results = this.results;
|
||||||
|
if (results) {
|
||||||
|
var val = results.shift();
|
||||||
|
return val;
|
||||||
|
}
|
||||||
// try message first:
|
// try message first:
|
||||||
try {
|
try {
|
||||||
var value = this.msgContext.lookup(name);
|
var value = this.msgContext.lookup(name);
|
||||||
@ -45,23 +64,40 @@ module.exports = function(RED) {
|
|||||||
value = value.replace(/\f/g, "\\f");
|
value = value.replace(/\f/g, "\\f");
|
||||||
value = value.replace(/[\b]/g, "\\b");
|
value = value.replace(/[\b]/g, "\\b");
|
||||||
}
|
}
|
||||||
|
this.promises.push(Promise.resolve(value));
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// try node context:
|
// try flow/global context:
|
||||||
var dot = name.indexOf(".");
|
var context = parseContext(name);
|
||||||
/* istanbul ignore else */
|
if (context) {
|
||||||
if (dot > 0) {
|
var type = context.type;
|
||||||
var contextName = name.substr(0, dot);
|
var store = context.store;
|
||||||
var variableName = name.substr(dot + 1);
|
var field = context.field;
|
||||||
|
var target = this.nodeContext[type];
|
||||||
if (contextName === "flow" && this.nodeContext.flow) {
|
if (target) {
|
||||||
return this.nodeContext.flow.get(variableName);
|
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) {
|
else {
|
||||||
return this.nodeContext.global.get(variableName);
|
this.promises.push(Promise.resolve(''));
|
||||||
|
return '';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
this.promises.push(Promise.resolve(''));
|
||||||
|
return '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch(err) {
|
catch(err) {
|
||||||
throw err;
|
throw err;
|
||||||
@ -69,7 +105,7 @@ module.exports = function(RED) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
NodeContext.prototype.push = function push (view) {
|
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) {
|
function TemplateNode(n) {
|
||||||
@ -83,8 +119,33 @@ module.exports = function(RED) {
|
|||||||
|
|
||||||
var node = this;
|
var node = this;
|
||||||
node.on("input", function(msg) {
|
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 {
|
try {
|
||||||
var value;
|
|
||||||
/***
|
/***
|
||||||
* Allow template contents to be defined externally
|
* Allow template contents to be defined externally
|
||||||
* through inbound msg.template IFF node.template empty
|
* through inbound msg.template IFF node.template empty
|
||||||
@ -97,31 +158,18 @@ module.exports = function(RED) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (node.syntax === "mustache") {
|
if (node.syntax === "mustache") {
|
||||||
if (node.outputFormat === "json") {
|
var is_json = (node.outputFormat === "json");
|
||||||
value = mustache.render(template,new NodeContext(msg, node.context(), null, true));
|
var promises = [];
|
||||||
} else {
|
mustache.render(template, new NodeContext(msg, node.context(), null, is_json, promises, null));
|
||||||
value = mustache.render(template,new NodeContext(msg, node.context(), null, false));
|
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 {
|
} 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) {
|
catch(err) {
|
||||||
node.error(err.message);
|
node.error(err.message);
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
var should = require("should");
|
var should = require("should");
|
||||||
var templateNode = require("../../../../nodes/core/core/80-template.js");
|
var templateNode = require("../../../../nodes/core/core/80-template.js");
|
||||||
|
var Context = require("../../../../red/runtime/nodes/context");
|
||||||
var helper = require("node-red-node-test-helper");
|
var helper = require("node-red-node-test-helper");
|
||||||
|
|
||||||
describe('template node', function() {
|
describe('template node', function() {
|
||||||
@ -28,8 +29,32 @@ describe('template node', function() {
|
|||||||
helper.stopServer(done);
|
helper.stopServer(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
beforeEach(function(done) {
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
function initContext(done) {
|
||||||
|
Context.init({
|
||||||
|
contextStorage: {
|
||||||
|
memory1: {
|
||||||
|
module: "memory"
|
||||||
|
},
|
||||||
|
memory2: {
|
||||||
|
module: "memory"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Context.load().then(function () {
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function() {
|
||||||
helper.unload();
|
helper.unload().then(function () {
|
||||||
|
return Context.clean({allNodes:{}});
|
||||||
|
}).then(function () {
|
||||||
|
return Context.close();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
@ -116,7 +141,6 @@ describe('template node', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
it('should modify payload from flow context', function(done) {
|
it('should modify payload from flow context', function(done) {
|
||||||
var flow = [{id:"n1",z:"t1", type:"template", field:"payload", template:"payload={{flow.value}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
|
var flow = [{id:"n1",z:"t1", type:"template", field:"payload", template:"payload={{flow.value}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
|
||||||
helper.load(templateNode, flow, function() {
|
helper.load(templateNode, flow, function() {
|
||||||
@ -132,6 +156,44 @@ describe('template node', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should modify payload from persistable flow context', function(done) {
|
||||||
|
var flow = [{id:"n1",z:"t1", type:"template", field:"payload", template:"payload={{flow[memory1].value}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
|
||||||
|
helper.load(templateNode, flow, function() {
|
||||||
|
initContext(function () {
|
||||||
|
var n1 = helper.getNode("n1");
|
||||||
|
var n2 = helper.getNode("n2");
|
||||||
|
n2.on("input", function(msg) {
|
||||||
|
msg.should.have.property('topic', 'bar');
|
||||||
|
msg.should.have.property('payload', 'payload=foo');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
n1.context().flow.set("value","foo","memory1",function (err) {
|
||||||
|
n1.receive({payload:"foo",topic: "bar"});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should modify payload from two persistable flow context', function(done) {
|
||||||
|
var flow = [{id:"n1",z:"t1", type:"template", field:"payload", template:"payload={{flow[memory1].value}}/{{flow[memory2].value}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
|
||||||
|
helper.load(templateNode, flow, function() {
|
||||||
|
initContext(function() {
|
||||||
|
var n1 = helper.getNode("n1");
|
||||||
|
var n2 = helper.getNode("n2");
|
||||||
|
n2.on("input", function(msg) {
|
||||||
|
msg.should.have.property('topic', 'bar');
|
||||||
|
msg.should.have.property('payload', 'payload=foo/bar');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
n1.context().flow.set("value","foo","memory1",function (err) {
|
||||||
|
n1.context().flow.set("value","bar","memory2",function (err) {
|
||||||
|
n1.receive({payload:"foo",topic: "bar"});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should modify payload from global context', function(done) {
|
it('should modify payload from global context', function(done) {
|
||||||
var flow = [{id:"n1",z:"t1", type:"template", field:"payload", template:"payload={{global.value}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
|
var flow = [{id:"n1",z:"t1", type:"template", field:"payload", template:"payload={{global.value}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
|
||||||
helper.load(templateNode, flow, function() {
|
helper.load(templateNode, flow, function() {
|
||||||
@ -147,6 +209,64 @@ describe('template node', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should modify payload from persistable global context', function(done) {
|
||||||
|
var flow = [{id:"n1",z:"t1", type:"template", field:"payload", template:"payload={{global[memory1].value}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
|
||||||
|
helper.load(templateNode, flow, function() {
|
||||||
|
initContext(function () {
|
||||||
|
var n1 = helper.getNode("n1");
|
||||||
|
var n2 = helper.getNode("n2");
|
||||||
|
n2.on("input", function(msg) {
|
||||||
|
msg.should.have.property('topic', 'bar');
|
||||||
|
msg.should.have.property('payload', 'payload=foo');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
n1.context().global.set("value","foo","memory1", function (err) {
|
||||||
|
n1.receive({payload:"foo",topic: "bar"});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should modify payload from two persistable global context', function(done) {
|
||||||
|
var flow = [{id:"n1",z:"t1", type:"template", field:"payload", template:"payload={{global[memory1].value}}/{{global[memory2].value}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
|
||||||
|
helper.load(templateNode, flow, function() {
|
||||||
|
initContext(function () {
|
||||||
|
var n1 = helper.getNode("n1");
|
||||||
|
var n2 = helper.getNode("n2");
|
||||||
|
n2.on("input", function(msg) {
|
||||||
|
msg.should.have.property('topic', 'bar');
|
||||||
|
msg.should.have.property('payload', 'payload=foo/bar');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
n1.context().global.set("value","foo","memory1", function (err) {
|
||||||
|
n1.context().global.set("value","bar","memory2", function (err) {
|
||||||
|
n1.receive({payload:"foo",topic: "bar"});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should modify payload from persistable flow & global context', function(done) {
|
||||||
|
var flow = [{id:"n1",z:"t1", type:"template", field:"payload", template:"payload={{flow[memory1].value}}/{{global[memory1].value}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
|
||||||
|
helper.load(templateNode, flow, function() {
|
||||||
|
initContext(function () {
|
||||||
|
var n1 = helper.getNode("n1");
|
||||||
|
var n2 = helper.getNode("n2");
|
||||||
|
n2.on("input", function(msg) {
|
||||||
|
msg.should.have.property('topic', 'bar');
|
||||||
|
msg.should.have.property('payload', 'payload=foo/bar');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
n1.context().flow.set("value","foo","memory1", function (err) {
|
||||||
|
n1.context().global.set("value","bar","memory1", function (err) {
|
||||||
|
n1.receive({payload:"foo",topic: "bar"});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should handle missing node context', function(done) {
|
it('should handle missing node context', function(done) {
|
||||||
// this is artificial test because in flow there is missing z property (probably never happen in real usage)
|
// this is artificial test because in flow there is missing z property (probably never happen in real usage)
|
||||||
var flow = [{id:"n1",type:"template", field:"payload", template:"payload={{flow.value}},{{global.value}}",wires:[["n2"]]},{id:"n2",type:"helper"}];
|
var flow = [{id:"n1",type:"template", field:"payload", template:"payload={{flow.value}},{{global.value}}",wires:[["n2"]]},{id:"n2",type:"helper"}];
|
||||||
@ -206,6 +326,27 @@ describe('template node', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should modify persistable flow context', function(done) {
|
||||||
|
var flow = [{id:"n1",z:"t1", type:"template", field:"#:(memory1)::payload", fieldType:"flow", template:"payload={{payload}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
|
||||||
|
helper.load(templateNode, flow, function() {
|
||||||
|
initContext(function () {
|
||||||
|
var n1 = helper.getNode("n1");
|
||||||
|
var n2 = helper.getNode("n2");
|
||||||
|
n2.on("input", function(msg) {
|
||||||
|
// mesage is intact
|
||||||
|
msg.should.have.property('topic', 'bar');
|
||||||
|
msg.should.have.property('payload', 'foo');
|
||||||
|
// result is in flow context
|
||||||
|
n2.context().flow.get("payload", "memory", function (err, val) {
|
||||||
|
val.should.equal("payload=foo");
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
n1.receive({payload:"foo",topic: "bar"});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should modify global context', function(done) {
|
it('should modify global context', function(done) {
|
||||||
var flow = [{id:"n1",z:"t1", type:"template", field:"payload", fieldType:"global", template:"payload={{payload}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
|
var flow = [{id:"n1",z:"t1", type:"template", field:"payload", fieldType:"global", template:"payload={{payload}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
|
||||||
helper.load(templateNode, flow, function() {
|
helper.load(templateNode, flow, function() {
|
||||||
@ -223,6 +364,27 @@ describe('template node', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should modify persistable global context', function(done) {
|
||||||
|
var flow = [{id:"n1",z:"t1", type:"template", field:"#:(memory1)::payload", fieldType:"global", template:"payload={{payload}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
|
||||||
|
helper.load(templateNode, flow, function() {
|
||||||
|
initContext(function () {
|
||||||
|
var n1 = helper.getNode("n1");
|
||||||
|
var n2 = helper.getNode("n2");
|
||||||
|
n2.on("input", function(msg) {
|
||||||
|
// mesage is intact
|
||||||
|
msg.should.have.property('topic', 'bar');
|
||||||
|
msg.should.have.property('payload', 'foo');
|
||||||
|
// result is in global context
|
||||||
|
n2.context().global.get("payload", "memory", function (err, val) {
|
||||||
|
val.should.equal("payload=foo");
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
n1.receive({payload:"foo",topic: "bar"});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should handle if the field isn\'t set', function(done) {
|
it('should handle if the field isn\'t set', function(done) {
|
||||||
var flow = [{id:"n1", type:"template", template: "payload={{payload}}",wires:[["n2"]]},{id:"n2",type:"helper"}];
|
var flow = [{id:"n1", type:"template", template: "payload={{payload}}",wires:[["n2"]]},{id:"n2",type:"helper"}];
|
||||||
helper.load(templateNode, flow, function() {
|
helper.load(templateNode, flow, function() {
|
||||||
@ -264,6 +426,7 @@ describe('template node', function() {
|
|||||||
n1.receive({payload:{A:"abc"}});
|
n1.receive({payload:{A:"abc"}});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should raise error if passed bad template', function(done) {
|
it('should raise error if passed bad template', function(done) {
|
||||||
var flow = [{id:"n1", type:"template", field: "payload", template: "payload={{payload",wires:[["n2"]]},{id:"n2",type:"helper"}];
|
var flow = [{id:"n1", type:"template", field: "payload", template: "payload={{payload",wires:[["n2"]]},{id:"n2",type:"helper"}];
|
||||||
helper.load(templateNode, flow, function() {
|
helper.load(templateNode, flow, function() {
|
||||||
|
Loading…
Reference in New Issue
Block a user