Implicitly convert Map to Configuration

This commit is contained in:
md_5 2016-08-25 11:04:19 +10:00
parent 6563a9241b
commit 98e3c70460
2 changed files with 20 additions and 0 deletions

View File

@ -108,6 +108,11 @@ public final class Configuration
public void set(String path, Object value) public void set(String path, Object value)
{ {
if ( value instanceof Map )
{
value = new Configuration( (Map) value, ( defaults == null ) ? null : defaults.getSection( path ) );
}
Configuration section = getSectionFor( path ); Configuration section = getSectionFor( path );
if ( section == this ) if ( section == this )
{ {

View File

@ -2,6 +2,7 @@ package net.md_5.bungee.config;
import java.io.StringReader; import java.io.StringReader;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.junit.Assert; import org.junit.Assert;
@ -125,4 +126,18 @@ public class YamlConfigurationTest
Assert.assertEquals( null, conf.get( "null.object" ) ); Assert.assertEquals( null, conf.get( "null.object" ) );
Assert.assertEquals( "", conf.getString( "null.object" ) ); Assert.assertEquals( "", conf.getString( "null.object" ) );
} }
@Test
public void testMapAddition()
{
Configuration conf = ConfigurationProvider.getProvider( YamlConfiguration.class ).load( TEST_DOCUMENT );
conf.set( "addition", Collections.singletonMap( "foo", "bar" ) );
// Order matters
Assert.assertEquals( "bar", conf.getSection( "addition" ).getString( "foo" ) );
Assert.assertEquals( "bar", conf.getString( "addition.foo" ) );
Assert.assertTrue( conf.get( "addition" ) instanceof Configuration );
}
} }