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 # 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 ## 0.5.0 / 2015-12-10
### Changed ### Changed

View file

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

View file

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

View file

@ -110,11 +110,16 @@ public class TableArrayTest {
assertNull(toml.getTable("a[1]")); 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) @Test(expected = IllegalStateException.class)
public void should_fail_on_empty_table_array_name() { public void should_fail_on_empty_table_array_name() {
new Toml().read("[[]]"); new Toml().read("[[]]");
} }
private File file(String fileName) { private File file(String fileName) {
return new File(getClass().getResource(fileName + ".toml").getFile()); return new File(getClass().getResource(fileName + ".toml").getFile());
} }