Replaced "key group" by "table"

This commit is contained in:
moandji.ezana 2014-04-06 22:25:21 +02:00
parent 66a18af38a
commit bc78ff64b5
8 changed files with 45 additions and 45 deletions

View file

@ -82,7 +82,7 @@ public class Toml {
}
@SuppressWarnings("unchecked")
public Toml getKeyGroup(String key) {
public Toml getTable(String key) {
return new Toml((Map<String, Object>) get(key));
}

View file

@ -18,19 +18,19 @@ class TomlParser extends BaseParser<Object> {
static class Results {
public Map<String, Object> values = new HashMap<String, Object>();
public Set<String> keyGroups = new HashSet<String>();
public Set<String> tables = new HashSet<String>();
public StringBuilder errors = new StringBuilder();
}
public Rule Toml() {
return Sequence(push(new TomlParser.Results()), push(((TomlParser.Results) peek()).values), OneOrMore(FirstOf(KeyGroup(), '\n', Comment(), Key())));
return Sequence(push(new TomlParser.Results()), push(((TomlParser.Results) peek()).values), OneOrMore(FirstOf(Table(), '\n', Comment(), Key())));
}
Rule KeyGroup() {
return Sequence(Sequence(KeyGroupDelimiter(), KeyGroupName(), addKeyGroup((String) pop()), KeyGroupDelimiter(), Spacing()), checkKeyGroup(match()));
Rule Table() {
return Sequence(Sequence(TableDelimiter(), TableName(), addTable((String) pop()), TableDelimiter(), Spacing()), checkTable(match()));
}
boolean checkKeyGroup(String definition) {
boolean checkTable(String definition) {
String afterBracket = definition.substring(definition.indexOf(']') + 1);
for (char character : afterBracket.toCharArray()) {
if (character == '#') {
@ -48,8 +48,8 @@ class TomlParser extends BaseParser<Object> {
return Sequence(Spacing(), KeyName(), EqualsSign(), VariableValues(), Spacing(), swap(), addKey((String) pop(), pop()));
}
Rule KeyGroupName() {
return Sequence(OneOrMore(TestNot(KeyGroupDelimiter()), FirstOf(Letter(), Digit(), ANY)), push(match()));
Rule TableName() {
return Sequence(OneOrMore(TestNot(TableDelimiter()), FirstOf(Letter(), Digit(), ANY)), push(match()));
}
Rule KeyName() {
@ -113,7 +113,7 @@ class TomlParser extends BaseParser<Object> {
}
@SuppressNode
Rule KeyGroupDelimiter() {
Rule TableDelimiter() {
return AnyOf("[]");
}
@ -153,35 +153,35 @@ class TomlParser extends BaseParser<Object> {
}
@SuppressWarnings("unchecked")
boolean addKeyGroup(String name) {
boolean addTable(String name) {
String[] split = name.split("\\.");
while (getContext().getValueStack().size() > 2) {
drop();
}
Map<String, Object> newKeyGroup = (Map<String, Object>) getContext().getValueStack().peek();
Map<String, Object> newTable = (Map<String, Object>) getContext().getValueStack().peek();
if (!results().keyGroups.add(name)) {
if (!results().tables.add(name)) {
results().errors.append("Could not create key group ").append(name).append(": key group already exists!\n");
return true;
}
for (String splitKey : split) {
if (!newKeyGroup.containsKey(splitKey)) {
newKeyGroup.put(splitKey, new HashMap<String, Object>());
if (!newTable.containsKey(splitKey)) {
newTable.put(splitKey, new HashMap<String, Object>());
}
Object currentValue = newKeyGroup.get(splitKey);
Object currentValue = newTable.get(splitKey);
if (!(currentValue instanceof Map)) {
results().errors.append("Could not create key group ").append(name).append(": key already has a value!\n");
return true;
}
newKeyGroup = (Map<String, Object>) currentValue;
newTable = (Map<String, Object>) currentValue;
}
push(newKeyGroup);
push(newTable);
return true;
}

View file

@ -24,7 +24,7 @@ public class RealWorldTest {
assertEquals("TOML Example", toml.getString("title"));
Toml owner = toml.getKeyGroup("owner");
Toml owner = toml.getTable("owner");
assertEquals("Tom Preston-Werner", owner.getString("name"));
assertEquals("GitHub", owner.getString("organization"));
assertEquals("GitHub Cofounder & CEO\nLikes tater tots and beer.", owner.getString("bio"));
@ -35,21 +35,21 @@ public class RealWorldTest {
dob.setTimeZone(TimeZone.getTimeZone("UTC"));
assertEquals(dob.getTime(), owner.getDate("dob"));
Toml database = toml.getKeyGroup("database");
Toml database = toml.getTable("database");
assertEquals("192.168.1.1", database.getString("server"));
assertEquals(5000L, database.getLong("connection_max").longValue());
assertTrue(database.getBoolean("enabled"));
assertEquals(Arrays.asList(8001L, 8001L, 8002L), database.getList("ports", Long.class));
Toml servers = toml.getKeyGroup("servers");
Toml alphaServers = servers.getKeyGroup("alpha");
Toml servers = toml.getTable("servers");
Toml alphaServers = servers.getTable("alpha");
assertEquals("10.0.0.1", alphaServers.getString("ip"));
assertEquals("eqdc10", alphaServers.getString("dc"));
Toml betaServers = servers.getKeyGroup("beta");
Toml betaServers = servers.getTable("beta");
assertEquals("10.0.0.2", betaServers.getString("ip"));
assertEquals("eqdc10", betaServers.getString("dc"));
Toml clients = toml.getKeyGroup("clients");
Toml clients = toml.getTable("clients");
assertEquals(asList(asList("gamma", "delta"), asList(1L, 2L)), clients.getList("data", String.class));
assertEquals(asList("alpha", "omega"), clients.getList("hosts", String.class));
}
@ -63,7 +63,7 @@ public class RealWorldTest {
assertEquals(asList("Test #11 ]proved that", "Experiment #9 was a success"), toml.getList("the.hard.test_array2", String.class));
assertEquals(" Same thing, but with a string #", toml.getString("the.hard.another_test_string"));
assertEquals(" And when \"'s are in the string, along with # \"", toml.getString("the.hard.harder_test_string"));
Toml theHardBit = toml.getKeyGroup("the.hard.bit#");
Toml theHardBit = toml.getTable("the.hard.bit#");
assertEquals("You don't think some user won't do that?", theHardBit.getString("what?"));
assertEquals(asList("]"), theHardBit.getList("multi_line_array", String.class));
}
@ -72,8 +72,8 @@ public class RealWorldTest {
public void should_allow_keys_with_same_name_in_different_groups() throws Exception {
Toml toml = new Toml().parse(new File(getClass().getResource("should_allow_keys_with_same_name_in_different_groups.toml").getFile()));
assertTrue(toml.getKeyGroup("siteInfo.local.sh").getBoolean("enable"));
assertFalse(toml.getKeyGroup("siteInfo.localMobile.sh").getBoolean("enable"));
assertTrue(toml.getTable("siteInfo.local.sh").getBoolean("enable"));
assertFalse(toml.getTable("siteInfo.localMobile.sh").getBoolean("enable"));
}
@SuppressWarnings("unchecked")

View file

@ -48,14 +48,14 @@ public class TomlDefaultsTest {
public void should_fall_back_to_key_group() throws Exception {
Toml toml = new Toml(defaultToml).parse("");
assertEquals("a", toml.getKeyGroup("group").getString("a"));
assertEquals("a", toml.getTable("group").getString("a"));
}
@Test
public void should_fall_back_to_key_within_key_group() throws Exception {
Toml toml = new Toml(defaultToml).parse("[group]\nb=1");
assertEquals(1, toml.getKeyGroup("group").getLong("b").intValue());
assertEquals("a", toml.getKeyGroup("group").getString("a"));
assertEquals(1, toml.getTable("group").getLong("b").intValue());
assertEquals("a", toml.getTable("group").getString("a"));
}
}

View file

@ -81,7 +81,7 @@ public class TomlTest {
public void should_get_key_group() throws Exception {
Toml toml = new Toml().parse("[group]\nkey = \"value\"");
Toml group = toml.getKeyGroup("group");
Toml group = toml.getTable("group");
assertEquals("value", group.getString("key"));
}
@ -104,14 +104,14 @@ public class TomlTest {
public void should_get_key_group_for_multi_key() throws Exception {
Toml toml = new Toml().parse("[group]\nother=1\n[group.sub]\nkey = \"value\"");
assertEquals("value", toml.getKeyGroup("group.sub").getString("key"));
assertEquals("value", toml.getTable("group.sub").getString("key"));
}
@Test
public void should_get_key_group_for_multi_key_with_no_parent_key_group() throws Exception {
Toml toml = new Toml().parse("[group.sub]\nkey = \"value\"");
assertEquals("value", toml.getKeyGroup("group.sub").getString("key"));
assertEquals("value", toml.getTable("group.sub").getString("key"));
}
@Test
@ -119,7 +119,7 @@ public class TomlTest {
Toml toml = new Toml().parse("[a.b]\nc=1\n[a]\nd=2");
assertEquals(2, toml.getLong("a.d").intValue());
assertEquals(1, toml.getKeyGroup("a.b").getLong("c").intValue());
assertEquals(1, toml.getTable("a.b").getLong("c").intValue());
}
@Test

View file

@ -3,8 +3,8 @@ package com.moandjiezana.toml;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import com.moandjiezana.toml.testutils.KeyGroupAsMap;
import com.moandjiezana.toml.testutils.TomlKeyGroups;
import com.moandjiezana.toml.testutils.TableAsMap;
import com.moandjiezana.toml.testutils.TomlTables;
import com.moandjiezana.toml.testutils.TomlPrimitives;
import java.io.File;
@ -38,10 +38,10 @@ public class TomlToClassTest {
String fileName = "should_convert_key_groups.toml";
Toml toml = new Toml().parse(file(fileName));
TomlKeyGroups tomlKeyGroups = toml.to(TomlKeyGroups.class);
TomlTables tomlTables = toml.to(TomlTables.class);
assertEquals("value1", tomlKeyGroups.group1.string);
assertEquals("value2", tomlKeyGroups.group2.string);
assertEquals("value1", tomlTables.group1.string);
assertEquals("value2", tomlTables.group2.string);
}
@Test
@ -49,10 +49,10 @@ public class TomlToClassTest {
Toml defaults = new Toml().parse(file("should_convert_key_groups.toml"));
Toml toml = new Toml(defaults).parse("");
TomlKeyGroups tomlKeyGroups = toml.to(TomlKeyGroups.class);
TomlTables tomlTables = toml.to(TomlTables.class);
assertEquals("value1", tomlKeyGroups.group1.string);
assertEquals("value2", tomlKeyGroups.group2.string);
assertEquals("value1", tomlTables.group1.string);
assertEquals("value2", tomlTables.group2.string);
}
@Test
@ -64,9 +64,9 @@ public class TomlToClassTest {
@Test
public void should_convert_key_group_as_map() throws Exception {
KeyGroupAsMap keyGroupAsMap = new Toml().parse("[group]\nkey=\"value\"").to(KeyGroupAsMap.class);
TableAsMap tableAsMap = new Toml().parse("[group]\nkey=\"value\"").to(TableAsMap.class);
assertEquals("value", keyGroupAsMap.group.get("key"));
assertEquals("value", tableAsMap.group.get("key"));
}
private File file(String fileName) {

View file

@ -2,7 +2,7 @@ package com.moandjiezana.toml.testutils;
import java.util.Map;
public class KeyGroupAsMap {
public class TableAsMap {
public Map<String, Object> group;
}

View file

@ -1,7 +1,7 @@
package com.moandjiezana.toml.testutils;
public class TomlKeyGroups {
public class TomlTables {
public TomlPrimitives group1;
public TomlPrimitives group2;