From e4087c23f1a37f3ec672ca44d9dd4a4b3e545afd Mon Sep 17 00:00:00 2001 From: Cameron Ross Date: Sun, 10 Jun 2018 03:52:12 -0300 Subject: [PATCH] add ability to set Node style --- source/dyaml/node.d | 102 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/source/dyaml/node.d b/source/dyaml/node.d index bfb7121..a537f1e 100644 --- a/source/dyaml/node.d +++ b/source/dyaml/node.d @@ -1984,6 +1984,108 @@ struct Node else static if(is(Unqual!T==bool)){return isBool();} else {return false;} } + /** + * Sets the style of this node when dumped. + * + * Params: style = Any valid style. + */ + void setStyle(CollectionStyle style) @safe + { + enforce(!isValid || isSequence || isMapping, new NodeException( + "Cannot set collection style for non-collection nodes", startMark_)); + collectionStyle = style; + } + /// Ditto + void setStyle(ScalarStyle style) @safe + { + enforce(!isValid || (!isSequence && !isMapping), new NodeException( + "Cannot set scalar style for non-scalar nodes", startMark_)); + scalarStyle = style; + } + /// + @safe unittest + { + import dyaml.dumper; + import dyaml.stream; + auto stream = new YMemoryStream(); + auto node = Node([1, 2, 3, 4, 5]); + node.setStyle(CollectionStyle.Block); + + auto dumper = Dumper(stream); + dumper.dump(node); + } + /// + @safe unittest + { + import dyaml.dumper; + import dyaml.stream; + auto stream = new YMemoryStream(); + auto node = Node(4); + node.setStyle(ScalarStyle.Literal); + + auto dumper = Dumper(stream); + dumper.dump(node); + } + @safe unittest + { + assertThrown!NodeException(Node(4).setStyle(CollectionStyle.Block)); + assertThrown!NodeException(Node([4]).setStyle(ScalarStyle.Literal)); + } + @safe unittest + { + import dyaml.dumper; + import dyaml.stream; + { + auto stream = new YMemoryStream(); + auto node = Node([1, 2, 3, 4, 5]); + node.setStyle(CollectionStyle.Block); + auto dumper = Dumper(stream); + dumper.explicitEnd = false; + dumper.explicitStart = false; + dumper.YAMLVersion = null; + dumper.dump(node); + + //Block style should start with a hyphen. + assert(stream.data[0] == '-'); + } + { + auto stream = new YMemoryStream(); + auto node = Node([1, 2, 3, 4, 5]); + node.setStyle(CollectionStyle.Flow); + auto dumper = Dumper(stream); + dumper.explicitEnd = false; + dumper.explicitStart = false; + dumper.YAMLVersion = null; + dumper.dump(node); + + //Flow style should start with a bracket. + assert(stream.data[0] == '['); + } + { + auto stream = new YMemoryStream(); + auto node = Node(1); + node.setStyle(ScalarStyle.SingleQuoted); + auto dumper = Dumper(stream); + dumper.explicitEnd = false; + dumper.explicitStart = false; + dumper.YAMLVersion = null; + dumper.dump(node); + + assert(stream.data == "!!int '1'\n"); + } + { + auto stream = new YMemoryStream(); + auto node = Node(1); + node.setStyle(ScalarStyle.DoubleQuoted); + auto dumper = Dumper(stream); + dumper.explicitEnd = false; + dumper.explicitStart = false; + dumper.YAMLVersion = null; + dumper.dump(node); + + assert(stream.data == "!!int \"1\"\n"); + } + } private: // Implementation of contains() and containsKey().