Skip to content

Commit

Permalink
hocon: Ensure leading spaces in comments are stripped
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 committed May 4, 2021
1 parent 1fd68a7 commit 2509780
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,15 @@ protected void loadInternal(final CommentedConfigurationNode node, final Buffere
private static void readConfigValue(final ConfigValue value, final CommentedConfigurationNode node) {
if (!value.origin().comments().isEmpty()) {
node.comment(value.origin().comments().stream()
.map(input -> input.replace("\r", ""))
.collect(Collectors.joining("\n")));
.map(input -> {
final String lineStripped = input.replace("\r", "");
if (!lineStripped.isEmpty() && lineStripped.charAt(0) == ' ') {
return lineStripped.substring(1);
} else {
return lineStripped;
}
})
.collect(Collectors.joining("\n")));
}

switch (value.valueType()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.spongepowered.configurate.CommentedConfigurationNode;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationOptions;
import org.spongepowered.configurate.loader.AtomicFiles;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
Expand Down Expand Up @@ -64,7 +65,7 @@ void testSimpleLoading(final @TempDir Path tempDir) throws IOException {
assertEquals("unicorn", node.node("test", "op-level").raw());
assertEquals("dragon", node.node("other", "op-level").raw());
final CommentedConfigurationNode testNode = node.node("test");
assertEquals(" Test node", testNode.comment());
assertEquals("Test node", testNode.comment());
assertEquals("dog park", node.node("other", "location").raw());
loader.save(node);
assertEquals(Resources.readLines(requireResource("roundtrip-test.conf"), StandardCharsets.UTF_8), Files
Expand Down Expand Up @@ -176,6 +177,19 @@ void testCreateEmptyObjectMappingSection(final @TempDir Path tempDir) throws IOE
assertLinesMatch(Resources.readLines(rsrc, StandardCharsets.UTF_8), Files.readAllLines(output, StandardCharsets.UTF_8));
}

@Test
void testCommentAtBeginningStripped() throws ConfigurateException {
final URL resource = requireResource("comments-test.conf");

final CommentedConfigurationNode root = HoconConfigurationLoader.builder()
.url(resource)
.build()
.load();

assertEquals("fruit", root.node("test", "apple").comment());
assertEquals("tasty", root.node("test", "donut").comment());
}

private URL requireResource(final String path) {
final @Nullable URL resource = this.getClass().getResource('/' + path);
assertNotNull(resource, () -> "Resource " + path + " was not present when expected to be!");
Expand Down

0 comments on commit 2509780

Please sign in to comment.