This commit is contained in:
Philippe Collin 2017-05-24 15:02:57 +00:00 committed by GitHub
commit f0060c5c35
7 changed files with 36 additions and 7 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

@ -873,7 +873,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) {
// strings may also be convertible to lists if empty
if (value instanceof SimpleConfigList || value instanceof SimpleConfigObject || value instanceof ConfigString) {
return true;
} else {
return false;

View File

@ -11,6 +11,7 @@ import com.typesafe.config.ConfigValue;
public class ArraysConfig {
List<Integer> empty;
List<Integer> fromEmptyString;
List<Integer> ofInt;
List<String> ofString;
List<Double> ofDouble;
@ -33,6 +34,10 @@ public class ArraysConfig {
this.empty = empty;
}
public List<Integer> getFromEmptyString() { return fromEmptyString; }
public void setFromEmptyString(List<Integer> fromEmptyString) { this.fromEmptyString = fromEmptyString; }
public List<Integer> getOfInt() {
return ofInt;
}

View File

@ -40,6 +40,7 @@
"arrays" : {
"empty" : [],
"fromEmptyString": "",
"ofInt" : [1, 2, 3],
"ofString" : [ ${strings.a}, ${strings.b}, ${strings.c} ],
"of-double" : [3.14, 4.14, 5.14],

View File

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

View File

@ -48,10 +48,9 @@ class ConfigBeanFactoryTest extends TestUtils {
ConfigBeanFactory.create(config, classOf[ValidationBeanConfig])
}
val expecteds = Seq(Missing("propNotListedInConfig", 77, "string"),
WrongType("shouldBeInt", 78, "number", "boolean"),
WrongType("should-be-boolean", 79, "boolean", "number"),
WrongType("should-be-list", 80, "list", "string"))
val expecteds = Seq(Missing("propNotListedInConfig", 78, "string"),
WrongType("shouldBeInt", 79, "number", "boolean"),
WrongType("should-be-boolean", 80, "boolean", "number"))
checkValidationException(e, expecteds)
}
@ -100,6 +99,7 @@ class ConfigBeanFactoryTest extends TestUtils {
val beanConfig: ArraysConfig = ConfigBeanFactory.create(loadConfig().getConfig("arrays"), classOf[ArraysConfig])
assertNotNull(beanConfig)
assertEquals(List().asJava, beanConfig.getEmpty)
assertEquals(List().asJava, beanConfig.getFromEmptyString)
assertEquals(List(1, 2, 3).asJava, beanConfig.getOfInt)
assertEquals(List(32L, 42L, 52L).asJava, beanConfig.getOfLong)
assertEquals(List("a", "b", "c").asJava, beanConfig.getOfString)

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)
@ -661,6 +673,10 @@ class ConfigTest extends TestUtils {
conf.getObjectList("arrays.ofInt")
}
intercept[ConfigException.WrongType] {
conf.getList("strings.abcd")
}
intercept[ConfigException.WrongType] {
conf.getMilliseconds("ints")
}