Repository: decorators-squad/eo-yaml (java)

Issue:
When Reading a Scalar string, remove the escaping apostrophes or quotes
At the moment, calling ``mapping.string("color")``  for the following YAML

```yaml
color: '#404040'
```

returns ``'#404040'``. It should return simply ``#404040``, without the apostrophes/quotes used in escaping.

Pull request:
unescaping read scalar
fixes #199 

Patch:
diff --git a/src/main/java/com/amihaiemil/eoyaml/ReadPlainScalarValue.java b/src/main/java/com/amihaiemil/eoyaml/ReadPlainScalarValue.java
index 2cddb49..67b14be 100644
--- a/src/main/java/com/amihaiemil/eoyaml/ReadPlainScalarValue.java
+++ b/src/main/java/com/amihaiemil/eoyaml/ReadPlainScalarValue.java
@@ -67,7 +67,28 @@ final class ReadPlainScalarValue extends ComparableScalar {
                 + "Instead, the line is: [" + this.line.trimmed() + "]."
             );
         }
-        return value;
+        return this.unescape(value);
     }
 
+    /**
+     * Remove the possible escaping quotes or apostrophes surrounding the
+     * given value.
+     * @param value The value to unescape.
+     * @return The value without quotes or apostrophes.
+     */
+    private String unescape(final String value) {
+        final String unescaped;
+        if(value == null || value.length()<=2) {
+            unescaped = value;
+        } else {
+            if (value.startsWith("\"") && value.endsWith("\"")) {
+                unescaped = value.substring(1, value.length() - 1);
+            } else if (value.startsWith("'") && value.endsWith("'")) {
+                unescaped = value.substring(1, value.length() - 1);
+            } else {
+                unescaped = value;
+            }
+        }
+        return unescaped;
+    }
 }


Test patch:
diff --git a/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java b/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java
index c918e60..cbbb058 100644
--- a/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java
@@ -94,7 +94,7 @@ public final class RtYamlInputTest {
             read, Matchers.equalTo(expected)
         );
     }
-    
+
     /**
      * RtYamlnput can read and indent a complex mapping.
      * @throws Exception If something goes wrong.
@@ -132,7 +132,7 @@ public final class RtYamlInputTest {
         ).readYamlMapping();
         MatcherAssert.assertThat(expected, Matchers.equalTo(actual));
     }
-    
+
     /**
      * RtYamlnput can read and indent a complex sequence.
      * @throws Exception If something goes wrong.
@@ -188,20 +188,20 @@ public final class RtYamlInputTest {
         MatcherAssert.assertThat(
             keys.contains(new BuiltPlainScalar("workspace")),
             Matchers.is(Boolean.TRUE)
-        );        
-        
+        );
+
         final YamlMapping palette = mapping.yamlMapping("palette");
         MatcherAssert.assertThat(palette.keys().size(), Matchers.is(12));
         MatcherAssert.assertThat(palette.values().size(), Matchers.is(12));
         MatcherAssert.assertThat(
-            palette.string("gray"),
-            Matchers.is("'#404040'")
+            palette.string("light_gray"),
+            Matchers.is("#484848")
         );
         MatcherAssert.assertThat(
             palette.string("magenta"),
             Matchers.is("rgb(255, 102, 255)")
         );
-        
+
         final YamlMapping workspace = mapping.yamlMapping("workspace");
         MatcherAssert.assertThat(workspace.keys().size(), Matchers.is(3));
         MatcherAssert.assertThat(workspace.values().size(), Matchers.is(3));
@@ -222,7 +222,7 @@ public final class RtYamlInputTest {
             Matchers.is("stroke rgba(255, 255, 255, 0.3)")
         );
     }
-    
+
     /**
      * At the moment, when reading a sequence, we should ignore any YAML
      * Directive or Marker line, such as "%YAML 1.2" or "---" or "...".
@@ -242,19 +242,19 @@ public final class RtYamlInputTest {
         MatcherAssert.assertThat(
             first.values().size(), Matchers.equalTo(1)
         );
-        
+
         final YamlMapping palette = first.yamlMapping("palette");
         MatcherAssert.assertThat(palette.keys().size(), Matchers.is(12));
         MatcherAssert.assertThat(palette.values().size(), Matchers.is(12));
         MatcherAssert.assertThat(
             palette.string("gray"),
-            Matchers.is("'#404040'")
+            Matchers.is("#404040")
         );
         MatcherAssert.assertThat(
             palette.string("magenta"),
             Matchers.is("rgb(255, 102, 255)")
         );
-        
+
         final YamlMapping second = sequence.yamlMapping(1);
         MatcherAssert.assertThat(
             second.keys().size(), Matchers.equalTo(1)
@@ -262,7 +262,7 @@ public final class RtYamlInputTest {
         MatcherAssert.assertThat(
             second.values().size(), Matchers.equalTo(1)
         );
-        
+
         final YamlMapping workspace = second.yamlMapping("workspace");
         MatcherAssert.assertThat(workspace.keys().size(), Matchers.is(3));
         MatcherAssert.assertThat(workspace.values().size(), Matchers.is(3));
@@ -283,7 +283,7 @@ public final class RtYamlInputTest {
             Matchers.is("stroke rgba(255, 255, 255, 0.3)")
         );
     }
-    
+
     /**
      * A YamlSequence in block style can be read.
      * @throws Exception If something goes wrong.
@@ -304,7 +304,7 @@ public final class RtYamlInputTest {
 
         MatcherAssert.assertThat(read, Matchers.equalTo(expected));
     }
-    
+
     /**
      * Reading of YamlStream is not yet implemented, an exception should
      * be thrown.
diff --git a/src/test/resources/mapping_ignore_directives.yml b/src/test/resources/mapping_ignore_directives.yml
index e723d35..539d982 100644
--- a/src/test/resources/mapping_ignore_directives.yml
+++ b/src/test/resources/mapping_ignore_directives.yml
@@ -14,7 +14,7 @@ palette:
   # - Grayscale -
   black: '#303030'
   gray: '#404040'
-  light_gray: '#484848'
+  light_gray: "#484848"
   white: white
 
   # - Colors -
@@ -30,11 +30,11 @@ palette:
 workspace:
 
   background: gray
-  
+
   guides:
     lines: stroke rgba(255, 255, 255, 0.3)
     grid: stroke rgba(255, 255, 255, 0.3)
-  
+
   elements:
     devices:
       pins:
@@ -46,4 +46,4 @@ workspace:
       wires:
         inactive: 'stroke #808080'
         active: activePin
-        creator: stroke rgba(255, 255, 255, 0.125)
\ No newline at end of file
+        creator: stroke rgba(255, 255, 255, 0.125)