diff --git a/src/etl/LoadBalancer.cpp b/src/etl/LoadBalancer.cpp index f16c5b98..213f7ffa 100644 --- a/src/etl/LoadBalancer.cpp +++ b/src/etl/LoadBalancer.cpp @@ -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 diff --git a/src/util/config/Config.hpp b/src/util/config/Config.hpp index 74480954..9521e86d 100644 --- a/src/util/config/Config.hpp +++ b/src/util/config/Config.hpp @@ -369,26 +369,23 @@ private: { using boost::json::value_to; - auto has_error = false; - if constexpr (std::is_same_v) { - if (not value.is_bool()) - has_error = true; - } else if constexpr (std::is_same_v) { - if (not value.is_string()) - has_error = true; - } else if constexpr (std::is_same_v) { - if (not value.is_number()) - has_error = true; - } else if constexpr (std::is_convertible_v || std::is_convertible_v) { - 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() + "'" + ); + } + }; - if (has_error) { - throw std::runtime_error( - "Type for key '" + key + "' is '" + std::string{to_string(value.kind())} + "' in JSON but requested '" + - impl::typeName() + "'" - ); + if constexpr (std::is_same_v) { + error_if(not value.is_bool()); + } else if constexpr (std::is_same_v) { + error_if(not value.is_string()); + } else if constexpr (std::is_same_v or std::is_same_v) { + error_if(not value.is_number()); + } else if constexpr (std::is_convertible_v || std::is_convertible_v) { + error_if(not value.is_int64() && not value.is_uint64()); } return value_to(value); diff --git a/src/util/config/impl/Helpers.hpp b/src/util/config/impl/Helpers.hpp index 0987578e..19cc2140 100644 --- a/src/util/config/impl/Helpers.hpp +++ b/src/util/config/impl/Helpers.hpp @@ -159,4 +159,11 @@ typeName() return "double"; } +template <> +constexpr char const* +typeName() +{ + return "float"; +} + }; // namespace util::impl diff --git a/unittests/ConfigTests.cpp b/unittests/ConfigTests.cpp index 7189a995..acc16b31 100644 --- a/unittests/ConfigTests.cpp +++ b/unittests/ConfigTests.cpp @@ -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("section.test.str", "fallback"), "hello"); ASSERT_EQ(cfg.valueOr("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("section.test.double", 0.42f), 3.14f); + ASSERT_EQ(cfg.valueOr("section.test.float", 0.42f), 42.0f); + ASSERT_EQ(cfg.valueOr("section.test.float", 0.42), 42.0); ASSERT_ANY_THROW((void)cfg.valueOr("section.test.bool", 1234)); // wrong type requested }