Key names can have dots in them

This commit is contained in:
moandji.ezana 2014-08-12 22:44:24 +02:00
parent b2888519fb
commit ff4c0cbc16
6 changed files with 79 additions and 9 deletions

View file

@ -12,6 +12,7 @@ import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
@ -246,8 +247,18 @@ public class Toml {
private Object get(String key) {
String[] split = key.split("\\.");
Object current = new HashMap<String, Object>(values);
for (String splitKey : split) {
for (int i = 0; i < split.length; i++) {
if (i == 0 && values.containsKey(key)) {
return values.get(key);
}
String keyWithDot = join(Arrays.copyOfRange(split, i, split.length));
if (current instanceof Map && ((Map<String, Object>) current).containsKey(keyWithDot)) {
return ((Map<String, Object>) current).get(keyWithDot);
}
String splitKey = split[i];
Matcher matcher = ARRAY_INDEX_PATTERN.matcher(splitKey);
int index = -1;
@ -269,6 +280,20 @@ public class Toml {
return current;
}
private String join(String[] strings) {
StringBuilder sb = new StringBuilder();
for (String string : strings) {
sb.append(string).append('.');
}
if (sb.length() > 0) {
sb.deleteCharAt(sb.length() - 1);
}
return sb.toString();
}
private Toml(Toml defaults, Map<String, Object> values) {
this.values = values != null ? values : Collections.<String, Object>emptyMap();

View file

@ -14,7 +14,6 @@ import java.util.TimeZone;
import org.junit.After;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import com.google.gson.Gson;
@ -115,11 +114,15 @@ public class BurntSushiValidTest {
}
@Test
@Ignore
public void key_special_chars() throws Exception {
run("key-special-chars");
}
@Test
public void keys_with_dots() throws Exception {
run("keys-with-dots");
}
@Test
public void long_float() throws Exception {
run("long-float");

View file

@ -232,6 +232,19 @@ public class TomlTest {
assertTrue(toml.getBoolean("key?"));
}
@Test
public void should_support_dots_in_key_names() throws Exception {
Toml toml = new Toml().parse(file("should_support_dots_in_key_names"));
assertEquals(1, toml.getLong("a").intValue());
assertEquals(2, toml.getLong("b.c").intValue());
assertEquals(3, toml.getTable("b").getLong("c").intValue());
assertEquals(4, toml.getLong("b.a.b").intValue());
assertEquals(5, toml.getLong("d.e.a").intValue());
assertEquals(6, toml.getLong("d.e.a.b.c").intValue());
assertEquals(6, toml.getTable("d.e").getLong("a.b.c").intValue());
}
@Test
public void should_support_underscores_in_table_names() throws Exception {
@ -302,11 +315,6 @@ public class TomlTest {
assertEquals(asList(1L, 2L, 3L), toml.getList("key", Long.class));
}
@Test(expected = IllegalStateException.class)
public void should_fail_when_dot_in_key_name() throws Exception {
new Toml().parse("a.a = 1");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_empty_key_name() throws Exception {

View file

@ -0,0 +1,14 @@
{
"plain": {"type": "integer", "value": "1"},
"with.dot": {"type": "integer", "value": "2"},
"plain_table": {
"plain": {"type": "integer", "value": "3"},
"with.dot": {"type": "integer", "value": "4"}
},
"table": {
"withdot": {
"plain": {"type": "integer", "value": "5"},
"key.with.dots": {"type": "integer", "value": "6"}
}
}
}

View file

@ -0,0 +1,10 @@
plain = 1
with.dot = 2
[plain_table]
plain = 3
with.dot = 4
[table.withdot]
plain = 5
key.with.dots = 6

View file

@ -0,0 +1,10 @@
a = 1
b.c = 2
[b]
c = 3
a.b = 4
[d.e]
a = 5
a.b.c = 6