Skip to content

Commit

Permalink
A pretty flow YAML output (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr14huashao authored Aug 4, 2020
1 parent 93bb8d2 commit 61d9495
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/com/esotericsoftware/yamlbeans/YamlConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,11 @@ public void setFlowStyle(boolean flowStyle) {
public boolean isFlowStyle() {
return flowStyle;
}

/** If true, the YAML output will be pretty flow. Default is false. */
public void setPrettyFlow(boolean prettyFlow) {
emitterConfig.setPrettyFlow(prettyFlow);
}
}

static public class ReadConfig {
Expand Down
18 changes: 18 additions & 0 deletions src/com/esotericsoftware/yamlbeans/emitter/Emitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ public void expect () throws IOException {
writer.writeIndicator("]", false, false, false);
state = states.remove(0);
} else {
if (config.prettyFlow) {
writer.writeIndent(flowLevel * config.indentSize);
}
if (config.canonical || writer.column > config.wrapColumn) writer.writeIndent(indent);
states.add(0, S_FLOW_SEQUENCE_ITEM);
expectNode(false, true, false, false);
Expand All @@ -213,13 +216,19 @@ public void expect () throws IOException {
if (event.type == SEQUENCE_END) {
indent = indents.remove(0);
flowLevel--;
if (config.prettyFlow) {
writer.writeIndent(flowLevel * config.indentSize);
}
if (config.canonical) {
writer.writeIndent(indent);
}
writer.writeIndicator("]", false, false, false);
state = states.remove(0);
} else {
writer.writeIndicator(",", false, false, false);
if (config.prettyFlow) {
writer.writeIndent(flowLevel * config.indentSize);
}
if (config.canonical || writer.column > config.wrapColumn) writer.writeIndent(indent);
states.add(0, S_FLOW_SEQUENCE_ITEM);
expectNode(false, true, false, false);
Expand All @@ -234,6 +243,9 @@ public void expect () throws IOException {
writer.writeIndicator("}", false, false, false);
state = states.remove(0);
} else {
if (config.prettyFlow) {
writer.writeIndent(flowLevel * config.indentSize);
}
if (config.canonical || writer.column > config.wrapColumn) writer.writeIndent(indent);
if (!config.canonical && checkSimpleKey()) {
states.add(0, S_FLOW_MAPPING_SIMPLE_VALUE);
Expand Down Expand Up @@ -266,13 +278,19 @@ public void expect () throws IOException {
if (event.type == MAPPING_END) {
indent = indents.remove(0);
flowLevel--;
if (config.prettyFlow) {
writer.writeIndent(flowLevel * config.indentSize);
}
if (config.canonical) {
writer.writeIndent(indent);
}
writer.writeIndicator("}", false, false, false);
state = states.remove(0);
} else {
writer.writeIndicator(",", false, false, false);
if (config.prettyFlow) {
writer.writeIndent(flowLevel * config.indentSize);
}
if (config.canonical || writer.column > config.wrapColumn) writer.writeIndent(indent);
if (!config.canonical && checkSimpleKey()) {
states.add(0, S_FLOW_MAPPING_SIMPLE_VALUE);
Expand Down
6 changes: 6 additions & 0 deletions src/com/esotericsoftware/yamlbeans/emitter/EmitterConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class EmitterConfig {
int indentSize = 3;
int wrapColumn = 100;
boolean escapeUnicode = true;
boolean prettyFlow;

/** If true, the YAML output will be canonical. Default is false. */
public void setCanonical (boolean canonical) {
Expand Down Expand Up @@ -50,4 +51,9 @@ public void setUseVerbatimTags (boolean useVerbatimTags) {
public void setEscapeUnicode (boolean escapeUnicode) {
this.escapeUnicode = escapeUnicode;
}

/** If true, the YAML output will be pretty flow. Default is false. */
public void setPrettyFlow(boolean prettyFlow) {
this.prettyFlow = prettyFlow;
}
}
44 changes: 42 additions & 2 deletions test/com/esotericsoftware/yamlbeans/YamlConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -502,7 +503,6 @@ public void testSetQuoteChar() throws YamlException {
yamlWriter = new YamlWriter(stringWriter, yamlConfig);
yamlWriter.write("test\ntest");
yamlWriter.close();
System.out.println(stringWriter.toString());
assertEquals("|-" + LINE_SEPARATOR + " test" + LINE_SEPARATOR + " test" + LINE_SEPARATOR,
stringWriter.toString());

Expand All @@ -511,7 +511,6 @@ public void testSetQuoteChar() throws YamlException {
yamlWriter = new YamlWriter(stringWriter, yamlConfig);
yamlWriter.write("test\ntest");
yamlWriter.close();
System.out.println(stringWriter.toString());
assertEquals(">-" + LINE_SEPARATOR + " test" + LINE_SEPARATOR + LINE_SEPARATOR + " test" + LINE_SEPARATOR,
stringWriter.toString());
}
Expand Down Expand Up @@ -603,6 +602,47 @@ public void testSetFlowStyle() throws YamlException {
assertEquals("{key: value}" + LINE_SEPARATOR, stringWriter.toString());
}

@Test
public void testSetPrettyFlow() throws YamlException {
Map<String, String> map = new LinkedHashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");

List<String> list = new ArrayList<String>();
list.add("111");
list.add("222");
list.add("333");

yamlConfig.writeConfig.setWriteRootTags(false);
yamlConfig.writeConfig.setFlowStyle(true);
StringWriter stringWriter = new StringWriter();
YamlWriter yamlWriter = new YamlWriter(stringWriter, yamlConfig);
yamlWriter.write(map);
yamlWriter.close();
assertEquals("{key1: value1, key2: value2}" + LINE_SEPARATOR, stringWriter.toString());

stringWriter = new StringWriter();
yamlWriter = new YamlWriter(stringWriter, yamlConfig);
yamlWriter.write(list);
yamlWriter.close();
assertEquals("[111, 222, 333]" + LINE_SEPARATOR, stringWriter.toString());

yamlConfig.writeConfig.setPrettyFlow(true);
stringWriter = new StringWriter();
yamlWriter = new YamlWriter(stringWriter, yamlConfig);
yamlWriter.write(map);
yamlWriter.close();
assertEquals("{" + LINE_SEPARATOR + " key1: value1," + LINE_SEPARATOR + " key2: value2" + LINE_SEPARATOR
+ "}" + LINE_SEPARATOR, stringWriter.toString());

stringWriter = new StringWriter();
yamlWriter = new YamlWriter(stringWriter, yamlConfig);
yamlWriter.write(list);
yamlWriter.close();
assertEquals("[" + LINE_SEPARATOR + " 111," + LINE_SEPARATOR + " 222," + LINE_SEPARATOR + " 333"
+ LINE_SEPARATOR + "]" + LINE_SEPARATOR, stringWriter.toString());
}

private String multipleSpaces(int indentSize) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < indentSize; i++) {
Expand Down

0 comments on commit 61d9495

Please sign in to comment.