20 #include "envgatewayparser.h" 24 bool EnvironmentGatewayParser::parseEnvironmentGatewayConfigElement(
25 const json_t *element,
26 EnvironmentVariable &result,
27 const EnvironmentVariables &store)
30 if (!requireNonEmptyKeyValue(element,
"name", result.first)
31 || !requireNonEmptyKeyValue(element,
"value", result.second)) {
35 std::string mode =
"set";
36 std::string separator =
"";
39 log_error() <<
"Could not parse \"mode\" key";
44 std::transform(mode.begin(), mode.end(), mode.begin(), ::tolower);
46 std::vector<std::string> validModes = {
"set",
"prepend",
"append" };
47 if (validModes.end() == std::find(validModes.begin(), validModes.end(), mode)) {
48 log_error() <<
"Invalid mode, only " << validModes <<
" are valid";
53 log_error() <<
"Could not parse \"separator\" key";
57 if (store.count(result.first) == 0) {
59 log_info() <<
"Env variable \"" << result.first <<
"\" was configured to " 60 <<
"be appended/prepended but the variable has not previously" 61 <<
" been set, so it will be created. Value is set to: \"" 62 << result.second <<
"\"";
66 if (mode ==
"append") {
67 result.second = store.at(result.first) + separator + result.second;
69 }
else if (mode ==
"prepend") {
70 result.second = result.second + separator + store.at(result.first);
73 log_error() <<
"Env variable " << result.first
74 <<
" already defined with value : " << store.at(result.first);
80 bool EnvironmentGatewayParser::requireNonEmptyKeyValue(
const json_t *element,
81 const std::string key,
85 log_error() <<
"Key " << key <<
" missing or not a string in json configuration";
89 if (result.length() == 0) {
90 log_error() <<
"Value for " << key <<
" key is an empty string";
static bool readOptional(const json_t *element, const char *key, std::string &result)
Reads an optional value from a JSON object.
Developers guide to adding a config item:
static bool read(const json_t *element, const char *key, std::string &result)
Reads a string from a JSON Object.