From fcf008405b378b3629b46e9b1b087d25bd6f542b Mon Sep 17 00:00:00 2001 From: pdkst Date: Thu, 9 May 2024 23:11:27 +0800 Subject: [PATCH] improve snakeyml compatibility for 2.2 (#336) --- .../util/parse/DefaultYamlConfigParse.java | 48 +++++++++---------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/nacos-spring-context/src/main/java/com/alibaba/nacos/spring/util/parse/DefaultYamlConfigParse.java b/nacos-spring-context/src/main/java/com/alibaba/nacos/spring/util/parse/DefaultYamlConfigParse.java index 5c03ff7..60d0177 100644 --- a/nacos-spring-context/src/main/java/com/alibaba/nacos/spring/util/parse/DefaultYamlConfigParse.java +++ b/nacos-spring-context/src/main/java/com/alibaba/nacos/spring/util/parse/DefaultYamlConfigParse.java @@ -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; @@ -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()) { @@ -88,7 +84,7 @@ protected static boolean process(MatchCallback callback, Yaml yaml, String conte } return (count > 0); } - + protected static boolean process(Map map, MatchCallback callback) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Merging document (no matchers set): " + map); @@ -96,7 +92,7 @@ protected static boolean process(Map map, MatchCallback callback callback.process(getFlattenedMap(map)); return true; } - + @SuppressWarnings("unchecked") protected static Map asMap(Object object) { // YAML can have numbers as keys @@ -106,7 +102,7 @@ protected static Map asMap(Object object) { result.put("document", object); return result; } - + Map map = (Map) object; for (Map.Entry entry : map.entrySet()) { Object key = entry.getKey(); @@ -122,9 +118,9 @@ protected static Map 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) { @@ -133,13 +129,13 @@ public void addImplicitResolver(Tag tag, Pattern regexp, String first) { super.addImplicitResolver(tag, regexp, first); } } - + protected static Map getFlattenedMap(Map source) { Map result = new LinkedHashMap(); buildFlattenedMap(result, source, null); return result; } - + protected static void buildFlattenedMap(Map result, Map source, String path) { for (Map.Entry entry : source.entrySet()) { String key = entry.getKey(); @@ -169,7 +165,7 @@ protected static void buildFlattenedMap(Map result, Map parse(String configText) { final AtomicReference> result = new AtomicReference>(); @@ -181,14 +177,14 @@ public void process(Map map) { }, createYaml(), configText); return result.get(); } - + @Override public String processType() { return ConfigType.YAML.getType(); } - + protected interface MatchCallback { - + /** * Put Map to Properties. * @@ -196,5 +192,5 @@ protected interface MatchCallback { */ void process(Map map); } - + }