Skip to content

Commit

Permalink
NestedReadYamlNode with some failed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Apr 28, 2017
1 parent ffbd6e9 commit 5087599
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 10 deletions.
42 changes: 42 additions & 0 deletions src/main/java/com/amihaiemil/camel/Nested.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright (c) 2016-2017, Mihai Emil Andronache
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
package com.amihaiemil.camel;

/**
* Nested Yaml types.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 1.0.2
*/
class Nested {
static final String YAML = ":";
static final String WRAPPED_SEQUENCE = "-";
static final String POINTED_SCALAR = ">";
static final String PIPED_SCALAR = "|";
static final String KEY_YAML = "?";
}
41 changes: 39 additions & 2 deletions src/main/java/com/amihaiemil/camel/NestedReadYamlNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public String indent(final int indentation) {
/**
* Turn the given lines into an appropriate YamlNode, based
* on the last character(s) of the previous line.
* @todo #105:30m/DEV This method needs to be implemented. It will be
* @todo #107:30m/DEV This method needs to be implemented. It will be
* similar to former AbstractYamlLines.toYamlNode(), but it will also
* cover the wrapper sequence/scalar cases. When this method is ready,
* the tests from {@link NestedReadYamlNodeTest} should be enabled.
Expand All @@ -81,7 +81,44 @@ public String indent(final int indentation) {
private static YamlNode linesToNode(
final YamlLine prev, final AbstractYamlLines lines
) {
return null;
final String trimmed = prev.trimmed();
final String last = trimmed.substring(trimmed.length()-1);
final YamlNode node;
switch (last) {
case Nested.YAML:
final boolean sequence = lines.iterator()
.next().trimmed().startsWith("-");
if(sequence) {
node = new ReadYamlSequence(lines);
} else {
node = new ReadYamlMapping(lines);
}
break;
case Nested.KEY_YAML:
final boolean sequenceKey = lines.iterator()
.next().trimmed().startsWith("-");
if(sequenceKey) {
node = new ReadYamlSequence(lines);
} else {
node = new ReadYamlMapping(lines);
}
break;
case Nested.WRAPPED_SEQUENCE:
node = new ReadYamlSequence(lines);
break;
case Nested.PIPED_SCALAR:
node = new ReadPipeScalar(lines);
break;
case Nested.POINTED_SCALAR:
node = new ReadPointedScalar(lines);
break;
default:
throw new IllegalStateException(
"No nested Yaml node after line " + prev.number() +
" which has [" + last + "] character at the end"
);
}
return node;
}

}
4 changes: 3 additions & 1 deletion src/main/java/com/amihaiemil/camel/OrderedYamlLines.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ public Iterator<YamlLine> iterator() {
final YamlLine line = lines.next();
if("-".equals(line.trimmed())) {
nodesInSequence.put(
this.nested(line.number()).toYamlNode(),
new NestedReadYamlNode(
line, this.nested(line.number())
),
index
);
dashes.add(line);
Expand Down
21 changes: 15 additions & 6 deletions src/main/java/com/amihaiemil/camel/ReadYamlMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ public Collection<YamlNode> children() {
continue;
} else {
if(trimmed.endsWith(":")) {
kids.add(ordered.nested(line.number()).toYamlNode());
kids.add(
new NestedReadYamlNode(
line, this.lines.nested(line.number())
)
);
} else {
final String[] parts = trimmed.split(":");
if(parts.length < 2) {
Expand Down Expand Up @@ -125,8 +129,9 @@ public String string(final YamlNode key) {
for (final YamlLine line : this.lines) {
final String trimmed = line.trimmed();
if("?".equals(trimmed)) {
YamlNode keyNode = this.lines.nested(line.number())
.toYamlNode();
final YamlNode keyNode = new NestedReadYamlNode(
line, this.lines.nested(line.number())
);
if(keyNode.equals(key)) {
found = true;
continue;
Expand Down Expand Up @@ -186,8 +191,8 @@ private YamlNode nodeValue(final YamlNode key, final boolean map) {
for (final YamlLine line : this.lines) {
final String trimmed = line.trimmed();
if("?".equals(trimmed)) {
AbstractYamlLines keyLines = this.lines.nested(line.number());
YamlNode keyNode = keyLines.toYamlNode();
final AbstractYamlLines keyLines = this.lines.nested(line.number());
final YamlNode keyNode = new NestedReadYamlNode(line, keyLines);
if(keyNode.equals(key)) {
int colonLine = line.number() + keyLines.count() + 1;
if (map) {
Expand All @@ -213,7 +218,11 @@ Set<YamlNode> keys() {
if(":".equals(trimmed)) {
continue;
} else if ("?".equals(trimmed)) {
keys.add(this.lines.nested(line.number()).toYamlNode());
keys.add(
new NestedReadYamlNode(
line, this.lines.nested(line.number())
)
);
} else {
final String[] parts = trimmed.split(":");
if(parts.length < 2 && !trimmed.endsWith(":")) {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/amihaiemil/camel/ReadYamlSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ public Collection<YamlNode> children() {
final AbstractYamlLines ordered = new OrderedYamlLines(this.lines);
for(final YamlLine line : ordered) {
if("-".equals(line.trimmed())) {
kids.add(this.lines.nested(line.number()).toYamlNode());
kids.add(
new NestedReadYamlNode(
line, this.lines.nested(line.number())
)
);
} else {
final String trimmed = line.trimmed();
kids.add(
Expand Down

0 comments on commit 5087599

Please sign in to comment.