Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search for indexed property names before flattened comma separated value name when loading Collections #1202

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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