diff --git a/src/main/java/com/moandjiezana/toml/Toml.java b/src/main/java/com/moandjiezana/toml/Toml.java index fd14171..a641069 100644 --- a/src/main/java/com/moandjiezana/toml/Toml.java +++ b/src/main/java/com/moandjiezana/toml/Toml.java @@ -12,6 +12,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.HashMap; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -39,7 +40,7 @@ import com.google.gson.JsonElement; * */ public class Toml { - + private static final Gson DEFAULT_GSON = new Gson(); private Map values = new HashMap(); @@ -281,6 +282,50 @@ public class Toml { return DEFAULT_GSON.fromJson(json, targetClass); } + + public Set entrySet() { + Set entries = new LinkedHashSet(); + + for (Map.Entry entry : values.entrySet()) { + Class entryClass = entry.getValue().getClass(); + + if (Map.class.isAssignableFrom(entryClass)) { + entries.add(new Toml.Entry(entry.getKey(), getTable(entry.getKey()))); + } else if (List.class.isAssignableFrom(entryClass)) { + List value = (List) entry.getValue(); + if (value.isEmpty()) { + entries.add(new Toml.Entry(entry.getKey(), Collections.emptyList())); + } else if (value.get(0) instanceof Map) { + entries.add(new Toml.Entry(entry.getKey(), getTables(entry.getKey()))); + } else { + entries.add(new Toml.Entry(entry.getKey(), entry.getValue())); + } + } else { + entries.add(new Toml.Entry(entry.getKey(), entry.getValue())); + } + } + + return entries; + } + + public static class Entry { + + private final String key; + private final Object value; + + public String getKey() { + return key; + } + + public Object getValue() { + return value; + } + + private Entry(String key, Object value) { + this.key = key; + this.value = value; + } + } @SuppressWarnings("unchecked") private Object get(String key) { @@ -314,7 +359,7 @@ public class Toml { return current; } - + private Toml(Toml defaults, Map values) { this.values = values != null ? values : Collections.emptyMap(); this.defaults = defaults; diff --git a/src/test/java/com/moandjiezana/toml/IteratorTest.java b/src/test/java/com/moandjiezana/toml/IteratorTest.java new file mode 100644 index 0000000..e94d148 --- /dev/null +++ b/src/test/java/com/moandjiezana/toml/IteratorTest.java @@ -0,0 +1,85 @@ +package com.moandjiezana.toml; + +import static java.util.Arrays.asList; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.hasEntry; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Test; + +import com.moandjiezana.toml.testutils.Utils; + +public class IteratorTest { + + @Test + public void should_iterate_over_primitive() throws Exception { + Toml toml = new Toml().parse(file("long")); + Toml.Entry entry = toml.entrySet().iterator().next(); + + assertEquals("long", entry.getKey()); + assertEquals(2L, entry.getValue()); + } + + @Test + @SuppressWarnings("unchecked") + public void should_iterate_over_list() throws Exception { + Toml toml = new Toml().parse(file("list")); + + Toml.Entry entry = toml.entrySet().iterator().next(); + + assertEquals("list", entry.getKey()); + assertThat((List) entry.getValue(), contains("a", "b", "c")); + } + + @Test + public void should_iterate_over_table() throws Exception { + Toml toml = new Toml().parse(file("table")); + Toml.Entry entry = toml.entrySet().iterator().next(); + + assertEquals("table", entry.getKey()); + assertEquals("a", ((Toml) entry.getValue()).getString("a")); + } + + @Test + @SuppressWarnings("unchecked") + public void should_iterate_over_table_array() throws Exception { + Toml toml = new Toml().parse(file("table_array")); + + Toml.Entry entry = toml.entrySet().iterator().next(); + List tableArray = (List) entry.getValue(); + + assertEquals("table_array", entry.getKey()); + assertThat(tableArray, contains(instanceOf(Toml.class), instanceOf(Toml.class))); + } + + @Test + @SuppressWarnings("unchecked") + public void should_iterate_over_multiple_entries() throws Exception { + Toml toml = new Toml().parse(file("multiple")); + + Map entries = new HashMap(); + for (Toml.Entry entry : toml.entrySet()) { + entries.put(entry.getKey(), entry.getValue()); + } + + assertThat(entries.keySet(), containsInAnyOrder("a", "b", "c", "e")); + assertThat(entries, hasEntry("a", (Object) "a")); + assertThat(entries, hasEntry("b", (Object) asList(1L, 2L, 3L))); + assertTrue(((Toml) entries.get("c")).getBoolean("d")); + assertThat(((List) entries.get("e")), hasSize(1)); + } + + private File file(String name) { + return Utils.file(getClass(), "/IteratorTest/" + name); + } +} diff --git a/src/test/resources/IteratorTest/list.toml b/src/test/resources/IteratorTest/list.toml new file mode 100644 index 0000000..fdcf4ec --- /dev/null +++ b/src/test/resources/IteratorTest/list.toml @@ -0,0 +1 @@ +list = ["a", "b", "c"] diff --git a/src/test/resources/IteratorTest/long.toml b/src/test/resources/IteratorTest/long.toml new file mode 100644 index 0000000..3641e48 --- /dev/null +++ b/src/test/resources/IteratorTest/long.toml @@ -0,0 +1 @@ +long = 2 diff --git a/src/test/resources/IteratorTest/multiple.toml b/src/test/resources/IteratorTest/multiple.toml new file mode 100644 index 0000000..38af207 --- /dev/null +++ b/src/test/resources/IteratorTest/multiple.toml @@ -0,0 +1,7 @@ +a = "a" +b = [1, 2, 3] + +[c] + d = true + +[[e]] diff --git a/src/test/resources/IteratorTest/table.toml b/src/test/resources/IteratorTest/table.toml new file mode 100644 index 0000000..541ee71 --- /dev/null +++ b/src/test/resources/IteratorTest/table.toml @@ -0,0 +1 @@ +table = { a = "a" } diff --git a/src/test/resources/IteratorTest/table_array.toml b/src/test/resources/IteratorTest/table_array.toml new file mode 100644 index 0000000..b14906e --- /dev/null +++ b/src/test/resources/IteratorTest/table_array.toml @@ -0,0 +1,3 @@ +[[table_array]] + +[[table_array]]