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

convert subflow env vars to dict

This commit is contained in:
Hiroyasu Nishiyama 2019-02-02 08:34:33 +09:00
parent ce507b3b52
commit e843f192ec
2 changed files with 26 additions and 54 deletions

View File

@ -38,7 +38,7 @@ class Subflow extends Flow {
* @param {[type]} subflowInstance [description] * @param {[type]} subflowInstance [description]
*/ */
constructor(parent,globalFlow,subflowDef,subflowInstance) { constructor(parent,globalFlow,subflowDef,subflowInstance) {
// console.log(subflowDef); console.log(subflowDef);
// console.log("CREATE SUBFLOW",subflowDef.id,subflowInstance.id); // console.log("CREATE SUBFLOW",subflowDef.id,subflowInstance.id);
// console.log("SubflowInstance\n"+JSON.stringify(subflowInstance," ",2)); // console.log("SubflowInstance\n"+JSON.stringify(subflowInstance," ",2));
// console.log("SubflowDef\n"+JSON.stringify(subflowDef," ",2)); // console.log("SubflowDef\n"+JSON.stringify(subflowDef," ",2));
@ -91,6 +91,15 @@ class Subflow extends Flow {
this.subflowDef = subflowDef; this.subflowDef = subflowDef;
this.subflowInstance = subflowInstance; this.subflowInstance = subflowInstance;
this.node_map = node_map; this.node_map = node_map;
var env = [];
if (this.subflowDef.env) {
this.subflowDef.env.forEach(e => { env[e.name] = e; });
}
if (this.subflowInstance.env) {
this.subflowInstance.env.forEach(e => { env[e.name] = e; });
}
this.env = env;
} }
/** /**
@ -199,57 +208,18 @@ class Subflow extends Flow {
* @return {Object} val value of env var * @return {Object} val value of env var
*/ */
getSetting(name) { getSetting(name) {
function lookup(env, name) { var env = this.env;
for(var i = env.length -1; i >= 0; i--) { if (env && env.hasOwnProperty(name)) {
var item = env[i]; var val = env[name];
if (item.name === name) {
return item;
}
}
return null;
}
function lookupFlow0(flow, name) {
if (flow.subflowInstance && flow.subflowInstance.env) {
var val = lookup(flow.subflowInstance.env, name);
if (val) {
return val;
}
}
if (flow.subflowDef && flow.subflowDef.env) {
var val = lookup(flow.subflowDef.env, name);
if (val) {
return val;
}
}
return null;
}
function lookupFlow(flow, name) {
var val = lookupFlow0(flow, name);
if (val) {
if ((typeof val === "str) && (val.type" !== "str")) {
try { try {
return redUtil.evaluateNodeProperty(val.value, val.type, null, null, null); var ret = redUtil.evaluateNodeProperty(val.value, val.type, null, null, null);
return ret;
} }
catch (e) { catch (e) {
console.log(e); this.error(e);
return undefined; return undefined;
} }
} }
return val.value;
}
return null;
}
var node = this.node;
if (node) {
var flow = node._flow;
if (flow) {
var val = lookupFlow(flow, name);
if (val) {
return val;
}
}
}
var parent = this.parent; var parent = this.parent;
if (parent) { if (parent) {
var val = parent.getSetting(name); var val = parent.getSetting(name);

View File

@ -567,17 +567,19 @@ describe('Subflow', function() {
}); });
describe("#env var", function() { describe("#env var", function() {
// should be changed according to internal env var representation
function setEnv(node, key, val) { function setEnv(node, key, val) {
var flow = node._flow; var flow = node._flow;
if (flow) { if (flow) {
var sfi = flow.subflowInstance; var env = flow.env;
sfi.env = [ if (!env) {
{ env = flow.env = {};
}
env[key] = {
name: key, name: key,
type: "str", type: "str",
value: val value: val
} };
];
} }
} }