2011-08-16 12:53:13 +00:00
|
|
|
===============
|
2014-08-01 15:07:08 +00:00
|
|
|
Getting started
|
2011-08-16 12:53:13 +00:00
|
|
|
===============
|
|
|
|
|
2014-08-01 15:07:08 +00:00
|
|
|
Welcome to D:YAML! D:YAML is a `YAML <http://en.wikipedia.org/wiki/YAML>`_ parser
|
|
|
|
library for the `D programming language <http://dlang.org>`_. This tutorial will
|
|
|
|
explain how to set D:YAML up and use it in your projects.
|
2011-08-16 12:53:13 +00:00
|
|
|
|
2014-08-01 15:07:08 +00:00
|
|
|
This is meant to be the **simplest possible** introduction to D:YAML. Some of this
|
|
|
|
information might already be known to you. Only basic usage is covered.
|
2011-08-16 12:53:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
----------
|
|
|
|
Setting up
|
|
|
|
----------
|
|
|
|
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
Install the DMD compiler
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
2014-08-01 15:07:08 +00:00
|
|
|
Digital Mars D compiler, or DMD, is the most commonly used D compiler. You can find its
|
|
|
|
newest version `here <http://dlang.org/download.html>`_. Download the version of DMD
|
|
|
|
for your operating system and install it.
|
2011-08-16 12:53:13 +00:00
|
|
|
|
2014-08-01 15:07:08 +00:00
|
|
|
.. note::
|
|
|
|
Other D compilers exist, such as
|
|
|
|
`GDC <http://gdcproject.org/>`_ and
|
2014-08-01 00:46:32 +00:00
|
|
|
`LDC <http://bitbucket.org/goshawk/gdc/wiki/Home>`_.
|
2011-08-16 12:53:13 +00:00
|
|
|
|
2014-08-01 15:07:08 +00:00
|
|
|
^^^^^^^^^^^
|
|
|
|
Install dub
|
|
|
|
^^^^^^^^^^^
|
2011-08-16 12:53:13 +00:00
|
|
|
|
2014-08-01 15:07:08 +00:00
|
|
|
`dub <http://code.dlang.org/about>`_ is a build system and package manager for D.
|
|
|
|
It is the standard way to manage D projects and their dependencies, compilation and so
|
|
|
|
on.
|
2011-08-16 12:53:13 +00:00
|
|
|
|
2014-08-01 15:07:08 +00:00
|
|
|
DMD may include DUB in future releases, but at this point we need to install it
|
|
|
|
separately. See
|
|
|
|
`installation instructions <https://github.com/D-Programming-Language/dub#installation>`_.
|
2011-08-16 12:53:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
-------------------------
|
2014-08-01 15:07:08 +00:00
|
|
|
Your first D:YAML project
|
2011-08-16 12:53:13 +00:00
|
|
|
-------------------------
|
|
|
|
|
2014-08-01 15:07:08 +00:00
|
|
|
Create a directory for your project and in that directory, create a new file named
|
|
|
|
``input.yaml`` and paste this code into the file:
|
2011-08-16 12:53:13 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
2014-08-01 15:07:08 +00:00
|
|
|
Hello World : [Hello, World]
|
2011-08-16 12:53:13 +00:00
|
|
|
Answer: 42
|
|
|
|
|
|
|
|
This will serve as input for our example.
|
|
|
|
|
2014-08-01 15:07:08 +00:00
|
|
|
Now we need to parse it. Create a new file with name ``main.d``. Paste following code
|
2011-08-16 12:53:13 +00:00
|
|
|
into the file:
|
|
|
|
|
|
|
|
.. code-block:: d
|
|
|
|
|
|
|
|
import std.stdio;
|
2014-08-01 00:46:32 +00:00
|
|
|
import yaml;
|
2011-08-16 12:53:13 +00:00
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
2011-10-15 14:31:23 +00:00
|
|
|
//Read the input.
|
2011-10-12 21:49:42 +00:00
|
|
|
Node root = Loader("input.yaml").load();
|
2011-10-15 14:31:23 +00:00
|
|
|
|
|
|
|
//Display the data read.
|
2011-08-16 12:53:13 +00:00
|
|
|
foreach(string word; root["Hello World"])
|
|
|
|
{
|
|
|
|
writeln(word);
|
|
|
|
}
|
2011-10-22 15:06:32 +00:00
|
|
|
writeln("The answer is ", root["Answer"].as!int);
|
2011-10-15 14:31:23 +00:00
|
|
|
|
|
|
|
//Dump the loaded document to output.yaml.
|
|
|
|
Dumper("output.yaml").dump(root);
|
2011-08-16 12:53:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
Explanation of the code
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
2014-08-01 15:07:08 +00:00
|
|
|
First, we import the *dyaml.all* module. This is the only D:YAML module you need to
|
|
|
|
import - it automatically imports all needed modules.
|
|
|
|
|
|
|
|
Next we load the file using the *Loader.load()* method. *Loader* is a struct used for
|
|
|
|
parsing YAML documents. The *load()* method loads the file as **one** YAML document, or
|
|
|
|
throws *YAMLException*, D:YAML exception type, if the file could not be parsed or
|
|
|
|
contains more than one document. Note that we don't do any error checking here in order
|
|
|
|
to keep the example as simple as possible.
|
|
|
|
|
|
|
|
*Node* represents a node in a YAML document. It can be a sequence (array), mapping
|
|
|
|
(associative array) or a scalar (value). Here the root node is a mapping, and we use the
|
|
|
|
index operator to get subnodes with keys "Hello World" and "Answer". We iterate over the
|
|
|
|
former, as it is a sequence, and use the *Node.as()* method on the latter to read its
|
|
|
|
value as an integer.
|
|
|
|
|
|
|
|
You can iterate over a mapping or sequence as if it was an associative or normal array,
|
|
|
|
respectively. If you try to iterate over a scalar, it will throw a *YAMLException*.
|
|
|
|
|
|
|
|
You can iterate using *Node* as the iterated type, or specify the type iterated nodes
|
|
|
|
are expected to have. D:YAML will automatically convert to that type if possible. Here
|
|
|
|
we specify the *string* type, so we iterate over the "Hello World" sequence as an array
|
|
|
|
of strings. If it is not possible to convert to iterated type, a *YAMLException* is
|
|
|
|
thrown. For instance, if we specified *int* here, we would get an error, as "Hello"
|
2011-08-16 12:53:13 +00:00
|
|
|
cannot be converted to an integer.
|
|
|
|
|
2014-08-01 15:07:08 +00:00
|
|
|
The *Node.as()* method is used to read value of a scalar node as specified type. If the
|
|
|
|
scalar does not have the specified type, D:YAML will try to convert it, throwing
|
|
|
|
*YAMLException* if not possible.
|
|
|
|
|
|
|
|
Finally we dump the document we just read to ``output.yaml`` with the *Dumper.dump()*
|
|
|
|
method. *Dumper* is a struct used to dump YAML documents. The *dump()* method writes
|
|
|
|
one or more documents to a file, throwing *YAMLException* if the file could not be
|
|
|
|
written to.
|
2011-08-16 12:53:13 +00:00
|
|
|
|
2014-08-01 15:07:08 +00:00
|
|
|
D:YAML tries to preserve style information in documents so e.g. ``[Hello, World]`` is
|
|
|
|
not turned into:
|
2011-10-15 14:31:23 +00:00
|
|
|
|
2014-08-01 15:07:08 +00:00
|
|
|
| ``- Hello``
|
|
|
|
| ``- World``
|
|
|
|
|
|
|
|
However, comments are not preserved and neither are any extra formatting whitespace that
|
|
|
|
doesn't affect the meaning of YAML contents.
|
2011-10-15 14:31:23 +00:00
|
|
|
|
2011-08-16 12:53:13 +00:00
|
|
|
|
|
|
|
^^^^^^^^^
|
|
|
|
Compiling
|
|
|
|
^^^^^^^^^
|
|
|
|
|
2014-08-01 15:07:08 +00:00
|
|
|
We're going to use dub, which we installed at the beginning, to compile our project.
|
|
|
|
|
|
|
|
Create a file called ``dub.json`` with the following contents:
|
|
|
|
|
|
|
|
.. code-block:: json
|
2011-10-12 21:49:42 +00:00
|
|
|
|
2014-08-01 15:07:08 +00:00
|
|
|
{
|
|
|
|
"name": "getting-started",
|
|
|
|
"targetType": "executable",
|
|
|
|
"sourceFiles": ["main.d"],
|
|
|
|
"mainSourceFile": "main.d",
|
|
|
|
"dependencies":
|
|
|
|
{
|
2014-08-07 13:27:48 +00:00
|
|
|
"dyaml": { "version" : "~>0.5.0" },
|
2014-08-01 15:07:08 +00:00
|
|
|
},
|
|
|
|
}
|
2011-08-16 12:53:13 +00:00
|
|
|
|
2014-08-01 15:07:08 +00:00
|
|
|
This file tells dub that we're building an executable called ``getting-started`` from
|
|
|
|
a D source file ``main.d``, and that our project depends on D:YAML 0.5.0 or any newer,
|
|
|
|
bugfix release of D:YAML 0.5 . DUB will automatically find and download the correct
|
|
|
|
version of D:YAML when the project is built.
|
2011-08-16 12:53:13 +00:00
|
|
|
|
2014-08-01 15:07:08 +00:00
|
|
|
Now run the following command in your project's directory::
|
2011-08-16 12:53:13 +00:00
|
|
|
|
2014-08-01 15:07:08 +00:00
|
|
|
dub build
|
2011-08-16 12:53:13 +00:00
|
|
|
|
2014-08-01 15:07:08 +00:00
|
|
|
dub will automatically download D:YAML and compile it, and then then it will compile our
|
|
|
|
program. This will generate an executable called ``getting-started`` or
|
|
|
|
``getting-started.exe`` in your directory. When you run it, it should produce the
|
|
|
|
following output::
|
2011-08-16 12:53:13 +00:00
|
|
|
|
|
|
|
Hello
|
2013-12-17 14:14:44 +00:00
|
|
|
World
|
2014-08-01 15:07:08 +00:00
|
|
|
The answer is 42
|
2011-08-16 12:53:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
^^^^^^^^^^
|
|
|
|
Conclusion
|
|
|
|
^^^^^^^^^^
|
|
|
|
|
2014-08-01 15:07:08 +00:00
|
|
|
You should now have a basic idea about how to use D:YAML. To learn more, look at the
|
|
|
|
`API documentation <../api/index.html>`_ and other tutorials. You can find code for this
|
2011-08-16 12:53:13 +00:00
|
|
|
example in the ``example/getting_started`` directory in the package.
|