BinaryMuse / toml-node

TOML parser for Node.js and the Browser. Parses TOML v0.4.0

Home Page:http://binarymuse.github.io/toml-node/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Not possible to define Array of tables (2.3.0)

coussej opened this issue · comments

I'm trying to parse the example from the TOML GitHub:

[[products]]
name = "Hammer"
sku = 738594937

[[products]]

[[products]]
name = "Nail"
sku = 284758393
color = "gray"

however, I get the following exception:

Error: Cannot redefine existing key 'products'.
    at genError (/home/nitrous/code/nodejs/node-opcua-logger/node_modules/toml/lib/compiler.js:33:14)
    at addTableArray (/home/nitrous/code/nodejs/node-opcua-logger/node_modules/toml/lib/compiler.js:131:7)
    at reduce (/home/nitrous/code/nodejs/node-opcua-logger/node_modules/toml/lib/compiler.js:24:9)
    at Object.compile (/home/nitrous/code/nodejs/node-opcua-logger/node_modules/toml/lib/compiler.js:10:10)
    at Object.module.exports.parse (/home/nitrous/code/nodejs/node-opcua-logger/node_modules/toml/index.js:7:21)
    at loadConfig (/home/nitrous/code/nodejs/node-opcua-logger/logger.js:35:15)
    at Object.<anonymous> (/home/nitrous/code/nodejs/node-opcua-logger/logger.js:11:14)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)

For some reason, it does work on http://binarymuse.github.io/toml-node/

It seems that context returned by https://github.com/BinaryMuse/toml-node/blob/master/lib/compiler.js#L119 is an empty object, resulting in the error.

What version of toml-node are you using? I'm unable to reproduce this problem:

> require("./package.json").version
'2.3.0'

> t = fs.readFileSync(process.cwd() + "/test/table_arrays_easy.toml", 'utf8')
'[[products]]\nname = "Hammer"\nsku = 738594937\n\n[[products]]\n\n[[products]]\nname = "Nail"\nsku = 284758393\ncolor = "gray"\n'

> toml.parse(t)
{ products:
   [ { name: 'Hammer', sku: 738594937 },
     {},
     { name: 'Nail', sku: 284758393, color: 'gray' } ] }

In fact, this exact example is included and tested as part of the library's test suite.

You're right, sorry. I should have isolated this specific piece of code before opening an issue.

Apparently, parse() is failing because of another package that is required in the same file, node-opcua. This piece of code does not work:

var opcua = require("node-opcua");
var toml = require("toml");

var path = require("path").resolve(__dirname, 'config.toml');
var text = require("fs").readFileSync(path, "utf8");

console.log(toml.parse(text));

When you comment the first line, everything works as expected. No idea why this happens.

I believe that this is something specific to your setup; I'm unable to reproduce this problem even with node-opcua installed and required:

$ jq .dependencies package.json
{
  "node-opcua": "0.0.52",
  "toml": "^2.3.0"
}

$ cat config.toml
[[products]]
name = "Hammer"
sku = 738594937

[[products]]

[[products]]
name = "Nail"
sku = 284758393
color = "gray"

$ cat index.js
var opcua = require("node-opcua");
var toml = require("toml");

var path = require("path").resolve(__dirname, 'config.toml');
var text = require("fs").readFileSync(path, "utf8");

console.log(toml.parse(text));

$ node index.js
{ products:
   [ { name: 'Hammer', sku: 738594937 },
     {},
     { name: 'Nail', sku: 284758393, color: 'gray' } ] }

If you can reduce this to a minimal reproducible example (e.g. a small repo or gist I can clone and take a look at) I'm happy to take a closer look.

Believe it or not, but I just copied your entire example to my environment in a new dir, ran nmp install, node index.js and I get the error I initially reported. Commenting var opcua = require("node-opcua"); makes it work.

When I modify config.toml to this:

[products]
name = "Hammer"
sku = 738594937

the output is also completely messed up:

{ products: { 'function (predicate) {\n        if (this === null) {\n            throw new TypeError(\'Array.prototype.findIndex called on null or undefined\');\n        }\n
       if (typeof predicate !== \'function\') {\n            throw new TypeError(\'predicate must be a function\');\n        }\n        var list = Object(this);\n        var
length = list.length >>> 0;\n        var thisArg = arguments[1];\n        var value;\n\n        for (var i = 0; i < length; i++) {\n            value = list[i];\n
if (predicate.call(thisArg, value, i, list)) {\n                return i;\n            }\n        }\n        return -1;\n    }': { name: 'Hammer', sku: 738594937 } } }

Strange. I'm running a somewhat older version of node (v0.12.5), could this have something to do with it?

Just installed 5.8.0, it works now 😞

@coussej Glad you got it working. I'm going to close this out as there's nothing actionable from my end, but please feel free to reopen if you find otherwise!