Allow relaxed names to include prefixes
This commit is contained in:
@@ -97,13 +97,19 @@ public final class RelaxedNames implements Iterable<String> {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
UNDERSCORE {
|
HYPHEN_TO_UNDERSCORE {
|
||||||
@Override
|
@Override
|
||||||
public String apply(String value) {
|
public String apply(String value) {
|
||||||
return value.replace("-", "_");
|
return value.replace("-", "_");
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
UNCAMELCASE {
|
PERIOD_TO_UNDERSCORE {
|
||||||
|
@Override
|
||||||
|
public String apply(String value) {
|
||||||
|
return value.replace(".", "_");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
CAMELCASE_TO_UNDERSCORE {
|
||||||
@Override
|
@Override
|
||||||
public String apply(String value) {
|
public String apply(String value) {
|
||||||
value = value.replaceAll("([^A-Z-])([A-Z])", "$1_$2");
|
value = value.replaceAll("([^A-Z-])([A-Z])", "$1_$2");
|
||||||
@@ -119,14 +125,18 @@ public final class RelaxedNames implements Iterable<String> {
|
|||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
CAMELCASE {
|
UNDERSCORE_TO_CAMELCASE {
|
||||||
@Override
|
@Override
|
||||||
public String apply(String value) {
|
public String apply(String value) {
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
for (String field : UNDERSCORE.apply(value).split("_")) {
|
for (String field : value.split("[_\\-.]")) {
|
||||||
builder.append(builder.length() == 0 ? field : StringUtils
|
builder.append(builder.length() == 0 ? field : StringUtils
|
||||||
.capitalize(field));
|
.capitalize(field));
|
||||||
}
|
}
|
||||||
|
for (String suffix : new String[] { "_", "-", "." })
|
||||||
|
if (value.endsWith(suffix)) {
|
||||||
|
builder.append(suffix);
|
||||||
|
}
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -81,10 +81,9 @@ public class RelaxedPropertyResolver implements PropertyResolver {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> T getProperty(String key, Class<T> targetType, T defaultValue) {
|
public <T> T getProperty(String key, Class<T> targetType, T defaultValue) {
|
||||||
for (String relaxedKey : new RelaxedNames(key)) {
|
for (String relaxedKey : new RelaxedNames(this.prefix + key)) {
|
||||||
if (this.resolver.containsProperty(this.prefix + relaxedKey)) {
|
if (this.resolver.containsProperty(relaxedKey)) {
|
||||||
return this.resolver.getProperty(this.prefix + relaxedKey, targetType,
|
return this.resolver.getProperty(relaxedKey, targetType, defaultValue);
|
||||||
defaultValue);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
@@ -92,10 +91,9 @@ public class RelaxedPropertyResolver implements PropertyResolver {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> Class<T> getPropertyAsClass(String key, Class<T> targetType) {
|
public <T> Class<T> getPropertyAsClass(String key, Class<T> targetType) {
|
||||||
for (String relaxedKey : new RelaxedNames(key)) {
|
for (String relaxedKey : new RelaxedNames(this.prefix + key)) {
|
||||||
if (this.resolver.containsProperty(this.prefix + relaxedKey)) {
|
if (this.resolver.containsProperty(relaxedKey)) {
|
||||||
return this.resolver.getPropertyAsClass(this.prefix + relaxedKey,
|
return this.resolver.getPropertyAsClass(relaxedKey, targetType);
|
||||||
targetType);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@@ -103,8 +101,8 @@ public class RelaxedPropertyResolver implements PropertyResolver {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean containsProperty(String key) {
|
public boolean containsProperty(String key) {
|
||||||
for (String relaxedKey : new RelaxedNames(key)) {
|
for (String relaxedKey : new RelaxedNames(this.prefix + key)) {
|
||||||
if (this.resolver.containsProperty(this.prefix + relaxedKey)) {
|
if (this.resolver.containsProperty(relaxedKey)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -126,7 +124,7 @@ public class RelaxedPropertyResolver implements PropertyResolver {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a Map of all values from all underlying properties that start with the
|
* Return a Map of all values from all underlying properties that start with the
|
||||||
* specified key. NOTE: this method can only be used in the underlying resolver is a
|
* specified key. NOTE: this method can only be used if the underlying resolver is a
|
||||||
* {@link ConfigurableEnvironment}.
|
* {@link ConfigurableEnvironment}.
|
||||||
* @param keyPrefix the key prefix used to filter results
|
* @param keyPrefix the key prefix used to filter results
|
||||||
* @return a map of all sub properties starting with the specified key prefix.
|
* @return a map of all sub properties starting with the specified key prefix.
|
||||||
|
|||||||
@@ -76,4 +76,27 @@ public class RelaxedNamesTests {
|
|||||||
assertThat(iterator.hasNext(), equalTo(false));
|
assertThat(iterator.hasNext(), equalTo(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void fromPeriods() throws Exception {
|
||||||
|
Iterator<String> iterator = new RelaxedNames("spring.value").iterator();
|
||||||
|
assertThat(iterator.next(), equalTo("spring.value"));
|
||||||
|
assertThat(iterator.next(), equalTo("spring_value"));
|
||||||
|
assertThat(iterator.next(), equalTo("springValue"));
|
||||||
|
assertThat(iterator.next(), equalTo("springvalue"));
|
||||||
|
assertThat(iterator.next(), equalTo("SPRING.VALUE"));
|
||||||
|
assertThat(iterator.next(), equalTo("SPRING_VALUE"));
|
||||||
|
assertThat(iterator.next(), equalTo("SPRINGVALUE"));
|
||||||
|
assertThat(iterator.hasNext(), equalTo(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void fromPrefixEndingInPeriod() throws Exception {
|
||||||
|
Iterator<String> iterator = new RelaxedNames("spring.").iterator();
|
||||||
|
assertThat(iterator.next(), equalTo("spring."));
|
||||||
|
assertThat(iterator.next(), equalTo("spring_"));
|
||||||
|
assertThat(iterator.next(), equalTo("SPRING."));
|
||||||
|
assertThat(iterator.next(), equalTo("SPRING_"));
|
||||||
|
assertThat(iterator.hasNext(), equalTo(false));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -144,6 +144,14 @@ public class RelaxedPropertyResolverTests {
|
|||||||
assertThat(this.resolver.getProperty("d"), equalTo("test"));
|
assertThat(this.resolver.getProperty("d"), equalTo("test"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void prefixedRelaxed() throws Exception {
|
||||||
|
this.resolver = new RelaxedPropertyResolver(this.environment, "a.");
|
||||||
|
this.source.put("A_B", "test");
|
||||||
|
assertThat(this.resolver.containsProperty("b"), equalTo(true));
|
||||||
|
assertThat(this.resolver.getProperty("b"), equalTo("test"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void subProperties() throws Exception {
|
public void subProperties() throws Exception {
|
||||||
this.source.put("x.y.my-sub.a.b", "1");
|
this.source.put("x.y.my-sub.a.b", "1");
|
||||||
|
|||||||
Reference in New Issue
Block a user