
\begin{DoxyItemize}
\item infos = Information about type plugin is in keys below
\item infos/author = Klemens Böswirth \href{mailto:k.boeswirth+git@gmail.com}{\tt k.\+boeswirth+git@gmail.\+com}
\item infos/licence = B\+SD
\item infos/provides = check
\item infos/needs =
\item infos/placements = postgetstorage presetstorage
\item infos/status = maintained nodep libc tested unittest recommended
\item infos/metadata = check/type type check/enum check/enum/\# check/enum/multi
\item infos/description = type checker using C\+O\+B\+RA data types
\end{DoxyItemize}

This plugin is a type checker plugin using the {\ttfamily C\+O\+R\+BA} data types.

A common and successful type system happens to be C\+O\+R\+BA. The system is well suited because of the many well-\/defined mappings it provides to other programming languages.

The type checker plugin supports these types\+: {\ttfamily short}, {\ttfamily unsigned\+\_\+short}, {\ttfamily long}, {\ttfamily unsigned\+\_\+long}, {\ttfamily long\+\_\+long}, {\ttfamily unsigned\+\_\+long\+\_\+long}, {\ttfamily float}, {\ttfamily double}, {\ttfamily char}, {\ttfamily wchar}, {\ttfamily boolean}, {\ttfamily any}, {\ttfamily enum}, {\ttfamily string}, {\ttfamily wstring} and {\ttfamily octet}.


\begin{DoxyItemize}
\item Checking {\ttfamily any} will always be successful, regardless of the content.
\item {\ttfamily string} matches any non-\/empty key value.
\item {\ttfamily empty} only matches empty key values.
\item {\ttfamily octet} and {\ttfamily char} are equivalent to each other.
\item {\ttfamily enum} will do enum checking as described below.
\item To use {\ttfamily wchar} and {\ttfamily wstring} the function {\ttfamily mbstowcs(3)} must be able convert the key value into a wide character string. {\ttfamily wstring}s can be of any non-\/zero length, {\ttfamily wchar} must have exactly length 1.
\end{DoxyItemize}

\subsection*{Enums}

If a key is set to the type {\ttfamily enum} the plugin will look for the metadata array {\ttfamily check/enum/\#}.

For example\+: \begin{DoxyVerb}check/enum = #3
check/enum/#0 = small
check/enum/#1 = middle
check/enum/#2 = large
check/enum/#3 = huge
\end{DoxyVerb}


Only the values listed in this array will be accepted. The array indices don\textquotesingle{}t have to be continuous, using e.\+g. only {\ttfamily \#1}, {\ttfamily \#2} and {\ttfamily \#4} is also allowed. Just make sure {\ttfamily check/enum} is set to the largest index in the array.

Furthermore {\ttfamily check/enum/multi} may contain a separator character, that separates multiple allowed occurrences. If {\ttfamily check/enum/multi} contain more than a single character validation will fail.

For example\+: \begin{DoxyVerb}check/enum/multi = _
\end{DoxyVerb}


Then the value {\ttfamily middle\+\_\+small} would validate. {\ttfamily middle\+\_\+small\+\_\+small} would be allowed as well, because multi-\/values are treated like bitfields.

\subsubsection*{Enum Conversion}

If a key is set to type {\ttfamily enum} and additionally the metadata {\ttfamily check/enum/convert} is set to {\ttfamily 1}, the plugin will convert the string value into its index on kdb\+Get and back again on kdb\+Set.

For example, using the array from above, a key with value {\ttfamily large} will be converted into the value {\ttfamily 2}.

\subsection*{Example}


\begin{DoxyCode}
#Mount the plugin
sudo kdb mount ctypetest.dump user/tests/ctype dump ctype

#Store a character value
kdb set user/tests/ctype/key a

#Only allow character values
kdb setmeta user/tests/ctype/key check/type char
kdb get user/tests/ctype/key
#> a

#If we store another character everything works fine
kdb set user/tests/ctype/key b
kdb get user/tests/ctype/key
#> b

#If we try to store a string Elektra will not change the value
kdb set user/tests/ctype/key 'Not a char'
# RET:5
# ERROR:52
kdb get user/tests/ctype/key
#> b

#Undo modifications to the database
kdb rm user/tests/ctype/key
sudo kdb umount user/tests/ctype
\end{DoxyCode}


For enums\+:


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

sudo kdb mount ctypeenum.ecf user/tests/ctype dump ctype

# valid initial value + setup valid enum list
kdb set user/tests/ctype/value middle
kdb setmeta user/tests/ctype/value check/enum '#2'
kdb setmeta user/tests/ctype/value 'check/enum/#0' 'low'
kdb setmeta user/tests/ctype/value 'check/enum/#1' 'middle'
kdb setmeta user/tests/ctype/value 'check/enum/#2' 'high'
kdb setmeta user/tests/ctype/value check/type enum

# should succeed
kdb set user/tests/ctype/value low

# should fail with error 52
kdb set user/tests/ctype/value no
# RET:5
# ERROR:52
\end{DoxyCode}


Or with multi-\/enums\+:


\begin{DoxyCode}
# valid initial value + setup array with valid enums
kdb set user/tests/ctype/multivalue middle\_small
kdb setmeta user/tests/ctype/multivalue check/enum/#0 small
kdb setmeta user/tests/ctype/multivalue check/enum/#1 middle
kdb setmeta user/tests/ctype/multivalue check/enum/#2 large
kdb setmeta user/tests/ctype/multivalue check/enum/#3 huge
kdb setmeta user/tests/ctype/multivalue check/enum/multi \_
kdb setmeta user/tests/ctype/multivalue check/enum "#3"
kdb setmeta user/tests/ctype/multivalue check/type enum

# should succeed
kdb set user/tests/ctype/multivalue small\_middle

# should fail with error 52
kdb set user/tests/ctype/multivalue all\_small
# RET:5
# ERROR:52

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


For booleans\+:


\begin{DoxyCode}
# Mount plugin
sudo kdb mount config.ecf user/tests/ctype dump ctype

# By default the plugin uses `1` (true) and `0` (false) to represent boolean values
kdb set user/tests/ctype/truthiness false
kdb setmeta user/tests/ctype/truthiness type boolean
kdb get user/tests/ctype/truthiness
#> 0

# The plugin does not change ordinary values
kdb set user/tests/ctype/key value
kdb get user/tests/ctype/key
#> value

# Undo changes
kdb rm -r user/tests/ctype
sudo kdb umount user/tests/ctype
\end{DoxyCode}



\begin{DoxyCode}
sudo kdb mount config.ecf user/tests/ctype dump ctype
kdb set user/tests/ctype/truthiness 0
kdb setmeta user/tests/ctype/truthiness type boolean
 kdb set user/tests/ctype/truthiness yes
# RET: 0
 # Undo changes
kdb rm -r user/tests/ctype
sudo kdb umount user/tests/ctype
\end{DoxyCode}


\subsection*{Limitations}

Records are part of other plugins.

The {\ttfamily C\+O\+R\+BA} type system also has its limits. The types {\ttfamily string} and {\ttfamily enum} can be unsatisfactory. While string is too general and makes no limit on how the sequence of characters is structured, the enumeration is too finite. For example, it is not possible to say that a string is not allowed to have a specific symbol in it. Combine this plugin with other type checker plugins to circumvent such limitations. 