Skip to content

Commit

Permalink
Add way to iterate config properties in ConfigSourceContext. (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored Sep 25, 2020
1 parent 7b23f21 commit 37b4e1b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
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();
}
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"));
}
}

0 comments on commit 37b4e1b

Please sign in to comment.