This commit is contained in:
Phillip Webb 2015-06-15 11:31:33 -07:00
parent 818d3bd230
commit 20d39f7af2

View File

@ -193,19 +193,27 @@ public class VcapApplicationListener implements
} }
} }
@SuppressWarnings("unchecked")
private void flatten(Properties properties, Map<String, Object> input, String path) { private void flatten(Properties properties, Map<String, Object> input, String path) {
for (Entry<String, Object> entry : input.entrySet()) { for (Entry<String, Object> entry : input.entrySet()) {
String key = entry.getKey(); String key = getFullKey(path, entry.getKey());
if (StringUtils.hasText(path)) { Object value = entry.getValue();
if (key.startsWith("[")) { if (value instanceof Map) {
key = path + key; // Need a compound key
} flatten(properties, (Map<String, Object>) value, key);
else { }
key = path + "." + key; else if (value instanceof Collection) {
// Need a compound key
Collection<Object> collection = (Collection<Object>) value;
properties.put(key,
StringUtils.collectionToCommaDelimitedString(collection));
int count = 0;
for (Object item : collection) {
String itemKey = "[" + (count++) + "]";
flatten(properties, Collections.singletonMap(itemKey, item), key);
} }
} }
Object value = entry.getValue(); else if (value instanceof String) {
if (value instanceof String) {
properties.put(key, value); properties.put(key, value);
} }
else if (value instanceof Number) { else if (value instanceof Number) {
@ -214,28 +222,20 @@ public class VcapApplicationListener implements
else if (value instanceof Boolean) { else if (value instanceof Boolean) {
properties.put(key, value.toString()); properties.put(key, value.toString());
} }
else if (value instanceof Map) {
// Need a compound key
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) value;
flatten(properties, map, key);
}
else if (value instanceof Collection) {
// Need a compound key
@SuppressWarnings("unchecked")
Collection<Object> collection = (Collection<Object>) value;
properties.put(key,
StringUtils.collectionToCommaDelimitedString(collection));
int count = 0;
for (Object object : collection) {
flatten(properties,
Collections.singletonMap("[" + (count++) + "]", object), key);
}
}
else { else {
properties.put(key, value == null ? "" : value); properties.put(key, value == null ? "" : value);
} }
} }
} }
private String getFullKey(String path, String key) {
if (!StringUtils.hasText(path)) {
return key;
}
if (key.startsWith("[")) {
return path + key;
}
return path + "." + key;
}
} }