Elektra
0.8.24
|
The YAML CPP plugin reads and writes configuration data via the yaml-cpp library.
You can mount this plugin via kdb mount
:
. To unmount the plugin use kdb umount
:
. The following examples show how you can store and retrieve data via yamlcpp
.
`` <h1>Mount yamlcpp plugin to cascading namespace
/tests/yamlcpp` sudo kdb mount config.yaml /tests/yamlcpp yamlcpp
echo "π : π³" > kdb file /tests/yamlcpp
kdb get /tests/yamlcpp/π #> π³
echo "some key: @some value" >> kdb file /tests/yamlcpp
kdb get "/tests/yamlcpp/some key"
echo "π: value" > kdb file /tests/yamlcpp
kdb set
kdb set /tests/yamlcpp π΅ kdb set /tests/yamlcpp/fleetwood mac kdb set /tests/yamlcpp/the chain
kdb get /tests/yamlcpp #> π΅ kdb get /tests/yamlcpp/the #> chain kdb get /tests/yamlcpp/fleetwood #> mac
kdb rm -r /tests/yamlcpp sudo kdb umount /tests/yamlcpp
/tests/yamlcpp
sudo kdb mount config.yaml /tests/yamlcpp yamlcpp
echo 'sunny:' > kdb file /tests/yamlcpp
echo ' - Charlie' >> kdb file /tests/yamlcpp
echo ' - Dee' >> kdb file /tests/yamlcpp
kdb ls /tests/yamlcpp #> user/tests/yamlcpp/sunny #> user/tests/yamlcpp/sunny/#0 #> user/tests/yamlcpp/sunny/#1
kdb get user/tests/yamlcpp/sunny/#1 #> Dee
array
kdb getmeta /tests/yamlcpp/sunny array
kdb set user/tests/yamlcpp/sunny/#2 Dennis kdb set user/tests/yamlcpp/sunny/#3 Frank kdb set user/tests/yamlcpp/sunny/#4 Mac
kdb set user/tests/yamlcpp/sunny/#_10 'The Waitress' kdb getmeta user/tests/yamlcpp/sunny array #> #_10 kdb get user/tests/yamlcpp/sunny/#_9
kdb get user/tests/yamlcpp/sunny/$(kdb getmeta user/tests/yamlcpp/sunny array) #> The Waitress
kdb setmeta user/tests/yamlcpp/empty array '' kdb export user/tests/yamlcpp/empty yamlcpp #> []
array
kdb set user/tests/yamlcpp/movies kdb set user/tests/yamlcpp/movies/#0 'A Silent Voice' kdb getmeta user/tests/yamlcpp/movies array #> #0 kdb export user/tests/yamlcpp/movies yamlcpp #> - A Silent Voice
kdb rm -r /tests/yamlcpp sudo kdb umount /tests/yamlcpp
/tests/yamlcpp
sudo kdb mount config.yaml /tests/yamlcpp yamlcpp
kdb set /tests/yamlcpp/key value kdb set /tests/yamlcpp/array/#0 scalar kdb set /tests/yamlcpp/array/#1/key value kdb set /tests/yamlcpp/array/#1/π π
kdb ls /tests/yamlcpp #> user/tests/yamlcpp/array #> user/tests/yamlcpp/array/#0 #> user/tests/yamlcpp/array/#1/key #> user/tests/yamlcpp/array/#1/π #> user/tests/yamlcpp/key
kdb get /tests/yamlcpp/array/#1/key #> value
echo "/tests/yamlcpp/array: β`kdb get /tests/yamlcpp/array`β" #> /tests/yamlcpp/array: ββ
kdb rm /tests/yamlcpp/array/#1/key
kdb ls /tests/yamlcpp #> user/tests/yamlcpp/array #> user/tests/yamlcpp/array/#0 #> user/tests/yamlcpp/array/#1/π #> user/tests/yamlcpp/key
key
or array
could be the βfirstβ key -key
before we retrieve the data. This waykdb rm /tests/yamlcpp/key kdb file /tests/yamlcpp | xargs cat #> array: #> - scalar #> - π: π
kdb rm -r /tests/yamlcpp sudo kdb umount /tests/yamlcpp
. As we can see above the value containing metadata is marked by the tag handle !elektra/meta
. The data type contains a list with two elements. The first element of this list specifies the value of the key, while the second element contains a map saving the metadata for the key. The data above represents the following key set in Elektra if we mount the file directly to the namespace user
:
Name | Value | Metaname | Metavalue |
---|---|---|---|
user/key without metadata | value1 | β | β |
user/key with metadata | value2 | metakey | metavalue |
empty metakey | β | ||
another metakey | another metavalue |
. The example below shows how we can read and write metadata using the yamlcpp
plugin via kdb
.
`` <h1>Mount yamlcpp plugin to cascading namespace
/tests/yamlcpp` sudo kdb mount config.yaml user/tests/yamlcpp yamlcpp
echo "π: !elektra/meta [π¦, {comment: Unicorn}]" > kdb file user/tests/yamlcpp
kdb lsmeta user/tests/yamlcpp/π #> comment kdb getmeta user/tests/yamlcpp/π comment #> Unicorn
kdb set user/tests/yamlcpp/brand new kdb setmeta user/tests/yamlcpp/brand comment "The Devil And God Are Raging Inside Me" kdb setmeta user/tests/yamlcpp/brand rationale "Because I Love It"
kdb lsmeta user/tests/yamlcpp/brand #> comment #> rationale kdb getmeta user/tests/yamlcpp/brand rationale #> Because I Love It
kdb rm -r user/tests/yamlcpp sudo kdb umount user/tests/yamlcpp
sudo kdb mount config.yaml /tests/yamlcpp yamlcpp type kdb set /tests/yamlcpp/typetest/number 21 kdb setmeta /tests/yamlcpp/typetest/number check/type short
kdb set /tests/yamlcpp/typetest/number "One"
kdb get /tests/yamlcpp/typetest/number #> 21
kdb rm -r /tests/yamlcpp sudo kdb umount /tests/yamlcpp
/tests/binary
sudo kdb mount test.yaml /tests/binary yamlcpp
echo 'bin: !!binary aGk=' > kdb file /tests/binary
aGk=
to hi
and stores the value in binary form.kdb get
prints the data as hexadecimal byte values.kdb get /tests/binary/bin #>
bash
to convert the hexadecimal value returned by kdb get
bash -c 'printf kdb get /tests/binary/bin
' #> hi
kdb set /tests/binary/text mate
kdb get /tests/binary/text #> mate
aGk=
even after YAML CPP wrote a new configuration.grep -q 'bin: !.* aGk=' kdb file user/tests/binary
kdb rm -r /tests/binary sudo kdb umount /tests/binary
user/tests/yamlcpp
sudo kdb mount test.yaml user/tests/yamlcpp yamlcpp
kdb set user/tests/yamlcpp/null kdb set user/tests/yamlcpp/null/level1/level2 kdb setmeta user/tests/yamlcpp/null/level1/level2 comment 'Null key'
kdb ls user/tests/yamlcpp/null #> user/tests/yamlcpp/null #> user/tests/yamlcpp/null/level1/level2 kdb get -v user/tests/yamlcpp/null | grep -q 'The key is null.' kdb get -v user/tests/yamlcpp/null/level1/level2 | grep -q 'The key is null.'
kdb set user/tests/yamlcpp/empty "" kdb set user/tests/yamlcpp/empty/level1/level2
kdb ls user/tests/yamlcpp/empty #> user/tests/yamlcpp/empty #> user/tests/yamlcpp/empty/level1/level2 kdb get -v user/tests/yamlcpp/empty | grep -vq 'The key is null.'
kdb rm -r user/tests/yamlcpp sudo kdb umount user/tests/yamlcpp
stores all of the values (π
, leaf
and π
) in the leaves of the mapping. The drawing below makes this situation a little bit clearer.
The key set that this plugin creates using the data above looks like this (assuming we mount the plugin to user/tests/yamlcpp
):
Name | Value |
---|---|
user/tests/yamlcpp/level | |
user/tests/yamlcpp/level 1/level 2 | |
user/tests/yamlcpp/level 1/level 2/level 3 | π |
user/tests/yamlcpp/root | |
user/tests/yamlcpp/root/below root | leaf |
user/tests/yamlcpp/root/subtree | π |
. Now why is this plugin unable to store values outside leaf nodes? For example, why can we not store a value inside user/tests/yamlcpp/level 1/level 2
? To answer this question we need to look at the YAML representation:
. In a naive approach we might just try to add a value e.g. π
right next to level 2:
. This however would be not correct, since then the YAML node level 2
would contain both a scalar value (π
) and a mapping ({ level 3: π }
). We could solve this dilemma using a list:
. However, if we use this approach we are not able to support Elektraβs array type properly.
To overcome the limitation described above, the YAML CPP plugin requires the Directory Value plugin. This plugin converts the value of a non-leaf node to a leaf node with the name ___dirdata
. For example, let us assume we have the following key set:
. The Directory Value plugin will convert the key set in the set (write) direction to
. Consequently the YAML plugin will store the key set as
. A user of the YAML plugin will not notice this feature unless he edits the configuration file by hand, as the following example shows:
`` <h1>Mount YAML CPP plugin at
user/tests/yamlcpp` sudo kdb mount test.yaml user/tests/yamlcpp yamlcpp
kdb set user/tests/yamlcpp/directory 'Directory Data' kdb setmeta user/tests/yamlcpp/directory comment 'Directory Metadata' kdb set user/tests/yamlcpp/directory/file 'Leaf Data'
kdb ls user/tests/yamlcpp/directory #> user/tests/yamlcpp/directory #> user/tests/yamlcpp/directory/file
kdb get user/tests/yamlcpp/directory #> Directory Data kdb getmeta user/tests/yamlcpp/directory comment #> Directory Metadata kdb get user/tests/yamlcpp/directory/file #> Leaf Data
kdb rm -r user/tests/yamlcpp sudo kdb umount user/tests/yamlcpp
kdb mount default.yaml / yamlcpp ```
all tests should work correctly. The problem here is that Elektra does not load additional required plugins (infos/needs
) for a default storage plugin.