From a8b6a6b4aa737c59ee50c41d4481fe592624e917 Mon Sep 17 00:00:00 2001 From: md_5 Date: Mon, 23 Sep 2013 10:28:30 +1000 Subject: [PATCH] Finish basic Yaml configuration API, complete with unit tests. Needs a lot of work with regards to how sections are handled, open to massive improvements from anyone that has more know-how. --- api/pom.xml | 12 +-- config/pom.xml | 9 +-- .../net/md_5/bungee/config/Configuration.java | 76 ++++++++++++------- .../bungee/config/ConfigurationProvider.java | 2 +- .../md_5/bungee/config/YamlConfiguration.java | 3 +- .../bungee/config/YamlConfigurationTest.java | 61 +++++++++++++++ pom.xml | 1 + proxy/pom.xml | 8 +- 8 files changed, 126 insertions(+), 46 deletions(-) create mode 100644 config/src/test/java/net/md_5/bungee/config/YamlConfigurationTest.java diff --git a/api/pom.xml b/api/pom.xml index 2db53be4..1828ce78 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -25,6 +25,12 @@ 14.0.1 compile + + net.md-5 + bungeecord-config + ${project.version} + compile + net.md-5 bungeecord-event @@ -37,11 +43,5 @@ ${project.version} compile - - org.yaml - snakeyaml - 1.12 - compile - diff --git a/config/pom.xml b/config/pom.xml index cafe2d25..27467217 100644 --- a/config/pom.xml +++ b/config/pom.xml @@ -19,16 +19,11 @@ Generic java configuration API intended for use with BungeeCord - - junit - junit - 4.11 - test - org.yaml snakeyaml - 1.11 + 1.13 + compile diff --git a/config/src/main/java/net/md_5/bungee/config/Configuration.java b/config/src/main/java/net/md_5/bungee/config/Configuration.java index 43af0fff..eee2af6c 100644 --- a/config/src/main/java/net/md_5/bungee/config/Configuration.java +++ b/config/src/main/java/net/md_5/bungee/config/Configuration.java @@ -2,7 +2,7 @@ package net.md_5.bungee.config; import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import lombok.AccessLevel; @@ -14,35 +14,52 @@ public final class Configuration private static final char SEPARATOR = '.'; private final Map self; - private Map comments = new HashMap<>(); private final Configuration defaults; - private Map getHolder(String path, Map parent, boolean create) - { - return null; - } - - private Object get(String path, Map holder) + private Configuration getSectionFor(String path) { int index = path.indexOf( SEPARATOR ); - String first, second; if ( index == -1 ) { - second = path; - } else - { - first = path.substring( 0, index ); - second = path.substring( index + 1, path.length() ); + return this; } - return null; + + String root = path.substring( 0, index ); + Object section = self.get( root ); + if ( section == null ) + { + section = new LinkedHashMap<>(); + self.put( root, section ); + } + if ( section instanceof Configuration ) + { + return (Configuration) section; + } + + return new Configuration( (Map) section, ( defaults == null ) ? null : defaults.getSectionFor( path ) ); + } + + private String getChild(String path) + { + int index = path.indexOf( SEPARATOR ); + return ( index == -1 ) ? path : path.substring( index + 1 ); } /*------------------------------------------------------------------------*/ @SuppressWarnings("unchecked") public T get(String path, T def) { - Object val = get( path, self ); - return ( val != null && val.getClass().isInstance( def ) ) ? (T) val : (T) defaults.get( path ); + Configuration section = getSectionFor( path ); + Object val; + if ( section == this ) + { + val = self.get( path ); + } else + { + val = section.get( getChild( path ), def ); + } + + return ( val != null ) ? (T) val : def; } public Object get(String path) @@ -52,19 +69,26 @@ public final class Configuration public Object getDefault(String path) { - return defaults.get( path ); - } - - public void set(String path, Object value, String comment) - { - String child = path.substring( path.indexOf( SEPARATOR ) + 1 ); - getHolder( path, self, true ).put( child, value ); - getHolder( path, comments, true ).put( child, value ); + return ( defaults == null ) ? null : defaults.get( path ); } public void set(String path, Object value) { - set( path, value, null ); + Configuration section = getSectionFor( path ); + if ( section == this ) + { + self.put( path, value ); + } else + { + section.set( getChild( path ), value ); + } + } + + /*------------------------------------------------------------------------*/ + public Configuration getSection(String path) + { + Object def = getDefault( path ); + return new Configuration( (Map) ( get( path, ( def instanceof Map ) ? def : Collections.EMPTY_MAP ) ), ( defaults == null ) ? null : defaults.getSection( path ) ); } /*------------------------------------------------------------------------*/ diff --git a/config/src/main/java/net/md_5/bungee/config/ConfigurationProvider.java b/config/src/main/java/net/md_5/bungee/config/ConfigurationProvider.java index 7d1b5327..5329c579 100644 --- a/config/src/main/java/net/md_5/bungee/config/ConfigurationProvider.java +++ b/config/src/main/java/net/md_5/bungee/config/ConfigurationProvider.java @@ -15,7 +15,7 @@ public abstract class ConfigurationProvider providers.put( YamlConfiguration.class, new YamlConfiguration() ); } - public ConfigurationProvider getProvider(Class provider) + public static ConfigurationProvider getProvider(Class provider) { return providers.get( provider ); } diff --git a/config/src/main/java/net/md_5/bungee/config/YamlConfiguration.java b/config/src/main/java/net/md_5/bungee/config/YamlConfiguration.java index 74d84ab8..666aef6b 100644 --- a/config/src/main/java/net/md_5/bungee/config/YamlConfiguration.java +++ b/config/src/main/java/net/md_5/bungee/config/YamlConfiguration.java @@ -1,7 +1,6 @@ package net.md_5.bungee.config; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.Reader; @@ -9,7 +8,7 @@ import java.util.Map; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; -public class YamlConfiguration extends ConfigurationProvider +class YamlConfiguration extends ConfigurationProvider { private final ThreadLocal yaml = new ThreadLocal() diff --git a/config/src/test/java/net/md_5/bungee/config/YamlConfigurationTest.java b/config/src/test/java/net/md_5/bungee/config/YamlConfigurationTest.java new file mode 100644 index 00000000..7a9de2be --- /dev/null +++ b/config/src/test/java/net/md_5/bungee/config/YamlConfigurationTest.java @@ -0,0 +1,61 @@ +package net.md_5.bungee.config; + +import java.util.List; +import java.util.Map; +import org.junit.Assert; +import org.junit.Test; + +public class YamlConfigurationTest +{ + + private String docuement = "" + + "receipt: Oz-Ware Purchase Invoice\n" + + "date: 2012-08-06\n" + + "customer:\n" + + " given: Dorothy\n" + + " family: Gale\n" + + "\n" + + "items:\n" + + " - part_no: A4786\n" + + " descrip: Water Bucket (Filled)\n" + + " price: 1.47\n" + + " quantity: 4\n" + + "\n" + + " - part_no: E1628\n" + + " descrip: High Heeled \"Ruby\" Slippers\n" + + " size: 8\n" + + " price: 100.27\n" + + " quantity: 1\n" + + "\n" + + "bill-to: &id001\n" + + " street: |\n" + + " 123 Tornado Alley\n" + + " Suite 16\n" + + " city: East Centerville\n" + + " state: KS\n" + + "\n" + + "ship-to: *id001\n" + + "\n" + + "specialDelivery: >\n" + + " Follow the Yellow Brick\n" + + " Road to the Emerald City.\n" + + " Pay no attention to the\n" + + " man behind the curtain."; + + @Test + public void testRead() throws Exception + { + Configuration conf = ConfigurationProvider.getProvider( YamlConfiguration.class ).load( docuement ); + + Assert.assertEquals( "receipt", "Oz-Ware Purchase Invoice", conf.getString( "receipt" ) ); + // Assert.assertEquals( "date", "2012-08-06", conf.get( "date" ).toString() ); + + Configuration customer = conf.getSection( "customer" ); + Assert.assertEquals( "customer.given", "Dorothy", customer.getString( "given" ) ); + Assert.assertEquals( "customer.given", "Dorothy", conf.getString( "customer.given" ) ); + + List items = conf.getList( "items" ); + Map item = (Map) items.get( 0 ); + Assert.assertEquals( "items[0].part_no", "A4786", item.get( "part_no" ) ); + } +} diff --git a/pom.xml b/pom.xml index 7bc68976..f2438781 100644 --- a/pom.xml +++ b/pom.xml @@ -38,6 +38,7 @@ api + config event protocol proxy diff --git a/proxy/pom.xml b/proxy/pom.xml index dff71165..994f2984 100644 --- a/proxy/pom.xml +++ b/proxy/pom.xml @@ -19,7 +19,7 @@ Proxy component of the Elastic Portal Suite - yyyyMMdd + yyyyMMdd @@ -43,13 +43,13 @@ net.md-5 - bungeecord-protocol + bungeecord-api ${project.version} compile net.md-5 - bungeecord-api + bungeecord-protocol ${project.version} compile @@ -97,7 +97,7 @@ - net.md_5.bungee.BungeeCord + net.md_5.bungee.Bootstrap ${describe} ${maven.build.timestamp}