diff --git a/CHANGELOG.md b/CHANGELOG.md index fcd7db1..8ae89ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # 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 ## Added diff --git a/README.md b/README.md index e499377..2f8836d 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ MyClass myClass = toml.to(MyClass.class); ## 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 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. +`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. ```toml diff --git a/src/main/java/com/moandjiezana/toml/Toml.java b/src/main/java/com/moandjiezana/toml/Toml.java index 4babb93..685686d 100644 --- a/src/main/java/com/moandjiezana/toml/Toml.java +++ b/src/main/java/com/moandjiezana/toml/Toml.java @@ -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) { this(defaults, new HashMap()); @@ -115,6 +115,18 @@ public class Toml { 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. * diff --git a/src/test/java/com/moandjiezana/toml/TomlReadTest.java b/src/test/java/com/moandjiezana/toml/TomlReadTest.java index acf593d..5b63b73 100644 --- a/src/test/java/com/moandjiezana/toml/TomlReadTest.java +++ b/src/test/java/com/moandjiezana/toml/TomlReadTest.java @@ -59,4 +59,24 @@ public class TomlReadTest { 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")); + } }