Elektra
0.8.19
|
This plugin uses if-then-else like conditions. It also works as global plugin.
Stored in the metakey check/condition
to validate data is:
(IF-condition) ? (THEN-condition) : (ELSE-condition)
where the ELSE-condition is optional
Condition: Key
Operation `('String' | '1234.56' | Key | '')`
Operations: !=, ==, <, <=, =>, >, :=
, where:
:=
is used to set a key value(! a/key)
evaluates to true if the key a/key
doesn't exist, to false if it exists.
(IF-condition) ? ('ThenValue') : ('ElseValue')
Depending on if the condition is met, either 'ThenValue' or 'ElseValue' will be assigned as key value if the metakey assign/condition
is used.
Multiple conditions can be nested and combined using parentheses and &&
(logical AND) or ||
(logical OR). Additional parentheses must be used to form valid conditions again. (
(condition 1) && (condition 2)
)
The condition/validsuffix
can be used to define a list of valid suffixes to numeric values. If two operants have the same valid suffix or one of them no suffix they will be treated by their numeric value ignoring their suffix. `condition/validsuffix = 'm', 'cm', 'km'would treat
2.3mjust as the numeric value
2.3` when comparing to another value having the same or no suffix.
Keynames are all either relative to to-be-tested key (starting with ./
or ../
), relative to the parentkey (starting with @/
) or absolute (e.g. system/key
).
(this/key != 'value') ? (then/key == some/other/key) : (or/key <= '125')
Meaning: IF this/key
NOT EQUAL TO 'value'
THEN then/key
MUST EQUAL some/other/key
ELSE or/key
MUST BE LESS THAN 125
Another full example:
``` #Backup-and-Restore:/examples/conditionals sudo kdb mount conditionals.dump /examples/conditionals conditionals dump kdb set user/examples/conditionals/fkey 3.0 kdb set user/examples/conditionals/hkey hello
kdb setmeta user/examples/conditionals/key check/condition "(../hkey == 'hello') ? (../fkey == '3.0')"
kdb setmeta user/examples/conditionals/key check/condition "(../hkey == 'hello') ? (../fkey == '5.0')"
#
# kdb rm -r /examples/conditionals sudo kdb umount /examples/conditionals ```
Assignment example:
``` #Backup-and-Restore:/examples/conditionals sudo kdb mount conditionals.dump /examples/conditionals conditionals dump kdb set user/examples/conditionals/hkey Hello kdb setmeta user/examples/conditionals/hkey assign/condition "(./ == 'Hello') ? ('World')"
kdb get user/examples/conditionals/hkey World #
# kdb rm -r /examples/conditionals sudo kdb umount /examples/conditionals ```
Global plugin example:
``` #Backup-and-Restore:/examples/conditionals sudo kdb mount main.ini /examples/conditionals ni sudo kdb mount sub.ini /examples/conditionals/sub ini #
# sudo kdb global-mount conditionals #
# $ echo "key1 = val1" > kdb file /examples/conditionals
$ echo "[key1]" >> kdb file /examples/conditionals
$ echo "check/condition = (./ == 'val1') ? (../sub/key == 'true')" >> kdb file /examples/conditionals
$ echo "key = false" > kdb file /examples/conditionals/sub
#
# kdb export /examples/conditionals ini sub/key = false key1 = val1
kdb set /examples/conditionals/sub/key true #
# kdb export /examples/conditionals ini sub/key = true key1 = val1 #
# kdb rm -r /examples/conditionals sudo kdb umount /examples/conditionals/sub sudo kdb umount /examples/conditionals ```