fix loop in loading settings from map
This commit is contained in:
parent
1f80fa1740
commit
74a3a2d75e
8 changed files with 106 additions and 41 deletions
|
@ -184,8 +184,9 @@ subprojects {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
nexusStaging {
|
||||
packageGroup = "org.xbib"
|
||||
}
|
||||
}
|
||||
|
||||
nexusStaging {
|
||||
packageGroup = "org.xbib"
|
||||
//stagingProfileId = "org.xbib"
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package org.xbib.content.settings;
|
||||
|
||||
import org.xbib.content.XContent;
|
||||
import org.xbib.content.XContentBuilder;
|
||||
import org.xbib.content.XContentGenerator;
|
||||
import org.xbib.content.XContentParser;
|
||||
import org.xbib.content.io.BytesReference;
|
||||
|
@ -8,7 +9,7 @@ import org.xbib.content.io.BytesStreamOutput;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -33,29 +34,16 @@ public abstract class AbstractSettingsLoader implements SettingsLoader {
|
|||
}
|
||||
}
|
||||
|
||||
public String flatMapAsString(BytesReference bytesReference) throws IOException {
|
||||
try (XContentParser parser = content().createParser(bytesReference);
|
||||
BytesStreamOutput bytesStreamOutput = new BytesStreamOutput();
|
||||
XContentGenerator generator = content().createGenerator(bytesStreamOutput)) {
|
||||
generator.writeStartObject();
|
||||
for (Map.Entry<String, String> entry : load(parser).entrySet()) {
|
||||
generator.writeFieldName(entry.getKey());
|
||||
String value = entry.getValue();
|
||||
if (value == null) {
|
||||
generator.writeNull();
|
||||
} else {
|
||||
generator.writeString(value);
|
||||
}
|
||||
}
|
||||
generator.writeEndObject();
|
||||
generator.flush();
|
||||
return bytesStreamOutput.bytes().toUtf8();
|
||||
}
|
||||
@Override
|
||||
public Map<String, String> load(Map<String, Object> map) throws IOException {
|
||||
XContentBuilder builder = XContentBuilder.builder(content());
|
||||
builder.map(map);
|
||||
return load(builder.string());
|
||||
}
|
||||
|
||||
public Map<String, String> load(XContentParser xContentParser) throws IOException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Map<String, String> map = new HashMap<>();
|
||||
Map<String, String> map = new LinkedHashMap<>();
|
||||
List<String> path = new ArrayList<>();
|
||||
XContentParser.Token token = xContentParser.nextToken();
|
||||
if (token == null) {
|
||||
|
@ -65,7 +53,33 @@ public abstract class AbstractSettingsLoader implements SettingsLoader {
|
|||
return map;
|
||||
}
|
||||
|
||||
private void parseObject(Map<String, String> settings, StringBuilder sb, List<String> path,
|
||||
public String flatMapAsString(BytesReference bytesReference) throws IOException {
|
||||
try (XContentParser parser = content().createParser(bytesReference);
|
||||
BytesStreamOutput bytesStreamOutput = new BytesStreamOutput();
|
||||
XContentGenerator generator = content().createGenerator(bytesStreamOutput)) {
|
||||
return flatMapAsString(parser, bytesStreamOutput, generator);
|
||||
}
|
||||
}
|
||||
|
||||
private String flatMapAsString(XContentParser parser,
|
||||
BytesStreamOutput bytesStreamOutput,
|
||||
XContentGenerator generator) throws IOException {
|
||||
generator.writeStartObject();
|
||||
for (Map.Entry<String, String> entry : load(parser).entrySet()) {
|
||||
generator.writeFieldName(entry.getKey());
|
||||
String value = entry.getValue();
|
||||
if (value == null) {
|
||||
generator.writeNull();
|
||||
} else {
|
||||
generator.writeString(value);
|
||||
}
|
||||
}
|
||||
generator.writeEndObject();
|
||||
generator.flush();
|
||||
return bytesStreamOutput.bytes().toUtf8();
|
||||
}
|
||||
|
||||
private void parseObject(Map<String, String> map, StringBuilder sb, List<String> path,
|
||||
XContentParser parser, String objFieldName) throws IOException {
|
||||
if (objFieldName != null) {
|
||||
path.add(objFieldName);
|
||||
|
@ -75,13 +89,13 @@ public abstract class AbstractSettingsLoader implements SettingsLoader {
|
|||
XContentParser.Token token;
|
||||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
||||
if (token == XContentParser.Token.START_OBJECT) {
|
||||
parseObject(settings, sb, path, parser, currentFieldName);
|
||||
parseObject(map, sb, path, parser, currentFieldName);
|
||||
} else if (token == XContentParser.Token.START_ARRAY) {
|
||||
parseArray(settings, sb, path, parser, currentFieldName);
|
||||
parseArray(map, sb, path, parser, currentFieldName);
|
||||
} else if (token == XContentParser.Token.FIELD_NAME) {
|
||||
currentFieldName = parser.currentName();
|
||||
} else {
|
||||
parseValue(settings, sb, path, parser, currentFieldName);
|
||||
parseValue(map, sb, path, parser, currentFieldName);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -90,31 +104,31 @@ public abstract class AbstractSettingsLoader implements SettingsLoader {
|
|||
}
|
||||
}
|
||||
|
||||
private void parseArray(Map<String, String> settings, StringBuilder sb, List<String> path,
|
||||
private void parseArray(Map<String, String> map, StringBuilder sb, List<String> path,
|
||||
XContentParser parser, String name) throws IOException {
|
||||
XContentParser.Token token;
|
||||
int counter = 0;
|
||||
String fieldName = name;
|
||||
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
||||
if (token == XContentParser.Token.START_OBJECT) {
|
||||
parseObject(settings, sb, path, parser, fieldName + '.' + (counter++));
|
||||
parseObject(map, sb, path, parser, fieldName + '.' + (counter++));
|
||||
} else if (token == XContentParser.Token.START_ARRAY) {
|
||||
parseArray(settings, sb, path, parser, fieldName + '.' + (counter++));
|
||||
parseArray(map, sb, path, parser, fieldName + '.' + (counter++));
|
||||
} else if (token == XContentParser.Token.FIELD_NAME) {
|
||||
fieldName = parser.currentName();
|
||||
} else {
|
||||
parseValue(settings, sb, path, parser, fieldName + '.' + (counter++));
|
||||
parseValue(map, sb, path, parser, fieldName + '.' + (counter++));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void parseValue(Map<String, String> settings, StringBuilder sb, List<String> path,
|
||||
private void parseValue(Map<String, String> map, StringBuilder sb, List<String> path,
|
||||
XContentParser parser, String fieldName) throws IOException {
|
||||
sb.setLength(0);
|
||||
for (String s : path) {
|
||||
sb.append(s).append('.');
|
||||
}
|
||||
sb.append(fieldName);
|
||||
settings.put(sb.toString(), parser.text());
|
||||
map.put(sb.toString(), parser.text());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package org.xbib.content.settings;
|
||||
|
||||
import org.xbib.content.io.BytesReference;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
|
@ -34,6 +37,30 @@ public class PropertiesSettingsLoader implements SettingsLoader {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> load(BytesReference ref) throws IOException {
|
||||
Properties props = new Properties();
|
||||
try (Reader reader = new InputStreamReader(ref.streamInput())) {
|
||||
props.load(reader);
|
||||
Map<String, String> result = new HashMap<>();
|
||||
for (Map.Entry<Object, Object> entry : props.entrySet()) {
|
||||
result.put((String) entry.getKey(), (String) entry.getValue());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> load(Map<String, Object> source) throws IOException {
|
||||
Properties props = new Properties();
|
||||
props.putAll(source);
|
||||
Map<String, String> result = new HashMap<>();
|
||||
for (Map.Entry<Object, Object> entry : props.entrySet()) {
|
||||
result.put((String) entry.getKey(), (String) entry.getValue());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canLoad(String source) {
|
||||
return true;
|
||||
|
|
|
@ -3,9 +3,7 @@ package org.xbib.content.settings;
|
|||
import static org.xbib.content.util.unit.ByteSizeValue.parseBytesSizeValue;
|
||||
import static org.xbib.content.util.unit.TimeValue.parseTimeValue;
|
||||
|
||||
import org.xbib.content.XContentBuilder;
|
||||
import org.xbib.content.json.JsonSettingsLoader;
|
||||
import org.xbib.content.json.JsonXContent;
|
||||
import org.xbib.content.util.unit.ByteSizeValue;
|
||||
import org.xbib.content.util.unit.TimeValue;
|
||||
|
||||
|
@ -614,8 +612,9 @@ public class Settings {
|
|||
* @return builder
|
||||
*/
|
||||
public Builder loadFromMap(Map<String, Object> map) {
|
||||
try (XContentBuilder builder = JsonXContent.contentBuilder()) {
|
||||
put(new JsonSettingsLoader().load(builder.map(map).string()));
|
||||
SettingsLoader settingsLoader = new JsonSettingsLoader();
|
||||
try {
|
||||
put(settingsLoader.load(map));
|
||||
} catch (Exception e) {
|
||||
throw new SettingsException("Failed to load settings from [" + map + "]", e);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package org.xbib.content.settings;
|
||||
|
||||
import org.xbib.content.io.BytesReference;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
@ -24,5 +25,9 @@ public interface SettingsLoader {
|
|||
*/
|
||||
Map<String, String> load(String source) throws IOException;
|
||||
|
||||
Map<String, String> load(BytesReference bytesReference) throws IOException;
|
||||
|
||||
Map<String, String> load(Map<String, Object> source) throws IOException;
|
||||
|
||||
boolean canLoad(String source);
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ import java.nio.charset.StandardCharsets;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Logger;
|
||||
|
@ -156,4 +157,22 @@ public class SettingsTest extends Assert {
|
|||
String result = loader.flatMapAsString(ref);
|
||||
assertEquals("{\"a.b\":\"c\"}", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadFromMap() throws IOException {
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
Map<String, Object> code = new LinkedHashMap<>();
|
||||
code.put("a", "b");
|
||||
code.put("b", "c");
|
||||
Map<String, Object> name = new LinkedHashMap<>();
|
||||
name.put("a", "b");
|
||||
name.put("b", "c");
|
||||
List<String> list = Arrays.asList("a","b");
|
||||
map.put("code", code);
|
||||
map.put("name", name);
|
||||
map.put("list", list);
|
||||
JsonSettingsLoader loader = new JsonSettingsLoader();
|
||||
Map<String, String> result = loader.load(map);
|
||||
assertEquals("{code.a=b, code.b=c, name.a=b, name.b=c, list.0=a, list.1=b}", result.toString());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
group = org.xbib
|
||||
name = content
|
||||
version = 2.0.4
|
||||
version = 2.0.5
|
||||
|
||||
xbib-net.version = 2.0.3
|
||||
jackson.version = 2.9.10
|
||||
|
|
4
gradle/wrapper/gradle-wrapper.properties
vendored
4
gradle/wrapper/gradle-wrapper.properties
vendored
|
@ -1,5 +1,5 @@
|
|||
#Sun Sep 15 22:30:22 CEST 2019
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6-all.zip
|
||||
#Fri Nov 22 14:55:55 CET 2019
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
zipStorePath=wrapper/dists
|
||||
|
|
Loading…
Reference in a new issue