now use ??? and allow infix notiation for more than two alternative values

This commit is contained in:
Michael Hoennig 2024-11-01 16:48:11 +01:00
parent bd9c79a39d
commit 96e740f0d5

View File

@ -6,6 +6,7 @@ import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -121,19 +122,24 @@ public class TemplateResolver {
skipChar('}'); skipChar('}');
} }
private Object propVal(final String name) { private Object propVal(final String nameExpression) {
if (name.endsWith(IF_NOT_FOUND_SYMBOL)) { if (nameExpression.endsWith(IF_NOT_FOUND_SYMBOL)) {
final String pureName = name.substring(0, name.length() - IF_NOT_FOUND_SYMBOL.length()); final String pureName = nameExpression.substring(0, nameExpression.length() - IF_NOT_FOUND_SYMBOL.length());
return properties.get(pureName); return properties.get(pureName);
} else if (name.contains(IF_NOT_FOUND_SYMBOL)) { } else if (nameExpression.contains(IF_NOT_FOUND_SYMBOL)) {
final var parts = StringUtils.split(name, IF_NOT_FOUND_SYMBOL); final var parts = StringUtils.split(nameExpression, IF_NOT_FOUND_SYMBOL);
final var head = properties.get(parts[0]); return Arrays.stream(parts).filter(Objects::nonNull).findFirst().orElseGet(() -> {
final var tail = properties.get(parts[1]); if ( parts[parts.length-1].isEmpty() ) {
return head != null ? head : tail; // FIXME: What if tail is null as well? // => whole expression ends with IF_NOT_FOUND_SYMBOL, thus last null element was optional
return null;
}
// => last alternative element in expression was null and not optional
throw new IllegalStateException("Missing required value in property-chain: " + nameExpression);
});
} else { } else {
final var val = properties.get(name); final var val = properties.get(nameExpression);
if (val == null) { if (val == null) {
throw new IllegalStateException("Missing required property: " + name); throw new IllegalStateException("Missing required property: " + nameExpression);
} }
return val; return val;
} }