If a bean sees foo-bar and fooBar, prefer camel name

This commit is contained in:
Havoc Pennington 2015-02-27 16:27:26 -05:00
parent 4c9d5aae4f
commit 26eec7be90
4 changed files with 48 additions and 3 deletions

View File

@ -33,9 +33,17 @@ public class ConfigBeanImpl {
Map<String, AbstractConfigValue> configProps = new HashMap<String, AbstractConfigValue>();
Map<String, String> originalNames = new HashMap<String, String>();
for (Map.Entry<String, ConfigValue> configProp : config.root().entrySet()) {
String camelName = ConfigImplUtil.toCamelCase(configProp.getKey());
configProps.put(camelName, (AbstractConfigValue) configProp.getValue());
originalNames.put(camelName, configProp.getKey());
String originalName = configProp.getKey();
String camelName = ConfigImplUtil.toCamelCase(originalName);
// if a setting is in there both as some hyphen name and the camel name,
// the camel one wins
if (originalNames.containsKey(camelName) && originalName != camelName) {
// if we aren't a camel name to start with, we lose.
// if we are or we are the first matching key, we win.
} else {
configProps.put(camelName, (AbstractConfigValue) configProp.getValue());
originalNames.put(camelName, originalName);
}
}
BeanInfo beanInfo = null;

View File

@ -0,0 +1,22 @@
package beanconfig;
public class PreferCamelNamesConfig {
private String fooBar;
private String bazBar;
public String getFooBar() {
return fooBar;
}
public void setFooBar(String v) {
this.fooBar = v;
}
public String getBazBar() {
return bazBar;
}
public void setBazBar(String v) {
this.bazBar = v;
}
}

View File

@ -62,5 +62,11 @@
"shouldBeInt" : true,
"should-be-boolean" : 42,
"should-be-list" : "hello"
},
"preferCamelNames" : {
"foo-bar" : "no",
"fooBar" : "yes",
"baz-bar" : "no",
"bazBar" : "yes"
}
}

View File

@ -118,6 +118,15 @@ class ConfigBeanFactoryTest extends TestUtils {
assertEquals(ConfigMemorySize.ofBytes(1000), beanConfig.getThousandBytes)
}
@Test
def testPreferCamelNames() {
val beanConfig = ConfigBeanFactory.create(loadConfig().getConfig("preferCamelNames"), classOf[PreferCamelNamesConfig])
assertNotNull(beanConfig)
assertEquals("yes", beanConfig.getFooBar)
assertEquals("yes", beanConfig.getBazBar)
}
private def loadConfig(): Config = {
val configIs: InputStream = this.getClass().getClassLoader().getResourceAsStream("beanconfig/beanconfig01.conf")
try {