An empty string config value is now converted to an empty list when asking for a list.

This commit is contained in:
Philippe Collin 2016-03-30 13:09:45 -04:00
parent 09146706db
commit 909b11854b
4 changed files with 22 additions and 3 deletions

View File

@ -48,7 +48,12 @@ final class DefaultTransformer {
}
break;
case LIST:
// can't go STRING to LIST automatically
// can't go STRING to LIST automatically unless the string is empty
if (s.equals("")) {
// Convert empty string to empty list
ArrayList<AbstractConfigValue> emptyList = new ArrayList<AbstractConfigValue>();
return new SimpleConfigList(value.origin(), emptyList);
}
break;
case OBJECT:
// can't go STRING to OBJECT automatically

View File

@ -838,7 +838,8 @@ final class SimpleConfig implements Config, MergeableValue, Serializable {
}
} else if (referenceType == ConfigValueType.LIST) {
// objects may be convertible to lists if they have numeric keys
if (value instanceof SimpleConfigList || value instanceof SimpleConfigObject) {
// empty strings may also be converted to lists
if (value instanceof SimpleConfigList || value instanceof SimpleConfigObject || value instanceof ConfigString) {
return true;
} else {
return false;

View File

@ -26,7 +26,8 @@
"true" : "true",
"yes" : "yes",
"false" : "false",
"no" : "no"
"no" : "no",
"empty": ""
},
"arrays" : {

View File

@ -588,6 +588,18 @@ class ConfigTest extends TestUtils {
assertEquals(Seq(), conf.getBooleanList("arrays.empty").asScala)
assertEquals(Seq(), conf.getNumberList("arrays.empty").asScala)
assertEquals(Seq(), conf.getList("arrays.empty").asScala)
// get empty array from empty string as any type of array
assertEquals(Seq(), conf.getAnyRefList("strings.empty").asScala)
assertEquals(Seq(), conf.getIntList("strings.empty").asScala)
assertEquals(Seq(), conf.getLongList("strings.empty").asScala)
assertEquals(Seq(), conf.getStringList("strings.empty").asScala)
assertEquals(Seq(), conf.getLongList("strings.empty").asScala)
assertEquals(Seq(), conf.getDoubleList("strings.empty").asScala)
assertEquals(Seq(), conf.getObjectList("strings.empty").asScala)
assertEquals(Seq(), conf.getBooleanList("strings.empty").asScala)
assertEquals(Seq(), conf.getNumberList("strings.empty").asScala)
assertEquals(Seq(), conf.getList("strings.empty").asScala)
// get typed arrays
assertEquals(Seq(1, 2, 3), conf.getIntList("arrays.ofInt").asScala)