Add support of subflow env var

This commit is contained in:
Hiroyasu Nishiyama
2019-01-26 23:15:20 +09:00
parent 4baaaa8d59
commit a413f3cded
13 changed files with 835 additions and 24 deletions

View File

@@ -34,6 +34,9 @@ function Node(n) {
if (n._alias) {
this._alias = n._alias;
}
if (n.env) {
this.env = n.env;
}
if (n._flow) {
// Make this a non-enumerable property as it may cause
// circular references. Any existing code that tries to JSON serialise
@@ -236,6 +239,22 @@ Node.prototype.receive = function(msg) {
}
};
Node.prototype.getenv = function(name) {
var flow = this._flow;
if (flow) {
var val = flow.getSetting(name);
return val;
}
return undefined;
};
Node.prototype.setenv = function(name, val) {
var flow = this._flow;
if (flow) {
flow.setSetting(name, val);
}
};
function log_helper(self, level, msg) {
var o = {
level: level,

View File

@@ -461,6 +461,10 @@ class Flow {
}
console.log("==================")
}
getenv(name) {
return process.env[name];
}
}
/**

View File

@@ -22,6 +22,35 @@ const flowUtil = require("./util");
var Log;
/**
* Convert env var definition of subflow template and instance to env var dic
* @param {Hash} env1 env var definition of template
* @param {Hash} env2 env var definition of instance
* @return {Hash} env var dic
*/
function env2Dic(env0, env1) {
var dic = {};
function add(env) {
for(var i = 0; i < env.length; i++) {
var item = env[i];
var name = item.name;
var info = Object.assign({}, item.info);
info.name = name;
dic[name] = item;
dic[name+"_type"] = { value: item.type };
dic[name+"_info"] = { value: info };
}
}
if (env0) {
add(env0);
}
if (env1) {
add(env1);
}
return dic;
}
/**
* This class represents a subflow - which is handled as a special type of Flow
*/
@@ -104,12 +133,14 @@ class Subflow extends Flow {
var self = this;
// Create a subflow node to accept inbound messages and route appropriately
var Node = require("../Node");
var env = env2Dic(this.subflowDef.env, this.subflowInstance.env);
var subflowInstanceConfig = {
id: this.subflowInstance.id,
type: this.subflowInstance.type,
z: this.subflowInstance.z,
name: this.subflowInstance.name,
wires: [],
env: env,
_flow: this
}
if (this.subflowDef.in) {
@@ -164,7 +195,7 @@ class Subflow extends Flow {
self.node._updateWires(subflowInstanceConfig.wires);
}
}
}
};
// Wire the subflow outputs
if (this.subflowDef.out) {
@@ -172,7 +203,7 @@ class Subflow extends Flow {
for (var i=0;i<this.subflowDef.out.length;i++) {
// i: the output index
// This is what this Output is wired to
wires = this.subflowDef.out[i].wires;
var wires = this.subflowDef.out[i].wires;
for (var j=0;j<wires.length;j++) {
if (wires[j].id === this.subflowDef.id) {
// A subflow input wired straight to a subflow output
@@ -192,6 +223,42 @@ class Subflow extends Flow {
super.start(diff);
}
/**
* Get environment variable of subflow
* @param {String} name name of env var
* @return {Object} val value of env var
*/
getSetting(name) {
var node = this.node;
if (node) {
var env = node.env;
if (env && env.hasOwnProperty(name)) {
return env[name].value;
}
}
var parent = this.parent;
if (parent) {
var val = parent.getSetting(name);
return val;
}
return undefined;
}
/**
* Set environment variable of subflow
* @param {String} name name of env var
* @param {Object} val value of env var
*/
setSetting(name, val) {
var node = this.node;
if (node) {
var env = node.env;
if (env) {
env[name] = { name: name, type: "str", value: val, info: null };
}
}
}
/**
* Handle a status event from a node within this flow.
* @param {Node} node The original node that triggered the event
@@ -234,7 +301,6 @@ class Subflow extends Flow {
return handled;
}
}