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

Merge pull request #1995 from node-red-hitachi/debug-node-with-jsonata

Add support of output editing in DEBUG node using JSONata
This commit is contained in:
Nick O'Leary 2018-12-13 11:05:21 +00:00 committed by GitHub
commit 29a257d17a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 96 additions and 9 deletions

View File

@ -4,7 +4,9 @@
<label for="node-input-typed-complete"><i class="fa fa-list"></i> <span data-i18n="debug.output"></span></label>
<input id="node-input-typed-complete" type="text" style="width: 70%">
<input id="node-input-complete" type="hidden">
<input id="node-input-targetType" type="hidden">
</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%">
@ -42,11 +44,15 @@
tosidebar: {value:true},
console: {value:false},
tostatus: {value:false},
complete: {value:"false", required:true}
complete: {value:"false", required:true},
targetType: {value:undefined}
},
label: function() {
var suffix = "";
if (this.console === true || this.console === "true") { suffix = " ⇲"; }
if (this.targetType === "jsonata") {
return (this.name || "JSONata") + suffix;
}
if (this.complete === true || this.complete === "true") {
return (this.name||"msg") + suffix;
} else {
@ -245,6 +251,11 @@
delete RED._debug;
},
oneditprepare: function() {
var none = {
value: "none",
label: RED._("node-red:debug.none"),
hasValue: false
};
if (this.tosidebar === undefined) {
this.tosidebar = true;
$("#node-input-tosidebar").prop('checked', true);
@ -254,8 +265,21 @@
$("#node-input-console").prop('checked', this.console);
$("#node-input-tosidebar").prop('checked', true);
}
$("#node-input-typed-complete").typedInput({types:['msg', {value:"full",label:RED._("node-red:debug.msgobj"),hasValue:false}]});
if (this.complete === "true" || this.complete === true) {
var fullType = {
value: "full",
label: RED._("node-red:debug.msgobj"),
hasValue: false
};
$("#node-input-typed-complete").typedInput({
default: "msg",
types:['msg', fullType, "jsonata"],
typeField: $("#node-input-targetType")
});
if (this.targetType === "jsonata") {
var property = this.complete || "";
$("#node-input-typed-complete").typedInput('type','jsonata');
$("#node-input-typed-complete").typedInput('value',property);
} else if ((this.targetType === "full") || this.complete === "true" || this.complete === true) {
// show complete message object
$("#node-input-typed-complete").typedInput('type','full');
} else {

View File

@ -1,4 +1,3 @@
module.exports = function(RED) {
"use strict";
var util = require("util");
@ -9,9 +8,11 @@ module.exports = function(RED) {
util.inspect.styles.boolean = "red";
function DebugNode(n) {
var is_edit = (n.targetType === "jsonata");
var edit_exp = is_edit ? n.complete : null;
RED.nodes.createNode(this,n);
this.name = n.name;
this.complete = (n.complete||"payload").toString();
this.complete = is_edit ? null : (n.complete||"payload").toString();
if (this.complete === "false") { this.complete = "payload"; }
this.console = ""+(n.console || false);
this.tostatus = n.tostatus || false;
@ -43,8 +44,48 @@ module.exports = function(RED) {
"50": "green",
"60": "blue"
};
var edit = null;
if (edit_exp) {
try {
edit = RED.util.prepareJSONataExpression(edit_exp, this);
}
catch (e) {
node.error(RED._("debug.invalid-exp", {error: edit_exp}));
return;
}
}
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);
}
});
}
this.on("input",function(msg) {
function output_e(msg) {
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});
}
}).catch(err => {
node.error(err);
});
}
function output(msg) {
if (this.complete === "true") {
// debug complete msg object
if (this.console === "true") {
@ -87,7 +128,9 @@ module.exports = function(RED) {
}
}
}
});
}
this.on("input", (edit_exp ? output_e : output));
}
RED.nodes.registerType("debug",DebugNode, {

View File

@ -15,7 +15,7 @@
-->
<script type="text/x-red" data-help-name="debug">
<p>Displays selected message properties in the debug sidebar tab and optionally the runtime log. By default it displays <code>msg.payload</code>.</p>
<p>Displays selected message properties in the debug sidebar tab and optionally the runtime log. By default it displays <code>msg.payload</code>, but can be configured to display any property, the full message or the result of a JSONata expression.</p>
<h3>Details</h3>
<p>The debug sidebar provides a structured view of the messages it is sent, making it easier to understand their structure.</p>
<p>JavaScript objects and arrays can be collapsed and expanded as required. Buffer objects can be displayed as raw data or as a string if possible.</p>

View File

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

View File

@ -15,7 +15,7 @@
-->
<script type="text/x-red" data-help-name="debug">
<p>サイドバーの「デバッグ」タブに、選択したメッセージプロパティの値を表示します。設定により、ランタイムログへの出力も可能です。デフォルトの表示対象は<code>msg.payload</code>です。</p>
<p>サイドバーの「デバッグ」タブに、選択したメッセージプロパティの値を表示します。設定により、ランタイムログへの出力も可能です。デフォルトの表示対象は<code>msg.payload</code>ですが、設定により、指定したプロパティ、メッセージ全体、もしくは、JSONata式の評価結果を出力できます</p>
<h3>詳細</h3>
<p>「デバッグ」サイドバーは受け取ったメッセージの階層構造を表示する機能を備えます。この機能によりメッセージの構造を容易に理解できます。</p>
<p>JavaScriptオブジェクトと配列は必要に応じて折り畳んだり展開したりできます。バッファオブジェクトを生データとして表示したり、表現可能な場合に文字列として表示したりすることも可能です。</p>

View File

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

View File

@ -356,6 +356,22 @@ describe('debug node', function() {
});
});
it('should publish complete message with edit', function(done) {
var flow = [{id:"n1", type:"debug", name:"Debug", complete: "true",
targetType: "jsonata", complete: '"<" & 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() {