Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YAML comments support #410

Open
wants to merge 19 commits into
base: trunk
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(format/yaml): Test and fix for complex keys
zml2008 committed Oct 15, 2023

Verified

This commit was signed with the committer’s verified signature.
erikmd Erik Martin-Dorel
commit cc14b5c0c6aab502ae0a098ff08626ce554ddf01
Original file line number Diff line number Diff line change
@@ -27,7 +27,6 @@
import org.yaml.snakeyaml.nodes.MappingNode;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.NodeId;
import org.yaml.snakeyaml.nodes.ScalarNode;

import java.util.Collection;
import java.util.Collections;
@@ -65,13 +64,13 @@ protected Object constructObjectNoCheck(final Node yamlNode) {

((MappingNode) yamlNode).getValue().forEach(tuple -> {
// I don't think it's possible to have a non-scalar node as key
final ScalarNode keyNode = (ScalarNode) tuple.getKeyNode();
final ConfigurationNode keyNode = (ConfigurationNode) this.constructObject(tuple.getKeyNode());
final Node valueNode = tuple.getValueNode();

// comments are on the key, not the value
node.node(keyNode.getValue())
node.node(keyNode.raw())
.from((ConfigurationNode) constructObject(valueNode))
.comment(commentFor(keyNode.getBlockComments()));
.comment(commentFor(tuple.getKeyNode().getBlockComments()));
});

return node.comment(commentFor(yamlNode.getBlockComments()));
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import io.leangen.geantyref.TypeToken;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.spongepowered.configurate.BasicConfigurationNode;
@@ -35,6 +36,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@@ -46,7 +49,7 @@ class YamlConfigurationLoaderTest {

@Test
void testSimpleLoading() throws ConfigurateException {
final URL url = this.getClass().getResource("/example.yml");
final URL url = this.resource("/example.yml");
final ConfigurationLoader<CommentedConfigurationNode> loader = YamlConfigurationLoader.builder()
.url(url).build();
final ConfigurationNode node = loader.load();
@@ -73,15 +76,15 @@ void testReadWithTabs() throws ConfigurateException {
});
});

final URL url = this.getClass().getResource("/tab-example.yml");
final URL url = this.resource("/tab-example.yml");
final ConfigurationLoader<CommentedConfigurationNode> loader = YamlConfigurationLoader.builder()
.url(url).build();
final ConfigurationNode node = loader.load();
assertEquals(expected, node);
}

@Test
void testWriteBasicFile(final @TempDir Path tempDir) throws ConfigurateException, IOException {
void testWriteBasicFile(final @TempDir Path tempDir) throws IOException {
final Path target = tempDir.resolve("write-basic.yml");
final ConfigurationNode node = BasicConfigurationNode.root(n -> {
n.node("mapping", "first").set("hello");
@@ -102,7 +105,7 @@ void testWriteBasicFile(final @TempDir Path tempDir) throws ConfigurateException

loader.save(node);

assertEquals(readLines(this.getClass().getResource("write-expected.yml")), Files.readAllLines(target, StandardCharsets.UTF_8));
assertEquals(readLines(this.resource("write-expected.yml")), Files.readAllLines(target, StandardCharsets.UTF_8));
}

@Test
@@ -119,7 +122,7 @@ void testReadComments() throws IOException {
})
));

final URL url = this.getClass().getResource("comments-test.yml");
final URL url = this.resource("comments-test.yml");
final YamlConfigurationLoader loader = YamlConfigurationLoader.builder()
.url(url).build();

@@ -150,14 +153,14 @@ void testWriteComments(final @TempDir Path tempDir) throws IOException {
loader.save(node);

assertEquals(
readLines(this.getClass().getResource("comments-test.yml")),
readLines(this.resource("comments-test.yml")),
Files.readAllLines(target, StandardCharsets.UTF_8)
);
}

@Test
void testReadWriteComments(final @TempDir Path tempDir) throws IOException {
final URL source = this.getClass().getResource("comments-test.yml");
final URL source = this.resource("comments-test.yml");
final Path destination = tempDir.resolve("comments-readwrite.yml");

final YamlConfigurationLoader loader = YamlConfigurationLoader.builder()
@@ -175,6 +178,26 @@ void testReadWriteComments(final @TempDir Path tempDir) throws IOException {
);
}

@Test
void testComplexKeys() throws ConfigurateException {
final URL source = this.resource("complex-keys.yaml");
final CommentedConfigurationNode node = YamlConfigurationLoader.builder()
.url(source)
.build().load();

assertEquals("good", node.node("mapping", Arrays.asList("one", "two")).getString());
assertEquals("bad", node.node("mapping", Arrays.asList("red", "blue")).getString());
assertEquals("cat", node.node("mapping", Collections.singletonMap("name", "Meow")).getString());
}

private URL resource(final String path) {
final @Nullable URL res = this.getClass().getResource(path);
if (res == null) {
throw new IllegalArgumentException("No resource found for path '" + path + "'");
}
return res;
}

private static List<String> readLines(final URL source) throws IOException {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(source.openStream(), StandardCharsets.UTF_8))) {
return reader.lines().collect(Collectors.toList());
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mapping:
[one, two]: good
[red, blue]: bad
{name: "Meow"}: cat