agrison / jtoml

TOML for Java

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for arrays of tables

quux00 opened this issue · comments

The TOML GitHub page has an example of arrays of tables (which may or may not be nested) that jtoml seems not to support. The example is:

foo = "bar"

[[fruit]]
  name = "apple"

  [[fruit.variety]]
    name = "red delicious"
  [[fruit.variety]]
    name = "granny smith"

[[fruit]]
  name = "banana"

  [[fruit.variety]]
    name = "plantain"

baz = "quux"

Note: I added the foo and baz sections to check that something was parsing.

When I try this example in jtoml, it returns nothing for the key "fruit".

File f = new File("fruit.toml");
Toml t = Toml.parse(f);
System.out.println(t.getString("foo")); // prints bar
System.out.println(t.getString("baz")); // prints null
Object object = t.get("fruit");
System.out.println(object);             // prints null

Request: jtoml should support this.

Note: I used version 1.1.0 for this test.

Also, notice that the getString("baz") returns null. If I put the baz="quux" at the top of the file, it returns "quux", so it looks like the jtoml parser just stops when it hits a part of the file it considers an error and stops parsing. This seems like a defect in the parser. In my view, it should throw an exception if the TOML file is invalid. Perhaps I should file another issue?

Hey @quux00 thank you for that issue. You're totally right, the table of tables are not implemented at all in jtoml, but I will take a look at it today or tomorrow, it seems not that complicated since it looks alot like toml tables.

For your final note, I think that jtoml is strict regarding the toml spec where we can see table keys explained like this.

Under that, and until the next table or EOF are the key/values of that table. Keys are on the left of the equals sign and values are on the right.

So in your example the baz key should be grouped in the fruit.variety context. This means also that tables and table of tables are dedicated to be defined at the end of a Toml file.

Besides, since table of tables is not implemented, they are seen as simple toml tables by jtoml (meaning no duplicates) and the key is what the regex has matched inside [(.*)] which is [fruit], so if you System.out.println(t.get("[fruit]")); it outputs {name=banana} which is a map having a simple key name whose value's banana.

I'll take a look at implementing it asap 😉

Cool. I agree with your analysis of the "baz" section of the doc. I tired it with another parser and it interprets baz as a property of the nested fruit array, so I retract that issue.

However, I have noticed that jtoml does not error out when a file has an invalid format. I just stops parsing. I think that is really dangerous.