1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

[Inject] Use legacy properies by preference

This commit is contained in:
Nick O'Leary 2020-05-11 16:07:26 +01:00
parent 247fa0ce7c
commit 717bfffa63
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
3 changed files with 52 additions and 41 deletions

View File

@ -168,16 +168,14 @@
color:"#a6bbcf", color:"#a6bbcf",
defaults: { defaults: {
name: {value:""}, name: {value:""},
props:{value:[{p:"payload",v:"",vt:"date"},{p:"topic",v:"",vt:"str"}]}, props:{value:[{p:"payload"},{p:"topic",vt:"str"}]},
repeat: {value:"", validate:function(v) { return ((v === "") || (RED.validators.number(v) && (v >= 0) && (v <= 2147483))) }}, repeat: {value:"", validate:function(v) { return ((v === "") || (RED.validators.number(v) && (v >= 0) && (v <= 2147483))) }},
crontab: {value:""}, crontab: {value:""},
once: {value:false}, once: {value:false},
onceDelay: {value:0.1}, onceDelay: {value:0.1},
/* Legacy */
topic: {value:""}, topic: {value:""},
payload: {value:"", validate: RED.validators.typedInput("payloadType")}, payload: {value:"", validate: RED.validators.typedInput("payloadType")},
payloadType: {value:"date"}, payloadType: {value:"date"},
/* */
}, },
icon: "inject.svg", icon: "inject.svg",
inputs:0, inputs:0,
@ -206,10 +204,10 @@
} }
lab += props[i].p+": "; lab += props[i].p+": ";
var propType = props[i].vt; var propType = props[i].p === "payload"? this.payloadType : props[i].vt;
if (propType === "json") { if (propType === "json") {
try { try {
var parsedProp = JSON.parse(props[i].v); var parsedProp = JSON.parse(props[i].p === "payload"? this.payload : props[i].v);
propType = typeof parsedProp; propType = typeof parsedProp;
if (propType === "object" && Array.isArray(parsedProp)) { if (propType === "object" && Array.isArray(parsedProp)) {
propType = "Array"; propType = "Array";
@ -237,30 +235,9 @@
return this.name+suffix; return this.name+suffix;
} }
var payloadProperty; var payload = this.payload || "";
var topicProperty; var payloadType = this.payloadType || "str";
var payload; var topic = this.topic || "";
var payloadType;
var topic;
if (Array.isArray(this.props)) {
// find the payload & topic if they are set
for (var i=0,l=this.props.length; i<l; i++) {
if (this.props[i].p === 'payload') {
payloadProperty = this.props[i];
} else if (this.props[i].p === 'topic' && this.props[i].vt === 'str') {
topicProperty = this.props[i];
}
}
payload = payloadProperty === undefined ? "" : payloadProperty.v;
payloadType = payloadProperty === undefined ? "str" : payloadProperty.vt;
topic = topicProperty === undefined ? "" : topicProperty.v;
} else {
/* Legacy */
payload = this.payload || "";
payloadType = this.payloadType || "str";
topic = this.topic || "";
}
if (payloadType === "string" || if (payloadType === "string" ||
payloadType === "str" || payloadType === "str" ||
@ -490,7 +467,16 @@
for (var i=0; i<this.props.length; i++) { for (var i=0; i<this.props.length; i++) {
var prop = this.props[i]; var prop = this.props[i];
$("#node-input-property-container").editableList('addItem',prop); var newProp = { p: prop.p, v: prop.v, vt: prop.vt };
if (newProp.v === undefined) {
if (prop.p === 'payload') {
newProp.v = this.payload ? this.payload : '';
newProp.vt = this.payloadType ? this.payloadType : 'date';
} else if (prop.p === 'topic' && prop.vt === "str") {
newProp.v = this.topic ? this.topic : '';
}
}
$("#node-input-property-container").editableList('addItem',newProp);
} }
$("#inject-time-type-select").trigger("change"); $("#inject-time-type-select").trigger("change");
@ -606,14 +592,13 @@
if (p.p === "payload") { // save payload to old "legacy" property if (p.p === "payload") { // save payload to old "legacy" property
node.payloadType = p.vt; node.payloadType = p.vt;
node.payload = p.v; node.payload = p.v;
} delete p.v;
else if (p.p === "topic" && p.vt === "str") { delete p.vt;
} else if (p.p === "topic" && p.vt === "str") {
node.topic = p.v; node.topic = p.v;
node.props.push(p); delete p.v;
}
else {
node.props.push(p);
} }
node.props.push(p);
} }
}); });
}, },

View File

@ -20,7 +20,7 @@ module.exports = function(RED) {
function InjectNode(n) { function InjectNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
/* Handle legacy */ /* Handle legacy */
if(!Array.isArray(n.props)){ if(!Array.isArray(n.props)){
n.props = []; n.props = [];
@ -34,8 +34,17 @@ module.exports = function(RED) {
v:n.topic, v:n.topic,
vt:'str' vt:'str'
}); });
} else {
for (var i=0,l=n.props.length; i<l; i++) {
if (n.props[i].p === 'payload' && !n.props[i].hasOwnProperty('v')) {
n.props[i].v = n.payload;
n.props[i].vt = n.payloadType;
} else if (n.props[i].p === 'topic' && n.props[i].vt === 'str' && !n.props[i].hasOwnProperty('v')) {
n.props[i].v = n.topic;
}
}
} }
this.props = n.props; this.props = n.props;
this.repeat = n.repeat; this.repeat = n.repeat;
this.crontab = n.crontab; this.crontab = n.crontab;
@ -43,9 +52,6 @@ module.exports = function(RED) {
this.onceDelay = (n.onceDelay || 0.1) * 1000; this.onceDelay = (n.onceDelay || 0.1) * 1000;
this.interval_id = null; this.interval_id = null;
this.cronjob = null; this.cronjob = null;
if (n.payload && n.payloadType) { // load up original payload
this.props.unshift({p:"payload",v:n.payload,vt:n.payloadType});
}
var node = this; var node = this;
node.props.forEach(function (prop) { node.props.forEach(function (prop) {

View File

@ -510,6 +510,26 @@ describe('inject node', function() {
}); });
}); });
it('should inject multiple properties using legacy props if needed', function (done) {
var flow = [{id: "n1", type: "inject", payload:"123", payloadType:"num", topic:"foo", props: [{p:"topic", vt:"str"}, {p:"payload"}], wires: [["n2"]], z: "flow"},
{id: "n2", type: "helper"}];
helper.load(injectNode, flow, function () {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function (msg) {
try {
msg.should.have.property("topic", "foo");
msg.should.have.property("payload", 123);
done();
} catch (err) {
done(err);
}
});
n1.receive({});
});
});
it('should report invalid JSONata expression', function (done) { it('should report invalid JSONata expression', function (done) {
var flow = [{id: "n1", type: "inject", props: [{p:"topic", v:"t1", vt:"str"}, {p:"payload", v:"@", vt:"jsonata"}], wires: [["n2"]], z: "flow"}, var flow = [{id: "n1", type: "inject", props: [{p:"topic", v:"t1", vt:"str"}, {p:"payload", v:"@", vt:"jsonata"}], wires: [["n2"]], z: "flow"},
{id: "n2", type: "helper"}]; {id: "n2", type: "helper"}];