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

fix env var access using $env for groups

This commit is contained in:
Hiroyasu Nishiyama 2021-11-27 19:29:57 +09:00
parent 9439cd0e3d
commit c331da7323
4 changed files with 61 additions and 12 deletions

View File

@ -439,8 +439,6 @@ class Flow {
} }
} }
return [env.name, env]; return [env.name, env];
return [env.name, env];
}); });
group._env = Object.fromEntries(entries); group._env = Object.fromEntries(entries);
} }
@ -457,24 +455,24 @@ class Flow {
const val const val
= ((value === "true") || = ((value === "true") ||
(value === true)); (value === true));
return { return [{
val: val val: val
}; }, null];
} }
if (type === "cred") { if (type === "cred") {
return { return [{
val: value val: value
}; }, null];
} }
try { try {
var val = redUtil.evaluateNodeProperty(value, type, node, null, null); var val = redUtil.evaluateNodeProperty(value, type, node, null, null);
return { return [{
val: val val: val
}; }, null];
} }
catch (e) { catch (e) {
this.error(e); this.error(e);
return null; return [null, null];
} }
} }
} }
@ -488,7 +486,7 @@ class Flow {
return this.getGroupEnvSetting(node, parent, name); return this.getGroupEnvSetting(node, parent, name);
} }
} }
return null; return [null, name];
} }
@ -545,6 +543,9 @@ class Flow {
} }
} }
} }
else {
key = key.substring(8);
}
} }
return this.parent.getSetting(key); return this.parent.getSetting(key);
} }

View File

@ -373,10 +373,11 @@ class Subflow extends Flow {
const node = this.subflowInstance; const node = this.subflowInstance;
if (node.g) { if (node.g) {
const group = this.getGroupNode(node.g); const group = this.getGroupNode(node.g);
const result = this.getGroupEnvSetting(node, group, name); const [result, newName] = this.getGroupEnvSetting(node, group, name);
if (result) { if (result) {
return result.val; return result.val;
} }
name = newName;
} }

View File

@ -526,10 +526,11 @@ function getSetting(node, name, flow_) {
if (flow) { if (flow) {
if (node && node.g) { if (node && node.g) {
const group = flow.getGroupNode(node.g); const group = flow.getGroupNode(node.g);
const result = flow.getGroupEnvSetting(node, group, name); const [result, newName] = flow.getGroupEnvSetting(node, group, name);
if (result) { if (result) {
return result.val; return result.val;
} }
name = newName;
} }
return flow.getSetting(name); return flow.getSetting(name);
} }

View File

@ -1275,6 +1275,52 @@ describe('Flow', function() {
} }
}); });
it("can access environment variable property using $parent", function (done) {
try {
after(function() {
delete process.env.V0;
delete process.env.V1;
})
process.env.V0 = "gv0";
process.env.V1 = "gv1";
var config = flowUtils.parseConfig([
{id:"t1",type:"tab",env:[
{"name": "V0", value: "v0", type: "str"}
]},
{id:"g1",type:"group",z:"t1",env:[
{"name": "V0", value: "v1", type: "str"},
{"name": "V1", value: "v2", type: "str"}
]},
{id:"g2",type:"group",z:"t1",g:"g1",env:[
{"name": "V1", value: "v3", type: "str"}
]},
{id:"1",x:10,y:10,z:"t1",type:"test",foo:"${$parent.V0}",wires:[]},
{id:"2",x:10,y:10,z:"t1",g:"g1",type:"test",foo:"${$parent.V0}",wires:[]},
{id:"3",x:10,y:10,z:"t1",g:"g1",type:"test",foo:"${$parent.V1}",wires:[]},
{id:"4",x:10,y:10,z:"t1",g:"g2",type:"test",foo:"${$parent.V1}",wires:[]},
{id:"5",x:10,y:10,z:"t1",type:"test",foo:"${$parent.V1}",wires:[]},
]);
var flow = Flow.create({getSetting:v=>process.env[v]},config,config.flows["t1"]);
flow.start();
var activeNodes = flow.getActiveNodes();
activeNodes["1"].foo.should.equal("gv0");
activeNodes["2"].foo.should.equal("v0");
activeNodes["3"].foo.should.equal("gv1");
activeNodes["4"].foo.should.equal("v2");
activeNodes["5"].foo.should.equal("gv1");
flow.stop().then(function() {
done();
});
}
catch (e) {
console.log(e.stack);
done(e);
}
});
}); });