Skip to content

Commit

Permalink
Support property names in KeyMapBackedConfigSource (#919)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored Mar 31, 2023
1 parent 9b2d355 commit 444689c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
17 changes: 17 additions & 0 deletions implementation/src/main/java/io/smallrye/config/KeyMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Map;
Expand Down Expand Up @@ -310,6 +311,22 @@ public void putAll(final Map<? extends String, ? extends KeyMap<V>> m) {
}
}

@Override
public Set<String> keySet() {
Set<String> keys = super.keySet();
Set<String> allKeys = new HashSet<>();
for (String key : keys) {
Set<String> childKeys = find(key).keySet();
for (String childKey : childKeys) {
allKeys.add(key + "." + childKey);
}
if (hasRootValue(key)) {
allKeys.add(key);
}
}
return allKeys;
}

@SuppressWarnings("ResultOfMethodCallIgnored")
public StringBuilder toString(StringBuilder b) {
b.append("KeyMap(");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ KeyMap<String> getKeyMapProperties() {

@Override
public Set<String> getPropertyNames() {
return Collections.emptySet();
return properties.keySet();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ public OptionalInt getPriority() {
}
}));

withDefaultValue("smallrye.config.secret-handlers", "all");
interceptors.add(new InterceptorWithPriority(new ConfigSourceInterceptorFactory() {
@Override
public ConfigSourceInterceptor getInterceptor(final ConfigSourceInterceptorContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static io.smallrye.config.ConfigMappingInterfaceTest.MapKeyEnum.ClientId.NAF;
import static io.smallrye.config.ConfigMappingInterfaceTest.MapKeyEnum.ClientId.SOS_DAH;
import static io.smallrye.config.KeyValuesConfigSource.config;
import static io.smallrye.config.SmallRyeConfig.SMALLRYE_CONFIG_MAPPING_VALIDATE_UNKNOWN;
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toList;
import static java.util.stream.StreamSupport.stream;
Expand Down Expand Up @@ -356,8 +357,9 @@ void defaults() {
assertEquals("bar", defaults.bar());
assertEquals("foo", config.getRawValue("foo"));

final List<String> propertyNames = stream(config.getPropertyNames().spliterator(), false).collect(toList());
assertFalse(propertyNames.contains("foo"));
List<String> propertyNames = stream(config.getPropertyNames().spliterator(), false).collect(toList());
assertTrue(propertyNames.contains("foo"));
assertTrue(propertyNames.contains("bar"));
}

@Test
Expand Down Expand Up @@ -1619,6 +1621,7 @@ void defaultsBuilderAndMapping() {
.addDefaultInterceptors()
.withMapping(DefaultsBuilderAndMapping.class)
.withDefaultValue("server.host", "localhost")
.withDefaultValue(SMALLRYE_CONFIG_MAPPING_VALIDATE_UNKNOWN, "false")
.build();

DefaultsBuilderAndMapping mapping = config.getConfigMapping(DefaultsBuilderAndMapping.class);
Expand Down Expand Up @@ -1901,4 +1904,26 @@ interface MapKeys {

Map<String, List<String>> list();
}

@Test
void defaultsPropertyNames() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.addDefaultInterceptors()
.withMapping(DefaultsPropertyNames.class)
.build();

DefaultsPropertyNames mapping = config.getConfigMapping(DefaultsPropertyNames.class);
assertEquals("value", mapping.value());
assertEquals("value", config.getRawValue("defaults.myProperty"));

Set<String> properties = stream(config.getPropertyNames().spliterator(), false).collect(Collectors.toSet());
assertTrue(properties.contains("defaults.myProperty"));
}

@ConfigMapping(prefix = "defaults")
interface DefaultsPropertyNames {
@WithDefault("value")
@WithName("myProperty")
String value();
}
}
16 changes: 16 additions & 0 deletions implementation/src/test/java/io/smallrye/config/KeyMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import static java.util.stream.Collectors.toList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -280,4 +282,18 @@ void star() {
assertEquals("value", map.findRootValue("map.key.one.two"));
assertEquals("value", map.findRootValue("map.key.one.two.three"));
}

@Test
void keySet() {
KeyMap<String> map = new KeyMap<>();
map.findOrAdd("root.foo").putRootValue("bar");
map.findOrAdd("root.foo.bar").putRootValue("baz");
map.findOrAdd("root.foo.bar.*").putRootValue("baz");
map.findOrAdd("root.foo.bar.*.baz").putRootValue("anything");

Set<String> keys = map.keySet();
assertEquals(2, keys.size());
assertTrue(keys.contains("root.foo"));
assertTrue(keys.contains("root.foo.bar"));
}
}

0 comments on commit 444689c

Please sign in to comment.