Skip to content

Commit

Permalink
improve snakeyml compatibility for 2.2 (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdkst authored May 9, 2024
1 parent 1ada9f4 commit fcf0084
Showing 1 changed file with 22 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,14 @@
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.constructor.SafeConstructor;
import org.yaml.snakeyaml.nodes.MappingNode;
import org.yaml.snakeyaml.nodes.Tag;
import org.yaml.snakeyaml.parser.ParserException;
import org.yaml.snakeyaml.representer.Representer;
import org.yaml.snakeyaml.resolver.Resolver;

import java.util.AbstractMap;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Pattern;

Expand All @@ -49,30 +45,30 @@
* @since 0.3.0
*/
public class DefaultYamlConfigParse extends AbstractConfigParse {

protected static final Logger LOGGER = LoggerFactory.getLogger(DefaultYamlConfigParse.class);

private static final String YAML_ALLOW_COMPLEX_OBJECT = "yamlAllowComplexObject";

private static boolean getYamlAllowComplexObject() {
return Boolean.getBoolean(YAML_ALLOW_COMPLEX_OBJECT);
}

protected static Yaml createYaml() {
LoaderOptions loaderOptions = new LoaderOptions();
loaderOptions.setAllowDuplicateKeys(false);
SafeConstructor constructor;
if (getYamlAllowComplexObject()) {
constructor = new Constructor();
constructor = new Constructor(loaderOptions);
} else {
constructor = new SafeConstructor();
constructor = new SafeConstructor(loaderOptions);
}
Representer representer = new Representer();
DumperOptions dumperOptions = new DumperOptions();
Representer representer = new Representer(dumperOptions);
LimitedResolver resolver = new LimitedResolver();
LoaderOptions loaderOptions = new LoaderOptions();
loaderOptions.setAllowDuplicateKeys(false);
return new Yaml(constructor, representer, dumperOptions, loaderOptions, resolver);
}

protected static boolean process(MatchCallback callback, Yaml yaml, String content) {
int count = 0;
if (LOGGER.isDebugEnabled()) {
Expand All @@ -88,15 +84,15 @@ protected static boolean process(MatchCallback callback, Yaml yaml, String conte
}
return (count > 0);
}

protected static boolean process(Map<String, Object> map, MatchCallback callback) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Merging document (no matchers set): " + map);
}
callback.process(getFlattenedMap(map));
return true;
}

@SuppressWarnings("unchecked")
protected static Map<String, Object> asMap(Object object) {
// YAML can have numbers as keys
Expand All @@ -106,7 +102,7 @@ protected static Map<String, Object> asMap(Object object) {
result.put("document", object);
return result;
}

Map<Object, Object> map = (Map<Object, Object>) object;
for (Map.Entry<Object, Object> entry : map.entrySet()) {
Object key = entry.getKey();
Expand All @@ -122,9 +118,9 @@ protected static Map<String, Object> asMap(Object object) {
}
return result;
}

private static class LimitedResolver extends Resolver {

@Override
public void addImplicitResolver(Tag tag, Pattern regexp, String first) {
if (tag == Tag.TIMESTAMP) {
Expand All @@ -133,13 +129,13 @@ public void addImplicitResolver(Tag tag, Pattern regexp, String first) {
super.addImplicitResolver(tag, regexp, first);
}
}

protected static Map<String, Object> getFlattenedMap(Map<String, Object> source) {
Map<String, Object> result = new LinkedHashMap<String, Object>();
buildFlattenedMap(result, source, null);
return result;
}

protected static void buildFlattenedMap(Map<String, Object> result, Map<String, Object> source, String path) {
for (Map.Entry<String, Object> entry : source.entrySet()) {
String key = entry.getKey();
Expand Down Expand Up @@ -169,7 +165,7 @@ protected static void buildFlattenedMap(Map<String, Object> result, Map<String,
}
}
}

@Override
public Map<String, Object> parse(String configText) {
final AtomicReference<Map<String, Object>> result = new AtomicReference<Map<String, Object>>();
Expand All @@ -181,20 +177,20 @@ public void process(Map<String, Object> map) {
}, createYaml(), configText);
return result.get();
}

@Override
public String processType() {
return ConfigType.YAML.getType();
}

protected interface MatchCallback {

/**
* Put Map to Properties.
*
* @param map {@link Map}
*/
void process(Map<String, Object> map);
}

}

0 comments on commit fcf0084

Please sign in to comment.