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

#621 More tests #624

Merged
merged 7 commits into from
Apr 22, 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
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ Here is what we have so far:
- It can be used as a **Java Module** (if you're on JDK 9+).
- It is **lightweight**! It has 0 dependencies.

Also, you can have a look under [src/test/resources](https://github.com/decorators-squad/eo-yaml/tree/master/src/test/resources) to see the kinds of YAML that the library can read and handle so far.

Here is what we're **still missing and working on**:

* Flow and Recursive representation
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/amihaiemil/eoyaml/BaseYamlMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@
public abstract class BaseYamlMapping
extends BaseYamlNode implements YamlMapping {

/**
* Ctor.
*/
protected BaseYamlMapping() {
this(false);
}

/**
* Ctor.
* @param alwaysPrintBlock Print it as block with disregard to whether it
* came from JSON/Flow-Style or not.
*/
protected BaseYamlMapping(final boolean alwaysPrintBlock) {
super(alwaysPrintBlock);
}

@Override
public final Node type() {
return Node.MAPPING;
Expand Down
26 changes: 25 additions & 1 deletion src/main/java/com/amihaiemil/eoyaml/BaseYamlNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@
*/
abstract class BaseYamlNode implements YamlNode {

/**
* Print it as block with disregard to whether it came
* from JSON/Flow-Style or not.
*/
private final boolean alwaysPrintBlock;

/**
* Ctor.
*/
BaseYamlNode() {
this(false);
}

/**
* Ctor.
* @param alwaysPrintBlock Print it as block with disregard to whether it
* came from JSON/Flow-Style or not.
*/
BaseYamlNode(final boolean alwaysPrintBlock) {
this.alwaysPrintBlock = alwaysPrintBlock;
}

@Override
public final Scalar asScalar()
throws YamlReadingException, ClassCastException {
Expand Down Expand Up @@ -93,7 +115,9 @@ public final <T extends YamlNode> T asClass(final Class<T> clazz,
@Override
public final String toString() {
final StringWriter writer = new StringWriter();
final YamlPrinter printer = new RtYamlPrinter(writer);
final YamlPrinter printer = new RtYamlPrinter(
writer, this.alwaysPrintBlock
);
try {
printer.print(this);
return writer.toString();
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/amihaiemil/eoyaml/BaseYamlSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@
public abstract class BaseYamlSequence
extends BaseYamlNode implements YamlSequence {

/**
* Ctor.
*/
protected BaseYamlSequence() {
this(false);
}

/**
* Ctor.
* @param alwaysPrintBlock Print it as block with disregard to whether it
* came from JSON/Flow-Style or not.
*/
protected BaseYamlSequence(final boolean alwaysPrintBlock) {
super(alwaysPrintBlock);
}

@Override
public final Node type() {
return Node.SEQUENCE;
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/amihaiemil/eoyaml/CollapsedFlowLines.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ public Iterator<YamlLine> iterator() {
collapsed.add(line);
}
}

if(bracketCount != 0) {
throw new IllegalStateException(
"Flow YamlNode starting at line "
+ this.lines.line(0).number()
+ " not closed. "
+"Closing bracket " + this.closingBracket + " not found."
);
}
return collapsed.iterator();
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/amihaiemil/eoyaml/JsonYamlMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ final class JsonYamlMapping extends BaseYamlMapping {
* @param object Json object being mapped.
*/
JsonYamlMapping(final JsonObject object) {
super(true);
this.object = object;
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/amihaiemil/eoyaml/JsonYamlSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ final class JsonYamlSequence extends BaseYamlSequence {
* @param array Json array being mapped.
*/
JsonYamlSequence(final JsonArray array) {
super(true);
this.array = array;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ public YamlMappingBuilder add(final YamlNode key, final YamlNode value) {

@Override
public YamlMapping build(final String comment) {
YamlMapping mapping = new RtYamlMapping(this.pairs, comment);
if (pairs.isEmpty()) {
mapping = new EmptyYamlMapping(mapping);
}
return mapping;
return new RtYamlMapping(this.pairs, comment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ public YamlSequenceBuilder add(final YamlNode node) {

@Override
public YamlSequence build(final String comment) {
YamlSequence sequence = new RtYamlSequence(this.nodes, comment);
if (this.nodes.isEmpty()) {
sequence = new EmptyYamlSequence(sequence);
}
return sequence;
return new RtYamlSequence(this.nodes, comment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,7 @@ public YamlMappingBuilder add(final YamlNode key, final YamlNode value) {

@Override
public YamlMapping build(final String comment) {
YamlMapping mapping = new RtYamlMapping(this.pairs, comment);
if (pairs.isEmpty()) {
mapping = new EmptyYamlMapping(mapping);
}
return mapping;
return new RtYamlMapping(this.pairs, comment);
}

}
33 changes: 31 additions & 2 deletions src/main/java/com/amihaiemil/eoyaml/RtYamlPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,26 @@ final class RtYamlPrinter implements YamlPrinter {
*/
private final String lineSeparator;

/**
* Print all the nodes in block-style.
*/
private final boolean alwaysBlock;

/**
* Constructor.
* @param writer Destination writer.
*/
RtYamlPrinter(final Writer writer) {
this(writer, System.lineSeparator());
this(writer, false);
}

/**
* Constructor.
* @param writer Destination writer.
* @param alwaysBlock Print all the nodes in block-style.
*/
RtYamlPrinter(final Writer writer, final boolean alwaysBlock) {
this(writer, System.lineSeparator(), alwaysBlock);
}

/**
Expand All @@ -63,15 +77,30 @@ final class RtYamlPrinter implements YamlPrinter {
* @param lineSeparator Line separator.
*/
RtYamlPrinter(final Writer writer, final String lineSeparator) {
this(writer, lineSeparator, false);
}

/**
* Constructor.
* @param writer Destination writer.
* @param lineSeparator Line separator.
* @param alwaysBlock Print all the nodes in block-style.
*/
RtYamlPrinter(
final Writer writer,
final String lineSeparator,
final boolean alwaysBlock
) {
this.writer = writer;
this.lineSeparator = lineSeparator;
this.alwaysBlock = alwaysBlock;
}

@Override
public void print(final YamlNode node) throws IOException {
try {
final YamlVisitor<String> visitor = new YamlPrintVisitor(
this.lineSeparator
this.lineSeparator, this.alwaysBlock
);
if (node.type().equals(Node.SCALAR)) {
this.writer.append("---")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ public YamlSequenceBuilder add(final YamlNode node) {

@Override
public YamlSequence build(final String comment) {
YamlSequence sequence = new RtYamlSequence(this.nodes, comment);
if (this.nodes.isEmpty()) {
sequence = new EmptyYamlSequence(sequence);
}
return sequence;
return new RtYamlSequence(this.nodes, comment);
}
}
41 changes: 41 additions & 0 deletions src/main/java/com/amihaiemil/eoyaml/YamlMappingBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
*/
package com.amihaiemil.eoyaml;

import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonStructure;
import java.util.Collection;

/**
Expand Down Expand Up @@ -68,6 +71,44 @@ public interface YamlMappingBuilder {
*/
YamlMappingBuilder add(final String key, final YamlNode value);

/**
* Add a pair to the mapping.
* @param key YamlNode (sequence or mapping)
* @param value JsonStructure ({@link javax.json.JsonObject}
* or {@link javax.json.JsonArray})
* @return Builder
*/
default YamlMappingBuilder add(
final YamlNode key, final JsonStructure value
) {
final YamlNode node;
if(value instanceof JsonObject) {
node = new JsonYamlMapping((JsonObject) value);
} else {
node = new JsonYamlSequence((JsonArray) value);
}
return this.add(key, node);
}

/**
* Add a pair to the mapping.
* @param key String
* @param value JsonStructure ({@link javax.json.JsonObject}
* or {@link javax.json.JsonArray})
* @return Builder
*/
default YamlMappingBuilder add(
final String key, final JsonStructure value
) {
final YamlNode node;
if(value instanceof JsonObject) {
node = new JsonYamlMapping((JsonObject) value);
} else {
node = new JsonYamlSequence((JsonArray) value);
}
return this.add(key, node);
}

/**
* Build the YamlMapping.
* @return Built YamlMapping.
Expand Down
40 changes: 21 additions & 19 deletions src/main/java/com/amihaiemil/eoyaml/YamlPrintVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,42 +53,41 @@ final class YamlPrintVisitor implements YamlVisitor<String> {
private final String lineSeparator;

/**
* Ctor. Defaults to indentation 2 and the System line separator.
* Print each node as block-style always.
*/
YamlPrintVisitor() {
this(2, System.lineSeparator());
}

/**
* Ctor.
* @param indentation Number of indentation spaces.
*/
YamlPrintVisitor(final int indentation) {
this(indentation, System.lineSeparator());
}
private final boolean alwaysBlock;

/**
* Ctor.
* @param lineSeparator Line separator.
* @param alwaysBlock Print each node as block-style always.
*/
YamlPrintVisitor(final String lineSeparator) {
this(2, lineSeparator);
YamlPrintVisitor(final String lineSeparator, final boolean alwaysBlock) {
this(2, lineSeparator, alwaysBlock);
}

/**
* Ctor.
* @param indentation Number of spaces to use for indentation.
* @param lineSeparator Line separator.
* @param alwaysBlock Print each node as block-style always.
*/
YamlPrintVisitor(final int indentation, final String lineSeparator) {
YamlPrintVisitor(
final int indentation,
final String lineSeparator,
final boolean alwaysBlock
) {
this.indentation = indentation;
this.lineSeparator = lineSeparator;
this.alwaysBlock = alwaysBlock;
}
@Override
public String visitYamlMapping(final YamlMapping node) {
final StringWriter writer = new StringWriter();
final String printed;
if(node instanceof ReadFlowMapping) {
if((node instanceof ReadFlowMapping
|| node instanceof JsonYamlMapping)
&& !this.alwaysBlock) {
this.printFlowMapping(node, writer);
printed = writer.toString();
} else {
Expand All @@ -109,7 +108,9 @@ public String visitYamlMapping(final YamlMapping node) {
public String visitYamlSequence(final YamlSequence node) {
final StringWriter writer = new StringWriter();
final String printed;
if(node instanceof ReadFlowSequence) {
if((node instanceof ReadFlowSequence
|| node instanceof JsonYamlSequence)
&& !this.alwaysBlock) {
this.printFlowSequence(node, writer);
printed = writer.toString();
} else {
Expand Down Expand Up @@ -467,10 +468,11 @@ public boolean needsEscaping(final String value) {
final String blockSequence = "[ ]*\\-+.*";
final String isNullRef = "null";
final String justSpaces = "[ ]+";
final String otherSpecialChars = ".*[?#:>|%&@`!*,'\"]+.*";
final String spaceAfterColon = "(.*:[\\s]+.*)|(^:.*$)";
final String otherSpecialChars = ".*[?#>|%&@`!*,'\"]+.*";
final List<String> cases = Arrays.asList(
flowMap, flowSequence, blockSequence,
isNullRef, justSpaces, otherSpecialChars
isNullRef, justSpaces, spaceAfterColon, otherSpecialChars
);
for(final String regex : cases) {
if(value.matches(regex)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public void canMapJsonObject(){
MatcherAssert.assertThat(
jsonMapping, Matchers.equalTo(expected)
);
System.out.println(jsonMapping);
}

/**
Expand Down
Loading
Loading