commit
d356a24a07
|
|
@ -510,6 +510,18 @@ public final class ConfigurationPropertyName implements Comparable<Configuration
|
||||||
return of(name, false);
|
return of(name, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a {@link ConfigurationPropertyName} for the specified string or {@code null}
|
||||||
|
* if the name is not valid.
|
||||||
|
* @param name the source name
|
||||||
|
* @return a {@link ConfigurationPropertyName} instance
|
||||||
|
* @throws InvalidConfigurationPropertyNameException if the name is not valid
|
||||||
|
* @since 2.3.1
|
||||||
|
*/
|
||||||
|
public static ConfigurationPropertyName ofIfValid(CharSequence name) {
|
||||||
|
return of(name, true);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a {@link ConfigurationPropertyName} for the specified string.
|
* Return a {@link ConfigurationPropertyName} for the specified string.
|
||||||
* @param name the source name
|
* @param name the source name
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ final class SystemEnvironmentPropertyMapper implements PropertyMapper {
|
||||||
StringBuilder result = new StringBuilder();
|
StringBuilder result = new StringBuilder();
|
||||||
for (int i = 0; i < numberOfElements; i++) {
|
for (int i = 0; i < numberOfElements; i++) {
|
||||||
if (result.length() > 0) {
|
if (result.length() > 0) {
|
||||||
result.append("_");
|
result.append('_');
|
||||||
}
|
}
|
||||||
result.append(name.getElement(i, Form.UNIFORM).toUpperCase(Locale.ENGLISH));
|
result.append(name.getElement(i, Form.UNIFORM).toUpperCase(Locale.ENGLISH));
|
||||||
}
|
}
|
||||||
|
|
@ -69,7 +69,7 @@ final class SystemEnvironmentPropertyMapper implements PropertyMapper {
|
||||||
StringBuilder result = new StringBuilder();
|
StringBuilder result = new StringBuilder();
|
||||||
for (int i = 0; i < name.getNumberOfElements(); i++) {
|
for (int i = 0; i < name.getNumberOfElements(); i++) {
|
||||||
if (result.length() > 0) {
|
if (result.length() > 0) {
|
||||||
result.append("_");
|
result.append('_');
|
||||||
}
|
}
|
||||||
result.append(convertLegacyNameElement(name.getElement(i, Form.ORIGINAL)));
|
result.append(convertLegacyNameElement(name.getElement(i, Form.ORIGINAL)));
|
||||||
}
|
}
|
||||||
|
|
@ -116,13 +116,19 @@ final class SystemEnvironmentPropertyMapper implements PropertyMapper {
|
||||||
if (!hasDashedEntries(name)) {
|
if (!hasDashedEntries(name)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
ConfigurationPropertyName legacyCompatibleName = buildLegacyCompatibleName(name);
|
||||||
|
return legacyCompatibleName != null && legacyCompatibleName.isAncestorOf(candidate);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ConfigurationPropertyName buildLegacyCompatibleName(ConfigurationPropertyName name) {
|
||||||
StringBuilder legacyCompatibleName = new StringBuilder();
|
StringBuilder legacyCompatibleName = new StringBuilder();
|
||||||
for (int i = 0; i < name.getNumberOfElements(); i++) {
|
for (int i = 0; i < name.getNumberOfElements(); i++) {
|
||||||
legacyCompatibleName.append((i != 0) ? "." : "");
|
if (i != 0) {
|
||||||
|
legacyCompatibleName.append('.');
|
||||||
|
}
|
||||||
legacyCompatibleName.append(name.getElement(i, Form.DASHED).replace('-', '.'));
|
legacyCompatibleName.append(name.getElement(i, Form.DASHED).replace('-', '.'));
|
||||||
}
|
}
|
||||||
return ConfigurationPropertyName.isValid(legacyCompatibleName)
|
return ConfigurationPropertyName.ofIfValid(legacyCompatibleName);
|
||||||
&& ConfigurationPropertyName.of(legacyCompatibleName).isAncestorOf(candidate);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean hasDashedEntries(ConfigurationPropertyName name) {
|
boolean hasDashedEntries(ConfigurationPropertyName name) {
|
||||||
|
|
|
||||||
|
|
@ -210,6 +210,18 @@ class ConfigurationPropertyNameTests {
|
||||||
assertThat(name.append("foo").toString()).isEqualTo("foo");
|
assertThat(name.append("foo").toString()).isEqualTo("foo");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void ofIfValidWhenNameIsValidReturnsName() {
|
||||||
|
ConfigurationPropertyName name = ConfigurationPropertyName.ofIfValid("spring.bo-ot");
|
||||||
|
assertThat(name).hasToString("spring.bo-ot");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void ofIfValidWhenNameIsNotValidReturnsNull() {
|
||||||
|
ConfigurationPropertyName name = ConfigurationPropertyName.ofIfValid("spring.bo!oot");
|
||||||
|
assertThat(name).isNull();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void adaptWhenNameIsNullShouldThrowException() {
|
void adaptWhenNameIsNullShouldThrowException() {
|
||||||
assertThatIllegalArgumentException().isThrownBy(() -> ConfigurationPropertyName.adapt(null, '.'))
|
assertThatIllegalArgumentException().isThrownBy(() -> ConfigurationPropertyName.adapt(null, '.'))
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue