Add Toml#read(Toml)

Fixes #32
This commit is contained in:
Moandji Ezana 2016-07-12 13:10:10 +02:00
parent e27ba3e8fb
commit 120d1396fd
4 changed files with 49 additions and 2 deletions

View file

@ -1,5 +1,11 @@
# toml4j Changelog # toml4j Changelog
## 0.7.0 / 2016-07-12
## Added
* Toml#read(Toml) merges two Toml instances (thanks to __[gustavkarlsson](https://github.com/gustavkarlsson)__)
## 0.6.0 / 2016-06-14 ## 0.6.0 / 2016-06-14
## Added ## Added

View file

@ -31,7 +31,7 @@ MyClass myClass = toml.to(MyClass.class);
## Usage ## Usage
A `com.moandjiezana.toml.Toml` instance is populated by calling one of `read(File)`, `read(InputStream)`, `read(Reader)` or `read(String)`. A `com.moandjiezana.toml.Toml` instance is populated by calling one of `read(File)`, `read(InputStream)`, `read(Reader)`, `read(String)` or `read(Toml)`.
```java ```java
Toml toml = new Toml().read("a=1"); Toml toml = new Toml().read("a=1");
@ -176,6 +176,15 @@ String network3Operator2Location = toml.getString("networks[2].operators[1].loca
The constructor can be given a set of default values that will be used as fallbacks. For tables and table arrays, a shallow merge is performed. The constructor can be given a set of default values that will be used as fallbacks. For tables and table arrays, a shallow merge is performed.
`Toml#read(Toml)` is used to merge two Toml instances:
```java
Toml toml1 = new Toml().read("a=1");
Toml toml2 = new Toml().read(getTomlFile());
Toml mergedToml = new Toml(toml1).read(toml2);
```
You can also call an overloaded version of the getters that take a default value. Note that the default value provided in the constructor take precedence over the one provided by the getter. You can also call an overloaded version of the getters that take a default value. Note that the default value provided in the constructor take precedence over the one provided by the getter.
```toml ```toml

View file

@ -54,7 +54,7 @@ public class Toml {
} }
/** /**
* @param defaults fallback values used when the requested key or table is not present. * @param defaults fallback values used when the requested key or table is not present in the TOML source that has been read.
*/ */
public Toml(Toml defaults) { public Toml(Toml defaults) {
this(defaults, new HashMap<String, Object>()); this(defaults, new HashMap<String, Object>());
@ -115,6 +115,18 @@ public class Toml {
return this; return this;
} }
/**
* Populates the current Toml instance with values from otherToml.
*
* @param otherToml
* @return this instance
*/
public Toml read(Toml otherToml) {
this.values = otherToml.values;
return this;
}
/** /**
* Populates the current Toml instance with values from tomlString. * Populates the current Toml instance with values from tomlString.
* *

View file

@ -59,4 +59,24 @@ public class TomlReadTest {
assertThat(e.getCause(), instanceOf(IOException.class)); assertThat(e.getCause(), instanceOf(IOException.class));
} }
} }
@Test
public void should_read_toml_without_defaults() {
Toml toml1 = new Toml().read("a = 1");
Toml toml2 = new Toml().read(toml1);
assertEquals(toml1.getLong("a"), toml2.getLong("a"));
}
@Test
public void should_read_toml_and_merge_with_defaults() {
Toml toml1 = new Toml().read("a = 1\nc = 3\nd = 5");
Toml toml2 = new Toml().read("b = 2\nc = 4");
Toml mergedToml = new Toml(toml1).read(toml2);
assertEquals(toml1.getLong("a"), mergedToml.getLong("a"));
assertEquals(toml2.getLong("b"), mergedToml.getLong("b"));
assertEquals(toml2.getLong("c"), mergedToml.getLong("c"));
assertEquals(toml1.getLong("d"), mergedToml.getLong("d"));
}
} }