| 51 | } |
| 52 | |
| 53 | std::string JsonReader::fetch_string(const std::vector<std::string> &keys, const std::string &default_value, |
| 54 | const std::function<std::string(const std::string &value)> &validator) |
| 55 | { |
| 56 | json::json_pointer ptr = json::json_pointer(to_json_pointer(keys)); |
| 57 | try |
| 58 | { |
| 59 | std::string result; |
| 60 | if (j.at(ptr).is_string()) |
| 61 | { |
| 62 | result = j.at(ptr).get<std::string>(); |
| 63 | } |
| 64 | else if (j.at(ptr).is_number()) |
| 65 | { |
| 66 | result = std::to_string(j.at(ptr).get<int64_t>()); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | throw nlohmann::detail::other_error::create(900, "type should be string"); |
| 71 | } |
| 72 | if (nullptr != validator) |
| 73 | { |
| 74 | std::string error_description = validator(result); |
| 75 | if (!error_description.empty()) |
| 76 | { |
| 77 | throw nlohmann::detail::other_error::create(901, error_description); |
| 78 | } |
| 79 | } |
| 80 | return result; |
| 81 | } |
| 82 | catch (const nlohmann::detail::exception &e) |
| 83 | { |
| 84 | if (get_exception_report()) |
| 85 | { |
| 86 | openrasp_error(LEVEL_WARNING, CONFIG_ERROR, _("%s, config \"%s\" use the default value \"%s\""), |
| 87 | e.what(), BaseReader::stringfy_keys(keys).c_str(), default_value.c_str()); |
| 88 | } |
| 89 | } |
| 90 | return default_value; |
| 91 | } |
| 92 | |
| 93 | int64_t JsonReader::fetch_int64(const std::vector<std::string> &keys, const int64_t &default_value, |
| 94 | const std::function<std::string(int64_t value)> &validator) |
no test coverage detected