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]
*/
constructor(parent,globalFlow,subflowDef,subflowInstance) {
// console.log(subflowDef);
console.log(subflowDef);
// console.log("CREATE SUBFLOW",subflowDef.id,subflowInstance.id);
// console.log("SubflowInstance\n"+JSON.stringify(subflowInstance," ",2));
// console.log("SubflowDef\n"+JSON.stringify(subflowDef," ",2));
@ -91,6 +91,15 @@ class Subflow extends Flow {
this.subflowDef = subflowDef;
this.subflowInstance = subflowInstance;
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,55 +208,16 @@ class Subflow extends Flow {
* @return {Object} val value of env var
*/
getSetting(name) {
function lookup(env, name) {
for(var i = env.length -1; i >= 0; i--) {
var item = env[i];
if (item.name === name) {
return item;
}
var env = this.env;
if (env && env.hasOwnProperty(name)) {
var val = env[name];
try {
var ret = redUtil.evaluateNodeProperty(val.value, val.type, null, null, null);
return ret;
}
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 {
return redUtil.evaluateNodeProperty(val.value, val.type, null, null, null);
}
catch (e) {
console.log(e);
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;
}
catch (e) {
this.error(e);
return undefined;
}
}
var parent = this.parent;

View File

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