Merge branch 'wip'

This commit is contained in:
moandji.ezana 2016-01-25 09:33:42 -04:00
commit acca966946
5 changed files with 52 additions and 17 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

@ -14,7 +14,7 @@ Add the following dependency to your POM (or equivalent for other dependency man
<dependency>
<groupId>com.moandjiezana.toml</groupId>
<artifactId>toml4j</artifactId>
<version>0.4.0</version>
<version>0.5.1</version>
</dependency>
```
@ -23,7 +23,7 @@ Requires Java 1.6.
## Quick start
```java
Toml toml = new Toml().parse(getTomlFile());
Toml toml = new Toml().read(getTomlFile());
String someValue = toml.getString("someKey");
Date someDate = toml.getDate("someTable.someDate");
MyClass myClass = toml.to(MyClass.class);
@ -31,10 +31,10 @@ MyClass myClass = toml.to(MyClass.class);
## Usage
A `com.moandjiezana.toml.Toml` instance is populated by calling one of `parse(File)`, `parse(InputStream)`, `parse(Reader)` or `parse(String)`.
A `com.moandjiezana.toml.Toml` instance is populated by calling one of `read(File)`, `read(InputStream)`, `read(Reader)` or `read(String)`.
```java
Toml toml = new Toml().parse("a=1");
Toml toml = new Toml().read("a=1");
```
An exception is thrown if the source is not valid TOML.
@ -70,7 +70,7 @@ class User {
```
```java
User user = new Toml().parse(tomlFile).to(User.class);
User user = new Toml().read(tomlFile).to(User.class);
assert user.name.equals("Mwanji Ezana");
assert user.address.street.equals("123 A Street");
@ -146,7 +146,7 @@ title = "TOML Example"
```
```java
Toml toml = new Toml().parse(getTomlFile());
Toml toml = new Toml().read(getTomlFile());
String title = toml.getString("title");
String subTitle = toml.getString("\"sub title\"");
@ -191,8 +191,8 @@ a = 1
```
```java
Toml defaults = new Toml().parse(getDefaultsFile());
Toml toml = new Toml(defaults).parse(getTomlFile());
Toml defaults = new Toml().read(getDefaultsFile());
Toml toml = new Toml(defaults).read(getTomlFile());
Long a = toml.getLong("a"); // returns 1, not 2
Long b = toml.getLong("b"); // returns 3, taken from defaults provided to constructor
@ -217,7 +217,7 @@ for (Map.Entry<String, Object> entry : myToml.entrySet()) {
You can also convert a Toml instance to a `Map<String, Object>`:
```java
Toml toml = new Toml().parse("a = 1");
Toml toml = new Toml().read("a = 1");
Map<String, Object> map = toml.to(Map.class);
```
@ -225,7 +225,7 @@ Map<String, Object> map = toml.to(Map.class);
```java
Toml toml = new Toml().parse("a = 1");
Toml toml = new Toml().read("a = 1");
toml.contains("a"); // true
toml.conatinsKey("a"); // true

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());
}