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

Merge pull request #3807 from node-red-hitachi/env-var-jsonata

Env var jsonata
This commit is contained in:
Nick O'Leary 2022-09-02 20:47:30 +01:00 committed by GitHub
commit 44300dbb34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 6 deletions

View File

@ -2,7 +2,7 @@ RED.editor.envVarList = (function() {
var currentLocale = 'en-US';
var DEFAULT_ENV_TYPE_LIST = ['str','num','bool','json','bin','env'];
var DEFAULT_ENV_TYPE_LIST_INC_CRED = ['str','num','bool','json','bin','env','cred'];
var DEFAULT_ENV_TYPE_LIST_INC_CRED = ['str','num','bool','json','bin','env','cred','jsonata'];
/**
* Create env var edit interface

View File

@ -696,13 +696,19 @@ function evaluateNodeProperty(value, type, node, msg, callback) {
function prepareJSONataExpression(value,node) {
var expr = jsonata(value);
expr.assign('flowContext', function(val, store) {
if (node) {
return node.context().flow.get(val, store);
}
return "";
});
expr.assign('globalContext', function(val, store) {
if (node) {
return node.context().global.get(val, store);
}
return "";
});
expr.assign('env', function(name) {
var val = getSetting(node, name, node._flow);
var val = getSetting(node, name, node ? node._flow : null);
if (typeof val !== 'undefined') {
return val;
} else {

View File

@ -260,7 +260,8 @@ describe('subflow', function() {
{name: "KB", type: "bool", value: "true"},
{name: "KJ", type: "json", value: "[1,2,3]"},
{name: "Kb", type: "bin", value: "[65,65]"},
{name: "Ke", type: "env", value: "KS"}
{name: "Ke", type: "env", value: "KS"},
{name: "Kj", type: "jsonata", value: "1+2"},
],
wires:[["n2"]]},
{id:"n2", x:10, y:10, z:"t0", type:"helper", wires:[]},
@ -279,7 +280,7 @@ describe('subflow', function() {
]
},
{id:"s1-n1", x:10, y:10, z:"s1", type:"function",
func:"msg.VE = env.get('Ke'); msg.VS = env.get('KS'); msg.VN = env.get('KN'); msg.VB = env.get('KB'); msg.VJ = env.get('KJ'); msg.Vb = env.get('Kb'); return msg;",
func:"msg.VE = env.get('Ke'); msg.VS = env.get('KS'); msg.VN = env.get('KN'); msg.VB = env.get('KB'); msg.VJ = env.get('KJ'); msg.Vb = env.get('Kb'); msg.Vj = env.get('Kj'); return msg;",
wires:[]}
];
helper.load(functionNode, flow, function() {
@ -294,6 +295,7 @@ describe('subflow', function() {
msg.should.have.property("Vb");
should.ok(msg.Vb instanceof Buffer);
msg.should.have.property("VE","STR");
msg.should.have.property("Vj",3);
done();
}
catch (e) {

View File

@ -1322,6 +1322,40 @@ describe('Flow', function() {
});
it("can define environment variable using JSONata", function (done) {
try {
after(function() {
delete process.env.V0;
})
var config = flowUtils.parseConfig([
{id:"t1",type:"tab",env:[
{"name": "V0", value: "1+2", type: "jsonata"}
]},
{id:"g1",type:"group",z:"t1",env:[
{"name": "V1", value: "2+3", type: "jsonata"},
]},
{id:"1",x:10,y:10,z:"t1",g:"g1",type:"test",foo:"$(V0)",wires:[]},
{id:"2",x:10,y:10,z:"t1",g:"g1",type:"test",foo:"$(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(3);
activeNodes["2"].foo.should.equal(5);
flow.stop().then(function() {
done();
});
}
catch (e) {
console.log(e.stack);
done(e);
}
});
});
});