variable_dict_from_file_type
Define a variable that contains key,value pairs (a dictionary) from a JSON, CSV or YAML file.
⚙️ Compatible targets: Linux
Parameters
Name | Documentation |
---|---|
prefix | The prefix of the variable name. This parameter is required. |
name | The variable to define, the full name will be prefix.name. This parameter is required. |
file_name | The file name to load data from. This parameter is required. |
file_type | The file type, can be "JSON", "CSV", "YAML" or "auto" for auto detection based on file extension, with a fallback to JSON (default is "auto"). Choices:
This parameter is optional. |
Outcome conditions
You need to replace ${name}
with its actual canonified value.
- ✅ Ok:
variable_dict_from_file_type_${name}_ok
- ☑️ Already compliant:
variable_dict_from_file_type_${name}_kept
- 🟨 Repaired:
variable_dict_from_file_type_${name}_repaired
- ☑️ Already compliant:
- ❌ Error:
variable_dict_from_file_type_${name}_error
Example
method: variable_dict_from_file_type
params:
prefix: VALUE
name: VALUE
file_name: VALUE
file_type: auto
Documentation
To use the generated variable, you must use the form ${prefix.name[key]}
with each name replaced with the parameters of this method.
Be careful that using a global variable can lead to unpredictable content in case of multiple definition, which is implicitly the case when a technique has more than one instance (directive).
This method will load data from various file formats (yaml, json, csv).
CSV parsing
The input file must use CRLF as line delimiter to be readable (as stated in RFC 4180).
Examples
# To read a json file with format auto detection
variable_dict_from_file_type("prefix", "var", "/tmp/file.json", "");
# To force yaml reading on a non file without yaml extension
variable_dict_from_file_type("prefix", "var", "/tmp/file", "YAML");
If /tmp/file.json
contains:
{
"key1": "value1"
}
You will be able to access the value1
value with ${prefix.var[key1]}
.