Skip to content

Commit

Permalink
Added JSONata for Object conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
klein0r committed Mar 3, 2020
1 parent f1fb776 commit 3840293
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
30 changes: 30 additions & 0 deletions admin/google-blockly/own/blocks_convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,33 @@ Blockly.JavaScript.convert_object2json = function (block) {
return ['JSON.stringify(' + Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_ATOMIC) + (prettify == 'TRUE' ? ', null, 2' : '') + ')', Blockly.JavaScript.ORDER_ATOMIC];
};

// --- to single value -------------------------------------------
Blockly.Convert.blocks['convert_jsonata'] =
'<block type="convert_jsonata">'
+ ' <value name="EXPRESSION">'
+ ' <shadow type="text">'
+ ' <field name="TEXT">*</field>'
+ ' </shadow>'
+ ' </value>'
+ ' <value name="TARGET">'
+ ' </value>'
+ '</block>';

Blockly.Blocks.convert_jsonata = {
init: function () {

this.appendValueInput('EXPRESSION')
.appendField(Blockly.Words['convert_jsonata'][systemLang]);

this.appendValueInput('TARGET')
.appendField(Blockly.Words['convert_jsonata_target'][systemLang]);

this.setInputsInline(true);
this.setColour(Blockly.Convert.HUE);
this.setOutput(true, 'String');
this.setTooltip(Blockly.Words['convert_jsonata_tooltip'][systemLang])
}
};
Blockly.JavaScript.convert_jsonata = function (block) {
return ['jsonataExpression(' + Blockly.JavaScript.valueToCode(block, 'TARGET', Blockly.JavaScript.ORDER_ATOMIC) + ',' + Blockly.JavaScript.valueToCode(block, 'EXPRESSION', Blockly.JavaScript.ORDER_ATOMIC) + ')', Blockly.JavaScript.ORDER_ATOMIC];
};
3 changes: 3 additions & 0 deletions admin/google-blockly/own/blocks_words.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lib/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function sandBox(script, name, verbose, debug, context) {
const eventObj = require('./eventObj');
const patternCompareFunctions = require('./patternCompareFunctions');
const nodeSchedule = require('node-schedule');
const jsonata = require("jsonata");

/** @type {ioBroker.Adapter} */
const adapter = context.adapter;
Expand Down Expand Up @@ -2811,6 +2812,9 @@ function sandBox(script, name, verbose, debug, context) {
debug: function (msg) {
sandbox.log(msg, 'debug');
}
},
jsonataExpression: function (data, expression) {
return jsonata(expression).evaluate(data);
}
};

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"suncalc2": "^1.8.1",
"typescript": "^3.6.3",
"virtual-tsc": "^0.5.0",
"wake_on_lan": "^1.0.0"
"wake_on_lan": "^1.0.0",
"jsonata": "^1.8.1"
},
"devDependencies": {
"@types/iobroker": "^1.4.12",
Expand Down

0 comments on commit 3840293

Please sign in to comment.