
\begin{DoxyItemize}
\item infos = Information about the conditionals plugin is in keys below
\item infos/author = Thomas Waser \href{mailto:thomas.waser@libelektra.org}{\tt thomas.\+waser@libelektra.\+org}
\item infos/licence = B\+SD
\item infos/provides =
\item infos/needs =
\item infos/recommends =
\item infos/placements = postgetstorage presetstorage
\item infos/status = unittest nodep libc global preview
\item infos/metadata = check/condition assign/condition condition/validsuffix check/condition/any/\# check/condition/all/\# check/condition/none/\# assign/condition/\#
\item infos/description = ensures key values through conditions
\end{DoxyItemize}

This plugin uses if-\/then-\/else like conditions. It also works as global plugin.

\subsection*{Check Syntax}

Stored in the metakey {\ttfamily check/condition} to validate data is\+:

{\ttfamily (I\+F-\/condition) ? (T\+H\+E\+N-\/condition) \+: (E\+L\+S\+E-\/condition)} where the E\+L\+S\+E-\/condition is optional

Condition\+: {\ttfamily Key} {\itshape Operation} `(\textquotesingle{}String\textquotesingle{} $\vert$ \textquotesingle{}1234.\+56\textquotesingle{} $\vert$ Key $\vert$ \textquotesingle{}\textquotesingle{})`

Operations\+: {\ttfamily !=, ==, $<$, $<$=, =$>$, $>$, \+:=}, where\+:


\begin{DoxyItemize}
\item {\ttfamily \+:=} is used to set a key value
\item others are for comparison as in C
\end{DoxyItemize}

\subsubsection*{Testing if Key Exists}

{\ttfamily (! a/key)} evaluates to true if the key {\ttfamily a/key} doesn\textquotesingle{}t exist, to false if it exists.

\subsubsection*{Assign Syntax}


\begin{DoxyCode}
(IF-condition) ? ('ThenValue') : ('ElseValue')
\end{DoxyCode}


Depending on if the condition is met, either \textquotesingle{}Then\+Value\textquotesingle{} or \textquotesingle{}Else\+Value\textquotesingle{} will be assigned as key value if the metakey {\ttfamily assign/condition} is used.

\subsubsection*{Experimental\+: Nested Conditions}

Multiple conditions can be nested and combined using parentheses and {\ttfamily \&\&} (logical A\+ND) or {\ttfamily $\vert$$\vert$} (logical OR). Additional parentheses must be used to form valid conditions again. {\ttfamily (} {\ttfamily (condition 1) \&\& (condition 2)} {\ttfamily )}

\subsubsection*{Valid Suffix}

The {\ttfamily 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 = \textquotesingle{}m\textquotesingle{}, \textquotesingle{}cm\textquotesingle{}, \textquotesingle{}km\textquotesingle{}{\ttfamily would treat}2.\+3m{\ttfamily just as the numeric value}2.\+3` when comparing to another value having the same or no suffix.

\subsubsection*{Keynames}

Keynames are all either relative to to-\/be-\/tested key (starting with {\ttfamily ./} or {\ttfamily ../}), relative to the parentkey (starting with {\ttfamily @/}) or absolute (e.\+g. {\ttfamily system/key}).

\subsubsection*{Multiple Statements}

It\textquotesingle{}s also possible to test multiple conditions using {\ttfamily check/condition/\{any,all,none\}} as a meta array. Where {\ttfamily any} means that at least one statement has to evaluate to true, {\ttfamily all} that all statements have to evaluate to true, and {\ttfamily none} that no statement is allowed to evaluate to false (default). For multiple assign statements use {\ttfamily assign/condition} as a meta array. The first {\ttfamily assign/condition/\#} statement that evaluates to true will be assigned and the rest ignored.

\subsection*{Example}


\begin{DoxyCode}
(this/key  != 'value') ? (then/key == some/other/key) : (or/key <= '125')
\end{DoxyCode}


Meaning\+: IF {\ttfamily this/key} N\+OT E\+Q\+U\+AL TO {\ttfamily \textquotesingle{}value\textquotesingle{}} T\+H\+EN {\ttfamily then/key} M\+U\+ST E\+Q\+U\+AL {\ttfamily some/other/key} E\+L\+SE {\ttfamily or/key} M\+U\+ST BE L\+E\+SS T\+H\+AN {\ttfamily 125}

Another full example\+:


\begin{DoxyCode}
#Backup-and-Restore:user/tests/conditionals

sudo kdb mount conditionals.dump user/tests/conditionals conditionals dump

kdb set user/tests/conditionals/fkey 3.0
kdb set user/tests/conditionals/hkey hello

# will succeed
kdb setmeta user/tests/conditionals/key check/condition "(../hkey == 'hello') ? (../fkey == '3.0')"

# will fail
kdb setmeta user/tests/conditionals/key check/condition "(../hkey == 'hello') ? (../fkey == '5.0')"
# RET:5
# ERROR:C03200
\end{DoxyCode}


Assignment example\+:


\begin{DoxyCode}
kdb set user/tests/conditionals/hkey Hello
kdb setmeta user/tests/conditionals/hkey assign/condition "(./ == 'Hello') ? ('World')"
# alternative syntax: "(../hkey == 'Hello') ? ('World')

kdb get user/tests/conditionals/hkey
#> World

# cleanup
kdb rm -r user/tests/conditionals
sudo kdb umount user/tests/conditionals
\end{DoxyCode}


Global plugin example\+:

``` \section*{Backup old list of global plugins}

kdb set user/tests/msr  kdb export system/elektra/globalplugins $>$ \$(kdb get user/tests/msr)

sudo kdb mount main.\+ini /tests/conditionals ni sudo kdb mount sub.\+ini /tests/conditionals/sub ini

\section*{mount conditionals as global plugin}

sudo kdb global-\/mount conditionals $\vert$$\vert$ \$(exit 0)

\section*{create testfiles}

echo \textquotesingle{}key1=val1\textquotesingle{} $>$ {\ttfamily kdb file /tests/conditionals} echo \textquotesingle{}\mbox{[}key1\mbox{]}\textquotesingle{} $>$$>$ {\ttfamily kdb file /tests/conditionals} echo \char`\"{}check/condition=(./ == \textquotesingle{}val1\textquotesingle{}) ? (../sub/key == \textquotesingle{}true\textquotesingle{})\char`\"{} $>$$>$ {\ttfamily kdb file /tests/conditionals}

echo \char`\"{}key=false\char`\"{} $>$ {\ttfamily kdb file /tests/conditionals/sub}

\section*{should fail and yield an error}

kdb export /tests/conditionals ini \#$>$ sub/key=false \#$>$ \# check/condition = (./ == \textquotesingle{}val1\textquotesingle{}) ? (../sub/key == \textquotesingle{}true\textquotesingle{}) \#$>$ key1=val1 \section*{E\+R\+R\+OR\+:C03200}

\section*{Sorry, module conditionals issued the error C03200\+:}

\section*{Validation failed\+: Validation of Key key1\+: (./ == \textquotesingle{}val1\textquotesingle{}) ? (../sub/key == \textquotesingle{}true\textquotesingle{}) failed. ((../sub/key == \textquotesingle{}true\textquotesingle{}) failed)}

kdb set /tests/conditionals/sub/key true

\section*{should succeed}

kdb export /tests/conditionals ini \#$>$ sub/key=true \#$>$ \# check/condition = (./ == \textquotesingle{}val1\textquotesingle{}) ? (../sub/key == \textquotesingle{}true\textquotesingle{}) \#$>$ key1=val1

\section*{cleanup}

kdb rm -\/r /tests/conditionals sudo kdb umount /tests/conditionals/sub sudo kdb umount /tests/conditionals

sudo kdb global-\/umount conditionals

kdb rm -\/r system/elektra/globalplugins kdb import system/elektra/globalplugins $<$ \$(kdb get user/tests/msr) rm \$(kdb get user/tests/msr) kdb rm user/tests/msr ``` 