mirror of
https://github.com/lightbend/config.git
synced 2025-03-22 23:30:27 +08:00
Merge 9be41b7a69
into d0021d1d3b
This commit is contained in:
commit
f0060c5c35
@ -48,7 +48,12 @@ final class DefaultTransformer {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case LIST:
|
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;
|
break;
|
||||||
case OBJECT:
|
case OBJECT:
|
||||||
// can't go STRING to OBJECT automatically
|
// can't go STRING to OBJECT automatically
|
||||||
|
@ -873,7 +873,8 @@ final class SimpleConfig implements Config, MergeableValue, Serializable {
|
|||||||
}
|
}
|
||||||
} else if (referenceType == ConfigValueType.LIST) {
|
} else if (referenceType == ConfigValueType.LIST) {
|
||||||
// objects may be convertible to lists if they have numeric keys
|
// 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;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
|
@ -11,6 +11,7 @@ import com.typesafe.config.ConfigValue;
|
|||||||
public class ArraysConfig {
|
public class ArraysConfig {
|
||||||
|
|
||||||
List<Integer> empty;
|
List<Integer> empty;
|
||||||
|
List<Integer> fromEmptyString;
|
||||||
List<Integer> ofInt;
|
List<Integer> ofInt;
|
||||||
List<String> ofString;
|
List<String> ofString;
|
||||||
List<Double> ofDouble;
|
List<Double> ofDouble;
|
||||||
@ -33,6 +34,10 @@ public class ArraysConfig {
|
|||||||
this.empty = empty;
|
this.empty = empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Integer> getFromEmptyString() { return fromEmptyString; }
|
||||||
|
|
||||||
|
public void setFromEmptyString(List<Integer> fromEmptyString) { this.fromEmptyString = fromEmptyString; }
|
||||||
|
|
||||||
public List<Integer> getOfInt() {
|
public List<Integer> getOfInt() {
|
||||||
return ofInt;
|
return ofInt;
|
||||||
}
|
}
|
||||||
|
@ -40,6 +40,7 @@
|
|||||||
|
|
||||||
"arrays" : {
|
"arrays" : {
|
||||||
"empty" : [],
|
"empty" : [],
|
||||||
|
"fromEmptyString": "",
|
||||||
"ofInt" : [1, 2, 3],
|
"ofInt" : [1, 2, 3],
|
||||||
"ofString" : [ ${strings.a}, ${strings.b}, ${strings.c} ],
|
"ofString" : [ ${strings.a}, ${strings.b}, ${strings.c} ],
|
||||||
"of-double" : [3.14, 4.14, 5.14],
|
"of-double" : [3.14, 4.14, 5.14],
|
||||||
|
@ -26,7 +26,8 @@
|
|||||||
"true" : "true",
|
"true" : "true",
|
||||||
"yes" : "yes",
|
"yes" : "yes",
|
||||||
"false" : "false",
|
"false" : "false",
|
||||||
"no" : "no"
|
"no" : "no",
|
||||||
|
"empty": ""
|
||||||
},
|
},
|
||||||
|
|
||||||
"arrays" : {
|
"arrays" : {
|
||||||
|
@ -48,10 +48,9 @@ class ConfigBeanFactoryTest extends TestUtils {
|
|||||||
ConfigBeanFactory.create(config, classOf[ValidationBeanConfig])
|
ConfigBeanFactory.create(config, classOf[ValidationBeanConfig])
|
||||||
}
|
}
|
||||||
|
|
||||||
val expecteds = Seq(Missing("propNotListedInConfig", 77, "string"),
|
val expecteds = Seq(Missing("propNotListedInConfig", 78, "string"),
|
||||||
WrongType("shouldBeInt", 78, "number", "boolean"),
|
WrongType("shouldBeInt", 79, "number", "boolean"),
|
||||||
WrongType("should-be-boolean", 79, "boolean", "number"),
|
WrongType("should-be-boolean", 80, "boolean", "number"))
|
||||||
WrongType("should-be-list", 80, "list", "string"))
|
|
||||||
|
|
||||||
checkValidationException(e, expecteds)
|
checkValidationException(e, expecteds)
|
||||||
}
|
}
|
||||||
@ -100,6 +99,7 @@ class ConfigBeanFactoryTest extends TestUtils {
|
|||||||
val beanConfig: ArraysConfig = ConfigBeanFactory.create(loadConfig().getConfig("arrays"), classOf[ArraysConfig])
|
val beanConfig: ArraysConfig = ConfigBeanFactory.create(loadConfig().getConfig("arrays"), classOf[ArraysConfig])
|
||||||
assertNotNull(beanConfig)
|
assertNotNull(beanConfig)
|
||||||
assertEquals(List().asJava, beanConfig.getEmpty)
|
assertEquals(List().asJava, beanConfig.getEmpty)
|
||||||
|
assertEquals(List().asJava, beanConfig.getFromEmptyString)
|
||||||
assertEquals(List(1, 2, 3).asJava, beanConfig.getOfInt)
|
assertEquals(List(1, 2, 3).asJava, beanConfig.getOfInt)
|
||||||
assertEquals(List(32L, 42L, 52L).asJava, beanConfig.getOfLong)
|
assertEquals(List(32L, 42L, 52L).asJava, beanConfig.getOfLong)
|
||||||
assertEquals(List("a", "b", "c").asJava, beanConfig.getOfString)
|
assertEquals(List("a", "b", "c").asJava, beanConfig.getOfString)
|
||||||
|
@ -588,6 +588,18 @@ class ConfigTest extends TestUtils {
|
|||||||
assertEquals(Seq(), conf.getBooleanList("arrays.empty").asScala)
|
assertEquals(Seq(), conf.getBooleanList("arrays.empty").asScala)
|
||||||
assertEquals(Seq(), conf.getNumberList("arrays.empty").asScala)
|
assertEquals(Seq(), conf.getNumberList("arrays.empty").asScala)
|
||||||
assertEquals(Seq(), conf.getList("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
|
// get typed arrays
|
||||||
assertEquals(Seq(1, 2, 3), conf.getIntList("arrays.ofInt").asScala)
|
assertEquals(Seq(1, 2, 3), conf.getIntList("arrays.ofInt").asScala)
|
||||||
@ -661,6 +673,10 @@ class ConfigTest extends TestUtils {
|
|||||||
conf.getObjectList("arrays.ofInt")
|
conf.getObjectList("arrays.ofInt")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
intercept[ConfigException.WrongType] {
|
||||||
|
conf.getList("strings.abcd")
|
||||||
|
}
|
||||||
|
|
||||||
intercept[ConfigException.WrongType] {
|
intercept[ConfigException.WrongType] {
|
||||||
conf.getMilliseconds("ints")
|
conf.getMilliseconds("ints")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user