Added support for arrays in inline tables

This commit is contained in:
moandji.ezana 2015-02-10 14:32:44 +02:00
parent a5a6ab22dc
commit 5bd87a6b56
4 changed files with 111 additions and 36 deletions

View file

@ -19,32 +19,36 @@ class ArrayConverter implements ValueConverter {
@Override
public Object convert(String s) {
return convert(s, new AtomicInteger(1), true);
AtomicInteger sharedIndex = new AtomicInteger(1);
Object converted = convert(s, sharedIndex);
char[] chars = s.toCharArray();
for (int i = sharedIndex.incrementAndGet(); i < chars.length; i++) {
char c = chars[i];
if (c == '#') {
break;
}
if (!Character.isWhitespace(c)) {
return INVALID;
}
}
return converted;
}
private Object convert(String s, AtomicInteger sharedIndex, boolean topLevel) {
Object convert(String s, AtomicInteger sharedIndex) {
char[] chars = s.toCharArray();
List<Object> arrayItems = new ArrayList<Object>();
boolean terminated = false;
StringType stringType = StringType.NONE;
StringBuilder current = new StringBuilder();
for (int i = 1; i < chars.length; i++, sharedIndex.incrementAndGet()) {
char c = chars[i];
if (terminated && !topLevel) {
break;
}
if (terminated) {
if (c == '#') {
break;
}
if (!Character.isWhitespace(c)) {
return INVALID;
}
continue;
}
for (; sharedIndex.get() < chars.length; sharedIndex.incrementAndGet()) {
int i = sharedIndex.get();
char c = chars[sharedIndex.get()];
if (stringType == StringType.NONE) {
if (c == ',') {
@ -56,8 +60,8 @@ class ArrayConverter implements ValueConverter {
}
if (c == '[') {
arrayItems.add(convert(s.substring(i), sharedIndex, false));
i = sharedIndex.get();
sharedIndex.incrementAndGet();
arrayItems.add(convert(s, sharedIndex));
continue;
}
@ -67,7 +71,7 @@ class ArrayConverter implements ValueConverter {
arrayItems.add(current.toString());
}
current = new StringBuilder();
continue;
break;
}
}

View file

@ -3,6 +3,7 @@ package com.moandjiezana.toml;
import static com.moandjiezana.toml.ValueConverterUtils.INVALID;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;
class InlineTableConverter implements ValueConverter {
@ -21,6 +22,7 @@ class InlineTableConverter implements ValueConverter {
boolean pairHasKey = false;
boolean inValue = false;
boolean quoted = false;
boolean inArray = false;
boolean inString = false;
boolean terminated = false;
StringBuilder currentKey = new StringBuilder();
@ -46,14 +48,35 @@ class InlineTableConverter implements ValueConverter {
(inValue ? current : currentKey).append(c);
} else if (quoted) {
(inKey ? currentKey : current).append(c);
} else if (c == ',') {
Object converted = CONVERTERS.convert(current.toString().trim());
} else if (c == '[' && inValue) {
AtomicInteger sharedIndex = new AtomicInteger(i);
sharedIndex.incrementAndGet();
Object converted = ArrayConverter.ARRAY_PARSER.convert(s, sharedIndex);
if (converted == INVALID) {
return INVALID;
}
results.put(currentKey.toString().trim(), converted);
i = sharedIndex.get();
inArray = true;
continue;
} else if (c == ']' && inArray) {
current.append(']');
inArray = false;
} else if (c == ',') {
if (inArray) {
inArray = false;
} else {
Object converted = CONVERTERS.convert(current.toString().trim());
if (converted == INVALID) {
return INVALID;
}
results.put(currentKey.toString().trim(), converted);
}
inKey = true;
pairHasKey = false;
inValue = false;

View file

@ -2,11 +2,13 @@ package com.moandjiezana.toml;
import static java.util.Arrays.asList;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasSize;
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.Arrays;
import java.util.List;
import org.junit.Test;
@ -36,6 +38,16 @@ public class ArrayTest {
assertEquals(asList(asList("gamma", "delta"), asList(1L, 2L)), clients.<String>getList("data"));
}
@Test
public void should_get_deeply_nested_arrays() throws Exception {
List<List<?>> data = new Toml().parse("data = [[[1], [2]], [3, 4]]").getList("data");
assertThat(data, hasSize(2));
assertEquals(Arrays.asList(1L), data.get(0).get(0));
assertEquals(asList(2L), data.get(0).get(1));
assertEquals(asList(3L, 4L), data.get(1));
}
@Test
@SuppressWarnings("unchecked")
@ -73,6 +85,22 @@ public class ArrayTest {
assertThat(toml.<String>getList("key"), contains("a]", "b]", "c]", "d]"));
}
@Test
public void should_support_array_of_inline_tables() throws Exception {
Toml toml = new Toml().parse(getClass().getResourceAsStream("should_support_array_of_inline_tables.toml"));
assertThat(toml.getList("points"), hasSize(4));
assertEquals(1, toml.getLong("points[0].x").longValue());
assertEquals(2, toml.getLong("points[0].y").longValue());
assertEquals(3, toml.getLong("points[0].z").longValue());
assertEquals(7, toml.getLong("points[1].x").longValue());
assertEquals(8, toml.getLong("points[1].y").longValue());
assertEquals(9, toml.getLong("points[1].z").longValue());
assertEquals(2, toml.getLong("points[2].x").longValue());
assertEquals(4, toml.getLong("points[2].y").longValue());
assertEquals(8, toml.getLong("points[2].z").longValue());
}
private File file(String file) {
return Utils.file(getClass(), file);
}

View file

@ -1,5 +1,6 @@
package com.moandjiezana.toml;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@ -8,6 +9,7 @@ import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;
import org.junit.Test;
@ -59,7 +61,6 @@ public class InlineTableTest {
public void should_read_inline_table_with_dates() throws Exception {
Toml toml = new Toml().parse("point = { x = 2015-02-09T22:05:00Z, y = 2015-02-09T21:05:00Z }");
Calendar x = Calendar.getInstance(UTC);
x.set(2015, Calendar.FEBRUARY, 9, 22, 5, 00);
x.set(Calendar.MILLISECOND, 0);
@ -73,18 +74,37 @@ public class InlineTableTest {
}
@Test
public void should_support_array_of_inline_tables() throws Exception {
Toml toml = new Toml().parse(getClass().getResourceAsStream("should_support_array_of_inline_tables.toml"));
public void should_read_arrays() throws Exception {
Toml toml = new Toml().parse("arrays = { integers = [1, 2, 3], strings = [\"a\", \"b\", \"c\"] }");
assertThat(toml.getList("points"), hasSize(4));
assertEquals(1, toml.getLong("points[0].x").longValue());
assertEquals(2, toml.getLong("points[0].y").longValue());
assertEquals(3, toml.getLong("points[0].z").longValue());
assertEquals(7, toml.getLong("points[1].x").longValue());
assertEquals(8, toml.getLong("points[1].y").longValue());
assertEquals(9, toml.getLong("points[1].z").longValue());
assertEquals(2, toml.getLong("points[2].x").longValue());
assertEquals(4, toml.getLong("points[2].y").longValue());
assertEquals(8, toml.getLong("points[2].z").longValue());
assertThat(toml.<Long>getList("arrays.integers"), contains(1L, 2L, 3L));
assertThat(toml.<String>getList("arrays.strings"), contains("a", "b", "c"));
}
@Test
public void should_read_nested_arrays() throws Exception {
Toml toml = new Toml().parse("arrays = { nested = [[1, 2, 3], [4, 5, 6]] }").getTable("arrays");
List<List<Long>> nested = toml.<List<Long>>getList("nested");
assertThat(nested, hasSize(2));
assertThat(nested.get(0), contains(1L, 2L, 3L));
assertThat(nested.get(1), contains(4L, 5L, 6L));
}
@Test
public void should_read_mixed_inline_table() throws Exception {
Toml toml = new Toml().parse("point = { date = 2015-02-09T22:05:00Z, bool = true, integer = 123, float = 123.456, string = \"abc\", list = [5, 6, 7, 8] }").getTable("point");
Calendar date = Calendar.getInstance(UTC);
date.set(2015, Calendar.FEBRUARY, 9, 22, 5, 00);
date.set(Calendar.MILLISECOND, 0);
assertEquals(date.getTime(), toml.getDate("date"));
assertTrue(toml.getBoolean("bool"));
assertEquals(123, toml.getLong("integer").intValue());
assertEquals(123.456, toml.getDouble("float"), 0);
assertEquals("abc", toml.getString("string"));
assertThat(toml.<Long>getList("list"), contains(5L, 6L, 7L, 8L));
}
}