Skip to content

Commit

Permalink
Merge pull request #3190 from beliakou/bugfix/#3189-handle-spring-def…
Browse files Browse the repository at this point in the history
…ault-values

#3189 - handle defaults in Spring placeholders
  • Loading branch information
dilipkrish committed Jan 6, 2020
2 parents 3848fa1 + 83545c5 commit 1c11ebc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import static org.springframework.util.StringUtils.*;

public class DescriptionResolver {
private static final Pattern PATTERN = Pattern.compile("\\Q${\\E(.+?)\\Q}\\E");
private static final Pattern PATTERN = Pattern.compile("\\Q${\\E(.+?)(:(.*))?\\Q}\\E");
private final Environment environment;
private Map<String, String> cache;

Expand Down Expand Up @@ -69,6 +69,7 @@ public String resolve(String expression) {

// Store the value
String key = matcher.group(1);
String defaultValue = matcher.group(3);

// Get the value
String value = environment.getProperty(key);
Expand All @@ -82,6 +83,11 @@ public String resolve(String expression) {
// return the value
return value;

} else if (defaultValue != null) {
// use default if value not found
cache.put(expression, defaultValue);

return defaultValue;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class DescriptionResolverSpec extends Specification {
value | key
"key1value" | '${key1}'
"key2value" | '${key2}'
"key2value" | '${key2:}'
"key2value" | '${key2:key2default}'
"key3default" | '${key3:key3default}'
"" | '${key3:}'
'${unknown}' | '${unknown}'
"key1" | 'key1'
"key2" | 'key2'
Expand Down

0 comments on commit 1c11ebc

Please sign in to comment.