Fix handling of tables with same name in different table array items.

Fixes https://github.com/mwanji/toml4j/issues/26
This commit is contained in:
moandji.ezana 2016-01-24 22:10:35 -04:00
parent b1436c557d
commit e024b51b0c
4 changed files with 42 additions and 7 deletions

View file

@ -1,5 +1,11 @@
# toml4j Changelog
## 0.5.1 / 2016-01-24
### Fixed
* [Handling of tables with same name in different table array items](https://github.com/mwanji/toml4j/issues/26) (thanks to __[stanofujdiar](https://github.com/stanofujdiar)__)
## 0.5.0 / 2015-12-10
### Changed

View file

@ -10,17 +10,24 @@ abstract class Container {
abstract boolean accepts(String key);
abstract void put(String key, Object value);
abstract Object get(String key);
abstract boolean isImplicit();
static class Table extends Container {
private final Map<String, Object> values = new HashMap<String, Object>();
final String name;
final boolean implicit;
Table() {
this.name = null;
this(null, false);
}
public Table(String name) {
this.name = name;
this(name, false);
}
public Table(String tableName, boolean implicit) {
this.name = tableName;
this.implicit = implicit;
}
@Override
@ -37,6 +44,10 @@ abstract class Container {
Object get(String key) {
return values.get(key);
}
boolean isImplicit() {
return implicit;
}
/**
* This modifies the Table's internal data structure, such that it is no longer usable.
@ -84,6 +95,10 @@ abstract class Container {
Object get(String key) {
throw new UnsupportedOperationException();
}
boolean isImplicit() {
return false;
}
List<Map<String, Object>> getValues() {
ArrayList<Map<String, Object>> unwrappedValues = new ArrayList<Map<String,Object>>();

View file

@ -205,9 +205,6 @@ class Results {
void startTables(Identifier id, AtomicInteger line) {
String tableName = id.getBareName();
if (!tables.add(tableName)) {
errors.duplicateTable(tableName, line.get());
}
while (stack.size() > 1) {
stack.pop();
@ -219,12 +216,16 @@ class Results {
Container currentContainer = stack.peek();
if (currentContainer.get(tablePart) instanceof Container) {
Container nextTable = (Container) currentContainer.get(tablePart);
if (i == tableParts.length - 1 && !nextTable.isImplicit()) {
errors.duplicateTable(tableName, line.get());
return;
}
stack.push(nextTable);
if (stack.peek() instanceof Container.TableArray) {
stack.push(((Container.TableArray) stack.peek()).getCurrent());
}
} else if (currentContainer.accepts(tablePart)) {
startTable(tablePart, line);
startTable(tablePart, i < tableParts.length - 1, line);
} else {
errors.tableDuplicatesKey(tablePart, line);
break;
@ -249,6 +250,14 @@ class Results {
return newTable;
}
private Container startTable(String tableName, boolean implicit, AtomicInteger line) {
Container newTable = new Container.Table(tableName, implicit);
addValue(tableName, newTable, line);
stack.push(newTable);
return newTable;
}
private String getInlineTablePath(String key) {
Iterator<Container> descendingIterator = stack.descendingIterator();

View file

@ -110,11 +110,16 @@ public class TableArrayTest {
assertNull(toml.getTable("a[1]"));
}
@Test
public void should_handle_repeated_tables() {
new Toml().read("[[a]]\n [a.b]\n [[a]]\n [a.b]");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_empty_table_array_name() {
new Toml().read("[[]]");
}
private File file(String fileName) {
return new File(getClass().getResource(fileName + ".toml").getFile());
}