Skip to content

Commit

Permalink
Support empty values in config maps (#4037)
Browse files Browse the repository at this point in the history
* Support empty values in config maps

* Rename method
  • Loading branch information
trask authored Sep 2, 2021
1 parent d185377 commit ec99db5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ private static ConfigParsingException newInvalidPropertyException(
}

static List<String> parseList(@SuppressWarnings("unused") String propertyName, String value) {
return Collections.unmodifiableList(filterBlanksAndNulls(value.split(",")));
return Collections.unmodifiableList(filterBlanks(value.split(",")));
}

static Map<String, String> parseMap(String propertyName, String value) {
return parseList(propertyName, value).stream()
.map(keyValuePair -> filterBlanksAndNulls(keyValuePair.split("=", 2)))
.map(keyValuePair -> trim(keyValuePair.split("=", 2)))
.map(
splitKeyValuePairs -> {
if (splitKeyValuePairs.size() != 2) {
if (splitKeyValuePairs.size() != 2 || splitKeyValuePairs.get(0).isEmpty()) {
throw new ConfigParsingException(
"Invalid map property: " + propertyName + "=" + value);
}
Expand All @@ -77,13 +77,17 @@ static Map<String, String> parseMap(String propertyName, String value) {
Map.Entry::getKey, Map.Entry::getValue, (first, next) -> next, LinkedHashMap::new));
}

private static List<String> filterBlanksAndNulls(String[] values) {
private static List<String> filterBlanks(String[] values) {
return Arrays.stream(values)
.map(String::trim)
.filter(s -> !s.isEmpty())
.collect(Collectors.toList());
}

private static List<String> trim(String[] values) {
return Arrays.stream(values).map(String::trim).collect(Collectors.toList());
}

static Duration parseDuration(String propertyName, String value) {
String unitString = getUnitString(value);
String numberString = value.substring(0, value.length() - unitString.length());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ void allValid() {
properties.put("double", "5.4");
properties.put("list", "cat,dog,bear");
properties.put("map", "cat=meow,dog=bark,bear=growl");
properties.put("mapWithEmptyValue", "cat=meow,dog=,bear=growl");
properties.put("duration", "1s");

ConfigProperties config = createConfig(properties);
Expand All @@ -41,6 +42,8 @@ void allValid() {
assertThat(config.getCommaSeparatedValues("list")).containsExactly("cat", "dog", "bear");
assertThat(config.getCommaSeparatedMap("map"))
.containsExactly(entry("cat", "meow"), entry("dog", "bark"), entry("bear", "growl"));
assertThat(config.getCommaSeparatedMap("mapWithEmptyValue"))
.containsExactly(entry("cat", "meow"), entry("dog", ""), entry("bear", "growl"));
assertThat(config.getDuration("duration")).isEqualTo(Duration.ofSeconds(1));
}

Expand Down Expand Up @@ -131,11 +134,6 @@ void uncleanMap() {

@Test
void invalidMap() {
assertThatThrownBy(
() ->
createConfig(Collections.singletonMap("map", "a=1,b=")).getCommaSeparatedMap("map"))
.isInstanceOf(ConfigurationException.class)
.hasMessage("Invalid map property: map=a=1,b=");
assertThatThrownBy(
() ->
createConfig(Collections.singletonMap("map", "a=1,b")).getCommaSeparatedMap("map"))
Expand Down

0 comments on commit ec99db5

Please sign in to comment.