Strict separation of settings from code.
DecoupleR is an attempt to port the excellent python-decouple library into R.
As stated by its original author, python-decouple makes it easy to:
- store parameters in ini or .env files;
- define comprehensive default values;
- properly convert values to the correct data type;
- have only one configuration module to rule all your instances.
DecoupleR general behavior have been in order to mimic as much as possible python-decouple, and is tested against python-decouple unit tests.
Priority rules and config files serach
DecoupleR always searches for Options in this order:
- Environment variables;
- Repository: ini or .env file;
- Default argument passed to config.
It has to be noted that environment variables have precedence over config files in order to be unix consistent.
Config file search
The config files are searched for, by default, in the working directory, or in the any other directory provided via the path argument. If no config files, settings.ini or .env is found in the directory, the search will continue in its parent directories.
Example
On-the-fly parameter evaluation
Parameter value can be retrieve anytime by invoking the DecoupleR::get_var function. If the config parameter is not provided, a config file search will be carried out at each function call.
get_var('R_HOME')
Implementated parser
DecoupleR supports both .ini and .env files.
undefined parameters
On the above example, all configuration parameters except SECRET_KEY = config('SECRET_KEY')
have a default value to fallback if it does not exist on the .env
file.
If SECRET_KEY
is not present in the .env
, DecoupleR will raise an error.
This fail fast policy helps you avoid chasing misbehaviors when you eventually forget a parameter.
Casting argument
By default, all values returned by DecoupleR
are strings
.
To easily specify a returned type, the get_var
function accepts a cast
argument which receives any callable, that will be used to transform the string value into something else. Some prefedined transformation have also been implementated:
- Integer:
int
,integer
- Bolean:
bool
,boolean
,logical
- Float:
float