Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1722 from sanssecours/🍁
Browse files Browse the repository at this point in the history
Directory Value Plugin & Minor Fixes
  • Loading branch information
markus2330 authored Dec 13, 2017
2 parents f270ff7 + 2cc311d commit 68eb32b
Show file tree
Hide file tree
Showing 33 changed files with 1,344 additions and 117 deletions.
3 changes: 3 additions & 0 deletions .oclint
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ rule-configurations:
value: 140
disable-rules:
- GotoStatement
max-priority-1: 0
max-priority-2: 0
max-priority-3: 0
2 changes: 1 addition & 1 deletion doc/METADATA.ini
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ type=namespace
status=implemented
usedby/api= ksLookup
description=Typically all namespaces are cascaded one after the other
in the build-in order.
in the built-in order.
Using this metadata you can avoid that some namespaces are used or
can change the order.

Expand Down
1 change: 1 addition & 0 deletions doc/markdownlinkconverter/elektraSpecialCharacters.sty
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
\DeclareUnicodeCharacter{0101}{\=a}
\newunicodechar{⏎}{$\hookleftarrow$} % \hookleftarrow ≅ ↩︎
\newunicodechar{🔑}{key}
\newunicodechar{😭}{loudly crying face}
\newunicodechar{🦄}{unicorn}
\newunicodechar{👾}{alien}
\newunicodechar{🐳}{whale}
Expand Down
6 changes: 3 additions & 3 deletions doc/news/_preparation_next_release.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ For a small demo see here:

## Highlights

- <<HIGHLIGHT1>>
- New plugin: [`directoryvalue`](https://www.libelektra.org/plugins/directoryvalue)
- <<HIGHLIGHT2>>
- <<HIGHLIGHT3>>

### New Plugins

### <<HIGHLIGHT1>>

- The [Directory Value plugin](https://www.libelektra.org/plugins/directoryvalue) supports storage plugins such as [YAJL](https://www.libelektra.org/plugins/yajl) and [YAML CPP ](https://www.libelektra.org/plugins/yamlcpp). It adds extra leaf values for directories (keys with children) that store the data of their parents. This way plugins that normally are only able to store values in leaf keys are able to support arbitrary key sets.

### <<HIGHLIGHT2>>

Expand Down
11 changes: 4 additions & 7 deletions doc/tutorials/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ The most direct way to validate keys is
sudo kdb mount validation.dump user/tutorial/together dump validation
kdb vset user/tutorial/together/test 123 "[1-9][0-9]*" "Not a number"
kdb set user/tutorial/together/test abc
# STDERR: The command kdb set failed while accessing the key database .*⏎
# Sorry, the error .#42. occurred ;(⏎
# Description: key value failed to validate⏎
# .*Reason: Not a number.*
# STDERR: The command kdb set failed while accessing the key database .*
# ERROR: 42
# RET:5
```

Expand Down Expand Up @@ -241,9 +239,8 @@ loads the validation plugin.
```sh
kdb set /tutorial/links/url "invalid url"
#> Using name user/tutorial/links/url
# STDERR: .*Sorry, the error.*#42.*occurred.*⏎
# Description: Key Value failed to validate⏎
# .*Reason: not a valid URL.*
# STDERR: .*key value failed to validate.*not a valid URL.*
# ERROR: 42
# RET: 5
```

Expand Down
2 changes: 1 addition & 1 deletion scripts/kdb.fish
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ function __fish_kdb_subcommand_supports_option_debug -d 'Check if the current su
end

function __fish_kdb_subcommand_supports_option_force -d 'Check if the current subcommand supports the option force'
__fish_kdb_subcommand_includes check merge
__fish_kdb_subcommand_includes check merge rm
end

function __fish_kdb_subcommand_supports_option_null -d 'Check if the current subcommand supports binary null termination'
Expand Down
2 changes: 1 addition & 1 deletion scripts/sed
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ s/([^.`])git[hH]ub([^.][^\w])/\1GitHub\2/g

s/\<[Ll]ibreoffice/LibreOffice/g

s/\<mark[Dd]own\>/Markdown/g
s/([^/])\<mark[Dd]own\>/\1Markdown/g

s/\<unix|UNIX/Unix/g
s/\<XCode\>/Xcode/g
Expand Down
14 changes: 14 additions & 0 deletions src/error/specification
Original file line number Diff line number Diff line change
Expand Up @@ -1167,3 +1167,17 @@ severity:error
ingroup:plugin
module:ruby
macro:RUBY_GEN_ERROR

number:188
description:Unable to append directory prefix
severity:error
ingroup:plugin
module:directoryvalue
macro:DIRECTORY_VALUE_APPEND

number:189
description:Unable to insert array value
severity:error
ingroup:plugin
module:directoryvalue
macro:DIRECTORY_VALUE_ARRAY
1 change: 1 addition & 0 deletions src/include/kdbease.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extern "C" {
#endif

int elektraArrayIncName (Key * key);
int elektraArrayDecName (Key * key);
int elektraKsFilter (KeySet * result, KeySet * input, int (*filter) (const Key * k, void * argument), void * argument);

const char * elektraKeyGetRelativeName (Key const * cur, Key const * parentKey);
Expand Down
105 changes: 50 additions & 55 deletions src/libs/ease/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,46 +32,28 @@
*/
int elektraArrayValidateName (const Key * key)
{
if (!key)
{
return -1;
}
const char * current;
if (!key || !(current = keyBaseName (key)) || *current != '#') return -1;
if (!strcmp (current, "#")) return 0;

const char * current = keyBaseName (key);

if (!current)
{
return -1;
}
current++;
int underscores = 0;
int digits = 0;

if (!strcmp (current, "#"))
while (*current == '_')
{
return 0;
current++;
underscores++;
}

if (*current == '#')
while (isdigit ((unsigned char)*current))
{
current++;
int underscores = 0;
int digits = 0;

for (; *current == '_'; current++)
{
underscores++;
}

for (; isdigit ((unsigned char)*current); current++)
{
digits++;
}

if (underscores != digits - 1) return -1;
if (underscores + digits > ELEKTRA_MAX_ARRAY_SIZE - 2)
{
return -1;
}
digits++;
}
else

if (underscores != digits - 1) return -1;
if (underscores + digits > ELEKTRA_MAX_ARRAY_SIZE - 2)
{
return -1;
}
Expand Down Expand Up @@ -135,32 +117,14 @@ int elektraArrayIncName (Key * key)
const char * baseName = keyBaseName (key);

int arrayElement = elektraArrayValidateName (key);
if (arrayElement == -1)
{
return -1;
}
if (arrayElement == -1) return -1;

++baseName; // jump over #
while (*baseName == '_') // jump over all _
{
++baseName;
}
while (*(++baseName) == '_') // jump over initial `#` and all `_`
; //! OCLint

kdb_long_long_t oldIndex = 0;
if (!arrayElement)
{
// we have a start element
oldIndex = -1;
}
else
{
if (elektraReadArrayNumber (baseName, &oldIndex) == -1)
{
return -1;
}
}

kdb_long_long_t newIndex = oldIndex + 1; // we increment by one
if (arrayElement && elektraReadArrayNumber (baseName, &oldIndex) == -1) return -1;
kdb_long_long_t newIndex = arrayElement ? oldIndex + 1 : 0; // we increment by one or use 0 if the name contains no index yet

char newName[ELEKTRA_MAX_ARRAY_SIZE];

Expand All @@ -170,6 +134,37 @@ int elektraArrayIncName (Key * key)
return 0;
}

/**
* @brief Decrement the name of an array key by one.
*
* The alphabetical order will remain intact. For example,
* `user/abc/\#_10` will be changed to `user/abc/\#9`.
*
* @param This parameter determines the key name this function decrements.
*
* @retval -1 on error (e.g. new array index too small, non-valid array)
* @retval 0 on success
*/
int elektraArrayDecName (Key * key)
{
const char * baseName = keyBaseName (key);

int arrayElement = elektraArrayValidateName (key);
if (arrayElement == -1) return -1;

while (*(++baseName) == '_') // jump over initial `#` and all `_`
; //! OCLint

kdb_long_long_t oldIndex = 0;
if (elektraReadArrayNumber (baseName, &oldIndex) == -1 || oldIndex == 0) return -1;

char newName[ELEKTRA_MAX_ARRAY_SIZE];
elektraWriteArrayNumber (newName, oldIndex - 1);
keySetBaseName (key, newName);

return 0;
}

/**
* @internal
*
Expand Down
2 changes: 1 addition & 1 deletion src/libs/elektra/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ if ((buffer = elektraMalloc (length)) == 0) {
*
* @param size the requested size
*
* This function is compatible to ANSI-C elektraMalloc
* This function is compatible to ANSI-C malloc
* @see elektraFree
* @see elektraCalloc
*/
Expand Down
1 change: 1 addition & 0 deletions src/plugins/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ Rewrite unwanted characters with different techniques:

Transformations:

- [directoryvalue](directoryvalue/) converts directory values to leaf values
- [keytometa](keytometa/) transforms keys to metadata
- [rename](rename/) renames keys according to different rules
- [boolean](boolean/) canonicalizes boolean keys
Expand Down
15 changes: 5 additions & 10 deletions src/plugins/base64/base64.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,11 @@ static int unescape (Key * key, Key * parent)
*/
static bool shouldDecode (Key * key, bool metaMode)
{
if (metaMode)
{
return keyGetMeta (key, "type") && strcmp (keyValue (keyGetMeta (key, "type")), "binary") == 0;
}
else
{
const char * strVal = keyString (key);
return strlen (strVal) >= ELEKTRA_PLUGIN_BASE64_PREFIX_LENGTH &&
strncmp (strVal, ELEKTRA_PLUGIN_BASE64_PREFIX, ELEKTRA_PLUGIN_BASE64_PREFIX_LENGTH) == 0;
}
if (metaMode) return keyGetMeta (key, "type") && strcmp (keyValue (keyGetMeta (key, "type")), "binary") == 0;

const char * strVal = keyString (key);
return strlen (strVal) >= ELEKTRA_PLUGIN_BASE64_PREFIX_LENGTH &&
strncmp (strVal, ELEKTRA_PLUGIN_BASE64_PREFIX, ELEKTRA_PLUGIN_BASE64_PREFIX_LENGTH) == 0;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/camel/camel.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ static parserType * bufferChar (parserType * const parser)
{
return setErrorMalloc (parser, bufferSize);
}
strncpy (parser->bufferBase + bufferOffset, line, numberCharsRead + 1);
strncpy (parser->bufferBase + bufferOffset, line, numberCharsRead + 1); //! OCLint (constant conditional operator)
parser->buffer = parser->bufferBase + bufferOffset;
free (line);

Expand Down Expand Up @@ -439,7 +439,7 @@ static parserType * saveText (parserType * const parser, char ** location)
*location = elektraMalloc (length + 1);
if (!*location) return setErrorMalloc (parser, length + 1);

strncpy (*location, parser->match, length);
strncpy (*location, parser->match, length); //! OCLint (constant conditional operator)
(*location)[length] = '\0';

return parser;
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/conditionals/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ kdb setmeta user/examples/conditionals/key check/condition "(../hkey == 'hello')
# will fail
kdb setmeta user/examples/conditionals/key check/condition "(../hkey == 'hello') ? (../fkey == '5.0')"
# RET:5
# ERRORS:135
# ERROR:135
```

Assignment example:
Expand Down Expand Up @@ -117,7 +117,7 @@ kdb export /examples/conditionals ini
#> sub/key = false
#> #@META check/condition = (./ == 'val1') ? (../sub/key == 'true')
#> key1 = val1
# ERRORS:135
# ERROR:135
# Error (#135) occurred!
# Description: Validation failed
# Ingroup: plugin
Expand Down
59 changes: 59 additions & 0 deletions src/plugins/csvstorage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,65 @@ First line should determine the headers:

kdb mount test.csv /csv csvstorage "delimiter=;,header=colname,columns=2,columns/names,columns/names/#0=col0Name,columns/names/#1=col1Name"

### Usage

The example below shows how you can use this plugin to read and write CSV files.

```sh
# Mount plugin to cascading namespace `/examples/csv`
# We use the column names from the first line of the
# config file as key names
sudo kdb mount config.csv /examples/csv csvstorage "header=colname,columns/names/#0=col0Name,columns/names/#1=col1Name"

# Add some data
printf 'band,album\n' >> `kdb file /examples/csv`
printf 'Converge,All We Love We Leave Behind\n' >> `kdb file /examples/csv`
printf 'mewithoutYou,Pale Horses\n' >> `kdb file /examples/csv`
printf 'Kate Tempest,Everybody Down\n' >> `kdb file /examples/csv`

kdb ls /examples/csv
#> user/examples/csv/#0
#> user/examples/csv/#0/album
#> user/examples/csv/#0/band
#> user/examples/csv/#1
#> user/examples/csv/#1/album
#> user/examples/csv/#1/band
#> user/examples/csv/#2
#> user/examples/csv/#2/album
#> user/examples/csv/#2/band
#> user/examples/csv/#3
#> user/examples/csv/#3/album
#> user/examples/csv/#3/band

# The first array element contains the column names
kdb get /examples/csv/#0/band
#> band
kdb get /examples/csv/#0/album
#> album

# Retrieve data from the last entry
kdb get /examples/csv/#3/album
#> Everybody Down
kdb get /examples/csv/#3/band
#> Kate Tempest

# Change an existing item
kdb set /examples/csv/#1/album 'You Fail Me'
# Retrieve the new item
kdb get /examples/csv/#1/album
#> You Fail Me

# The configuration file reflects the changes
kdb file /examples/csv | xargs cat
#> album,band
#> You Fail Me,Converge
#> Pale Horses,mewithoutYou
#> Everybody Down,Kate Tempest

# Undo changes to the key database
kdb rm -r /examples/csv
sudo kdb umount /examples/csv
```

## Limitations

Expand Down
10 changes: 10 additions & 0 deletions src/plugins/directoryvalue/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
include (LibAddMacros)

add_plugin (directoryvalue
SOURCES
directoryvalue.h
directoryvalue.c
LINK_ELEKTRA
elektra-ease
ADD_TEST
)
Loading

0 comments on commit 68eb32b

Please sign in to comment.