Elektra
0.8.20
|
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
/examples/yamlcpp` sudo kdb mount config.yaml /examples/yamlcpp yamlcpp
echo "π : π³" > kdb file /examples/yamlcpp
kdb get /examples/yamlcpp/π #> π³
echo "some key: @some value" >> kdb file /examples/yamlcpp
kdb get "/examples/yamlcpp/some key"
echo "π: value" > kdb file /examples/yamlcpp
kdb set
kdb set /examples/yamlcpp/fleetwood mac kdb set /examples/yamlcpp/the chain
kdb get /examples/yamlcpp/the #> chain kdb get /examples/yamlcpp/fleetwood #> mac
kdb rm -r /examples/yamlcpp sudo kdb umount /examples/yamlcpp
/examples/yamlcpp
sudo kdb mount config.yaml /examples/yamlcpp yamlcpp
echo 'sunny:' > kdb file /examples/yamlcpp
echo ' - Charlie' >> kdb file /examples/yamlcpp
echo ' - Dee' >> kdb file /examples/yamlcpp
kdb ls /examples/yamlcpp #> user/examples/yamlcpp/sunny #> user/examples/yamlcpp/sunny/#0 #> user/examples/yamlcpp/sunny/#1
kdb get user/examples/yamlcpp/sunny/#1 #> Dee
array
kdb getmeta /examples/yamlcpp/sunny array
kdb set user/examples/yamlcpp/sunny/#2 Dennis kdb set user/examples/yamlcpp/sunny/#3 Frank kdb set user/examples/yamlcpp/sunny/#4 Mac
kdb get user/examples/yamlcpp/sunny/$(kdb getmeta user/examples/yamlcpp/sunny array) #> Mac
kdb rm -r /examples/yamlcpp sudo kdb umount /examples/yamlcpp
/examples/yamlcpp
sudo kdb mount config.yaml /examples/yamlcpp yamlcpp
kdb set /examples/yamlcpp/key value kdb set /examples/yamlcpp/array/#0 scalar kdb set /examples/yamlcpp/array/#1/key value kdb set /examples/yamlcpp/array/#1/π π
kdb ls /examples/yamlcpp #> user/examples/yamlcpp/array #> user/examples/yamlcpp/array/#0 #> user/examples/yamlcpp/array/#1 #> user/examples/yamlcpp/array/#1/key #> user/examples/yamlcpp/array/#1/π #> user/examples/yamlcpp/key
kdb get /examples/yamlcpp/array/#1/key #> value
echo "/examples/yamlcpp/array: β`kdb get /examples/yamlcpp/array`β" #> /examples/yamlcpp/array: ββ kdb get /examples/yamlcpp/array/#1 echo "/examples/yamlcpp/array/#1: β`kdb get /examples/yamlcpp/array/#1`β" #> /examples/yamlcpp/array/#1: ββ
kdb rm /examples/yamlcpp/array/#1/key
kdb ls /examples/yamlcpp #> user/examples/yamlcpp/array #> user/examples/yamlcpp/array/#0 #> user/examples/yamlcpp/array/#1 #> user/examples/yamlcpp/array/#1/π #> user/examples/yamlcpp/key
key
or array
could be the βfirstβ key -key
before we retrieve the data. This waykdb rm /examples/yamlcpp/key kdb file /examples/yamlcpp | xargs cat #> array: #> - scalar #> - π: π
kdb rm -r /examples/yamlcpp sudo kdb umount /examples/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
/examples/yamlcpp` sudo kdb mount config.yaml /examples/yamlcpp yamlcpp
echo "π: !elektra/meta [π¦, {comment: Unicorn}]" > kdb file /examples/yamlcpp
kdb lsmeta /examples/yamlcpp/π #> comment kdb getmeta /examples/yamlcpp/π comment #> Unicorn
kdb set /examples/yamlcpp/brand new kdb setmeta /examples/yamlcpp/brand comment "The Devil And God Are Raging Inside Me" kdb setmeta /examples/yamlcpp/brand rationale "Because I Love It"
kdb lsmeta /examples/yamlcpp/brand #> comment #> rationale kdb getmeta /examples/yamlcpp/brand rationale #> Because I Love It
kdb rm -r /examples/yamlcpp sudo kdb umount /examples/yamlcpp
sudo kdb mount config.yaml /examples/yamlcpp yamlcpp type kdb set /examples/yamlcpp/typetest/number 21 kdb setmeta /examples/yamlcpp/typetest/number check/type short
kdb set /examples/yamlcpp/typetest/number "One"
kdb get /examples/yamlcpp/typetest/number #> 21
kdb rm -r /examples/yamlcpp sudo kdb umount /examples/yamlcpp
/examples/binary
sudo kdb mount test.yaml /examples/binary yamlcpp
echo 'bin: !!binary aGk=' > kdb file /examples/binary
aGk=
to hi
and stores the value in binary form.kdb get
prints the data as hexadecimal byte values.kdb get /examples/binary/bin #>
ruby
to convert the hexadecimal value returned by kdb get
bash
or fish
as shell thenkdb get /examples/binary/bin
# Bashruby -e "print ARGV[0].split('\x')[1..-1].map {|byte| byte.to_i(16).chr }.join" kdb get /examples/binary/bin
#> hi
kdb set /examples/binary/text mate
kdb get /examples/binary/text #> mate
aGk=
even after YAML CPP wrote a new configuration.grep -q 'bin: !.* aGk=' kdb file user/examples/binary
kdb rm -r /examples/binary sudo kdb umount /examples/binary
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/examples/yamlcpp
):
Name | Value |
---|---|
user/examples/yamlcpp/level | |
user/examples/yamlcpp/level 1/level 2 | |
user/examples/yamlcpp/level 1/level 2/level 3 | π |
user/examples/yamlcpp/root | |
user/examples/yamlcpp/root/below root | leaf |
user/examples/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/examples/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.
tag:yaml.org,2002:binary
(shorthand: !!binary
).