Description
I am trying to create a TOML parser node for the node-RED project where one can parse and stringify to and from TOML.
I am using this particular library to make the parser possible. The good news is that toml.parse()
is working well and providing a valid javascript object.
I am using the standard mentioned on learnxinyminutes docs for TOML
# You can create nested arrays of tables as well. Each double-bracketed
# sub-table will belong to the nearest table element above it.
[[fruit]]
name = "apple" # I am a property in fruit table/map
[fruit.geometry]
shape = "round"
note = "I am a property in geometry table/map"
[[fruit.color]]
name = "red"
note = "I am an array item in apple fruit's table/map"
[[fruit.color]]
name = "green"
note = "I am in the same array as red"
[[fruit]]
name = "banana"
[[fruit.color]]
name = "yellow"
note = "I am an array item in banana fruit's table/map"
Upon passing this in the node-RED editor
the above mentioned TOML configuration is in the template
node in the diagram, I am getting the following javascript object
{"fruit":[{"name":"apple","geometry":{"shape":"round","note":"I am a property in geometry table/map"},"color":[{"name":"red","note":"I am an array item in apple fruit's table/map"},{"name":"green","note":"I am in the same array as red"}]},{"name":"banana","color":[{"name":"yellow","note":"I am an array item in banana fruit's table/map"}]}]}
Problem
Now if were to take the same JSON result from above and let the TOML node stringify
it for me via the node-RED editor I get the following result:
fruit = [
{ name = 'apple', geometry.shape = 'round', geometry.note = 'I am a property in geometry table/map', color = [
{ name = 'red', note = "I am an array item in apple fruit's table/map" },
{ name = 'green', note = 'I am in the same array as red' },
] },
{ name = 'banana', color = [
{ name = 'yellow', note = "I am an array item in banana fruit's table/map" },
] },
]
If you pass the above toml to the a validator, the TOML is invalid.
The problem lies that the toml.stringify
does not resolve the:
geometry.shape = 'round', geometry.note = 'I am a property in geometry table/map'
correctly.
Code
module.exports = function(RED) {
"use strict";
var toml = require('@ltd/j-toml');
function TOMLNode(n) {
RED.nodes.createNode(this,n);
this.property = n.property||"payload";
var node = this;
this.on("input", function(msg,send,done) {
var value = RED.util.getMessageProperty(msg,node.property);
if (value !== undefined) {
// HERE IS TO PARSE TOML STRING
if (typeof value === "string") {
try {
value = toml.parse(value);
RED.util.setMessageProperty(msg,node.property,value);
send(msg);
done();
}
catch(e) { done(e.message); }
}
// HERE IS TO STRINGIFY JSON TO TOML'ish
else if (typeof value === "object") {
if (!Buffer.isBuffer(value)) {
try {
value = toml.stringify(value, {newline:'\n'});
RED.util.setMessageProperty(msg,node.property,value);
send(msg);
done();
}
catch(e) {
done(RED._("toml.errors.dropped-error"));
}
}
else { node.warn(RED._("toml.errors.dropped-object")); done(); }
}
else { node.warn(RED._("toml.errors.dropped")); done(); }
}
else { send(msg); done(); } // If no payload - just pass it on.
});
}
RED.nodes.registerType("toml",TOMLNode);
};
Any Idea what the problem could be?