Elektra
0.9.1
|
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_md783 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 meta-get user/tests/yamlcpp/movies array #> #0 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/#0 scalar kdb set user/tests/yamlcpp/array/#1/key value kdb set user/tests/yamlcpp/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 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_md823 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 set user/tests/yamlcpp/success 0 kdb get user/tests/yamlcpp/success #> 0
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.