Skip to content

Commit

Permalink
Improved escaping conditions at printing
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Apr 13, 2024
1 parent ac4b99a commit f790c3c
Show file tree
Hide file tree
Showing 17 changed files with 79 additions and 44 deletions.
29 changes: 27 additions & 2 deletions src/main/java/com/amihaiemil/eoyaml/YamlPrintVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
package com.amihaiemil.eoyaml;

import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;

/**
Expand Down Expand Up @@ -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 {
Expand All @@ -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<String> cases = Arrays.asList(
flowMap, flowSequence, blockSequence,
isNullRef, justSpaces, otherSpecialChars
);
for(final String regex : cases) {
if(value.matches(regex)) {
return true;
}
}
return false;
}
}
}
20 changes: 10 additions & 10 deletions src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
);
}


Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/amihaiemil/eoyaml/RtYamlMappingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public void printsEscapedScalars() {
.append("time: \"15:00\"").append(System.lineSeparator())
.append("color: \"#314132\"").append(System.lineSeparator())
.append("gt: \"&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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public void printsEscapedScalars() {
.append("- \"15:00\"").append(System.lineSeparator())
.append("- \"#314132\"").append(System.lineSeparator())
.append("- \"&gt;\"").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())
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/commentedMapping.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ developers:
- rultor
- salikjan
- sherif
name: "eo-yaml" # name of the project
name: eo-yaml # name of the project
8 changes: 4 additions & 4 deletions src/test/resources/complexMapping.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
- 2001-07-23
2 changes: 1 addition & 1 deletion src/test/resources/mappingWithDocumentComment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ developers:
- rultor
- salikjan
- sherif
name: "eo-yaml" # name of the project
name: eo-yaml # name of the project
6 changes: 3 additions & 3 deletions src/test/resources/printing_tests/yamlMappingAllNodes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: "eo-yaml"
name: eo-yaml
contributors:
# Developers here
developers:
Expand Down
8 changes: 4 additions & 4 deletions src/test/resources/quotedKeysMax.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/test/resources/scalarCommentsInMapping.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/test/resources/simpleMapping.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ developers:
- rultor
- salikjan
- sherif
name: "eo-yaml"
name: eo-yaml
conditions: "- none"
4 changes: 2 additions & 2 deletions src/test/resources/streamMixed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
- rultor
- salikjan
- sherif
name: "eo-yaml"
name: eo-yaml
---
- yegor
- paolo
- cesar
---
architect: felicia
developer: sara
name: "docker-java-api"
name: docker-java-api
6 changes: 3 additions & 3 deletions src/test/resources/streamOfMappings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
name: docker-java-api
4 changes: 4 additions & 0 deletions src/test/resources/typicalSpringApplicationProperties.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 13 additions & 9 deletions src/test/resources/typicalSpringApplicationProperties_printed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,4 +33,4 @@ spring:
kubernetes:
enabled: true
leader:
"config-map-name": "self-leader-election"
config-map-name: self-leader-election

0 comments on commit f790c3c

Please sign in to comment.