Skip to content

Commit

Permalink
Merge pull request #520 from coder-hugo/feature/fix-parsing-of-entrie…
Browse files Browse the repository at this point in the history
…s-with-colons

#517 The key of an entry must be followed by a space
  • Loading branch information
amihaiemil authored Oct 29, 2022
2 parents 5d01d13 + 2ca19da commit b612e8e
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 43 deletions.
30 changes: 15 additions & 15 deletions src/main/java/com/amihaiemil/eoyaml/ReadYamlMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@
*/
package com.amihaiemil.eoyaml;

import static com.amihaiemil.eoyaml.YamlLine.UNKNOWN_LINE_NUMBER;

import com.amihaiemil.eoyaml.exceptions.YamlReadingException;
import java.util.*;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.amihaiemil.eoyaml.YamlLine.UNKNOWN_LINE_NUMBER;

/**
* YamlMapping read from somewhere. YAML directives and
* document start/end markers are ignored. This is assumed
Expand All @@ -44,6 +48,8 @@
*/
final class ReadYamlMapping extends BaseYamlMapping {

private static final Pattern KEY_PATTERN = Pattern.compile("^-?\\s*(?<key>.+):(|\\s.*)$");

/**
* Yaml line just previous to the one where this mapping starts. E.g.
* <pre>
Expand Down Expand Up @@ -158,18 +164,12 @@ public Set<YamlNode> keys() {
if(!trimmed.contains(":")) {
continue;
}
final String key;
if(trimmed.startsWith("-")) {
key = trimmed.substring(
1, trimmed.lastIndexOf(":")
).trim();
} else {
key = trimmed.substring(
0, trimmed.lastIndexOf(":")
).trim();
}
if(!key.isEmpty()) {
keys.add(new PlainStringScalar(key));
final Matcher matcher = KEY_PATTERN.matcher(trimmed);
if (matcher.matches()) {
final String key = matcher.group("key");
if (!key.isEmpty()) {
keys.add(new PlainStringScalar(key));
}
}
}
prev = line;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/amihaiemil/eoyaml/ReadYamlSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ public Comment comment() {
*/
private boolean mappingStartsAtDash(final YamlLine dashLine) {
final String trimmed = dashLine.trimmed();
final boolean escapedScalar = trimmed.matches("^[ ]*\\-[ ]*\".*\"$")
|| trimmed.matches("^[ ]*\\-[ ]*\'.*\'$");
return trimmed.matches("^.*\\-.*\\:.*$") && !escapedScalar;
final boolean escapedScalar = trimmed.matches("^\\s*-\\s*\".*\"$")
|| trimmed.matches("^\\s*-\\s*'.*'$");
return trimmed.matches("^.*-.+:(|\\s.*)$") && !escapedScalar;
}
}
6 changes: 3 additions & 3 deletions src/main/java/com/amihaiemil/eoyaml/RtYamlInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ private AllYamlLines readInput() throws IOException {
private boolean mappingStartsAtDash(final String line){
//line without indentation.
final String trimmed = line.trim();
final boolean escapedScalar = trimmed.matches("^[ ]*-[ ]*\".*\"$")
|| trimmed.matches("^[ ]*-[ ]*'.*'$");
return trimmed.matches("^[ ]*-.*:.+$") && !escapedScalar;
final boolean escapedScalar = trimmed.matches("^\\s*-\\s*\".*\"$")
|| trimmed.matches("^\\s*-\\s*'.*'$");
return trimmed.matches("^\\s*-.+:\\s.*$") && !escapedScalar;
}
}
12 changes: 6 additions & 6 deletions src/main/java/com/amihaiemil/eoyaml/SameIndentationLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ public YamlNode toYamlNode(
*/
private boolean mappingStartsAtDash(final YamlLine dashLine) {
final String trimmed = dashLine.trimmed();
final boolean escapedScalar = trimmed.matches("^[ ]*\\-[ ]*\".*\"$")
|| trimmed.matches("^[ ]*\\-[ ]*\'.*\'$");
return trimmed.matches("^[ ]*\\-.*\\:.+$") && !escapedScalar;
final boolean escapedScalar = trimmed.matches("^\\s*-\\s*\".*\"$")
|| trimmed.matches("^\\s*-\\s*'.*'$");
return trimmed.matches("^\\s*-.*:\\s.+$") && !escapedScalar;
}

/**
Expand All @@ -128,9 +128,9 @@ private boolean mappingStartsAtDash(final YamlLine dashLine) {
*/
private boolean mapping(final YamlLine dashLine) {
final String trimmed = dashLine.trimmed();
final boolean escapedScalar = trimmed.matches("^[ ]*\".*\"$")
|| trimmed.matches("^[ ]*\'.*\'$");
return trimmed.matches("^[ ]*.*\\:.+$") && !escapedScalar;
final boolean escapedScalar = trimmed.matches("^\\s*\".*\"$")
|| trimmed.matches("^\\s*'.*'$");
return trimmed.matches("^.*:\\s.+$") && !escapedScalar;
}

}
2 changes: 1 addition & 1 deletion src/main/java/com/amihaiemil/eoyaml/WellIndented.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public Iterator<YamlLine> iterator() {
YamlLine line = iterator.next();
if(!(previous instanceof YamlLine.NullYamlLine)) {
int prevIndent = previous.indentation();
if(previous.trimmed().matches("^[ ]*\\-.*\\:.*$")) {
if(previous.trimmed().matches("^\\s*-.*:(|\\s.*)$")) {
prevIndent += 2;
}
int lineIndent = line.indentation();
Expand Down
20 changes: 15 additions & 5 deletions src/test/java/com/amihaiemil/eoyaml/ReadYamlMappingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@
*/
package com.amihaiemil.eoyaml;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Ignore;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Ignore;
import org.junit.Test;

/**
* Unit tests for {@link ReadYamlMapping}.
* @checkstyle MethodName (1500 lines)
Expand Down Expand Up @@ -1170,4 +1170,14 @@ public void shouldReadFromAnotherFromYamlMapping()
MatcherAssert.assertThat(copy.string("key2"), Matchers
.equalTo("Some other value."));
}

@Test
public void shouldReadKeyProperlyIfValueContainsColon() {
final List<YamlLine> lines = new ArrayList<>();
lines.add(new RtYamlLine("key: value:with-colon", 0));
ReadYamlMapping mapping = new ReadYamlMapping(new AllYamlLines(lines));

MatcherAssert.assertThat(mapping.keys(), Matchers.hasSize(1));
MatcherAssert.assertThat(mapping.keys().iterator().next().asScalar().value(), Matchers.equalTo("key"));
}
}
24 changes: 19 additions & 5 deletions src/test/java/com/amihaiemil/eoyaml/ReadYamlSequenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@
*/
package com.amihaiemil.eoyaml;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Ignore;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
* Unit tests for {@link ReadYamlSequence}.
* @author Mihai Andronache ([email protected])
Expand Down Expand Up @@ -484,4 +484,18 @@ public void printsEmptyYaml() throws Exception {
);
MatcherAssert.assertThat(sequence.toString(), Matchers.isEmptyString());
}

@Test
public void dontReturnMappingForScalarWithColon() {
final List<YamlLine> lines = new ArrayList<>();
lines.add(new RtYamlLine("- scalar:with-colon", 0));
final YamlSequence sequence = new ReadYamlSequence(
new AllYamlLines(lines)
);

MatcherAssert.assertThat(sequence.values(), Matchers.hasSize(1));
YamlNode sequenceItem = sequence.values().iterator().next();
MatcherAssert.assertThat(sequenceItem.type(), Matchers.equalTo(Node.SCALAR));
MatcherAssert.assertThat(sequenceItem.asScalar().value(), Matchers.equalTo("scalar:with-colon"));
}
}
51 changes: 46 additions & 5 deletions src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,21 @@
*/
package com.amihaiemil.eoyaml;

import java.io.*;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

import org.apache.commons.io.IOUtils;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Ignore;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

/**
* Unit tests for {@link RtYamlInput}.
* @author Mihai Andronache ([email protected])
Expand Down Expand Up @@ -977,6 +981,43 @@ public void supportsSpringPropertyRef() throws IOException {
MatcherAssert.assertThat(pretty, Matchers.equalTo(fileContents));
}

@Test
public void shouldReadKeysProperly() throws IOException {
final String filename = "issue_517_values_with_colons.yml";

final YamlMapping read = new RtYamlInput(
new FileInputStream("src/test/resources/" + filename)
).readYamlMapping();

MatcherAssert.assertThat(read.type(), Matchers.equalTo(Node.MAPPING));
MatcherAssert.assertThat(
read.asMapping().keys(),
Matchers.hasSize(2));

final YamlNode topLevelMapping = read.asMapping().value("a_mapping");
MatcherAssert.assertThat(
topLevelMapping.type(),
Matchers.equalTo(Node.MAPPING));
MatcherAssert.assertThat(
topLevelMapping.asMapping().value("a_scalar").type(),
Matchers.equalTo(Node.SCALAR));
MatcherAssert.assertThat(
topLevelMapping.asMapping().value("a_scalar").asScalar().value(),
Matchers.equalTo("value:with-colon"));

YamlNode topLevelSequence = read.asMapping().value("a_sequence");
MatcherAssert.assertThat(
topLevelSequence.type(),
Matchers.equalTo(Node.SEQUENCE));
MatcherAssert.assertThat(topLevelSequence.asSequence().values(),
Matchers.hasSize(1));
YamlNode sequenceItem = topLevelSequence.asSequence().values().iterator().next();
MatcherAssert.assertThat(sequenceItem.type(),
Matchers.equalTo(Node.SCALAR));
MatcherAssert.assertThat(sequenceItem.asScalar().value(),
Matchers.equalTo("value:with-colon"));
}

/**
* Read a test resource file's contents.
* @param fileName File to read.
Expand Down
5 changes: 5 additions & 0 deletions src/test/resources/issue_517_values_with_colons.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
a_mapping:
a_scalar: value:with-colon

a_sequence:
- value:with-colon

0 comments on commit b612e8e

Please sign in to comment.