Deprecate synchronous access to jsonata

This commit is contained in:
Nick O'Leary 2023-03-03 11:43:06 +00:00
parent e7c6178391
commit ca53712ee9
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
3 changed files with 54 additions and 34 deletions

View File

@ -294,32 +294,37 @@
} }
try { try {
var result = expr.evaluate(legacyMode?{msg:parsedData}:parsedData); expr.evaluate(legacyMode?{msg:parsedData}:parsedData, (err, result) => {
if (usesContext) { if (err) {
testResultEditor.setValue(RED._("expressionEditor.errors.context-unsupported"),-1); testResultEditor.setValue(RED._("expressionEditor.errors.eval",{message:err.message}),-1);
return; } else {
} if (usesContext) {
if (usesEnv) { testResultEditor.setValue(RED._("expressionEditor.errors.context-unsupported"),-1);
testResultEditor.setValue(RED._("expressionEditor.errors.env-unsupported"),-1); return;
return; }
} if (usesEnv) {
if (usesMoment) { testResultEditor.setValue(RED._("expressionEditor.errors.env-unsupported"),-1);
testResultEditor.setValue(RED._("expressionEditor.errors.moment-unsupported"),-1); return;
return; }
} if (usesMoment) {
if (usesClone) { testResultEditor.setValue(RED._("expressionEditor.errors.moment-unsupported"),-1);
testResultEditor.setValue(RED._("expressionEditor.errors.clone-unsupported"),-1); return;
return; }
} if (usesClone) {
testResultEditor.setValue(RED._("expressionEditor.errors.clone-unsupported"),-1);
var formattedResult; return;
if (result !== undefined) { }
formattedResult = JSON.stringify(result,null,4);
} else { var formattedResult;
formattedResult = RED._("expressionEditor.noMatch"); if (result !== undefined) {
} formattedResult = JSON.stringify(result,null,4);
testResultEditor.setValue(formattedResult,-1); } else {
} catch(err) { formattedResult = RED._("expressionEditor.noMatch");
}
testResultEditor.setValue(formattedResult,-1);
}
});
} catch(err) {
testResultEditor.setValue(RED._("expressionEditor.errors.eval",{message:err.message}),-1); testResultEditor.setValue(RED._("expressionEditor.errors.eval",{message:err.message}),-1);
} }
} }

View File

@ -117,14 +117,21 @@ module.exports = function(RED) {
if (p.v) { if (p.v) {
try { try {
var exp = RED.util.prepareJSONataExpression(p.v, node); var exp = RED.util.prepareJSONataExpression(p.v, node);
var val = RED.util.evaluateJSONataExpression(exp, msg); RED.util.evaluateJSONataExpression(exp, msg, (err, newValue) => {
RED.util.setMessageProperty(msg, property, val, true); if (err) {
} errors.push(err.toString())
catch (err) { } else {
RED.util.setMessageProperty(msg,property,newValue,true);
}
evaluateProperty(doneEvaluating)
});
} catch (err) {
errors.push(err.message); errors.push(err.message);
evaluateProperty(doneEvaluating)
} }
} else {
evaluateProperty(doneEvaluating)
} }
evaluateProperty(doneEvaluating)
} else { } else {
try { try {
RED.util.evaluateNodeProperty(value, valueType, node, msg, (err, newValue) => { RED.util.evaluateNodeProperty(value, valueType, node, msg, (err, newValue) => {

View File

@ -25,7 +25,7 @@ const moment = require("moment-timezone");
const safeJSONStringify = require("json-stringify-safe"); const safeJSONStringify = require("json-stringify-safe");
const util = require("util"); const util = require("util");
const { hasOwnProperty } = Object.prototype; const { hasOwnProperty } = Object.prototype;
const log = require("./log")
/** /**
* Safely returns the object construtor name. * Safely returns the object construtor name.
* @return {String} the name of the object constructor if it exists, empty string otherwise. * @return {String} the name of the object constructor if it exists, empty string otherwise.
@ -671,8 +671,11 @@ function evaluateNodeProperty(value, type, node, msg, callback) {
} else if (type === 'bool') { } else if (type === 'bool') {
result = /^true$/i.test(value); result = /^true$/i.test(value);
} else if (type === 'jsonata') { } else if (type === 'jsonata') {
var expr = prepareJSONataExpression(value,node); var expr = prepareJSONataExpression(value, node);
result = evaluateJSONataExpression(expr,msg); result = evaluateJSONataExpression(expr, msg, callback);
if (callback) {
return
}
} else if (type === 'env') { } else if (type === 'env') {
result = evaluateEnvProperty(value, node); result = evaluateEnvProperty(value, node);
} }
@ -767,6 +770,11 @@ function evaluateJSONataExpression(expr,msg,callback) {
}) })
}); });
} }
} else {
log.warn('Deprecated API warning: Calls to RED.util.evaluateJSONataExpression must include a callback. '+
'This will not be optional in Node-RED 4.0. Please identify the node from the following stack '+
'and check for an update on npm. If none is available, please notify the node author.')
log.warn(new Error().stack)
} }
return expr.evaluate(context, bindings, callback); return expr.evaluate(context, bindings, callback);
} }