Fix forwarding bug and float support for config (#1243)

This commit is contained in:
Sergey Kuznetsov
2024-03-07 14:39:25 +00:00
committed by GitHub
parent d47f3b71bd
commit 74455f5b99
4 changed files with 31 additions and 21 deletions

View File

@@ -229,7 +229,7 @@ LoadBalancer::forwardToRippled(
if (response and forwardingCache_ and not response->contains("error"))
forwardingCache_->put(request, *response);
return {};
return response;
}
boost::json::value

View File

@@ -369,26 +369,23 @@ private:
{
using boost::json::value_to;
auto has_error = false;
if constexpr (std::is_same_v<Return, bool>) {
if (not value.is_bool())
has_error = true;
} else if constexpr (std::is_same_v<Return, std::string>) {
if (not value.is_string())
has_error = true;
} else if constexpr (std::is_same_v<Return, double>) {
if (not value.is_number())
has_error = true;
} else if constexpr (std::is_convertible_v<Return, uint64_t> || std::is_convertible_v<Return, int64_t>) {
if (not value.is_int64() && not value.is_uint64())
has_error = true;
}
auto error_if = [&key, &value](bool condition) {
if (condition) {
throw std::runtime_error(
"Type for key '" + key + "' is '" + std::string{to_string(value.kind())} +
"' in JSON but requested '" + impl::typeName<Return>() + "'"
);
}
};
if (has_error) {
throw std::runtime_error(
"Type for key '" + key + "' is '" + std::string{to_string(value.kind())} + "' in JSON but requested '" +
impl::typeName<Return>() + "'"
);
if constexpr (std::is_same_v<Return, bool>) {
error_if(not value.is_bool());
} else if constexpr (std::is_same_v<Return, std::string>) {
error_if(not value.is_string());
} else if constexpr (std::is_same_v<Return, double> or std::is_same_v<Return, float>) {
error_if(not value.is_number());
} else if constexpr (std::is_convertible_v<Return, uint64_t> || std::is_convertible_v<Return, int64_t>) {
error_if(not value.is_int64() && not value.is_uint64());
}
return value_to<Return>(value);

View File

@@ -159,4 +159,11 @@ typeName<double>()
return "double";
}
template <>
constexpr char const*
typeName<float>()
{
return "float";
}
}; // namespace util::impl

View File

@@ -53,7 +53,9 @@ constexpr static auto JSONData = R"JSON(
"test": {
"str": "hello",
"int": 9042,
"bool": true
"bool": true,
"double": 3.14,
"float": 42.0
}
},
"top": 420
@@ -107,6 +109,10 @@ TEST_F(ConfigTest, Access)
ASSERT_EQ(cfg.valueOr<string>("section.test.str", "fallback"), "hello");
ASSERT_EQ(cfg.valueOr<string>("section.test.nonexistent", "fallback"), "fallback");
ASSERT_EQ(cfg.valueOr("section.test.bool", false), true);
ASSERT_EQ(cfg.valueOr("section.test.double", 0.42), 3.14);
ASSERT_EQ(cfg.valueOr<float>("section.test.double", 0.42f), 3.14f);
ASSERT_EQ(cfg.valueOr("section.test.float", 0.42f), 42.0f);
ASSERT_EQ(cfg.valueOr<double>("section.test.float", 0.42), 42.0);
ASSERT_ANY_THROW((void)cfg.valueOr("section.test.bool", 1234)); // wrong type requested
}