mirror of
https://github.com/plexusorg/toml4j.git
synced 2025-02-11 11:40:27 +00:00
Replaced "key group" by "table"
This commit is contained in:
parent
66a18af38a
commit
bc78ff64b5
8 changed files with 45 additions and 45 deletions
|
@ -82,7 +82,7 @@ public class Toml {
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public Toml getKeyGroup(String key) {
|
public Toml getTable(String key) {
|
||||||
return new Toml((Map<String, Object>) get(key));
|
return new Toml((Map<String, Object>) get(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,19 +18,19 @@ class TomlParser extends BaseParser<Object> {
|
||||||
|
|
||||||
static class Results {
|
static class Results {
|
||||||
public Map<String, Object> values = new HashMap<String, Object>();
|
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 StringBuilder errors = new StringBuilder();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Rule Toml() {
|
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() {
|
Rule Table() {
|
||||||
return Sequence(Sequence(KeyGroupDelimiter(), KeyGroupName(), addKeyGroup((String) pop()), KeyGroupDelimiter(), Spacing()), checkKeyGroup(match()));
|
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);
|
String afterBracket = definition.substring(definition.indexOf(']') + 1);
|
||||||
for (char character : afterBracket.toCharArray()) {
|
for (char character : afterBracket.toCharArray()) {
|
||||||
if (character == '#') {
|
if (character == '#') {
|
||||||
|
@ -48,8 +48,8 @@ class TomlParser extends BaseParser<Object> {
|
||||||
return Sequence(Spacing(), KeyName(), EqualsSign(), VariableValues(), Spacing(), swap(), addKey((String) pop(), pop()));
|
return Sequence(Spacing(), KeyName(), EqualsSign(), VariableValues(), Spacing(), swap(), addKey((String) pop(), pop()));
|
||||||
}
|
}
|
||||||
|
|
||||||
Rule KeyGroupName() {
|
Rule TableName() {
|
||||||
return Sequence(OneOrMore(TestNot(KeyGroupDelimiter()), FirstOf(Letter(), Digit(), ANY)), push(match()));
|
return Sequence(OneOrMore(TestNot(TableDelimiter()), FirstOf(Letter(), Digit(), ANY)), push(match()));
|
||||||
}
|
}
|
||||||
|
|
||||||
Rule KeyName() {
|
Rule KeyName() {
|
||||||
|
@ -113,7 +113,7 @@ class TomlParser extends BaseParser<Object> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressNode
|
@SuppressNode
|
||||||
Rule KeyGroupDelimiter() {
|
Rule TableDelimiter() {
|
||||||
return AnyOf("[]");
|
return AnyOf("[]");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,35 +153,35 @@ class TomlParser extends BaseParser<Object> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
boolean addKeyGroup(String name) {
|
boolean addTable(String name) {
|
||||||
String[] split = name.split("\\.");
|
String[] split = name.split("\\.");
|
||||||
|
|
||||||
while (getContext().getValueStack().size() > 2) {
|
while (getContext().getValueStack().size() > 2) {
|
||||||
drop();
|
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");
|
results().errors.append("Could not create key group ").append(name).append(": key group already exists!\n");
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (String splitKey : split) {
|
for (String splitKey : split) {
|
||||||
if (!newKeyGroup.containsKey(splitKey)) {
|
if (!newTable.containsKey(splitKey)) {
|
||||||
newKeyGroup.put(splitKey, new HashMap<String, Object>());
|
newTable.put(splitKey, new HashMap<String, Object>());
|
||||||
}
|
}
|
||||||
Object currentValue = newKeyGroup.get(splitKey);
|
Object currentValue = newTable.get(splitKey);
|
||||||
if (!(currentValue instanceof Map)) {
|
if (!(currentValue instanceof Map)) {
|
||||||
results().errors.append("Could not create key group ").append(name).append(": key already has a value!\n");
|
results().errors.append("Could not create key group ").append(name).append(": key already has a value!\n");
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
newKeyGroup = (Map<String, Object>) currentValue;
|
newTable = (Map<String, Object>) currentValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
push(newKeyGroup);
|
push(newTable);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ public class RealWorldTest {
|
||||||
|
|
||||||
assertEquals("TOML Example", toml.getString("title"));
|
assertEquals("TOML Example", toml.getString("title"));
|
||||||
|
|
||||||
Toml owner = toml.getKeyGroup("owner");
|
Toml owner = toml.getTable("owner");
|
||||||
assertEquals("Tom Preston-Werner", owner.getString("name"));
|
assertEquals("Tom Preston-Werner", owner.getString("name"));
|
||||||
assertEquals("GitHub", owner.getString("organization"));
|
assertEquals("GitHub", owner.getString("organization"));
|
||||||
assertEquals("GitHub Cofounder & CEO\nLikes tater tots and beer.", owner.getString("bio"));
|
assertEquals("GitHub Cofounder & CEO\nLikes tater tots and beer.", owner.getString("bio"));
|
||||||
|
@ -35,21 +35,21 @@ public class RealWorldTest {
|
||||||
dob.setTimeZone(TimeZone.getTimeZone("UTC"));
|
dob.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||||
assertEquals(dob.getTime(), owner.getDate("dob"));
|
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("192.168.1.1", database.getString("server"));
|
||||||
assertEquals(5000L, database.getLong("connection_max").longValue());
|
assertEquals(5000L, database.getLong("connection_max").longValue());
|
||||||
assertTrue(database.getBoolean("enabled"));
|
assertTrue(database.getBoolean("enabled"));
|
||||||
assertEquals(Arrays.asList(8001L, 8001L, 8002L), database.getList("ports", Long.class));
|
assertEquals(Arrays.asList(8001L, 8001L, 8002L), database.getList("ports", Long.class));
|
||||||
|
|
||||||
Toml servers = toml.getKeyGroup("servers");
|
Toml servers = toml.getTable("servers");
|
||||||
Toml alphaServers = servers.getKeyGroup("alpha");
|
Toml alphaServers = servers.getTable("alpha");
|
||||||
assertEquals("10.0.0.1", alphaServers.getString("ip"));
|
assertEquals("10.0.0.1", alphaServers.getString("ip"));
|
||||||
assertEquals("eqdc10", alphaServers.getString("dc"));
|
assertEquals("eqdc10", alphaServers.getString("dc"));
|
||||||
Toml betaServers = servers.getKeyGroup("beta");
|
Toml betaServers = servers.getTable("beta");
|
||||||
assertEquals("10.0.0.2", betaServers.getString("ip"));
|
assertEquals("10.0.0.2", betaServers.getString("ip"));
|
||||||
assertEquals("eqdc10", betaServers.getString("dc"));
|
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(asList("gamma", "delta"), asList(1L, 2L)), clients.getList("data", String.class));
|
||||||
assertEquals(asList("alpha", "omega"), clients.getList("hosts", 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(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(" 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"));
|
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("You don't think some user won't do that?", theHardBit.getString("what?"));
|
||||||
assertEquals(asList("]"), theHardBit.getList("multi_line_array", String.class));
|
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 {
|
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()));
|
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"));
|
assertTrue(toml.getTable("siteInfo.local.sh").getBoolean("enable"));
|
||||||
assertFalse(toml.getKeyGroup("siteInfo.localMobile.sh").getBoolean("enable"));
|
assertFalse(toml.getTable("siteInfo.localMobile.sh").getBoolean("enable"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
|
|
|
@ -48,14 +48,14 @@ public class TomlDefaultsTest {
|
||||||
public void should_fall_back_to_key_group() throws Exception {
|
public void should_fall_back_to_key_group() throws Exception {
|
||||||
Toml toml = new Toml(defaultToml).parse("");
|
Toml toml = new Toml(defaultToml).parse("");
|
||||||
|
|
||||||
assertEquals("a", toml.getKeyGroup("group").getString("a"));
|
assertEquals("a", toml.getTable("group").getString("a"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_fall_back_to_key_within_key_group() throws Exception {
|
public void should_fall_back_to_key_within_key_group() throws Exception {
|
||||||
Toml toml = new Toml(defaultToml).parse("[group]\nb=1");
|
Toml toml = new Toml(defaultToml).parse("[group]\nb=1");
|
||||||
|
|
||||||
assertEquals(1, toml.getKeyGroup("group").getLong("b").intValue());
|
assertEquals(1, toml.getTable("group").getLong("b").intValue());
|
||||||
assertEquals("a", toml.getKeyGroup("group").getString("a"));
|
assertEquals("a", toml.getTable("group").getString("a"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -81,7 +81,7 @@ public class TomlTest {
|
||||||
public void should_get_key_group() throws Exception {
|
public void should_get_key_group() throws Exception {
|
||||||
Toml toml = new Toml().parse("[group]\nkey = \"value\"");
|
Toml toml = new Toml().parse("[group]\nkey = \"value\"");
|
||||||
|
|
||||||
Toml group = toml.getKeyGroup("group");
|
Toml group = toml.getTable("group");
|
||||||
|
|
||||||
assertEquals("value", group.getString("key"));
|
assertEquals("value", group.getString("key"));
|
||||||
}
|
}
|
||||||
|
@ -104,14 +104,14 @@ public class TomlTest {
|
||||||
public void should_get_key_group_for_multi_key() throws Exception {
|
public void should_get_key_group_for_multi_key() throws Exception {
|
||||||
Toml toml = new Toml().parse("[group]\nother=1\n[group.sub]\nkey = \"value\"");
|
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
|
@Test
|
||||||
public void should_get_key_group_for_multi_key_with_no_parent_key_group() throws Exception {
|
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\"");
|
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
|
@Test
|
||||||
|
@ -119,7 +119,7 @@ public class TomlTest {
|
||||||
Toml toml = new Toml().parse("[a.b]\nc=1\n[a]\nd=2");
|
Toml toml = new Toml().parse("[a.b]\nc=1\n[a]\nd=2");
|
||||||
|
|
||||||
assertEquals(2, toml.getLong("a.d").intValue());
|
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
|
@Test
|
||||||
|
|
|
@ -3,8 +3,8 @@ package com.moandjiezana.toml;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import com.moandjiezana.toml.testutils.KeyGroupAsMap;
|
import com.moandjiezana.toml.testutils.TableAsMap;
|
||||||
import com.moandjiezana.toml.testutils.TomlKeyGroups;
|
import com.moandjiezana.toml.testutils.TomlTables;
|
||||||
import com.moandjiezana.toml.testutils.TomlPrimitives;
|
import com.moandjiezana.toml.testutils.TomlPrimitives;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
@ -38,10 +38,10 @@ public class TomlToClassTest {
|
||||||
String fileName = "should_convert_key_groups.toml";
|
String fileName = "should_convert_key_groups.toml";
|
||||||
Toml toml = new Toml().parse(file(fileName));
|
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("value1", tomlTables.group1.string);
|
||||||
assertEquals("value2", tomlKeyGroups.group2.string);
|
assertEquals("value2", tomlTables.group2.string);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -49,10 +49,10 @@ public class TomlToClassTest {
|
||||||
Toml defaults = new Toml().parse(file("should_convert_key_groups.toml"));
|
Toml defaults = new Toml().parse(file("should_convert_key_groups.toml"));
|
||||||
Toml toml = new Toml(defaults).parse("");
|
Toml toml = new Toml(defaults).parse("");
|
||||||
|
|
||||||
TomlKeyGroups tomlKeyGroups = toml.to(TomlKeyGroups.class);
|
TomlTables tomlTables = toml.to(TomlTables.class);
|
||||||
|
|
||||||
assertEquals("value1", tomlKeyGroups.group1.string);
|
assertEquals("value1", tomlTables.group1.string);
|
||||||
assertEquals("value2", tomlKeyGroups.group2.string);
|
assertEquals("value2", tomlTables.group2.string);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -64,9 +64,9 @@ public class TomlToClassTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_convert_key_group_as_map() throws Exception {
|
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) {
|
private File file(String fileName) {
|
||||||
|
|
|
@ -2,7 +2,7 @@ package com.moandjiezana.toml.testutils;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class KeyGroupAsMap {
|
public class TableAsMap {
|
||||||
|
|
||||||
public Map<String, Object> group;
|
public Map<String, Object> group;
|
||||||
}
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
package com.moandjiezana.toml.testutils;
|
package com.moandjiezana.toml.testutils;
|
||||||
|
|
||||||
|
|
||||||
public class TomlKeyGroups {
|
public class TomlTables {
|
||||||
|
|
||||||
public TomlPrimitives group1;
|
public TomlPrimitives group1;
|
||||||
public TomlPrimitives group2;
|
public TomlPrimitives group2;
|
Loading…
Reference in a new issue