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;
|
package org.xbib.content.settings;
|
||||||
|
|
||||||
import org.xbib.content.XContent;
|
import org.xbib.content.XContent;
|
||||||
|
import org.xbib.content.XContentBuilder;
|
||||||
import org.xbib.content.XContentGenerator;
|
import org.xbib.content.XContentGenerator;
|
||||||
import org.xbib.content.XContentParser;
|
import org.xbib.content.XContentParser;
|
||||||
import org.xbib.content.io.BytesReference;
|
import org.xbib.content.io.BytesReference;
|
||||||
|
@ -8,7 +9,7 @@ import org.xbib.content.io.BytesStreamOutput;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ -33,10 +34,36 @@ public abstract class AbstractSettingsLoader implements SettingsLoader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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 LinkedHashMap<>();
|
||||||
|
List<String> path = new ArrayList<>();
|
||||||
|
XContentParser.Token token = xContentParser.nextToken();
|
||||||
|
if (token == null) {
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
parseObject(map, sb, path, xContentParser, null);
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
public String flatMapAsString(BytesReference bytesReference) throws IOException {
|
public String flatMapAsString(BytesReference bytesReference) throws IOException {
|
||||||
try (XContentParser parser = content().createParser(bytesReference);
|
try (XContentParser parser = content().createParser(bytesReference);
|
||||||
BytesStreamOutput bytesStreamOutput = new BytesStreamOutput();
|
BytesStreamOutput bytesStreamOutput = new BytesStreamOutput();
|
||||||
XContentGenerator generator = content().createGenerator(bytesStreamOutput)) {
|
XContentGenerator generator = content().createGenerator(bytesStreamOutput)) {
|
||||||
|
return flatMapAsString(parser, bytesStreamOutput, generator);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String flatMapAsString(XContentParser parser,
|
||||||
|
BytesStreamOutput bytesStreamOutput,
|
||||||
|
XContentGenerator generator) throws IOException {
|
||||||
generator.writeStartObject();
|
generator.writeStartObject();
|
||||||
for (Map.Entry<String, String> entry : load(parser).entrySet()) {
|
for (Map.Entry<String, String> entry : load(parser).entrySet()) {
|
||||||
generator.writeFieldName(entry.getKey());
|
generator.writeFieldName(entry.getKey());
|
||||||
|
@ -51,21 +78,8 @@ public abstract class AbstractSettingsLoader implements SettingsLoader {
|
||||||
generator.flush();
|
generator.flush();
|
||||||
return bytesStreamOutput.bytes().toUtf8();
|
return bytesStreamOutput.bytes().toUtf8();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public Map<String, String> load(XContentParser xContentParser) throws IOException {
|
private void parseObject(Map<String, String> map, StringBuilder sb, List<String> path,
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
Map<String, String> map = new HashMap<>();
|
|
||||||
List<String> path = new ArrayList<>();
|
|
||||||
XContentParser.Token token = xContentParser.nextToken();
|
|
||||||
if (token == null) {
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
parseObject(map, sb, path, xContentParser, null);
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void parseObject(Map<String, String> settings, StringBuilder sb, List<String> path,
|
|
||||||
XContentParser parser, String objFieldName) throws IOException {
|
XContentParser parser, String objFieldName) throws IOException {
|
||||||
if (objFieldName != null) {
|
if (objFieldName != null) {
|
||||||
path.add(objFieldName);
|
path.add(objFieldName);
|
||||||
|
@ -75,13 +89,13 @@ public abstract class AbstractSettingsLoader implements SettingsLoader {
|
||||||
XContentParser.Token token;
|
XContentParser.Token token;
|
||||||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
||||||
if (token == XContentParser.Token.START_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) {
|
} 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) {
|
} else if (token == XContentParser.Token.FIELD_NAME) {
|
||||||
currentFieldName = parser.currentName();
|
currentFieldName = parser.currentName();
|
||||||
} else {
|
} 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 parser, String name) throws IOException {
|
||||||
XContentParser.Token token;
|
XContentParser.Token token;
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
String fieldName = name;
|
String fieldName = name;
|
||||||
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
||||||
if (token == XContentParser.Token.START_OBJECT) {
|
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) {
|
} 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) {
|
} else if (token == XContentParser.Token.FIELD_NAME) {
|
||||||
fieldName = parser.currentName();
|
fieldName = parser.currentName();
|
||||||
} else {
|
} 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 {
|
XContentParser parser, String fieldName) throws IOException {
|
||||||
sb.setLength(0);
|
sb.setLength(0);
|
||||||
for (String s : path) {
|
for (String s : path) {
|
||||||
sb.append(s).append('.');
|
sb.append(s).append('.');
|
||||||
}
|
}
|
||||||
sb.append(fieldName);
|
sb.append(fieldName);
|
||||||
settings.put(sb.toString(), parser.text());
|
map.put(sb.toString(), parser.text());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package org.xbib.content.settings;
|
package org.xbib.content.settings;
|
||||||
|
|
||||||
|
import org.xbib.content.io.BytesReference;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.Reader;
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
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
|
@Override
|
||||||
public boolean canLoad(String source) {
|
public boolean canLoad(String source) {
|
||||||
return true;
|
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.ByteSizeValue.parseBytesSizeValue;
|
||||||
import static org.xbib.content.util.unit.TimeValue.parseTimeValue;
|
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.JsonSettingsLoader;
|
||||||
import org.xbib.content.json.JsonXContent;
|
|
||||||
import org.xbib.content.util.unit.ByteSizeValue;
|
import org.xbib.content.util.unit.ByteSizeValue;
|
||||||
import org.xbib.content.util.unit.TimeValue;
|
import org.xbib.content.util.unit.TimeValue;
|
||||||
|
|
||||||
|
@ -614,8 +612,9 @@ public class Settings {
|
||||||
* @return builder
|
* @return builder
|
||||||
*/
|
*/
|
||||||
public Builder loadFromMap(Map<String, Object> map) {
|
public Builder loadFromMap(Map<String, Object> map) {
|
||||||
try (XContentBuilder builder = JsonXContent.contentBuilder()) {
|
SettingsLoader settingsLoader = new JsonSettingsLoader();
|
||||||
put(new JsonSettingsLoader().load(builder.map(map).string()));
|
try {
|
||||||
|
put(settingsLoader.load(map));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new SettingsException("Failed to load settings from [" + map + "]", e);
|
throw new SettingsException("Failed to load settings from [" + map + "]", e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package org.xbib.content.settings;
|
package org.xbib.content.settings;
|
||||||
|
|
||||||
|
import org.xbib.content.io.BytesReference;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -24,5 +25,9 @@ public interface SettingsLoader {
|
||||||
*/
|
*/
|
||||||
Map<String, String> load(String source) throws IOException;
|
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);
|
boolean canLoad(String source);
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@ import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
@ -156,4 +157,22 @@ public class SettingsTest extends Assert {
|
||||||
String result = loader.flatMapAsString(ref);
|
String result = loader.flatMapAsString(ref);
|
||||||
assertEquals("{\"a.b\":\"c\"}", result);
|
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
|
group = org.xbib
|
||||||
name = content
|
name = content
|
||||||
version = 2.0.4
|
version = 2.0.5
|
||||||
|
|
||||||
xbib-net.version = 2.0.3
|
xbib-net.version = 2.0.3
|
||||||
jackson.version = 2.9.10
|
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
|
#Fri Nov 22 14:55:55 CET 2019
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6-all.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip
|
||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
|
|
Loading…
Reference in a new issue