Skip to content

Commit

Permalink
Remote 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 d2e094f
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 57 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,5 +1,6 @@
package io.smallrye.config;

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

import org.eclipse.microprofile.config.spi.ConfigSource;
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 @@ -338,7 +338,7 @@ public SmallRyeConfig build() {

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

@Override
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 Down
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 d2e094f

Please sign in to comment.