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

Add way to iterate config properties in ConfigSourceContext. #402

Merged
merged 2 commits into from
Sep 25, 2020
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.smallrye.config;

import java.util.Iterator;

import io.smallrye.common.annotation.Experimental;

/**
Expand All @@ -8,4 +10,6 @@
@Experimental("ConfigSource API Enhancements")
public interface ConfigSourceContext {
ConfigValue getValue(String name);

Iterator<String> iterateNames();
radcortez marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
Expand Down Expand Up @@ -329,7 +330,18 @@ private static class ConfigSources implements Serializable {
.collect(toList());

final List<ConfigSourceInterceptorWithPriority> lateInterceptors = lateInitSources.stream()
.flatMap(configurableConfigSource -> configurableConfigSource.getConfigSources(initChain::proceed).stream())
.flatMap(configurableConfigSource -> configurableConfigSource.getConfigSources(
new ConfigSourceContext() {
@Override
public ConfigValue getValue(final String name) {
return initChain.proceed(name);
}

@Override
public Iterator<String> iterateNames() {
return initChain.iterateNames();
}
}).stream())
.map(ConfigSourceInterceptorWithPriority::new)
.collect(toList());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static java.util.stream.Collectors.toList;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.OptionalInt;
Expand Down Expand Up @@ -91,4 +92,33 @@ public OptionalInt getPriority() {
assertEquals("1234", config.getRawValue("any"));
assertEquals("test", config.getConfigValue("any").getConfigSourceName());
}

@Test
void iterate() {
final SmallRyeConfig config = new SmallRyeConfigBuilder()
.addDefaultSources()
.addDefaultInterceptors()
.withSources(KeyValuesConfigSource.config("smallrye.prop", "1", "smallrye.another", "2", "mp.prop", "1"))
.withSources(new ConfigurableConfigSource(new ConfigSourceFactory() {
@Override
public ConfigSource getConfigSource(final ConfigSourceContext context) {
Map<String, String> properties = new HashMap<>();
context.iterateNames().forEachRemaining(s -> {
if (s.startsWith("smallrye")) {
properties.put(s, "1234");
}
});
return KeyValuesConfigSource.config(properties);
}

@Override
public OptionalInt getPriority() {
return OptionalInt.of(1000);
}
})).build();

assertEquals("1234", config.getRawValue("smallrye.prop"));
assertEquals("1234", config.getRawValue("smallrye.another"));
assertEquals("1", config.getRawValue("mp.prop"));
}
}