Skip to content

Commit

Permalink
#382 fixed small bug regarding escaped scalars
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Jul 14, 2020
1 parent 1354b25 commit 5029085
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 7 deletions.
24 changes: 20 additions & 4 deletions src/main/java/com/amihaiemil/eoyaml/ReadPlainScalar.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,16 @@ final class ReadPlainScalar extends BaseScalar {
public String value() {
final String value;
final String trimmed = this.scalar.trimmed();
if(trimmed.contains(":") && !trimmed.endsWith(":")) {
value = trimmed.substring(trimmed.indexOf(":") + 1).trim();
} else if(trimmed.startsWith("-") && trimmed.length() > 1) {
if(this.escapedSequenceScalar(this.scalar)) {
value = trimmed.substring(trimmed.indexOf('-')+1).trim();
} else {
value = trimmed;
if (trimmed.contains(":") && !trimmed.endsWith(":")) {
value = trimmed.substring(trimmed.indexOf(":") + 1).trim();
} else if (trimmed.startsWith("-") && trimmed.length() > 1) {
value = trimmed.substring(trimmed.indexOf('-') + 1).trim();
} else {
value = trimmed;
}
}
if("null".equals(value)) {
return null;
Expand Down Expand Up @@ -125,4 +129,16 @@ private String unescape(final String value) {
}
return unescaped;
}

/**
* Returns true if there's a YamlMapping starting right after the
* dash, on the same line.
* @param dashLine Line.
* @return True of false.
*/
private boolean escapedSequenceScalar(final YamlLine dashLine) {
final String trimmed = dashLine.trimmed();
return trimmed.matches("^[ ]*\\-[ ]*\".*\"$")
|| trimmed.matches("^[ ]*\\-[ ]*\'.*\'$");
}
}
1 change: 1 addition & 0 deletions src/main/java/com/amihaiemil/eoyaml/ReadYamlMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ public Comment comment() {
* @param key String key.
* @return YamlNode.
* @checkstyle ReturnCount (50 lines)
* @checkstyle LineLength (30 lines)
*/
private YamlNode valueOfStringKey(final String key) {
YamlNode value = null;
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/com/amihaiemil/eoyaml/ReadYamlSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public Collection<YamlNode> values() {
)
);
} else {
if(trimmed.matches("^.*\\-.*\\:.*$")) {
if(this.mappingStartsAtDash(line)) {
kids.add(
new ReadYamlMapping(
new RtYamlLine("", line.number()-1),
Expand Down Expand Up @@ -202,4 +202,16 @@ public Comment comment() {
);
}

/**
* Returns true if there's a YamlMapping starting right after the
* dash, on the same line.
* @param dashLine Line.
* @return True of false.
*/
private boolean mappingStartsAtDash(final YamlLine dashLine) {
final String trimmed = dashLine.trimmed();
final boolean escapedScalar = trimmed.matches("^[ ]*\\-[ ]*\".*\"$")
|| trimmed.matches("^[ ]*\\-[ ]*\'.*\'$");
return trimmed.matches("^.*\\-.*\\:.*$") && !escapedScalar;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/amihaiemil/eoyaml/RtYamlPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ static class Escaped extends BaseScalar {
/**
* Special chars that need escaping.
*/
private final String RESERVED = "#:->|$%&";
private final String RESERVED = "#:->|$%&{}[]";

/**
* Original unescaped scalar.
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/com/amihaiemil/eoyaml/SameIndentationLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public Iterator<YamlLine> iterator() {
final YamlLine first = iterator.next();
sameIndentation.add(first);
int firstIndentation = first.indentation();
if(first.trimmed().matches("^[ ]*\\-.*\\:.+$")) {
if(this.mappingStartsAtDash(first)) {
firstIndentation += 2;
}
while (iterator.hasNext()) {
Expand Down Expand Up @@ -106,4 +106,17 @@ public YamlNode toYamlNode(
return this.yamlLines.toYamlNode(prev, guessIndentation);
}

/**
* Returns true if there's a YamlMapping starting right after the
* dash, on the same line.
* @param dashLine Line.
* @return True of false.
*/
private boolean mappingStartsAtDash(final YamlLine dashLine) {
final String trimmed = dashLine.trimmed();
final boolean escapedScalar = trimmed.matches("^[ ]*\\-[ ]*\".*\"$")
|| trimmed.matches("^[ ]*\\-[ ]*\'.*\'$");
return trimmed.matches("^[ ]*\\-.*\\:.+$") && !escapedScalar;
}

}
37 changes: 37 additions & 0 deletions src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,43 @@ public void readsSequenceWithDashMappings() throws Exception {
);
}

/**
* Some escaped scalars can be read from a sequence.
* @throws Exception If something goes wrong.
*/
@Test
public void readsEscapedScalarsFromSequence() throws Exception {
final YamlMapping map = Yaml.createYamlInput(
this.readTestResource("escapedScalarsInSequence.yml")
).readYamlMapping();
System.out.println(map);
final YamlSequence scalars = map.yamlSequence("scalars");
MatcherAssert.assertThat(
scalars.string(0),
Matchers.equalTo(
"escaped: scalar, looks like mapping"
)
);
MatcherAssert.assertThat(
scalars.string(1),
Matchers.equalTo(
"{escaped: scalar, like: flow}"
)
);
MatcherAssert.assertThat(
scalars.string(2),
Matchers.equalTo(
"- escapedScalar3"
)
);
MatcherAssert.assertThat(
scalars.string(3),
Matchers.equalTo(
"[scalar, like, flow, sequence]"
)
);
}

/**
* Read a test resource file's contents.
* @param fileName File to read.
Expand Down
5 changes: 5 additions & 0 deletions src/test/resources/escapedScalarsInSequence.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
scalars:
- "escaped: scalar, looks like mapping"
- "{escaped: scalar, like: flow}"
- "- escapedScalar3"
- "[scalar, like, flow, sequence]"

0 comments on commit 5029085

Please sign in to comment.