Merge pull request from racc/typesafeconfig-guice

Adds the ability for ConfigBeanImpl to handle a List of Beans.
This commit is contained in:
Havoc Pennington 2015-06-24 08:41:55 -04:00
commit d7d0a51776
5 changed files with 62 additions and 5 deletions
config/src
main/java/com/typesafe/config/impl
test
java/beanconfig
resources/beanconfig
scala/com/typesafe/config/impl

View File

@ -193,6 +193,13 @@ public class ConfigBeanImpl {
return config.getObjectList(configPropName);
} else if (elementType == ConfigValue.class) {
return config.getList(configPropName);
} else if (hasAtLeastOneBeanProperty((Class<?>) elementType)) {
List<Object> beanList = new ArrayList<Object>();
List<? extends Config> configList = config.getConfigList(configPropName);
for (Config listMember : configList) {
beanList.add(createInternal(listMember, (Class<?>) elementType));
}
return beanList;
} else {
throw new ConfigException.BadBean("Bean property '" + configPropName + "' of class " + beanClass.getName() + " has unsupported list element type " + elementType);
}

View File

@ -23,6 +23,7 @@ public class ArraysConfig {
List<ConfigValue> ofConfigValue;
List<Duration> ofDuration;
List<ConfigMemorySize> ofMemorySize;
List<StringsConfig> ofStringBean;
public List<Integer> getEmpty() {
return empty;
@ -127,4 +128,12 @@ public class ArraysConfig {
public void setOfMemorySize(List<ConfigMemorySize> ofMemorySize) {
this.ofMemorySize = ofMemorySize;
}
public List<StringsConfig> getOfStringBean() {
return ofStringBean;
}
public void setOfStringBean(List<StringsConfig> ofStringBean) {
this.ofStringBean = ofStringBean;
}
}

View File

@ -20,4 +20,26 @@ public class StringsConfig {
public void setYes(String s) {
yes = s;
}
@Override
public boolean equals(Object o) {
if (o instanceof StringsConfig) {
StringsConfig sc = (StringsConfig) o;
return sc.abcd.equals(abcd) &&
sc.yes.equals(yes);
} else {
return false;
}
}
@Override
public int hashCode() {
int h = 41 * (41 + abcd.hashCode());
return h + yes.hashCode();
}
@Override
public String toString() {
return "StringsConfig(" + abcd + "," + yes + ")";
}
}

View File

@ -52,7 +52,17 @@
"ofConfigObject" : [${numbers}, ${booleans}, ${strings}],
"ofConfigValue" : [1, 2, "a"],
"ofDuration" : [1, 2h, 3 days],
"ofMemorySize" : [1024, 1M, 1G]
"ofMemorySize" : [1024, 1M, 1G],
"ofStringBean" : [
{
abcd : "testAbcdOne"
yes : "testYesOne"
},
{
abcd : "testAbcdTwo"
yes : "testYesTwo"
}
]
},
"bytes" : {
"kilobyte" : "1kB",

View File

@ -46,10 +46,10 @@ class ConfigBeanFactoryTest extends TestUtils {
ConfigBeanFactory.create(config, classOf[ValidationBeanConfig])
}
val expecteds = Seq(Missing("propNotListedInConfig", 67, "string"),
WrongType("shouldBeInt", 68, "number", "boolean"),
WrongType("should-be-boolean", 69, "boolean", "number"),
WrongType("should-be-list", 70, "list", "string"))
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"))
checkValidationException(e, expecteds)
}
@ -111,6 +111,15 @@ class ConfigBeanFactoryTest extends TestUtils {
ConfigMemorySize.ofBytes(1048576),
ConfigMemorySize.ofBytes(1073741824)),
beanConfig.getOfMemorySize.asScala)
val stringsConfigOne = new StringsConfig();
stringsConfigOne.setAbcd("testAbcdOne")
stringsConfigOne.setYes("testYesOne")
val stringsConfigTwo = new StringsConfig();
stringsConfigTwo.setAbcd("testAbcdTwo")
stringsConfigTwo.setYes("testYesTwo")
assertEquals(List(stringsConfigOne, stringsConfigTwo).asJava, beanConfig.getOfStringBean)
}
@Test