Skip to content

Commit

Permalink
Upgrade snakeyaml to 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
zhfeng committed Mar 16, 2023
1 parent d0ca45f commit 0f4153b
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ public static String marshal(final Object value) {
DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setLineBreak(DumperOptions.LineBreak.getPlatformLineBreak());
if (value instanceof Collection) {
return new Yaml(new ShardingSphereYamlRepresenter(), dumperOptions).dumpAs(value, null, DumperOptions.FlowStyle.BLOCK);
return new Yaml(new ShardingSphereYamlRepresenter(dumperOptions), dumperOptions).dumpAs(value, null, DumperOptions.FlowStyle.BLOCK);
}
return new Yaml(new ShardingSphereYamlRepresenter(), dumperOptions).dumpAsMap(value);
return new Yaml(new ShardingSphereYamlRepresenter(dumperOptions), dumperOptions).dumpAsMap(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
*/
public final class ShardingSphereYamlRepresenter extends Representer {

public ShardingSphereYamlRepresenter() {
public ShardingSphereYamlRepresenter(final DumperOptions dumperOptions) {
super(dumperOptions);
Map<String, Class<?>> yamlShortcuts = new HashMap<>();
ShardingSphereServiceLoader.getServiceInstances(ShardingSphereYamlShortcuts.class).stream().map(ShardingSphereYamlShortcuts::getYamlShortcuts).forEach(yamlShortcuts::putAll);
yamlShortcuts.forEach((key, value) -> addClassTag(value, new Tag(key)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import org.apache.shardingsphere.infra.util.yaml.fixture.shortcuts.YamlShortcutsConfigurationFixture;
import org.junit.jupiter.api.Test;
import org.yaml.snakeyaml.constructor.ConstructorException;
import org.yaml.snakeyaml.composer.ComposerException;

import java.io.BufferedReader;
import java.io.File;
Expand Down Expand Up @@ -101,7 +101,7 @@ public void assertUnmarshalInvalidYaml() throws IOException {
yamlContent.append(line).append(System.lineSeparator());
}
}
assertThrows(ConstructorException.class, () -> YamlEngine.unmarshal(yamlContent.toString(), Object.class));
assertThrows(ComposerException.class, () -> YamlEngine.unmarshal(yamlContent.toString(), Object.class));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.apache.shardingsphere.infra.util.yaml.fixture.pojo.YamlConfigurationFixture;
import org.junit.jupiter.api.Test;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.ConstructorException;
import org.yaml.snakeyaml.composer.ComposerException;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -53,7 +53,7 @@ private void assertYamlObject(final YamlConfigurationFixture actual) {
@Test
public void assertToObjectWithNotAcceptClass() throws IOException {
try (InputStream inputStream = ShardingSphereYamlConstructorTest.class.getClassLoader().getResourceAsStream("yaml/accepted-class.yaml")) {
assertThrows(ConstructorException.class, () -> new Yaml(new ShardingSphereYamlConstructor(Object.class)).loadAs(inputStream, Object.class));
assertThrows(ComposerException.class, () -> new Yaml(new ShardingSphereYamlConstructor(Object.class)).loadAs(inputStream, Object.class));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public final class ShardingSphereYamlRepresenterTest {
@Test
public void assertToYamlWithoutContent() {
YamlConfigurationFixture actual = new YamlConfigurationFixture();
assertThat(new Yaml(new ShardingSphereYamlRepresenter(), new DumperOptions()).dumpAsMap(actual), is("{}\n"));
DumperOptions dumperOptions = new DumperOptions();
assertThat(new Yaml(new ShardingSphereYamlRepresenter(dumperOptions), dumperOptions).dumpAsMap(actual), is("{}\n"));
}

@Test
Expand All @@ -53,7 +54,8 @@ public void assertToYamlWithAllContents() {
actual.getEmbeddedMap().put("embedded_map_1", new LinkedHashMap<>());
actual.getEmbeddedMap().put("embedded_map_2", Collections.singletonMap("embedded_map_foo", "embedded_map_foo_value"));
actual.setCustomizedTag("customized_tag");
String expected = new Yaml(new ShardingSphereYamlRepresenter(), new DumperOptions()).dumpAsMap(actual);
DumperOptions dumperOptions = new DumperOptions();
String expected = new Yaml(new ShardingSphereYamlRepresenter(dumperOptions), dumperOptions).dumpAsMap(actual);
assertThat(expected, containsString("collection:\n- value1\n- value2\n"));
assertThat(expected, containsString("map:\n key1: value1\n key2: value2\n"));
assertThat(expected, not(containsString("embedded_map_1")));
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<transmittable-thread-local.version>2.14.2</transmittable-thread-local.version>

<antlr4.version>4.10.1</antlr4.version>
<snakeyaml.version>1.33</snakeyaml.version>
<snakeyaml.version>2.0</snakeyaml.version>
<gson.version>2.9.1</gson.version>
<jackson.version>2.13.4</jackson.version>
<groovy.version>4.0.6</groovy.version>
Expand Down

0 comments on commit 0f4153b

Please sign in to comment.