Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3189 - handle defaults in Spring placeholders #3190

Merged
merged 2 commits into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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