Repository: decorators-squad/eo-yaml
Pull request #620 (2024-04-13 06:00:10)

#605 Implemented printing as flow for read flow nodes
PR for #605 
Flow mappings and sequences read from somewhere will be printed as such. 



Patch:
diff --git a/src/main/java/com/amihaiemil/eoyaml/ReadFlowMapping.java b/src/main/java/com/amihaiemil/eoyaml/ReadFlowMapping.java
index 26fdd10..6bc75c7 100644
--- a/src/main/java/com/amihaiemil/eoyaml/ReadFlowMapping.java
+++ b/src/main/java/com/amihaiemil/eoyaml/ReadFlowMapping.java
@@ -41,8 +41,6 @@ import java.util.Set;
  * @author Mihai Andronache (amihaiemil@gmail.com)
  * @version $Id$
  * @since 8.0.0
- * @todo #601:30min Modify the yaml printing visitor, so it prints the flow
- *  nodes in Flow style instead of block.
  * @todo #615:60min Implement the comment() method properly (at the moment
  *  it always assumes there is no comment).
  */
diff --git a/src/main/java/com/amihaiemil/eoyaml/YamlPrintVisitor.java b/src/main/java/com/amihaiemil/eoyaml/YamlPrintVisitor.java
index 3813490..71e206a 100644
--- a/src/main/java/com/amihaiemil/eoyaml/YamlPrintVisitor.java
+++ b/src/main/java/com/amihaiemil/eoyaml/YamlPrintVisitor.java
@@ -29,7 +29,9 @@ package com.amihaiemil.eoyaml;
 
 import java.io.StringWriter;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.List;
+import java.util.Set;
 
 /**
  * Visitor which prints the YAML in block format.
@@ -85,78 +87,43 @@ final class YamlPrintVisitor implements YamlVisitor<String> {
     @Override
     public String visitYamlMapping(final YamlMapping node) {
         final StringWriter writer = new StringWriter();
-        for (final YamlNode key : node.keys()) {
-            final YamlNode value = node.value(key);
-            writer.append(this.printPossibleComment(value));
-            if(key.type().equals(Node.SCALAR)) {
-                writer.append(this.visitYamlNode(key));
+        final String printed;
+        if(node instanceof ReadFlowMapping) {
+            this.printFlowMapping(node, writer);
+            printed = writer.toString();
+        } else {
+            this.printBlockMapping(node, writer);
+            final String printedMapping = writer.toString();
+            if (printedMapping.length() > 0)  {
+                printed = printedMapping.substring(
+                    0, printedMapping.length() - 1
+                );
             } else {
-                writer.append("?").append(this.lineSeparator);
-                final String printedValue = this.visitYamlNode(key);
-                writer.append(this.indent(printedValue, this.indentation));
-                writer.append(this.lineSeparator);
+                printed = printedMapping;
             }
-            if(value == null || value.type().equals(Node.SCALAR)) {
-                writer.append(": ");
-                writer.append(this.visitYamlNode(value));
-            } else {
-                final String printedValue = this.visitYamlNode(value);
-                if("null".equals(printedValue) || "[]".equals(printedValue)
-                    || "{}".equals(printedValue)
-                ) {
-                    writer.append(": ");
-                    writer.append(printedValue);
-                } else {
-                    writer.append(":");
-                    writer.append(this.lineSeparator);
-                    writer.append(this.indent(printedValue, this.indentation));
-                }
-            }
-            writer.append(this.lineSeparator);
         }
-        final String printedMapping = writer.toString();
-        final String trimmed;
-        if (printedMapping.length() > 0)  {
-            trimmed = printedMapping.substring(0, printedMapping.length() - 1);
-        } else {
-            trimmed = printedMapping;
-        }
-        return trimmed;
+        return printed;
     }
 
     @Override
     public String visitYamlSequence(final YamlSequence node) {
         final StringWriter writer = new StringWriter();
-        for(final YamlNode value : node.values()) {
-            writer.append(this.printPossibleComment(value));
-            if(value == null || value.type().equals(Node.SCALAR)) {
-                writer.append("- ");
-                writer.append(this.visitYamlNode(value));
+        final String printed;
+        if(node instanceof ReadFlowSequence) {
+            this.printFlowSequence(node, writer);
+            printed = writer.toString();
+        } else {
+            this.printBlockSequence(node, writer);
+            final String printedSequence = writer.toString();
+            if (printedSequence.length() > 0)  {
+                printed = printedSequence.substring(
+                    0, printedSequence.length() - 1
+                );
             } else {
-                final String printedValue = this.visitYamlNode(value);
-                if("null".equals(printedValue) || "[]".equals(printedValue)
-                    || "{}".equals(printedValue)
-                ) {
-                    writer.append("- ");
-                    writer.append(printedValue);
-                } else {
-                    writer.append("-");
-                    writer.append(this.lineSeparator);
-                    writer.append(this.indent(printedValue, this.indentation));
-                }
+                printed = printedSequence;
             }
-            writer.append(this.lineSeparator);
-        }
-        final String printedSequence = writer.toString();
-        final String trimmed;
-        if (printedSequence.length() > 0)  {
-            trimmed = printedSequence.substring(
-                0, printedSequence.length() - 1
-            );
-        } else {
-            trimmed = printedSequence;
         }
-        return trimmed;
+        return printed;
     }
 
     @Override
@@ -326,6 +293,120 @@ final class YamlPrintVisitor implements YamlVisitor<String> {
         return writer.toString();
     }
 
+    /**
+     * Write a block mapping to the given StringWriter.
+     * @param node Block YamlMapping to print.
+     * @param writer String writer.
+     */
+    private void printBlockMapping(
+        final YamlMapping node, final StringWriter writer
+    ) {
+        for (final YamlNode key : node.keys()) {
+            final YamlNode value = node.value(key);
+            writer.append(this.printPossibleComment(value));
+            if(key.type().equals(Node.SCALAR)) {
+                writer.append(this.visitYamlNode(key));
+            } else {
+                writer.append("?").append(this.lineSeparator);
+                final String printedValue = this.visitYamlNode(key);
+                writer.append(this.indent(printedValue, this.indentation));
+                writer.append(this.lineSeparator);
+            }
+            if(value == null || value.type().equals(Node.SCALAR)) {
+                writer.append(": ");
+                writer.append(this.visitYamlNode(value));
+            } else {
+                final String printedValue = this.visitYamlNode(value);
+                if("null".equals(printedValue) || printedValue.startsWith("[")
+                    || printedValue.startsWith("{")
+                ) {
+                    writer.append(": ");
+                    writer.append(printedValue);
+                } else {
+                    writer.append(":");
+                    writer.append(this.lineSeparator);
+                    writer.append(this.indent(printedValue, this.indentation));
+                }
+            }
+            writer.append(this.lineSeparator);
+        }
+    }
+
+    /**
+     * Write a flow mapping to the given StringWriter.
+     * @param node Flow YamlMapping to print.
+     * @param writer String writer.
+     */
+    private void printFlowMapping(
+        final YamlMapping node, final StringWriter writer
+    ) {
+        writer.append("{");
+        int i=0;
+        final Set<YamlNode> keys = node.keys();
+        for (final YamlNode key : keys) {
+            final YamlNode value = node.value(key);
+            writer.append(this.visitYamlNode(key));
+            writer.append(": ");
+            writer.append(this.visitYamlNode(value));
+            i++;
+            if(i<keys.size()) {
+                writer.append(", ");
+            }
+        }
+        writer.append("}");
+    }
+
+    /**
+     * Write a block sequence to the given StringWriter.
+     * @param node Block YamlSequence to print.
+     * @param writer String writer.
+     */
+    private void printBlockSequence(
+        final YamlSequence node, final StringWriter writer
+    ) {
+        for(final YamlNode value : node.values()) {
+            writer.append(this.printPossibleComment(value));
+            if(value == null || value.type().equals(Node.SCALAR)) {
+                writer.append("- ");
+                writer.append(this.visitYamlNode(value));
+            } else {
+                final String printedValue = this.visitYamlNode(value);
+                if("null".equals(printedValue) || printedValue.startsWith("[")
+                    || printedValue.startsWith("{")
+                ) {
+                    writer.append("- ");
+                    writer.append(printedValue);
+                } else {
+                    writer.append("-");
+                    writer.append(this.lineSeparator);
+                    writer.append(this.indent(printedValue, this.indentation));
+                }
+            }
+            writer.append(this.lineSeparator);
+        }
+    }
+
+    /**
+     * Write a flow sequence to the given StringWriter.
+     * @param node Flow YamlSequence to print.
+     * @param writer String writer.
+     */
+    private void printFlowSequence(
+        final YamlSequence node, final StringWriter writer
+    ) {
+        writer.append("[");
+        int i=0;
+        final Collection<YamlNode> values = node.values();
+        for (final YamlNode value : values) {
+            writer.append(this.visitYamlNode(value));
+            i++;
+            if(i<values.size()) {
+                writer.append(", ");
+            }
+        }
+        writer.append("]");
+    }
+
     /**
      * A scalar which escapes its value.
      * @author Mihai Andronache (amihaiemil@gmail.com)


Test patch:
diff --git a/src/test/java/com/amihaiemil/eoyaml/ReadFlowMappingTestCase.java b/src/test/java/com/amihaiemil/eoyaml/ReadFlowMappingTestCase.java
index 0be460c..f19c9fc 100644
--- a/src/test/java/com/amihaiemil/eoyaml/ReadFlowMappingTestCase.java
+++ b/src/test/java/com/amihaiemil/eoyaml/ReadFlowMappingTestCase.java
@@ -89,11 +89,11 @@ public final class ReadFlowMappingTestCase {
         final Iterator<YamlNode> iterator = keys.iterator();
         MatcherAssert.assertThat(
             iterator.next().toString(),
-            Matchers.equalTo("- a")
+            Matchers.equalTo("[a]")
         );
         MatcherAssert.assertThat(
             iterator.next().toString(),
-            Matchers.equalTo("- c" + System.lineSeparator() + "- g")
+            Matchers.equalTo("[c, g]")
         );
         MatcherAssert.assertThat(
             iterator.next().toString(),
@@ -107,7 +107,7 @@ public final class ReadFlowMappingTestCase {
         );
         MatcherAssert.assertThat(
             iterator.next().toString(),
-            Matchers.equalTo("y: r")
+            Matchers.equalTo("{y: r}")
         );
         MatcherAssert.assertThat(
             iterator.next().toString(),
@@ -197,7 +197,7 @@ public final class ReadFlowMappingTestCase {
         );
         MatcherAssert.assertThat(
             iterator.next().toString(),
-            Matchers.equalTo("h: i")
+            Matchers.equalTo("{h: i}")
         );
         MatcherAssert.assertThat(
             iterator.next().toString(),
@@ -212,19 +212,7 @@ public final class ReadFlowMappingTestCase {
         MatcherAssert.assertThat(
             iterator.next().toString(),
             Matchers.equalTo(
-                "- a"
-                + System.lineSeparator()
-                + "- \"0,3\""
-                + System.lineSeparator()
-                + "- \"2, 3, 4\""
-                + System.lineSeparator()
-                + "-"
-                + System.lineSeparator()
-                + "  ii: \"5,6,7\""
-                + System.lineSeparator()
-                + "- b"
-                + System.lineSeparator()
-                + "- c"
+                "[a, \"0,3\", \"2, 3, 4\", {ii: \"5,6,7\"}, b, c]"
             )
         );
         MatcherAssert.assertThat(
diff --git a/src/test/resources/typicalSpringApplicationProperties.yml b/src/test/resources/typicalSpringApplicationProperties.yml
index a41231b..4714b96 100644
--- a/src/test/resources/typicalSpringApplicationProperties.yml
+++ b/src/test/resources/typicalSpringApplicationProperties.yml
@@ -27,6 +27,10 @@ spring:
   mail:
     host: mailhog.keycloak.svc.cluster.local
     port: 1025
+    users: {
+      admin: mihai,
+      other: vlad
+    }
   cloud:
     kubernetes:
       enabled: true
diff --git a/src/test/resources/typicalSpringApplicationProperties_printed.yml b/src/test/resources/typicalSpringApplicationProperties_printed.yml
index 2595b1f..aaba074 100644
--- a/src/test/resources/typicalSpringApplicationProperties_printed.yml
+++ b/src/test/resources/typicalSpringApplicationProperties_printed.yml
@@ -23,12 +23,11 @@ spring:
             map-mains-string: "{a:b, c:d, e:f}"
             null-string: "null"
             other: null
-            scope:
-              - api
-              - read_user
+            scope: [api, read_user]
   mail:
     host: mailhog.keycloak.svc.cluster.local
     port: 1025
+    users: {admin: mihai, other: vlad}
   cloud:
     kubernetes:
       enabled: true