Skip to content

Commit

Permalink
Search for indexed property names before flattened comma separated va…
Browse files Browse the repository at this point in the history
…lue name when loading Collections (smallrye#1202)
  • Loading branch information
radcortez authored Jul 30, 2024
1 parent 727fad4 commit 7c456fe
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 7 deletions.
4 changes: 2 additions & 2 deletions documentation/src/main/docs/config/indexed-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ The indexed property syntax uses the property name and square brackets with an i

A call to `Config#getValues("my.collection", String.class)`, will automatically create and convert a `List<String>`
that contains the values `dog`, `cat` and `turtle`. A call to `Config#getValues("my.indexed.collection", String.class)`
returns the exact same result. For compatibility reasons, if SmallRye Config finds the same property name in their
indexed and unindexed format, the unindexed value has priority.
returns the exact same result. If SmallRye Config finds the same property name in their indexed and unindexed format,
the indexed value has priority.

The indexed property is sorted by its index before being added to the target `Collection`. Any gaps in the indexes do
not resolve to the target `Collection`, which means that the `Collection` result will store all values without empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,11 @@ private static void generateDefaults(final ClassVisitor classVisitor, final Conf
.get(mapping.getInterfaceType())
.get("").entrySet()) {
if (entry.getValue().hasDefaultValue()) {
// Defaults for collections also come as a simple property with comma separated values, no need for the star name
if (entry.getKey().endsWith("[*]")) {
continue;
}

mv.visitVarInsn(ALOAD, 0);
mv.visitLdcInsn(entry.getKey());
mv.visitLdcInsn(entry.getValue().getDefaultValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.io.ObjectStreamException;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -183,9 +184,9 @@ 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 getValue(name, newCollectionConverter(converter, collectionFactory));
} catch (NoSuchElementException e) {
return getIndexedValues(name, converter, collectionFactory);
} catch (NoSuchElementException e) {
return getValue(name, newCollectionConverter(converter, collectionFactory));
}
}

Expand Down Expand Up @@ -355,8 +356,23 @@ public Map<String, String> getMapIndexedKeys(final String name) {
}

@Override
public <T> T getValue(String name, Class<T> aClass) {
return getValue(name, requireConverter(aClass));
@SuppressWarnings("unchecked")
public <T> T getValue(String name, Class<T> propertyType) {
if (propertyType.isArray()) {
ConfigValue configValue = getConfigValue(name);
if (configValue.getValue() != null) {
return getValue(name, requireConverter(propertyType));
}

List<?> values = getValues(name, propertyType.getComponentType());
Object array = Array.newInstance(propertyType.getComponentType(), values.size());
for (int i = 0, valuesSize = values.size(); i < valuesSize; i++) {
Array.set(array, i, values.get(i));
}
return (T) array;
}

return getValue(name, requireConverter(propertyType));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1000,4 +1000,35 @@ interface AnotherNested {
}
}
}

@Test
void overrideListDefaults() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withSources(config(
"list.defaults.values[0]", "baz",
"list.defaults.list-nested[0].value", "value"))
.withMapping(ListDefaults.class)
.build();

ListDefaults mapping = config.getConfigMapping(ListDefaults.class);
assertEquals(1, mapping.values().size());
assertEquals("baz", mapping.values().get(0));
assertNull(config.getRawValue("list.defaults.values[9]"));
}

@ConfigMapping(prefix = "list.defaults")
interface ListDefaults {
@WithDefault("foo,bar")
List<String> values();

List<Nested> listNested();

interface Nested {
@WithDefault("value")
String value();

@WithDefault("one,two")
List<String> list();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ void getIndexedValues() {

@Test
void getValuesNotIndexed() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withSources(config(
"server.environments", "dev,qa"))
.build();

List<String> environments = config.getValues("server.environments", String.class);
assertEquals(2, environments.size());
assertEquals("dev", environments.get(0));
assertEquals("qa", environments.get(1));
}

@Test
void getValuesIndexedPriority() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withSources(config(
"server.environments", "dev,qa",
Expand All @@ -136,9 +149,10 @@ void getValuesNotIndexed() {
.build();

List<String> environments = config.getValues("server.environments", String.class);
assertEquals(2, environments.size());
assertEquals(3, environments.size());
assertEquals("dev", environments.get(0));
assertEquals("qa", environments.get(1));
assertEquals("prod", environments.get(2));
}

@Test
Expand Down

0 comments on commit 7c456fe

Please sign in to comment.