Handle non-String values in a Properties object

The java docs for Properties say avoid this, but
it is possible, so just silently skip any non-strings
we find in the Properties.

Fixes #147
This commit is contained in:
萧岳 2014-03-05 10:33:29 +08:00 committed by Havoc Pennington
parent 5899bce827
commit 1336751ab6
2 changed files with 26 additions and 13 deletions

View File

@ -134,20 +134,22 @@ final class PropertiesParser {
/* Store string values in the associated scope maps */
for (Path path : valuePaths) {
Path parentPath = path.parent();
Map<String, AbstractConfigValue> parent = parentPath != null ? scopes
.get(parentPath) : root;
String last = path.last();
Object rawValue = pathMap.get(path);
AbstractConfigValue value;
if (convertedFromProperties) {
value = new ConfigString(origin, (String) rawValue);
} else {
value = ConfigImpl.fromAnyRef(pathMap.get(path), origin,
FromMapMode.KEYS_ARE_PATHS);
if(!convertedFromProperties || rawValue instanceof String || rawValue instanceof Number || rawValue==null) {
Path parentPath = path.parent();
Map<String, AbstractConfigValue> parent = parentPath != null ? scopes
.get(parentPath) : root;
String last = path.last();
AbstractConfigValue value;
if (convertedFromProperties) {
value = new ConfigString(origin, rawValue.toString());
} else {
value = ConfigImpl.fromAnyRef(pathMap.get(path), origin,
FromMapMode.KEYS_ARE_PATHS);
}
parent.put(last, value);
}
parent.put(last, value);
}
/*

View File

@ -5,7 +5,7 @@ package com.typesafe.config.impl
import org.junit.Assert._
import org.junit._
import java.util.Properties
import java.util.{ Date, Properties }
import com.typesafe.config.Config
import com.typesafe.config.ConfigParseOptions
import com.typesafe.config.ConfigFactory
@ -184,4 +184,15 @@ class PropertiesTest extends TestUtils {
assertEquals(Seq(-2, -1, 0, 1, 2, 3, 4, 5, 6), conf.getIntList("a").asScala.toSeq)
conf.checkValid(reference)
}
@Test
def skipNonStringsInProperties() {
val props = new Properties()
props.put("a", new ThreadLocal[String]())
props.put("b", new Date())
val conf = ConfigFactory.parseProperties(props)
assertEquals(0, conf.root().size())
}
}