Skip to content

Commit

Permalink
Do not index keys when populating a non Collection value Map (#1106)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Feb 6, 2024
1 parent 1e1f29e commit 98d006d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -575,10 +575,7 @@ public <K, V, C extends Collection<V>> ObjectCreator<T> values(
@Override
public Object apply(final String propertyName) {
usedProperties.add(propertyName);
for (String mapKey : config.getMapKeys(propertyName).values()) {
usedProperties.add(mapKey);
usedProperties.addAll(config.getIndexedProperties(mapKey));
}
usedProperties.addAll(config.getMapKeys(propertyName).values());
Converter<K> keyConverter = getConverter(keyRawType, keyConvertWith);
Converter<V> valueConverter = getConverter(valueRawType, valueConvertWith);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public <K, V, C extends Collection<V>> Map<K, C> getValues(
return getValue(name,
newMapConverter(keyConverter, newCollectionConverter(valueConverter, collectionFactory), mapFactory));
} catch (NoSuchElementException e) {
Map<String, String> mapCollectionKeys = getMapKeys(name);
Map<String, String> mapCollectionKeys = getMapIndexedKeys(name);
if (mapCollectionKeys.isEmpty()) {
throw new NoSuchElementException(ConfigMessages.msg.propertyNotFound(name));
}
Expand All @@ -267,8 +267,22 @@ public Map<String, String> getMapKeys(final String name) {
if (propertyName.length() > name.length() + 1
&& (name.isEmpty() || propertyName.charAt(name.length()) == '.')
&& propertyName.startsWith(name)) {
String key = unquoted(unindexed(propertyName), name.isEmpty() ? 0 : name.length() + 1);
mapKeys.put(key, unindexed(propertyName));
String key = unquoted(propertyName, name.isEmpty() ? 0 : name.length() + 1);
mapKeys.put(key, propertyName);
}
}
return mapKeys;
}

public Map<String, String> getMapIndexedKeys(final String name) {
Map<String, String> mapKeys = new HashMap<>();
for (String propertyName : getPropertyNames()) {
if (propertyName.length() > name.length() + 1
&& (name.isEmpty() || propertyName.charAt(name.length()) == '.')
&& propertyName.startsWith(name)) {
String unindexedName = unindexed(propertyName);
String key = unquoted(unindexedName, name.isEmpty() ? 0 : name.length() + 1);
mapKeys.put(key, unindexedName);
}
}
return mapKeys;
Expand Down Expand Up @@ -495,7 +509,7 @@ public <K, V, C extends Collection<V>> Optional<Map<K, C>> getOptionalValues(
return optionalValue;
}

Map<String, String> mapKeys = getMapKeys(name);
Map<String, String> mapKeys = getMapIndexedKeys(name);
if (mapKeys.isEmpty()) {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,25 +370,28 @@ void getValuesMap() {
.withSources(config(
"my.prop.key", "value",
"my.prop.key.nested", "value",
"my.prop.\"key.quoted\"", "value"))
"my.prop.\"key.quoted\"", "value",
"my.prop.key.indexed[0]", "value"))
.build();

Map<String, String> map = config.getValues("my.prop", String.class, String.class);
assertEquals(3, map.size());
assertEquals(4, map.size());
assertEquals("value", map.get("key"));
assertEquals("value", map.get("key.nested"));
assertEquals("value", map.get("key.quoted"));
assertEquals("value", map.get("key.indexed[0]"));

Converter<String> stringConverter = config.requireConverter(String.class);
Map<String, String> treeMap = config.getValues("my.prop", stringConverter, stringConverter, t -> new TreeMap<>());
assertTrue(treeMap instanceof TreeMap);

Optional<Map<String, String>> optionalMap = config.getOptionalValues("my.prop", String.class, String.class);
assertTrue(optionalMap.isPresent());
assertEquals(3, optionalMap.get().size());
assertEquals(4, optionalMap.get().size());
assertEquals("value", optionalMap.get().get("key"));
assertEquals("value", optionalMap.get().get("key.nested"));
assertEquals("value", optionalMap.get().get("key.quoted"));
assertEquals("value", optionalMap.get().get("key.indexed[0]"));

Optional<Map<String, String>> optionalTreeMap = config.getOptionalValues("my.prop", stringConverter, stringConverter,
t -> new TreeMap<>());
Expand Down

0 comments on commit 98d006d

Please sign in to comment.