@ -21,12 +21,9 @@ import java.util.Collections;
import java.util.LinkedHashSet ;
import java.util.LinkedHashSet ;
import java.util.List ;
import java.util.List ;
import java.util.Set ;
import java.util.Set ;
import java.util.function.Function ;
import java.util.stream.IntStream ;
import java.util.stream.IntStream ;
import org.springframework.boot.context.properties.source.ConfigurationPropertyName.Form ;
import org.springframework.boot.context.properties.source.ConfigurationPropertyName.Form ;
import org.springframework.core.env.PropertySource ;
import org.springframework.util.StringUtils ;
/ * *
/ * *
* { @link PropertyMapper } for system environment variables . Names are mapped by removing
* { @link PropertyMapper } for system environment variables . Names are mapped by removing
@ -52,7 +49,7 @@ final class SystemEnvironmentPropertyMapper implements PropertyMapper {
}
}
@Override
@Override
public List < PropertyMapping > map ( PropertySource < ? > propertySource ,
public List < PropertyMapping > map (
ConfigurationPropertyName configurationPropertyName ) {
ConfigurationPropertyName configurationPropertyName ) {
Set < String > names = new LinkedHashSet < > ( ) ;
Set < String > names = new LinkedHashSet < > ( ) ;
names . add ( convertName ( configurationPropertyName ) ) ;
names . add ( convertName ( configurationPropertyName ) ) ;
@ -60,39 +57,15 @@ final class SystemEnvironmentPropertyMapper implements PropertyMapper {
List < PropertyMapping > result = new ArrayList < > ( ) ;
List < PropertyMapping > result = new ArrayList < > ( ) ;
names . forEach ( ( name ) - > result
names . forEach ( ( name ) - > result
. add ( new PropertyMapping ( name , configurationPropertyName ) ) ) ;
. add ( new PropertyMapping ( name , configurationPropertyName ) ) ) ;
if ( isListShortcutPossible ( configurationPropertyName ) ) {
result . addAll ( mapListShortcut ( propertySource , configurationPropertyName ) ) ;
}
return result ;
return result ;
}
}
private boolean isListShortcutPossible ( ConfigurationPropertyName name ) {
return ( name . isLastElementIndexed ( ) & & isNumber ( name . getLastElement ( Form . UNIFORM ) )
& & name . getNumberOfElements ( ) > = 1 ) ;
}
private List < PropertyMapping > mapListShortcut ( PropertySource < ? > propertySource ,
ConfigurationPropertyName name ) {
String result = convertName ( name , name . getNumberOfElements ( ) - 1 ) + "__" ;
if ( propertySource . containsProperty ( result ) ) {
int index = Integer . parseInt ( name . getLastElement ( Form . UNIFORM ) ) ;
return Collections . singletonList (
new PropertyMapping ( result , name , new ElementExtractor ( index ) ) ) ;
}
return Collections . emptyList ( ) ;
}
@Override
@Override
public List < PropertyMapping > map ( PropertySource < ? > propertySource ,
public List < PropertyMapping > map ( String propertySourceName ) {
String propertySourceName ) {
ConfigurationPropertyName name = convertName ( propertySourceName ) ;
ConfigurationPropertyName name = convertName ( propertySourceName ) ;
if ( name = = null | | name . isEmpty ( ) ) {
if ( name = = null | | name . isEmpty ( ) ) {
return Collections . emptyList ( ) ;
return Collections . emptyList ( ) ;
}
}
if ( propertySourceName . endsWith ( "__" ) ) {
return expandListShortcut ( propertySourceName , name ,
propertySource . getProperty ( propertySourceName ) ) ;
}
return Collections . singletonList ( new PropertyMapping ( propertySourceName , name ) ) ;
return Collections . singletonList ( new PropertyMapping ( propertySourceName , name ) ) ;
}
}
@ -106,22 +79,6 @@ final class SystemEnvironmentPropertyMapper implements PropertyMapper {
}
}
}
}
private List < PropertyMapping > expandListShortcut ( String propertySourceName ,
ConfigurationPropertyName rootName , Object value ) {
if ( value = = null ) {
return Collections . emptyList ( ) ;
}
List < PropertyMapping > mappings = new ArrayList < > ( ) ;
String [ ] elements = StringUtils
. commaDelimitedListToStringArray ( String . valueOf ( value ) ) ;
for ( int i = 0 ; i < elements . length ; i + + ) {
ConfigurationPropertyName name = rootName . append ( "[" + i + "]" ) ;
mappings . add ( new PropertyMapping ( propertySourceName , name ,
new ElementExtractor ( i ) ) ) ;
}
return mappings ;
}
private String convertName ( ConfigurationPropertyName name ) {
private String convertName ( ConfigurationPropertyName name ) {
return convertName ( name , name . getNumberOfElements ( ) ) ;
return convertName ( name , name . getNumberOfElements ( ) ) ;
}
}
@ -159,26 +116,4 @@ final class SystemEnvironmentPropertyMapper implements PropertyMapper {
return ! hasNonDigit ;
return ! hasNonDigit ;
}
}
/ * *
* Function used to extract an element from a comma list .
* /
private static class ElementExtractor implements Function < Object , Object > {
private final int index ;
ElementExtractor ( int index ) {
this . index = index ;
}
@Override
public Object apply ( Object value ) {
if ( value = = null ) {
return null ;
}
return StringUtils
. commaDelimitedListToStringArray ( value . toString ( ) ) [ this . index ] ;
}
}
}
}