switch to a non-templated Dumper struct (#234)

switch to a non-templated Dumper struct
merged-on-behalf-of: Basile-z <Basile-z@users.noreply.github.com>
This commit is contained in:
Cameron Ross 2019-02-07 04:18:50 -03:30 committed by The Dlang Bot
parent 8de1a45922
commit e37c4daa7e
9 changed files with 68 additions and 80 deletions

View file

@ -14,5 +14,5 @@ void main()
writeln("The answer is ", root["Answer"].as!int);
//Dump the loaded document to output.yaml.
dumper(File("output.yaml", "w").lockingTextWriter).dump(root);
dumper().dump(File("output.yaml", "w").lockingTextWriter, root);
}

View file

@ -28,13 +28,13 @@ void main()
{
try
{
auto dumper = dumper(File("output.yaml", "w").lockingTextWriter);
auto dumper = dumper();
auto document = Node([Color(255, 0, 0),
Color(0, 255, 0),
Color(0, 0, 255)]);
dumper.dump(document);
dumper.dump(File("output.yaml", "w").lockingTextWriter, document);
}
catch(YAMLException e)
{

View file

@ -129,7 +129,7 @@ void main(string[] args) //@safe
{
if(dump)
{
dumper(File(file ~ ".dump", "w").lockingTextWriter).dump(nodes);
dumper().dump(File(file ~ ".dump", "w").lockingTextWriter, nodes);
}
}
void runGetBenchmark() @safe

View file

@ -297,16 +297,16 @@ void main(string[] args)
//Generate and dump the nodes.
Node[] generated = generate(configFile);
auto dumper = dumper(File(args[1], "w").lockingTextWriter);
auto dumper = dumper();
auto encoding = config["encoding"];
dumper.indent = config["indent"].as!uint;
dumper.textWidth = config["text-width"].as!uint;
switch(encoding.as!string)
{
case "utf-16": dumper.dump!wchar(generated); break;
case "utf-32": dumper.dump!dchar(generated); break;
default: dumper.dump!char(generated); break;
case "utf-16": dumper.dump!wchar(File(args[1], "w").lockingTextWriter, generated); break;
case "utf-32": dumper.dump!dchar(File(args[1], "w").lockingTextWriter, generated); break;
default: dumper.dump!char(File(args[1], "w").lockingTextWriter, generated); break;
}
}
catch(YAMLException e)

View file

@ -35,21 +35,19 @@ import dyaml.tagdirective;
*
* Setters are provided to affect output details (style, etc.).
*/
auto dumper(Range)(auto ref Range output)
if (isOutputRange!(Range, char) || isOutputRange!(Range, wchar) || isOutputRange!(Range, dchar))
auto dumper()
{
return Dumper!Range(output);
auto dumper = Dumper();
dumper.resolver_ = Resolver.withDefaultResolvers;
return dumper;
}
struct Dumper(Range)
struct Dumper
{
private:
//Resolver to resolve tags.
Resolver resolver_;
//Stream to write to.
Range stream_;
//Write scalars in canonical form?
bool canonical_;
//Indentation width.
@ -88,20 +86,8 @@ struct Dumper(Range)
{
defaultCollectionStyle_ = style;
}
@disable this();
@disable bool opEquals(ref Dumper!Range);
@disable int opCmp(ref Dumper!Range);
/**
* Construct a Dumper writing to a file.
*
* Params: filename = File name to write to.
*/
this(Range stream) @safe
{
resolver_ = Resolver.withDefaultResolvers;
stream_ = stream;
}
@disable bool opEquals(ref Dumper);
@disable int opCmp(ref Dumper);
///Set stream _name. Used in debugging messages.
@property void name(string name) pure @safe nothrow
@ -197,13 +183,13 @@ struct Dumper(Range)
///
@safe unittest
{
auto dumper = dumper(new Appender!string());
auto dumper = dumper();
string[string] directives;
directives["!short!"] = "tag:long.org,2011:";
//This will emit tags starting with "tag:long.org,2011"
//with a "!short!" prefix instead.
dumper.tagDirectives(directives);
dumper.dump(Node("foo"));
dumper.dump(new Appender!string(), Node("foo"));
}
/**
@ -218,12 +204,13 @@ struct Dumper(Range)
* Throws: YAMLException on error (e.g. invalid nodes,
* unable to write to file/stream).
*/
void dump(CharacterType = char)(Node[] documents ...) @trusted
if (isOutputRange!(Range, CharacterType))
void dump(CharacterType = char, Range)(Range range, Node[] documents ...) @trusted
if (isOutputRange!(Range, CharacterType) &&
isOutputRange!(Range, char) || isOutputRange!(Range, wchar) || isOutputRange!(Range, dchar))
{
try
{
auto emitter = new Emitter!(Range, CharacterType)(stream_, canonical_, indent_, textWidth_, lineBreak_);
auto emitter = new Emitter!(Range, CharacterType)(range, canonical_, indent_, textWidth_, lineBreak_);
auto serializer = Serializer!(Range, CharacterType)(emitter, resolver_, explicitStart_,
explicitEnd_, YAMLVersion_, tags_);
foreach(ref document; documents)
@ -247,12 +234,13 @@ struct Dumper(Range)
*
* Throws: YAMLException if unable to emit.
*/
void emit(CharacterType = char, T)(T events) @safe
if (isInputRange!T && is(ElementType!T == Event))
void emit(CharacterType = char, Range, T)(Range range, T events) @safe
if (isInputRange!T && is(ElementType!T == Event) &&
isOutputRange!(Range, char) || isOutputRange!(Range, wchar) || isOutputRange!(Range, dchar))
{
try
{
auto emitter = Emitter!(Range, CharacterType)(stream_, canonical_, indent_, textWidth_, lineBreak_);
auto emitter = Emitter!(Range, CharacterType)(range, canonical_, indent_, textWidth_, lineBreak_);
foreach(ref event; events)
{
emitter.emit(event);
@ -269,63 +257,63 @@ struct Dumper(Range)
@safe unittest
{
auto node = Node([1, 2, 3, 4, 5]);
dumper(new Appender!string()).dump(node);
dumper().dump(new Appender!string(), node);
}
///Write multiple YAML documents to a file
@safe unittest
{
auto node1 = Node([1, 2, 3, 4, 5]);
auto node2 = Node("This document contains only one string");
dumper(new Appender!string()).dump(node1, node2);
dumper().dump(new Appender!string(), node1, node2);
//Or with an array:
dumper(new Appender!string()).dump([node1, node2]);
dumper().dump(new Appender!string(), [node1, node2]);
}
///Write to memory
@safe unittest
{
auto stream = new Appender!string();
auto node = Node([1, 2, 3, 4, 5]);
dumper(stream).dump(node);
dumper().dump(stream, node);
}
///Use a custom resolver to support custom data types and/or implicit tags
@safe unittest
{
import std.regex : regex;
auto node = Node([1, 2, 3, 4, 5]);
auto dumper = dumper(new Appender!string());
auto dumper = dumper();
dumper.resolver.addImplicitResolver("!tag", regex("A.*"), "A");
dumper.dump(node);
dumper.dump(new Appender!string(), node);
}
/// Set default scalar style
@safe unittest
{
auto stream = new Appender!string();
auto node = Node("Hello world!");
auto dumper = dumper(stream);
auto dumper = dumper();
dumper.defaultScalarStyle = ScalarStyle.singleQuoted;
dumper.dump(node);
dumper.dump(stream, node);
}
/// Set default collection style
@safe unittest
{
auto stream = new Appender!string();
auto node = Node(["Hello", "world!"]);
auto dumper = dumper(stream);
auto dumper = dumper();
dumper.defaultCollectionStyle = CollectionStyle.flow;
dumper.dump(node);
dumper.dump(stream, node);
}
// Make sure the styles are actually used
@safe unittest
{
auto stream = new Appender!string();
auto node = Node([Node("Hello world!"), Node(["Hello", "world!"])]);
auto dumper = dumper(stream);
auto dumper = dumper();
dumper.defaultScalarStyle = ScalarStyle.singleQuoted;
dumper.defaultCollectionStyle = CollectionStyle.flow;
dumper.explicitEnd = false;
dumper.explicitStart = false;
dumper.YAMLVersion = null;
dumper.dump(node);
dumper.dump(stream, node);
assert(stream.data == "[!!str 'Hello world!', [!!str 'Hello', !!str 'world!']]\n");
}
// Explicit document start/end markers
@ -333,11 +321,11 @@ struct Dumper(Range)
{
auto stream = new Appender!string();
auto node = Node([1, 2, 3, 4, 5]);
auto dumper = dumper(stream);
auto dumper = dumper();
dumper.explicitEnd = true;
dumper.explicitStart = true;
dumper.YAMLVersion = null;
dumper.dump(node);
dumper.dump(stream, node);
//Skip version string
assert(stream.data[0..3] == "---");
//account for newline at end
@ -348,11 +336,11 @@ struct Dumper(Range)
{
auto stream = new Appender!string();
auto node = Node([1, 2, 3, 4, 5]);
auto dumper = dumper(stream);
auto dumper = dumper();
dumper.explicitEnd = false;
dumper.explicitStart = false;
dumper.YAMLVersion = null;
dumper.dump(node);
dumper.dump(stream, node);
//Skip version string
assert(stream.data[0..3] != "---");
//account for newline at end
@ -364,22 +352,22 @@ struct Dumper(Range)
auto node = Node(0);
{
auto stream = new Appender!string();
auto dumper = dumper(stream);
auto dumper = dumper();
dumper.explicitEnd = true;
dumper.explicitStart = true;
dumper.YAMLVersion = null;
dumper.lineBreak = LineBreak.windows;
dumper.dump(node);
dumper.dump(stream, node);
assert(stream.data == "--- 0\r\n...\r\n");
}
{
auto stream = new Appender!string();
auto dumper = dumper(stream);
auto dumper = dumper();
dumper.explicitEnd = true;
dumper.explicitStart = true;
dumper.YAMLVersion = null;
dumper.lineBreak = LineBreak.macintosh;
dumper.dump(node);
dumper.dump(stream, node);
assert(stream.data == "--- 0\r...\r");
}
}

View file

@ -630,7 +630,7 @@ struct Node
auto appender = new Appender!string;
// Dump struct to yaml document
dumper(appender).dump(Node(MyStruct(1,2,3)));
dumper().dump(appender, Node(MyStruct(1,2,3)));
// Read yaml document back as a MyStruct
auto loader = Loader.fromString(appender.data);
@ -669,7 +669,7 @@ struct Node
auto appender = new Appender!string;
// Dump struct to yaml document
dumper(appender).dump(Node(MyStruct(1,2,3)));
dumper().dump(appender, Node(MyStruct(1,2,3)));
// Read yaml document back as a MyStruct
auto loader = Loader.fromString(appender.data);
@ -711,7 +711,7 @@ struct Node
auto appender = new Appender!string;
// Dump struct to yaml document
dumper(appender).dump(Node(MyStruct(1,2,3)));
dumper().dump(appender, Node(MyStruct(1,2,3)));
// Read yaml document back as a MyStruct
auto loader = Loader.fromString(appender.data);
@ -767,7 +767,7 @@ struct Node
auto appender = new Appender!string;
// Dump class to yaml document
dumper(appender).dump(Node(new MyClass(1,2,3)));
dumper().dump(appender, Node(new MyClass(1,2,3)));
// Read yaml document back as a MyClass
auto loader = Loader.fromString(appender.data);
@ -2209,8 +2209,8 @@ struct Node
auto node = Node([1, 2, 3, 4, 5]);
node.setStyle(CollectionStyle.block);
auto dumper = dumper(stream);
dumper.dump(node);
auto dumper = dumper();
dumper.dump(stream, node);
}
///
@safe unittest
@ -2220,8 +2220,8 @@ struct Node
auto node = Node(4);
node.setStyle(ScalarStyle.literal);
auto dumper = dumper(stream);
dumper.dump(node);
auto dumper = dumper();
dumper.dump(stream, node);
}
@safe unittest
{
@ -2235,11 +2235,11 @@ struct Node
auto stream = new Appender!string();
auto node = Node([1, 2, 3, 4, 5]);
node.setStyle(CollectionStyle.block);
auto dumper = dumper(stream);
auto dumper = dumper();
dumper.explicitEnd = false;
dumper.explicitStart = false;
dumper.YAMLVersion = null;
dumper.dump(node);
dumper.dump(stream, node);
//Block style should start with a hyphen.
assert(stream.data[0] == '-');
@ -2248,11 +2248,11 @@ struct Node
auto stream = new Appender!string();
auto node = Node([1, 2, 3, 4, 5]);
node.setStyle(CollectionStyle.flow);
auto dumper = dumper(stream);
auto dumper = dumper();
dumper.explicitEnd = false;
dumper.explicitStart = false;
dumper.YAMLVersion = null;
dumper.dump(node);
dumper.dump(stream, node);
//Flow style should start with a bracket.
assert(stream.data[0] == '[');
@ -2261,11 +2261,11 @@ struct Node
auto stream = new Appender!string();
auto node = Node(1);
node.setStyle(ScalarStyle.singleQuoted);
auto dumper = dumper(stream);
auto dumper = dumper();
dumper.explicitEnd = false;
dumper.explicitStart = false;
dumper.YAMLVersion = null;
dumper.dump(node);
dumper.dump(stream, node);
assert(stream.data == "!!int '1'\n");
}
@ -2273,11 +2273,11 @@ struct Node
auto stream = new Appender!string();
auto node = Node(1);
node.setStyle(ScalarStyle.doubleQuoted);
auto dumper = dumper(stream);
auto dumper = dumper();
dumper.explicitEnd = false;
dumper.explicitStart = false;
dumper.YAMLVersion = null;
dumper.dump(node);
dumper.dump(stream, node);
assert(stream.data == "!!int \"1\"\n");
}

View file

@ -79,7 +79,7 @@ void testEmitterOnData(string dataFilename, string canonicalFilename) @safe
auto loader = Loader.fromFile(dataFilename);
auto events = loader.parse();
auto emitStream = new Appender!string;
dumper(emitStream).emit(events);
dumper().emit(emitStream, events);
static if(verbose)
{
@ -107,9 +107,9 @@ void testEmitterOnCanonical(string canonicalFilename) @safe
foreach(canonical; [false, true])
{
auto emitStream = new Appender!string;
auto dumper = dumper(emitStream);
auto dumper = dumper();
dumper.canonical = canonical;
dumper.emit(events);
dumper.emit(emitStream, events);
static if(verbose)
{
writeln("OUTPUT (canonical=", canonical, "):\n",
@ -164,7 +164,7 @@ void testEmitterStyles(string dataFilename, string canonicalFilename) @safe
styledEvents ~= event;
}
auto emitStream = new Appender!string;
dumper(emitStream).emit(styledEvents);
dumper().emit(emitStream, styledEvents);
static if(verbose)
{
writeln("OUTPUT (", filename, ", ", to!string(flowStyle), ", ",

View file

@ -53,8 +53,8 @@ void testRepresenterTypes(string codeFilename) @safe
}
auto emitStream = new Appender!(immutable(encoding)[]);
auto dumper = dumper(emitStream);
dumper.dump!encoding(expectedNodes);
auto dumper = dumper();
dumper.dump!encoding(emitStream, expectedNodes);
output = emitStream.data;

View file

@ -263,7 +263,7 @@ TestResult runTests(string tml) @safe
if (!loadFailed && !compareYAMLString.isNull && !shouldFail)
{
Appender!string buf;
dumper(buf).dump();
dumper().dump(buf);
compareLineByLine(buf.data, compareYAMLString, "Dumped YAML mismatch");
testsRun++;
}