mirror of
https://github.com/lightbend/config.git
synced 2025-01-30 06:00:09 +08:00
If a bean sees foo-bar and fooBar, prefer camel name
This commit is contained in:
parent
4c9d5aae4f
commit
26eec7be90
@ -33,9 +33,17 @@ public class ConfigBeanImpl {
|
|||||||
Map<String, AbstractConfigValue> configProps = new HashMap<String, AbstractConfigValue>();
|
Map<String, AbstractConfigValue> configProps = new HashMap<String, AbstractConfigValue>();
|
||||||
Map<String, String> originalNames = new HashMap<String, String>();
|
Map<String, String> originalNames = new HashMap<String, String>();
|
||||||
for (Map.Entry<String, ConfigValue> configProp : config.root().entrySet()) {
|
for (Map.Entry<String, ConfigValue> configProp : config.root().entrySet()) {
|
||||||
String camelName = ConfigImplUtil.toCamelCase(configProp.getKey());
|
String originalName = configProp.getKey();
|
||||||
configProps.put(camelName, (AbstractConfigValue) configProp.getValue());
|
String camelName = ConfigImplUtil.toCamelCase(originalName);
|
||||||
originalNames.put(camelName, configProp.getKey());
|
// 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;
|
BeanInfo beanInfo = null;
|
||||||
|
22
config/src/test/java/beanconfig/PreferCamelNamesConfig.java
Normal file
22
config/src/test/java/beanconfig/PreferCamelNamesConfig.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -62,5 +62,11 @@
|
|||||||
"shouldBeInt" : true,
|
"shouldBeInt" : true,
|
||||||
"should-be-boolean" : 42,
|
"should-be-boolean" : 42,
|
||||||
"should-be-list" : "hello"
|
"should-be-list" : "hello"
|
||||||
|
},
|
||||||
|
"preferCamelNames" : {
|
||||||
|
"foo-bar" : "no",
|
||||||
|
"fooBar" : "yes",
|
||||||
|
"baz-bar" : "no",
|
||||||
|
"bazBar" : "yes"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -118,6 +118,15 @@ class ConfigBeanFactoryTest extends TestUtils {
|
|||||||
assertEquals(ConfigMemorySize.ofBytes(1000), beanConfig.getThousandBytes)
|
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 = {
|
private def loadConfig(): Config = {
|
||||||
val configIs: InputStream = this.getClass().getClassLoader().getResourceAsStream("beanconfig/beanconfig01.conf")
|
val configIs: InputStream = this.getClass().getClassLoader().getResourceAsStream("beanconfig/beanconfig01.conf")
|
||||||
try {
|
try {
|
||||||
|
Loading…
Reference in New Issue
Block a user