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

Support output editting of DEBUG node using JSONata

This commit is contained in:
Hiroyasu Nishiyama 2018-12-01 18:03:04 +01:00
parent 2060af8a92
commit 3c4f4d27d6
7 changed files with 118 additions and 25 deletions

View File

@ -5,6 +5,14 @@
<input id="node-input-typed-complete" type="text" style="width: 70%">
<input id="node-input-complete" type="hidden">
</div>
<div class="form-row">
<label for="node-input-edit"><i class="fa fa-calculator"></i> <span data-i18n="debug.edit"></span></label>
<input type="text" id="node-input-edit" style="width:70%;">
<input type="hidden" id="node-input-editType">
</div>
<div class="form-row">
<label for="node-input-tosidebar"><i class="fa fa-random"></i> <span data-i18n="debug.to"></span></label>
<label for="node-input-tosidebar" style="width:70%">
@ -38,6 +46,8 @@
category: 'output',
defaults: {
name: {value:""},
edit: {value:""},
editType: {value:"none"},
active: {value:true},
tosidebar: {value:true},
console: {value:false},
@ -245,6 +255,18 @@
delete RED._debug;
},
oneditprepare: function() {
var none = {
value: "none",
label: RED._("node-red:debug.none"),
hasValue: false
};
$("#node-input-edit").typedInput({
default: 'none',
typeField: $("#node-input-editType"),
types:[none, 'jsonata']
});
$("#node-input-edit").change();
if (this.tosidebar === undefined) {
this.tosidebar = true;
$("#node-input-tosidebar").prop('checked', true);

View File

@ -1,4 +1,3 @@
module.exports = function(RED) {
"use strict";
var util = require("util");
@ -8,6 +7,23 @@ module.exports = function(RED) {
var useColors = RED.settings.debugUseColors || false;
util.inspect.styles.boolean = "red";
function editValue(exp, val) {
return new Promise((resolve, reject) => {
if (exp) {
RED.util.evaluateJSONataExpression(exp, val, (err, value) => {
if (err) {
reject(RED._("debug.invalid-exp", {error: edit_exp}));
} else {
resolve(value);
}
});
}
else {
resolve(val);
}
});
}
function DebugNode(n) {
RED.nodes.createNode(this,n);
this.name = n.name;
@ -43,16 +59,29 @@ module.exports = function(RED) {
"50": "green",
"60": "blue"
};
var edit = null;
if (n.editType === "jsonata") {
var edit_exp = n.edit;
try {
edit = RED.util.prepareJSONataExpression(edit_exp, this);
}
catch (e) {
node.error(RED._("debug.invalid-exp", {error: edit_exp}));
return;
}
}
this.on("input",function(msg) {
if (this.complete === "true") {
// debug complete msg object
if (this.console === "true") {
node.log("\n"+util.inspect(msg, {colors:useColors, depth:10}));
}
if (this.active && this.tosidebar) {
sendDebug({id:node.id, name:node.name, topic:msg.topic, msg:msg, _path:msg._path});
}
editValue(edit, msg).then(val => {
if (this.console === "true") {
node.log("\n"+util.inspect(val, {colors:useColors, depth:10}));
}
if (this.active && this.tosidebar) {
sendDebug({id:node.id, name:node.name, topic:val.topic, msg:val, _path:val._path});
}
});
}
else {
// debug user defined msg property
@ -66,26 +95,28 @@ module.exports = function(RED) {
output = undefined;
}
}
if (this.console === "true") {
if (typeof output === "string") {
node.log((output.indexOf("\n") !== -1 ? "\n" : "") + output);
} else if (typeof output === "object") {
node.log("\n"+util.inspect(output, {colors:useColors, depth:10}));
} else {
node.log(util.inspect(output, {colors:useColors}));
editValue(edit, output).then(val => {
if (this.console === "true") {
if (typeof val === "string") {
node.log((val.indexOf("\n") !== -1 ? "\n" : "") + val);
} else if (typeof val === "object") {
node.log("\n"+util.inspect(val, {colors:useColors, depth:10}));
} else {
node.log(util.inspect(val, {colors:useColors}));
}
}
}
if (this.tostatus === true) {
var st = util.inspect(output);
var severity = node.severity;
if (st.length > 32) { st = st.substr(0,32) + "..."; }
node.status({fill:colors[severity], shape:"dot", text:st});
}
if (this.active) {
if (this.tosidebar == true) {
sendDebug({id:node.id, z:node.z, name:node.name, topic:msg.topic, property:property, msg:output, _path:msg._path});
if (this.tostatus === true) {
var st = util.inspect(val);
var severity = node.severity;
if (st.length > 32) { st = st.substr(0,32) + "..."; }
node.status({fill:colors[severity], shape:"dot", text:st});
}
}
if (this.active) {
if (this.tosidebar == true) {
sendDebug({id:node.id, z:node.z, name:node.name, topic:msg.topic, property:property, msg:val, _path:msg._path});
}
}
});
}
});
}

View File

@ -23,4 +23,5 @@
Clicking on the source node id will reveal that node within the workspace.</p>
<p>The button on the node can be used to enable or disable its output. It is recommended to disable or remove any Debug nodes that are not being used.</p>
<p>The node can also be configured to send all messages to the runtime log, or to send short (32 characters) to the status text under the debug node.</p>
<p>Output of this node can be edited using JSONata expression by specifying the expression on <b>Edit</b> field.</p>
</script>

View File

@ -103,6 +103,9 @@
},
"debug": {
"output": "Output",
"edit": "Edit",
"none": "None",
"invalid-exp": "Invalid JSONata expression: __error__",
"msgprop": "message property",
"msgobj": "complete msg object",
"to": "To",

View File

@ -22,5 +22,6 @@
<p>メッセージを受信した時刻、送信ード、メッセージの型に関する情報を「デバッグ」サイドバーに表示されたメッセージに付随して表示します。送信元ードのIDを選択すると、ワークスペース内の対応ードを確認できます。</p>
<p>出力の有効/無効はード上のボタンで切り替えられます。フロー上で未使用のdebugードは、無効化するか削除することを推奨します。</p>
<p>全てのメッセージをランタイムログに送付、もしくは、(32文字の)短いデータをdebugードの下のステータステキストに表示することも可能です。</p>
<p>このードの出力は、JSONata式を<b>編集</b>フィールドに指定することで変更できます。</p>
</script>

View File

@ -103,6 +103,9 @@
},
"debug": {
"output": "対象",
"edit": "編集",
"none": "無し",
"invalid-exp": "JSONata式が不正: __error__",
"msgprop": "メッセージプロパティ",
"msgobj": "msgオブジェクト全体",
"to": "出力先",

View File

@ -356,6 +356,38 @@ describe('debug node', function() {
});
});
it('should publish payload with edit', function(done) {
var flow = [{id:"n1", type:"debug", name:"Debug",
editType: "jsonata", edit: '"<" & $ & ">"'}];
helper.load(debugNode, flow, function() {
var n1 = helper.getNode("n1");
websocket_test(function() {
n1.emit("input", {payload:"test"});
}, function(msg) {
JSON.parse(msg).should.eql([{
topic:"debug",data:{id:"n1",name:"Debug",msg:"<test>",
format:"string[6]",property:"payload"}
}]);
}, done);
});
});
it('should publish complete message with edit', function(done) {
var flow = [{id:"n1", type:"debug", name:"Debug", complete: "true",
editType: "jsonata", edit: '"<" & payload & ">"'}];
helper.load(debugNode, flow, function() {
var n1 = helper.getNode("n1");
websocket_test(function() {
n1.emit("input", {payload:"test"});
}, function(msg) {
JSON.parse(msg).should.eql([{
topic:"debug",data:{id:"n1",name:"Debug",msg:"<test>",
format:"string[6]"}
}]);
}, done);
});
});
it('should truncate a long message', function(done) {
var flow = [{id:"n1", type:"debug" }];
helper.load(debugNode, flow, function() {