From 0e82a00eb76ded44bcdf78e75396af936dc62136 Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Wed, 23 Sep 2020 17:47:35 +0100 Subject: [PATCH] Add way to iterate config properties in ConfigSourceContext. --- .../smallrye/config/ConfigSourceContext.java | 4 +++ .../io/smallrye/config/SmallRyeConfig.java | 14 ++++++++- .../config/ConfigConfigSourceTest.java | 30 +++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/implementation/src/main/java/io/smallrye/config/ConfigSourceContext.java b/implementation/src/main/java/io/smallrye/config/ConfigSourceContext.java index 1de649778..aeabda4cb 100644 --- a/implementation/src/main/java/io/smallrye/config/ConfigSourceContext.java +++ b/implementation/src/main/java/io/smallrye/config/ConfigSourceContext.java @@ -1,5 +1,7 @@ package io.smallrye.config; +import java.util.Iterator; + import io.smallrye.common.annotation.Experimental; /** @@ -8,4 +10,6 @@ @Experimental("ConfigSource API Enhancements") public interface ConfigSourceContext { ConfigValue getValue(String name); + + Iterator iterateNames(); } diff --git a/implementation/src/main/java/io/smallrye/config/SmallRyeConfig.java b/implementation/src/main/java/io/smallrye/config/SmallRyeConfig.java index f69320975..b86fe5de7 100644 --- a/implementation/src/main/java/io/smallrye/config/SmallRyeConfig.java +++ b/implementation/src/main/java/io/smallrye/config/SmallRyeConfig.java @@ -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; @@ -329,7 +330,18 @@ private static class ConfigSources implements Serializable { .collect(toList()); final List 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 iterateNames() { + return initChain.iterateNames(); + } + }).stream()) .map(ConfigSourceInterceptorWithPriority::new) .collect(toList()); diff --git a/implementation/src/test/java/io/smallrye/config/ConfigConfigSourceTest.java b/implementation/src/test/java/io/smallrye/config/ConfigConfigSourceTest.java index 3c1be567f..42793ca3c 100644 --- a/implementation/src/test/java/io/smallrye/config/ConfigConfigSourceTest.java +++ b/implementation/src/test/java/io/smallrye/config/ConfigConfigSourceTest.java @@ -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; @@ -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 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")); + } }