Skip to content

Commit

Permalink
YamlSequenceBuilder.add(JsonStructure)
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed May 1, 2024
1 parent dfac7f0 commit bd36e10
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/main/java/com/amihaiemil/eoyaml/YamlSequenceBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
*/
package com.amihaiemil.eoyaml;

import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonStructure;
import java.util.Collection;

/**
Expand All @@ -51,6 +54,23 @@ public interface YamlSequenceBuilder {
*/
YamlSequenceBuilder add(final YamlNode node);

/**
* Add a value to the sequence - this will be printed in Flow style
* (JSON-like).
* @param value JsonStructure ({@link javax.json.JsonObject}
* or {@link javax.json.JsonArray})
* @return Builder
*/
default YamlSequenceBuilder add(final JsonStructure value) {
final YamlNode node;
if(value instanceof JsonObject) {
node = new JsonYamlMapping((JsonObject) value);
} else {
node = new JsonYamlSequence((JsonArray) value);
}
return this.add(node);
}

/**
* Build the YamlSequence.
* @return Built YamlSequence
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/com/amihaiemil/eoyaml/YamlMappingPrintTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@ public void printsBuiltYamlMappingWithAllNodes() throws Exception {
Yaml.createYamlInput("[a, b, c]").readYamlSequence(),
"scalarToFlowSequence"
)
.add(
"someFlowSeq",
Yaml.createYamlSequenceBuilder()
.add(
Json.createArrayBuilder()
.add("a")
.add("b")
.add("c")
.build()
)
.build()
)
.build();
MatcherAssert.assertThat(
built.toString(),
Expand Down
4 changes: 3 additions & 1 deletion src/test/resources/printing_tests/yamlMappingAllNodes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ key10: {a: b, c: d, e: f}
: scalarToFlowMapping
?
[a, b, c]
: scalarToFlowSequence
: scalarToFlowSequence
someFlowSeq:
- [a, b, c]

0 comments on commit bd36e10

Please sign in to comment.