dlang-community / D-YAML

YAML parser and emitter for the D programming language
https://dlang-community.github.io/D-YAML/
Boost Software License 1.0
119 stars 37 forks source link

Building Node structures to dump out #143

Closed russel closed 6 years ago

russel commented 6 years ago

I believe all the examples show starting by reading a YAML file to create a Node tree and then fiddling with the tree before dumping it out. Are there any simple examples of building a Node tree programmatically from scratch; the yaml_gen example is hard to follow – but this maybe that I am not trying hard enough.

A small example. A loaded YAML results in:

Node([Pair(Node(use_opengl, line 2,column 1, ZeroString!"Tag"(7F149248E040), Plain, Invalid), Node(true, line 2,column 13, ZeroString!"Tag"(7F149248E220), Plain, Invalid))], line 2,column 1, ZeroString!"Tag"(7F149248E080), Invalid, Block)

How might I create programmatically this Node tree?

Herringway commented 6 years ago

For the most part, it's as simple as Node(someArray) or Node(someScalar). You can use (associative/regular) arrays of nodes for more complicated things.

This one is as simple as

auto node = Node(["use_opengl": true]);

or

auto node = Node(string[string].init);
node.add("use_opengl", true);

That second example is looking a bit ugly... Perhaps it would be a good idea to skip the mapping check if the node is empty and allow the use of auto node = Node(); in that example instead.

Herringway commented 6 years ago

Opened #144 to make the latter example easier.