Skip to content

Commit

Permalink
Avoid expensive exception and log when getValues fails lookup for ind…
Browse files Browse the repository at this point in the history
…exed properties and fallbacks to comma
  • Loading branch information
radcortez committed Aug 29, 2024
1 parent 036e7e0 commit 97cad4d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ public <T, C extends Collection<T>> C getValues(String name, Class<T> itemClass,
}

public <T, C extends Collection<T>> C getValues(String name, Converter<T> converter, IntFunction<C> collectionFactory) {
try {
return getIndexedValues(name, converter, collectionFactory);
} catch (NoSuchElementException e) {
return getValue(name, newCollectionConverter(converter, collectionFactory));
List<String> indexedProperties = getIndexedProperties(name);
if (!indexedProperties.isEmpty()) {
return getIndexedValues(indexedProperties, converter, collectionFactory);
}
return getValue(name, newCollectionConverter(converter, collectionFactory));
}

public <T, C extends Collection<T>> C getIndexedValues(String name, Converter<T> converter,
Expand All @@ -197,12 +197,15 @@ public <T, C extends Collection<T>> C getIndexedValues(String name, Converter<T>
if (indexedProperties.isEmpty()) {
throw new NoSuchElementException(ConfigMessages.msg.propertyNotFound(name));
}
return getIndexedValues(indexedProperties, converter, collectionFactory);
}

final C collection = collectionFactory.apply(indexedProperties.size());
private <T, C extends Collection<T>> C getIndexedValues(List<String> indexedProperties, Converter<T> converter,
IntFunction<C> collectionFactory) {
C collection = collectionFactory.apply(indexedProperties.size());
for (String indexedProperty : indexedProperties) {
collection.add(getValue(indexedProperty, converter));
}

return collection;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,18 @@ void getIndexedValues() {
"server.environments[2]", "prod"))
.build();

List<String> environments = config.getValues("server.environments", String.class);
List<String> environments = config.getIndexedValues("server.environments", config.requireConverter(String.class),
ArrayList::new);
assertEquals(3, environments.size());
assertEquals("dev", environments.get(0));
assertEquals("dev", config.getValue("server.environments[0]", String.class));
assertEquals("qa", environments.get(1));
assertEquals("qa", config.getValue("server.environments[1]", String.class));
assertEquals("prod", environments.get(2));
assertEquals("prod", config.getValue("server.environments[2]", String.class));

assertThrows(NoSuchElementException.class,
() -> config.getIndexedValues("not.found", config.requireConverter(String.class), ArrayList::new));
}

@Test
Expand Down

0 comments on commit 97cad4d

Please sign in to comment.