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

#618 comment() implemented in Flow nodes #622

Merged
merged 2 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
60 changes: 53 additions & 7 deletions src/main/java/com/amihaiemil/eoyaml/ReadFlowMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,19 @@
* @author Mihai Andronache ([email protected])
* @version $Id$
* @since 8.0.0
* @todo #615:60min Implement the comment() method properly (at the moment
* it always assumes there is no comment).
*/
final class ReadFlowMapping extends BaseYamlMapping {

/**
* All the lines of the YAML document.
*/
private final AllYamlLines all;

/**
* Previous line just before the one where this flow sequence starts.
*/
private final YamlLine previous;

/**
* The entries of this flow mapping as String.
*/
Expand Down Expand Up @@ -84,16 +92,24 @@ final class ReadFlowMapping extends BaseYamlMapping {
),
'{',
'}'
).line(previous.number() < 0 ? 0 : previous.number() + 1)
).line(previous.number() < 0 ? 0 : previous.number() + 1),
previous,
lines
);
}

/**
* Constructor.
* @param folded All the YAML lines of this flow mapping,
* folded into a single one.
* @param previous Line previous to where this flow mapping starts.
* @param all All the lines of the YAML document.
*/
ReadFlowMapping(final YamlLine folded) {
ReadFlowMapping(
final YamlLine folded, final YamlLine previous, final AllYamlLines all
) {
this.previous = previous;
this.all = all;
this.entries = new StringEntries(folded);
this.folded = folded;
}
Expand Down Expand Up @@ -123,7 +139,33 @@ public YamlNode value(final YamlNode key) {

@Override
public Comment comment() {
return new BuiltComment(this, "");
boolean documentComment = this.previous.number() < 0;
//@checkstyle LineLength (50 lines)
return new ReadComment(
new Backwards(
new FirstCommentFound(
new Backwards(
new Skip(
this.all,
line -> {
final boolean skip;
if(documentComment) {
skip = line.number() >= this.folded.number();
} else {
skip = line.number() >= this.previous.number();
}
return skip;
},
line -> line.trimmed().startsWith("..."),
line -> line.trimmed().startsWith("%"),
line -> line.trimmed().startsWith("!!")
)
),
documentComment
)
),
this
);
}

/**
Expand All @@ -135,11 +177,15 @@ private YamlNode stringToYamlNodeNode(final String node) {
final YamlNode yaml;
if (node.startsWith("[")) {
yaml = new ReadFlowSequence(
new RtYamlLine(node, this.folded.number())
new RtYamlLine(node, this.folded.number()),
this.previous,
this.all
);
} else if (node.startsWith("{")) {
yaml = new ReadFlowMapping(
new RtYamlLine(node, this.folded.number())
new RtYamlLine(node, this.folded.number()),
this.previous,
this.all
);
} else {
yaml = new PlainStringScalar(node.trim());
Expand Down
64 changes: 56 additions & 8 deletions src/main/java/com/amihaiemil/eoyaml/ReadFlowSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@
*/
final class ReadFlowSequence extends BaseYamlSequence {

/**
* All the lines of the document.
*/
private final AllYamlLines all;

/**
* Previous line just before the one where this flow sequence starts.
*/
private final YamlLine previous;

/**
* The entries of this flow sequence as String.
*/
Expand All @@ -61,14 +71,14 @@ final class ReadFlowSequence extends BaseYamlSequence {
/**
* Ctor.
* @param previous Line just before the start of this flow sequence.
* @param lines All lines of the YAML document.
* @param all All lines of the YAML document.
* @checkstyle AvoidInlineConditionals (30 lines)
*/
ReadFlowSequence(final YamlLine previous, final AllYamlLines lines) {
ReadFlowSequence(final YamlLine previous, final AllYamlLines all) {
this(
new CollapsedFlowLines(
new Skip(
lines,
all,
line -> line.number() <= previous.number(),
line -> line.trimmed().startsWith("#"),
line -> line.trimmed().startsWith("---"),
Expand All @@ -78,16 +88,24 @@ final class ReadFlowSequence extends BaseYamlSequence {
),
'[',
']'
).line(previous.number() < 0 ? 0 : previous.number() + 1)
).line(previous.number() < 0 ? 0 : previous.number() + 1),
previous,
all
);
}

/**
* Constructor.
* @param folded All the YAML lines of this flow sequence,
* folded into a single one.
* @param previous Line previous to where this flow mapping starts.
* @param all All the lines of the YAML document.
*/
ReadFlowSequence(final YamlLine folded) {
ReadFlowSequence(
final YamlLine folded, final YamlLine previous, final AllYamlLines all
) {
this.previous = previous;
this.all = all;
this.entries = new StringNodes(folded);
this.folded = folded;
}
Expand All @@ -99,13 +117,17 @@ public Collection<YamlNode> values() {
if (node.startsWith("[")) {
kids.add(
new ReadFlowSequence(
new RtYamlLine(node, this.folded.number())
new RtYamlLine(node, this.folded.number()),
this.previous,
this.all
)
);
} else if(node.startsWith("{")) {
kids.add(
new ReadFlowMapping(
new RtYamlLine(node, this.folded.number())
new RtYamlLine(node, this.folded.number()),
this.previous,
this.all
)
);
} else {
Expand All @@ -119,7 +141,33 @@ public Collection<YamlNode> values() {

@Override
public Comment comment() {
return new BuiltComment(this, "");
boolean documentComment = this.previous.number() < 0;
//@checkstyle LineLength (50 lines)
return new ReadComment(
new Backwards(
new FirstCommentFound(
new Backwards(
new Skip(
this.all,
line -> {
final boolean skip;
if(documentComment) {
skip = line.number() >= this.folded.number();
} else {
skip = line.number() >= this.previous.number();
}
return skip;
},
line -> line.trimmed().startsWith("..."),
line -> line.trimmed().startsWith("%"),
line -> line.trimmed().startsWith("!!")
)
),
documentComment
)
),
this
);
}

/**
Expand Down
44 changes: 38 additions & 6 deletions src/test/java/com/amihaiemil/eoyaml/ReadFlowMappingTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.mockito.Mockito;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
Expand All @@ -46,16 +44,50 @@
public final class ReadFlowMappingTestCase {

/**
* YamlMapping in flow format has no comment.
* ReadFlowMapping can return the document comment.
*/
@Test
public void hasNoComment() {
public void hasDocumentComment() {
final YamlMapping map = new ReadFlowMapping(
Mockito.mock(YamlLine.class)
new RtYamlLine("{a: b, c: d, e: f}", 2),
new RtYamlLine("---", 1),
new AllYamlLines(
Arrays.asList(
new RtYamlLine("# this is a flow mapping document", 0),
new RtYamlLine("---", 1),
new RtYamlLine("{a: b, c: d, e: f}", 2)
)
)
);
MatcherAssert.assertThat(
map.comment().value(),
Matchers.equalTo("this is a flow mapping document")
);
MatcherAssert.assertThat(
map.comment().yamlNode(),
Matchers.is(map)
);
}

/**
* ReadFlowMapping can return the comment referring to it.
*/
@Test
public void hasOwnNodeComment() {
final YamlMapping map = new ReadFlowMapping(
new RtYamlLine("{a: {i: j}, c: d, e: f}", 2),
new RtYamlLine("flow:", 1),
new AllYamlLines(
Arrays.asList(
new RtYamlLine("# this comment about the 'flow' map", 0),
new RtYamlLine("flow:", 1),
new RtYamlLine(" {a: {i: j}, c: d, e: f}", 2)
)
)
);
MatcherAssert.assertThat(
map.comment().value(),
Matchers.isEmptyString()
Matchers.equalTo("this comment about the 'flow' map")
);
MatcherAssert.assertThat(
map.comment().yamlNode(),
Expand Down
59 changes: 51 additions & 8 deletions src/test/java/com/amihaiemil/eoyaml/ReadFlowSequenceTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.junit.Test;
import org.mockito.Mockito;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
Expand All @@ -45,16 +46,50 @@
public final class ReadFlowSequenceTestCase {

/**
* Sequences in flow/json style have no comment.
* ReadFlowSequence can return the document comment.
*/
@Test
public void hasNoComment() {
public void hasDocumentComment() {
final YamlSequence seq = new ReadFlowSequence(
Mockito.mock(YamlLine.class)
new RtYamlLine("[{a: b}, {c: d}, {e: f}]", 2),
new RtYamlLine("---", 1),
new AllYamlLines(
Arrays.asList(
new RtYamlLine("# this is a flow sequence document", 0),
new RtYamlLine("---", 1),
new RtYamlLine("[{a: b}, {c: d}, {e: f}]", 2)
)
)
);
MatcherAssert.assertThat(
seq.comment().value(),
Matchers.isEmptyString()
Matchers.equalTo("this is a flow sequence document")
);
MatcherAssert.assertThat(
seq.comment().yamlNode(),
Matchers.is(seq)
);
}

/**
* ReadFlowMapping can return the comment referring to it.
*/
@Test
public void hasOwnNodeComment() {
final YamlSequence seq = new ReadFlowSequence(
new RtYamlLine("[{a: b}, {c: d}, {e: f}]", 2),
new RtYamlLine("flow_seq:", 1),
new AllYamlLines(
Arrays.asList(
new RtYamlLine("# this comment about the 'flow' seq", 0),
new RtYamlLine("flow:", 1),
new RtYamlLine(" [{a: b}, {c: d}, {e: f}]", 2)
)
)
);
MatcherAssert.assertThat(
seq.comment().value(),
Matchers.equalTo("this comment about the 'flow' seq")
);
MatcherAssert.assertThat(
seq.comment().yamlNode(),
Expand All @@ -68,7 +103,9 @@ public void hasNoComment() {
@Test
public void hasNoValues() {
final YamlSequence seq = new ReadFlowSequence(
new RtYamlLine("[]", 0)
new RtYamlLine("[]", 0),
Mockito.mock(YamlLine.class),
new AllYamlLines(new ArrayList<>())
);
MatcherAssert.assertThat(
seq.values(),
Expand All @@ -82,7 +119,9 @@ public void hasNoValues() {
@Test
public void hasOnlyScalars() {
final YamlSequence seq = new ReadFlowSequence(
new RtYamlLine("[a, b, c, d:e]", 0)
new RtYamlLine("[a, b, c, d:e]", 0),
Mockito.mock(YamlLine.class),
new AllYamlLines(new ArrayList<>())
);
final Collection<YamlNode> values = seq.values();
MatcherAssert.assertThat(values, Matchers.iterableWithSize(4));
Expand Down Expand Up @@ -111,7 +150,9 @@ public void hasOnlyScalars() {
@Test
public void hasOnlySequences() {
final YamlSequence seq = new ReadFlowSequence(
new RtYamlLine("[[a, b], [c,\"[a]\",'b]['], [d]]", 0)
new RtYamlLine("[[a, b], [c,\"[a]\",'b]['], [d]]", 0),
Mockito.mock(YamlLine.class),
new AllYamlLines(new ArrayList<>())
);
System.out.println(seq);
MatcherAssert.assertThat(
Expand Down Expand Up @@ -161,7 +202,9 @@ public void hasSequencesAndScalars() {
final YamlSequence seq = new ReadFlowSequence(
new RtYamlLine(
"[scalar, \"u][\", 'v}{', 'escalar', [a, b], other, [d]]", 0
)
),
Mockito.mock(YamlLine.class),
new AllYamlLines(new ArrayList<>())
);
System.out.println(seq);
MatcherAssert.assertThat(
Expand Down
Loading