Skip to content

Commit

Permalink
Remove toMap from KeyMap.
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Jul 31, 2020
1 parent 942a132 commit 40ec717
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 72 deletions.
26 changes: 0 additions & 26 deletions implementation/src/main/java/io/smallrye/config/KeyMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;
Expand Down Expand Up @@ -257,29 +256,4 @@ public StringBuilder toString(StringBuilder b) {
public String toString() {
return toString(new StringBuilder()).toString();
}

public Map<String, V> toMap() {
final Map<String, V> map = new HashMap<>();
unwrap(this, "", map);
return map;
}

private void unwrap(KeyMap<V> keyMap, String key, Map<String, V> map) {
for (String path : keyMap.keySet()) {
final KeyMap<V> nested = keyMap.get(path);
final String nestedKey = key.length() == 0 ? path : key + "." + path;
if (nested.any != null) {
map.put(nestedKey + ".*", nested.any.getRootValue());
unwrap(nested.any, nestedKey + ".*", map);
}
if (!nested.hasRootValue()) {
unwrap(nested, nestedKey, map);
} else {
map.put(nestedKey, nested.getRootValue());
if (!nested.keySet().isEmpty()) {
unwrap(nested, nestedKey, map);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package io.smallrye.config;

import java.util.Collections;
import java.util.Map;

import org.eclipse.microprofile.config.spi.ConfigSource;

import io.smallrye.config.common.AbstractConfigSource;

public abstract class KeyMapBackedConfigSource extends AbstractConfigSource {
public class KeyMapBackedConfigSource extends AbstractConfigSource {
private static final long serialVersionUID = 4378754290346888762L;

private final KeyMap<String> properties;
Expand All @@ -23,7 +24,7 @@ public KeyMapBackedConfigSource(final String name, final int ordinal, final KeyM

@Override
public Map<String, String> getProperties() {
return properties.toMap();
return Collections.emptyMap();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,19 +333,8 @@ public SmallRyeConfig build() {
if (!defaultValues.isEmpty() || !mappingProvider.getDefaultValues().isEmpty()) {
final KeyMap<String> mappingProviderDefaultValues = mappingProvider.getDefaultValues();
defaultValues.forEach((key, value) -> mappingProviderDefaultValues.findOrAdd(key).putRootValue(value));
withSources(new KeyMapBackedConfigSource("DefaultValuesConfigSource", mappingProviderDefaultValues) {
private static final long serialVersionUID = 7847969724478280439L;

@Override
public Map<String, String> getProperties() {
return new HashMap<>();
}

@Override
public int getOrdinal() {
return Integer.MIN_VALUE;
}
});
withSources(
new KeyMapBackedConfigSource("DefaultValuesConfigSource", Integer.MIN_VALUE, mappingProviderDefaultValues));
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.junit.jupiter.api.Test;

class KeyMapBackedConfigSourceTest {

@Test
void getProperties() {
KeyMap<String> map = new KeyMap<>();
Expand All @@ -17,12 +16,9 @@ void getProperties() {
map.findOrAdd("root.foo.bar.*").putRootValue("baz");
map.findOrAdd("root.foo.bar.*.baz").putRootValue("anything");

final ConfigSource source = getSource(map);
final Map<String, String> properties = source.getProperties();
assertTrue(properties.containsKey("root.foo"));
assertTrue(properties.containsKey("root.foo.bar"));
assertTrue(properties.containsKey("root.foo.bar.*"));
assertTrue(properties.containsKey("root.foo.bar.*.baz"));
ConfigSource source = getSource(map);
Map<String, String> properties = source.getProperties();
assertTrue(properties.isEmpty());
}

@Test
Expand All @@ -33,7 +29,7 @@ void getValue() {
map.findOrAdd("root.foo.bar.*").putRootValue("baz");
map.findOrAdd("root.foo.bar.*.baz").putRootValue("anything");

final ConfigSource source = getSource(map);
ConfigSource source = getSource(map);
assertEquals("bar", source.getValue("root.foo"));
assertEquals("baz", source.getValue("root.foo.bar"));
assertEquals("baz", source.getValue("root.foo.bar.x"));
Expand All @@ -45,7 +41,6 @@ void getValue() {
}

private ConfigSource getSource(final KeyMap<String> properties) {
return new KeyMapBackedConfigSource("test", 0, properties) {
};
return new KeyMapBackedConfigSource("test", 0, properties);
}
}
21 changes: 0 additions & 21 deletions implementation/src/test/java/io/smallrye/config/KeyMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -106,26 +105,6 @@ void empty() {
assertEquals("bar", map.findRootValue(".foo"));
}

@Test
void unwrap() {
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");

Map<String, String> properties = map.toMap();
assertTrue(properties.containsKey("root.foo"));
assertTrue(properties.containsKey("root.foo.bar"));
assertTrue(properties.containsKey("root.foo.bar.*"));
assertTrue(properties.containsKey("root.foo.bar.*.baz"));

assertEquals("bar", properties.get("root.foo"));
assertEquals("baz", properties.get("root.foo.bar"));
assertEquals("baz", properties.get("root.foo.bar.*"));
assertEquals("anything", properties.get("root.foo.bar.*.baz"));
}

@Test
void string() {
KeyMap<String> map = new KeyMap<>();
Expand Down

0 comments on commit 40ec717

Please sign in to comment.