Skip to content
Permalink
Browse files

feat: Maps now provides a method to convert a Map from properties

style to nested.
  • Loading branch information...
iocanel committed Sep 10, 2019
1 parent de41d56 commit fbeb5a3eca074f7efa203b4aff83e8be002b6717
Showing with 23 additions and 10 deletions.
  1. +23 −10 core/src/main/java/io/dekorate/utils/Maps.java
@@ -21,32 +21,28 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class Maps {
private static final String PROPERTY_PREFIX = "dekorate";
private static final String MULTIPART_SEPARATOR_PATTERN = Pattern.quote(".");
private static final String ARRAY_PATTERN = "[a-zA-Z0-9_]+\\[\\d+\\]";

/**
* Read a properties input stream and crate a configuration map.
* Read a Map representing a properties file and create a config map.
* The method performs the following conversions:
* 1. Unrolls nested entries (keys separated with dots) into nested maps.
* 2. Unrolls arrays.
* The configuration map follows all the required conventions in order to be usable by a Generator.
*
* @return a {@link Map} with in the Generator format.
*/
public static Map<String, Object> fromProperties(InputStream is) {
public static Map<String, Object> fromProperties(Map<String, Object> properties) {
Map<String, Object> result = new HashMap<>();
Properties properties = new Properties();
try {
properties.load(is);
} catch (IOException e) {
throw DekorateException.launderThrowable(e);
}

for (Object key : properties.keySet()) {
String k = String.valueOf(key);
@@ -68,6 +64,23 @@
// Second pass unroll arrays
unrollArrays(result);
return result;

}

/**
* Read a properties input stream and crate a configuration map.
* The configuration map follows all the required conventions in order to be usable by a Generator.
*
* @return a {@link Map} with in the Generator format.
*/
public static Map<String, Object> fromProperties(InputStream is) {
Properties properties = new Properties();
try {
properties.load(is);
} catch (IOException e) {
throw DekorateException.launderThrowable(e);
}
return fromProperties(properties.entrySet().stream().collect(Collectors.toMap(e -> String.valueOf(e.getKey()), e -> e.getValue())));
}

/**

0 comments on commit fbeb5a3

Please sign in to comment.
You can’t perform that action at this time.