Add JSONata expr tester and improved feedback

This commit is contained in:
Nick O'Leary
2017-05-05 11:23:24 +01:00
parent b030e935ce
commit dbf0486acb
15 changed files with 362 additions and 52 deletions

View File

@@ -398,9 +398,15 @@
"expressionEditor": {
"functions": "Functions",
"insert": "Insert",
"title": "JSONata Expression editor"
"title": "JSONata Expression editor",
"data": "Example message",
"result": "Result",
"format": "format expression",
"compatMode": "Compatibility mode enabled",
"compatModeDesc": "<h3>JSONata compatibility mode</h3><p> The current expression appears to still reference <code>msg</code> so will be evaluated in compatibility mode. Please update the expression to not use <code>msg</code> as this mode will be removed in the future.</p><p> When JSONata support was first added to Node-RED, it required the expression to reference the <code>msg</code> object. For example <code>msg.payload</code> would be used to access the payload.</p><p> That is no longer necessary as the expression will be evaluated against the message directly. To access the payload, the expression should be just <code>payload</code>.</p>"
},
"jsonEditor": {
"title": "JSON editor"
"title": "JSON editor",
"format": "format JSON"
}
}

View File

@@ -110,16 +110,11 @@
"args": "object",
"desc": "Splits an object containing key/value pairs into an array of objects, each of which has a single key/value pair from the input object. If the parameter is an array of objects, then the resultant array contains an object for every key/value pair in every object in the supplied array."
},
"$context": {
"args": "string",
"desc": "Retrieves a node context property."
},
"$flow": {
"$flowContext": {
"args": "string",
"desc": "Retrieves a flow context property."
},
"$global": {
"$globalContext": {
"args": "string",
"desc": "Retrieves a global context property."
}

View File

@@ -323,25 +323,33 @@ function evaluateNodeProperty(value, type, node, msg) {
} else if (type === 'bool') {
return /^true$/i.test(value);
} else if (type === 'jsonata') {
return prepareJSONataExpression(value,node).evaluate({msg:msg});
var expr = prepareJSONataExpression(value,node);
return evaluateJSONataExpression(expr,msg);
}
return value;
}
function prepareJSONataExpression(value,node) {
var expr = jsonata(value);
expr.assign('context',function(val) {
return node.context().get(val);
});
expr.assign('flow',function(val) {
expr.assign('flowContext',function(val) {
return node.context().flow.get(val);
});
expr.assign('global',function(val) {
expr.assign('globalContext',function(val) {
return node.context().global(val);
});
expr._legacyMode = /(^|[^a-zA-Z0-9_'"])msg([^a-zA-Z0-9_'"]|$)/.test(value);
return expr;
}
function evaluateJSONataExpression(expr,msg) {
var context = msg;
if (expr._legacyMode) {
context = {msg:msg};
}
return expr.evaluate(context);
}
function normaliseNodeTypeName(name) {
var result = name.replace(/[^a-zA-Z0-9]/g, " ");
result = result.trim();
@@ -366,5 +374,6 @@ module.exports = {
evaluateNodeProperty: evaluateNodeProperty,
normalisePropertyExpression: normalisePropertyExpression,
normaliseNodeTypeName: normaliseNodeTypeName,
prepareJSONataExpression: prepareJSONataExpression
prepareJSONataExpression: prepareJSONataExpression,
evaluateJSONataExpression: evaluateJSONataExpression
};