diff --git a/src/main/java/com/moandjiezana/toml/BooleanConverter.java b/src/main/java/com/moandjiezana/toml/BooleanConverter.java index 68bc7b0..88bf0f0 100644 --- a/src/main/java/com/moandjiezana/toml/BooleanConverter.java +++ b/src/main/java/com/moandjiezana/toml/BooleanConverter.java @@ -45,4 +45,9 @@ class BooleanConverter implements ValueConverter, ValueWriter { } private BooleanConverter() {} + + @Override + public String toString() { + return "boolean"; + } } diff --git a/src/main/java/com/moandjiezana/toml/DateConverter.java b/src/main/java/com/moandjiezana/toml/DateConverter.java index 19c7448..1e43a43 100644 --- a/src/main/java/com/moandjiezana/toml/DateConverter.java +++ b/src/main/java/com/moandjiezana/toml/DateConverter.java @@ -119,4 +119,9 @@ class DateConverter implements ValueConverter, ValueWriter { } private DateConverter() {} + + @Override + public String toString() { + return "datetime"; + } } diff --git a/src/main/java/com/moandjiezana/toml/MapValueWriter.java b/src/main/java/com/moandjiezana/toml/MapValueWriter.java index 9d6f5e4..8bdceaf 100644 --- a/src/main/java/com/moandjiezana/toml/MapValueWriter.java +++ b/src/main/java/com/moandjiezana/toml/MapValueWriter.java @@ -41,6 +41,7 @@ class MapValueWriter implements ValueWriter { valueWriter.write(fromValue, context); context.output.append('\n'); } else if (valueWriter == PRIMITIVE_ARRAY_VALUE_WRITER) { + context.setArrayKey(key.toString()); context.output.append(quoteKey(key)).append(" = "); valueWriter.write(fromValue, context); context.output.append('\n'); diff --git a/src/main/java/com/moandjiezana/toml/NumberConverter.java b/src/main/java/com/moandjiezana/toml/NumberConverter.java index 74a2edf..efea438 100644 --- a/src/main/java/com/moandjiezana/toml/NumberConverter.java +++ b/src/main/java/com/moandjiezana/toml/NumberConverter.java @@ -103,4 +103,8 @@ class NumberConverter implements ValueConverter, ValueWriter { return false; } + @Override + public String toString() { + return "number"; + } } diff --git a/src/main/java/com/moandjiezana/toml/PrimitiveArrayValueWriter.java b/src/main/java/com/moandjiezana/toml/PrimitiveArrayValueWriter.java index c4231fd..81ad1f4 100644 --- a/src/main/java/com/moandjiezana/toml/PrimitiveArrayValueWriter.java +++ b/src/main/java/com/moandjiezana/toml/PrimitiveArrayValueWriter.java @@ -2,6 +2,8 @@ package com.moandjiezana.toml; import java.util.Collection; +import static com.moandjiezana.toml.ValueWriters.WRITERS; + class PrimitiveArrayValueWriter extends ArrayValueWriter { static final ValueWriter PRIMITIVE_ARRAY_VALUE_WRITER = new PrimitiveArrayValueWriter(); @@ -20,12 +22,25 @@ class PrimitiveArrayValueWriter extends ArrayValueWriter { } boolean first = true; + ValueWriter firstWriter = null; + for (Object elem : values) { - if (!first) { + if (first) { + firstWriter = WRITERS.findWriterFor(elem); + first = false; + } else { + ValueWriter writer = WRITERS.findWriterFor(elem); + if (writer != firstWriter) { + throw new IllegalStateException( + context.getContextPath() + + ": cannot write a heterogeneous array; first element was of type " + firstWriter + + " but found " + writer + ); + } context.output.append(", "); } - ValueWriters.WRITERS.write(elem, context); - first = false; + + WRITERS.write(elem, context); } if (!context.getTomlWriter().wantTerseArrays()) { @@ -35,4 +50,9 @@ class PrimitiveArrayValueWriter extends ArrayValueWriter { } private PrimitiveArrayValueWriter() {} + + @Override + public String toString() { + return "primitive-array"; + } } diff --git a/src/main/java/com/moandjiezana/toml/StringConverter.java b/src/main/java/com/moandjiezana/toml/StringConverter.java index bcc2576..409aafd 100644 --- a/src/main/java/com/moandjiezana/toml/StringConverter.java +++ b/src/main/java/com/moandjiezana/toml/StringConverter.java @@ -129,4 +129,9 @@ class StringConverter implements ValueConverter, ValueWriter { } private StringConverter() {} + + @Override + public String toString() { + return "string"; + } } diff --git a/src/main/java/com/moandjiezana/toml/TableArrayValueWriter.java b/src/main/java/com/moandjiezana/toml/TableArrayValueWriter.java index d92ab17..c8b3499 100644 --- a/src/main/java/com/moandjiezana/toml/TableArrayValueWriter.java +++ b/src/main/java/com/moandjiezana/toml/TableArrayValueWriter.java @@ -24,4 +24,9 @@ class TableArrayValueWriter extends ArrayValueWriter { } private TableArrayValueWriter() {} + + @Override + public String toString() { + return "table-array"; + } } diff --git a/src/main/java/com/moandjiezana/toml/WriterContext.java b/src/main/java/com/moandjiezana/toml/WriterContext.java index e7622c7..61095f9 100644 --- a/src/main/java/com/moandjiezana/toml/WriterContext.java +++ b/src/main/java/com/moandjiezana/toml/WriterContext.java @@ -6,6 +6,7 @@ class WriterContext { private String key = ""; private String currentTableIndent = ""; private String currentFieldIndent = ""; + private String arrayKey = null; private boolean isArrayOfTable = false; private final TomlWriter tomlWriter; StringBuilder output = new StringBuilder(); @@ -83,4 +84,13 @@ class WriterContext { public TomlWriter getTomlWriter() { return tomlWriter; } + + public WriterContext setArrayKey(String arrayKey) { + this.arrayKey = arrayKey; + return this; + } + + public String getContextPath() { + return key.isEmpty() ? arrayKey : key + "." + arrayKey; + } } diff --git a/src/test/java/com/moandjiezana/toml/ValueWriterTest.java b/src/test/java/com/moandjiezana/toml/ValueWriterTest.java index b7bb224..2361d3e 100644 --- a/src/test/java/com/moandjiezana/toml/ValueWriterTest.java +++ b/src/test/java/com/moandjiezana/toml/ValueWriterTest.java @@ -210,6 +210,18 @@ public class ValueWriterTest { assertEquals("", new TomlWriter().write(new TestClass())); } + @Test(expected = IllegalStateException.class) + public void should_reject_heterogeneous_arrays() { + class BadArray { + Object[] array = new Object[2]; + } + BadArray badArray = new BadArray(); + badArray.array[0] = new Integer(1); + badArray.array[1] = "oops"; + + new TomlWriter().write(badArray); + } + @Test public void should_elide_empty_intermediate_tables() { class C {