|
|
|
@ -17,9 +17,12 @@
|
|
|
|
|
package com.alibaba.cloud.nacos.parser;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Properties;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
|
@ -32,6 +35,8 @@ public abstract class AbstractNacosDataParser {
|
|
|
|
|
|
|
|
|
|
protected static final String VALUE = "value";
|
|
|
|
|
|
|
|
|
|
protected static final String EMPTY_STRING = "";
|
|
|
|
|
|
|
|
|
|
private String extension;
|
|
|
|
|
|
|
|
|
|
private AbstractNacosDataParser nextParser;
|
|
|
|
@ -66,7 +71,7 @@ public abstract class AbstractNacosDataParser {
|
|
|
|
|
* @return result of Properties
|
|
|
|
|
* @throws IOException thrown if there is a problem parsing config.
|
|
|
|
|
*/
|
|
|
|
|
public final Properties parseNacosData(String data, String extension)
|
|
|
|
|
public final Map<String, Object> parseNacosData(String data, String extension)
|
|
|
|
|
throws IOException {
|
|
|
|
|
if (extension == null || extension.length() < 1) {
|
|
|
|
|
throw new IllegalStateException("The file extension cannot be empty");
|
|
|
|
@ -86,7 +91,7 @@ public abstract class AbstractNacosDataParser {
|
|
|
|
|
* @return result of Properties
|
|
|
|
|
* @throws IOException thrown if there is a problem parsing config.
|
|
|
|
|
*/
|
|
|
|
|
protected abstract Properties doParse(String data) throws IOException;
|
|
|
|
|
protected abstract Map<String, Object> doParse(String data) throws IOException;
|
|
|
|
|
|
|
|
|
|
protected AbstractNacosDataParser setNextParser(AbstractNacosDataParser nextParser) {
|
|
|
|
|
this.nextParser = nextParser;
|
|
|
|
@ -108,23 +113,36 @@ public abstract class AbstractNacosDataParser {
|
|
|
|
|
|| this.extension.contains(extension);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate key-value pairs from the map.
|
|
|
|
|
*/
|
|
|
|
|
protected Properties generateProperties(Map<String, Object> map) {
|
|
|
|
|
if (null == map || map.isEmpty()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
Properties properties = new Properties();
|
|
|
|
|
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
|
|
|
|
protected void flattenedMap(Map<String, Object> result, Map<String, Object> dataMap,
|
|
|
|
|
String parentKey) {
|
|
|
|
|
Set<Map.Entry<String, Object>> entries = dataMap.entrySet();
|
|
|
|
|
for (Iterator<Map.Entry<String, Object>> iterator = entries.iterator(); iterator
|
|
|
|
|
.hasNext();) {
|
|
|
|
|
Map.Entry<String, Object> entry = iterator.next();
|
|
|
|
|
String key = entry.getKey();
|
|
|
|
|
if (StringUtils.isEmpty(key)) {
|
|
|
|
|
Object value = entry.getValue();
|
|
|
|
|
|
|
|
|
|
String fullKey = StringUtils.isEmpty(parentKey) ? key : key.startsWith("[")
|
|
|
|
|
? parentKey.concat(key) : parentKey.concat(DOT).concat(key);
|
|
|
|
|
|
|
|
|
|
if (value instanceof Map) {
|
|
|
|
|
Map<String, Object> map = (Map<String, Object>) value;
|
|
|
|
|
flattenedMap(result, map, fullKey);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else if (value instanceof Collection) {
|
|
|
|
|
int count = 0;
|
|
|
|
|
Collection<Object> collection = (Collection<Object>) value;
|
|
|
|
|
for (Object object : collection) {
|
|
|
|
|
flattenedMap(result,
|
|
|
|
|
Collections.singletonMap("[" + (count++) + "]", object),
|
|
|
|
|
fullKey);
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
key = key.startsWith(DOT) ? key.replaceFirst("\\.", "") : key;
|
|
|
|
|
properties.put(key, entry.getValue());
|
|
|
|
|
|
|
|
|
|
result.put(fullKey, value);
|
|
|
|
|
}
|
|
|
|
|
return properties;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -134,7 +152,7 @@ public abstract class AbstractNacosDataParser {
|
|
|
|
|
if (map == null || map.isEmpty()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
Map<String, Object> result = new HashMap<>(map);
|
|
|
|
|
Map<String, Object> result = new LinkedHashMap<>(map);
|
|
|
|
|
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
|
|
|
|
String key = entry.getKey();
|
|
|
|
|
if (key.contains(DOT)) {
|
|
|
|
|