Elektra
0.9.2
|
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
.
`` @section autotoc_md761 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 /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
sudo kdb mount config.yaml user/tests/yamlcpp yamlcpp
echo 'sunny:' > kdb file user/tests/yamlcpp
echo ' - Charlie' >> kdb file user/tests/yamlcpp
echo ' - Dee' >> kdb file user/tests/yamlcpp
kdb ls user/tests/yamlcpp #> user/tests/yamlcpp/sunny #> user/tests/yamlcpp/sunny/#0 #> user/tests/yamlcpp/sunny/#1
kdb get user/tests/yamlcpp/sunny/#1 #> Dee
kdb meta-get user/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 meta-get user/tests/yamlcpp/sunny array #> #_10 kdb get user/tests/yamlcpp/sunny/#_9
kdb get user/tests/yamlcpp/sunny/$(kdb meta-get user/tests/yamlcpp/sunny array) #> The Waitress
kdb meta-set user/tests/yamlcpp/empty array '' kdb export user/tests/yamlcpp/empty yamlcpp #> []
kdb set user/tests/yamlcpp/movies kdb set user/tests/yamlcpp/movies/#0 'A Silent Voice' kdb export user/tests/yamlcpp/movies yamlcpp #> "#0": A Silent Voice kdb meta-set user/tests/yamlcpp/movies array '' kdb export user/tests/yamlcpp/movies yamlcpp #> - A Silent Voice
kdb rm -r user/tests/yamlcpp sudo kdb umount user/tests/yamlcpp
sudo kdb mount config.yaml user/tests/yamlcpp yamlcpp
kdb set user/tests/yamlcpp/key value kdb set user/tests/yamlcpp/array kdb set user/tests/yamlcpp/array/#0 scalar kdb set user/tests/yamlcpp/array/#1/key value kdb set user/tests/yamlcpp/array/#1/π π kdb meta-set user/tests/yamlcpp/array array '#1'
kdb ls user/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 user/tests/yamlcpp/array/#1/key #> value
echo "user/tests/yamlcpp/array: β`kdb get user/tests/yamlcpp/array`β" #> user/tests/yamlcpp/array: ββ
kdb rm user/tests/yamlcpp/array/#1/key
kdb ls user/tests/yamlcpp #> user/tests/yamlcpp/array #> user/tests/yamlcpp/array/#0 #> user/tests/yamlcpp/array/#1/π #> user/tests/yamlcpp/key
kdb rm user/tests/yamlcpp/key kdb file user/tests/yamlcpp | xargs cat #> array: #> - scalar #> - π: π
kdb rm -r user/tests/yamlcpp sudo kdb umount user/tests/yamlcpp
sudo kdb mount config.yaml user/tests/yamlcpp yamlcpp
kdb set user/tests/yamlcpp/#0/map/#1/#0 value kdb set user/tests/yamlcpp kdb meta-set user/tests/yamlcpp array '#0' kdb set user/tests/yamlcpp/#0/map kdb meta-set user/tests/yamlcpp/#0/map array '#1' kdb set user/tests/yamlcpp/#0/map/#1 kdb meta-set user/tests/yamlcpp/#0/map/#1 array '#0' kdb file user/tests/yamlcpp | xargs cat #> - map: #> - ~ #> - #> - value
kdb ls user/tests/yamlcpp #> user/tests/yamlcpp #> user/tests/yamlcpp/#0/map #> user/tests/yamlcpp/#0/map/#0 #> user/tests/yamlcpp/#0/map/#1 #> user/tests/yamlcpp/#0/map/#1/#0
kdb rm -r user/tests/yamlcpp sudo kdb umount user/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
.
`` @section autotoc_md802 Mount yamlcpp plugin to
user/tests/yamlcpp` sudo kdb mount config.yaml user/tests/yamlcpp yamlcpp
echo "π: !elektra/meta [π¦, {comment: Unicorn}]" > kdb file user/tests/yamlcpp
kdb meta-ls user/tests/yamlcpp/π #> comment kdb meta-get user/tests/yamlcpp/π comment #> Unicorn
kdb set user/tests/yamlcpp/brand new kdb meta-set user/tests/yamlcpp/brand comment "The Devil And God Are Raging Inside Me" kdb meta-set user/tests/yamlcpp/brand rationale "Because I Love It"
kdb meta-ls user/tests/yamlcpp/brand #> comment #> rationale kdb meta-get 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 user/tests/yamlcpp yamlcpp type kdb set user/tests/yamlcpp/typetest/number 21 kdb meta-set user/tests/yamlcpp/typetest/number check/type short
kdb set user/tests/yamlcpp/typetest/number "One"
kdb get user/tests/yamlcpp/typetest/number #> 21
kdb rm -r user/tests/yamlcpp sudo kdb umount user/tests/yamlcpp
sudo kdb mount test.yaml user/tests/binary yamlcpp
echo 'bin: !!binary aGk=' > kdb file user/tests/binary
kdb get user/tests/binary/bin #> \x68\x69
kdb set user/tests/binary/text mate
kdb get user/tests/binary/text #> mate
grep -q 'bin: !.* aGk=' kdb file user/tests/binary
kdb rm -r user/tests/binary sudo kdb umount user/tests/binary
sudo kdb mount test.yaml user/tests/yamlcpp yamlcpp
kdb set user/tests/yamlcpp/null kdb set user/tests/yamlcpp/null/level1/level2 kdb meta-set 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
sudo kdb mount config.yaml user/tests/yamlcpp yamlcpp
echo 'truth: true' > kdb file user/tests/yamlcpp
kdb get user/tests/yamlcpp/truth #> 1
kdb meta-get user/tests/yamlcpp/truth type #> boolean
kdb set user/tests/yamlcpp/success 0 kdb meta-set user/tests/yamlcpp/success type boolean kdb get user/tests/yamlcpp/success #> 0 kdb export user/tests/yamlcpp/success yamlcpp #> false
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:
`` @section autotoc_md831 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 meta-set 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 meta-get 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
sudo kdb mount test.yaml user/tests/yamlcpp yamlcpp
kdb set user/tests/yamlcpp/boolean true
kdb export user/tests/yamlcpp/boolean yamlcpp #> true
kdb get user/tests/yamlcpp/boolean #> 1
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.