diff --git a/src/main/java/com/amihaiemil/eoyaml/YamlPrintVisitor.java b/src/main/java/com/amihaiemil/eoyaml/YamlPrintVisitor.java index d1194ec8..38134907 100644 --- a/src/main/java/com/amihaiemil/eoyaml/YamlPrintVisitor.java +++ b/src/main/java/com/amihaiemil/eoyaml/YamlPrintVisitor.java @@ -28,6 +28,7 @@ package com.amihaiemil.eoyaml; import java.io.StringWriter; +import java.util.Arrays; import java.util.List; /** @@ -358,8 +359,7 @@ public String value() { } boolean alreadyEscaped = (toEscape.startsWith("'") && toEscape.endsWith("'")) || (toEscape.startsWith("\"") && toEscape.endsWith("\"")); - final String needsEscaping = ".*[?\\-#:>|$%&{}\\[\\]@`!*,'\"]+.*|[ ]+|null"; - if (!alreadyEscaped && toEscape.matches(needsEscaping)) { + if (!alreadyEscaped && this.needsEscaping(toEscape)) { if(toEscape.contains("\"")) { toEscape = "'" + toEscape + "'"; } else { @@ -373,5 +373,30 @@ public String value() { public Comment comment() { return this.original.comment(); } + + /** + * Checks if a value (String scalar) needs escaping or not. + * @param value Value to check. + * @return True if it needs to be escaped, false otherwise. + * @checkstyle ReturnCount (100 lines) + */ + public boolean needsEscaping(final String value) { + final String flowMap = "^\\{.*\\}$"; + final String flowSequence = "^\\[.*\\]$"; + final String blockSequence = "[ ]*\\-+.*"; + final String isNullRef = "null"; + final String justSpaces = "[ ]+"; + final String otherSpecialChars = ".*[?#:>|%&@`!*,'\"]+.*"; + final List cases = Arrays.asList( + flowMap, flowSequence, blockSequence, + isNullRef, justSpaces, otherSpecialChars + ); + for(final String regex : cases) { + if(value.matches(regex)) { + return true; + } + } + return false; + } } } diff --git a/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java b/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java index b7b9a71e..1c16dc4e 100644 --- a/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java +++ b/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java @@ -735,20 +735,20 @@ public void quotedKeysMinTest() throws IOException { MatcherAssert.assertThat(read.type(), Matchers.equalTo(Node.MAPPING)); MatcherAssert.assertThat( - read.asMapping().keys().size(), - Matchers.equalTo(1)); + read.asMapping().keys().size(), + Matchers.equalTo(1)); final YamlNode topLevelMapping = read.asMapping().value("a_mapping"); MatcherAssert.assertThat( - topLevelMapping.type(), - Matchers.equalTo(Node.MAPPING)); + topLevelMapping.type(), + Matchers.equalTo(Node.MAPPING)); MatcherAssert.assertThat( - topLevelMapping.asMapping().keys().size(), - Matchers.equalTo(1)); - - final String pretty = read.toString().trim(); - - MatcherAssert.assertThat(pretty, Matchers.equalTo(fileContents)); + topLevelMapping.asMapping().keys().size(), + Matchers.equalTo(1)); + MatcherAssert.assertThat( + topLevelMapping.asMapping().string("quoted-key"), + Matchers.equalTo("val0") + ); } diff --git a/src/test/java/com/amihaiemil/eoyaml/RtYamlMappingTest.java b/src/test/java/com/amihaiemil/eoyaml/RtYamlMappingTest.java index a1816812..7ed636a1 100644 --- a/src/test/java/com/amihaiemil/eoyaml/RtYamlMappingTest.java +++ b/src/test/java/com/amihaiemil/eoyaml/RtYamlMappingTest.java @@ -399,6 +399,7 @@ public void prettyPrintsSimpleYaml() throws Exception { .build() ) .add("name", "eo-yaml") + .add("conditions", "- none") .build(); System.out.print(yaml.toString()); String expected = this.readTestResource("simpleMapping.yml"); diff --git a/src/test/java/com/amihaiemil/eoyaml/YamlMappingPrintTest.java b/src/test/java/com/amihaiemil/eoyaml/YamlMappingPrintTest.java index dd8494f0..c011dba1 100644 --- a/src/test/java/com/amihaiemil/eoyaml/YamlMappingPrintTest.java +++ b/src/test/java/com/amihaiemil/eoyaml/YamlMappingPrintTest.java @@ -312,7 +312,7 @@ public void printsEscapedScalars() { .append("time: \"15:00\"").append(System.lineSeparator()) .append("color: \"#314132\"").append(System.lineSeparator()) .append("gt: \">\"").append(System.lineSeparator()) - .append("cash: \"$15\"").append(System.lineSeparator()) + .append("cash: $15").append(System.lineSeparator()) .append("compare: \"a>b\"").append(System.lineSeparator()) .append("xor: \"a || b\"").append(System.lineSeparator()) .append("degrees: \"-15C\"").append(System.lineSeparator()) diff --git a/src/test/java/com/amihaiemil/eoyaml/YamlSequencePrintTest.java b/src/test/java/com/amihaiemil/eoyaml/YamlSequencePrintTest.java index 0cc21037..305eef72 100644 --- a/src/test/java/com/amihaiemil/eoyaml/YamlSequencePrintTest.java +++ b/src/test/java/com/amihaiemil/eoyaml/YamlSequencePrintTest.java @@ -281,7 +281,7 @@ public void printsEscapedScalars() { .append("- \"15:00\"").append(System.lineSeparator()) .append("- \"#314132\"").append(System.lineSeparator()) .append("- \">\"").append(System.lineSeparator()) - .append("- \"$15\"").append(System.lineSeparator()) + .append("- $15").append(System.lineSeparator()) .append("- \"a>b\"").append(System.lineSeparator()) .append("- \"a || b\"").append(System.lineSeparator()) .append("- \"-15C\"").append(System.lineSeparator()) diff --git a/src/test/resources/commentedMapping.yml b/src/test/resources/commentedMapping.yml index 4a832afa..5528bc77 100644 --- a/src/test/resources/commentedMapping.yml +++ b/src/test/resources/commentedMapping.yml @@ -6,4 +6,4 @@ developers: - rultor - salikjan - sherif -name: "eo-yaml" # name of the project \ No newline at end of file +name: eo-yaml # name of the project \ No newline at end of file diff --git a/src/test/resources/complexMapping.yml b/src/test/resources/complexMapping.yml index c0831a8e..507d6720 100644 --- a/src/test/resources/complexMapping.yml +++ b/src/test/resources/complexMapping.yml @@ -2,11 +2,11 @@ - Atlanta Braves - New York Yankees : - - "2001-07-02" - - "2001-08-12" - - "2001-08-14" + - 2001-07-02 + - 2001-08-12 + - 2001-08-14 ? - Chicago cubs - Detroit Tigers : - - "2001-07-23" \ No newline at end of file + - 2001-07-23 \ No newline at end of file diff --git a/src/test/resources/mappingWithDocumentComment.yml b/src/test/resources/mappingWithDocumentComment.yml index e9258b07..e13e67a3 100644 --- a/src/test/resources/mappingWithDocumentComment.yml +++ b/src/test/resources/mappingWithDocumentComment.yml @@ -10,4 +10,4 @@ developers: - rultor - salikjan - sherif -name: "eo-yaml" # name of the project \ No newline at end of file +name: eo-yaml # name of the project \ No newline at end of file diff --git a/src/test/resources/printing_tests/yamlMappingAllNodes.txt b/src/test/resources/printing_tests/yamlMappingAllNodes.txt index 3456b3ce..149a6b1b 100644 --- a/src/test/resources/printing_tests/yamlMappingAllNodes.txt +++ b/src/test/resources/printing_tests/yamlMappingAllNodes.txt @@ -19,9 +19,9 @@ key7: [] - Atlanta Braves - New York Yankees : - - "2001-07-02" - - "2001-08-12" - - "2001-08-14" + - 2001-07-02 + - 2001-08-12 + - 2001-08-14 ? map: asKey : scalar \ No newline at end of file diff --git a/src/test/resources/printing_tests/yamlMappingIndentedComments.yml b/src/test/resources/printing_tests/yamlMappingIndentedComments.yml index 50f49ff7..bdf4b4bb 100644 --- a/src/test/resources/printing_tests/yamlMappingIndentedComments.yml +++ b/src/test/resources/printing_tests/yamlMappingIndentedComments.yml @@ -1,4 +1,4 @@ -name: "eo-yaml" +name: eo-yaml contributors: # Developers here developers: diff --git a/src/test/resources/quotedKeysMax.yml b/src/test/resources/quotedKeysMax.yml index 4a70003e..bb108c91 100644 --- a/src/test/resources/quotedKeysMax.yml +++ b/src/test/resources/quotedKeysMax.yml @@ -1,10 +1,10 @@ a_mapping: - "of-quoted": val0 - "also-quoted": val1 + of-quoted: val0 + also-quoted: val1 and_unquoted: val2 - "to-introduce-leading-spaces": + to-introduce-leading-spaces: key0: 1.0 - "and-quotes": + and-quotes: key1: 2.0 and_unquoted2: key2: 3.0 diff --git a/src/test/resources/scalarCommentsInMapping.yml b/src/test/resources/scalarCommentsInMapping.yml index b570f4e3..890d86db 100644 --- a/src/test/resources/scalarCommentsInMapping.yml +++ b/src/test/resources/scalarCommentsInMapping.yml @@ -5,7 +5,7 @@ developers: - rultor - salikjan - sherif -name: "eo-yaml" # name of the project +name: eo-yaml # name of the project # git web service provider: github devops: rultor diff --git a/src/test/resources/simpleMapping.yml b/src/test/resources/simpleMapping.yml index 379615e2..4c1a79a1 100644 --- a/src/test/resources/simpleMapping.yml +++ b/src/test/resources/simpleMapping.yml @@ -3,4 +3,5 @@ developers: - rultor - salikjan - sherif -name: "eo-yaml" \ No newline at end of file +name: eo-yaml +conditions: "- none" \ No newline at end of file diff --git a/src/test/resources/streamMixed.yml b/src/test/resources/streamMixed.yml index 415449e5..ff6ebdc3 100644 --- a/src/test/resources/streamMixed.yml +++ b/src/test/resources/streamMixed.yml @@ -4,7 +4,7 @@ - rultor - salikjan - sherif - name: "eo-yaml" + name: eo-yaml --- - yegor - paolo @@ -12,4 +12,4 @@ --- architect: felicia developer: sara - name: "docker-java-api" \ No newline at end of file + name: docker-java-api \ No newline at end of file diff --git a/src/test/resources/streamOfMappings.yml b/src/test/resources/streamOfMappings.yml index 9e18f983..32519257 100644 --- a/src/test/resources/streamOfMappings.yml +++ b/src/test/resources/streamOfMappings.yml @@ -4,13 +4,13 @@ - rultor - salikjan - sherif - name: "eo-yaml" + name: eo-yaml --- architect: vlad developers: - andrei - name: "eo-json-impl" + name: eo-json-impl --- architect: felicia developer: sara - name: "docker-java-api" \ No newline at end of file + name: docker-java-api \ No newline at end of file diff --git a/src/test/resources/typicalSpringApplicationProperties.yml b/src/test/resources/typicalSpringApplicationProperties.yml index 79605916..a41231bf 100644 --- a/src/test/resources/typicalSpringApplicationProperties.yml +++ b/src/test/resources/typicalSpringApplicationProperties.yml @@ -19,6 +19,10 @@ spring: authorization-grant-type: authorization_code redirect-uri: ${self_xdsd_base_url}/login/oauth2/code/gitlab client-authentication-method: 'basic' + array-remains-string: "[config1, config2, config3]" + map-mains-string: "{a:b, c:d, e:f}" + null-string: "null" + other: null scope: [api, read_user] mail: host: mailhog.keycloak.svc.cluster.local diff --git a/src/test/resources/typicalSpringApplicationProperties_printed.yml b/src/test/resources/typicalSpringApplicationProperties_printed.yml index 402eb8bc..2595b1fb 100644 --- a/src/test/resources/typicalSpringApplicationProperties_printed.yml +++ b/src/test/resources/typicalSpringApplicationProperties_printed.yml @@ -3,22 +3,26 @@ logging: com: selfxdsd: DEBUG file: - name: "${self_logging_path}self-web-xdsd.log" + name: ${self_logging_path}self-web-xdsd.log spring: security: oauth2: client: registration: github: - "client-id": "${gh_client_id}" - "client-secret": "${gh_client_secret}" + client-id: ${gh_client_id} + client-secret: ${gh_client_secret} scope: repo gitlab: - "client-id": "${gl_client_id}" - "client-secret": "${gl_client_secret}" - "authorization-grant-type": authorization_code - "redirect-uri": "${self_xdsd_base_url}/login/oauth2/code/gitlab" - "client-authentication-method": basic + client-id: ${gl_client_id} + client-secret: ${gl_client_secret} + authorization-grant-type: authorization_code + redirect-uri: ${self_xdsd_base_url}/login/oauth2/code/gitlab + client-authentication-method: basic + array-remains-string: "[config1, config2, config3]" + map-mains-string: "{a:b, c:d, e:f}" + null-string: "null" + other: null scope: - api - read_user @@ -29,4 +33,4 @@ spring: kubernetes: enabled: true leader: - "config-map-name": "self-leader-election" \ No newline at end of file + config-map-name: self-leader-election \ No newline at end of file