fix:fix GetValueFromConfig for bool type ()



Signed-off-by: luzhang <luzhang@zilliz.com>
Co-authored-by: luzhang <luzhang@zilliz.com>
pull/39586/head
zhagnlu 2025-01-24 16:17:05 +08:00 committed by GitHub
parent 6d8441ad7e
commit 8117d59f85
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 5 deletions
internal/core
src/index

View File

@ -82,13 +82,23 @@ void inline CheckParameter(Config& conf,
template <typename T>
inline std::optional<T>
GetValueFromConfig(const Config& cfg, const std::string& key) {
// cfg value are all string type
if (cfg.contains(key)) {
if constexpr (std::is_same_v<T, bool>) {
return boost::algorithm::to_lower_copy(
cfg.at(key).get<std::string>()) == "true";
try {
// compatibility for boolean string
if constexpr (std::is_same_v<T, bool>) {
if (cfg.at(key).is_boolean()) {
return cfg.at(key).get<bool>();
}
return boost::algorithm::to_lower_copy(
cfg.at(key).get<std::string>()) == "true";
}
return cfg.at(key).get<T>();
} catch (std::exception& e) {
PanicInfo(ErrorCode::UnexpectedError,
"get value from config for key {} failed, error: {}",
key,
e.what());
}
return cfg.at(key).get<T>();
}
return std::nullopt;
}

View File

@ -4469,3 +4469,28 @@ TEST(CApiTest, SearchIdTest) {
TEST(CApiTest, IsLoadWithDisk) {
ASSERT_TRUE(IsLoadWithDisk(INVERTED_INDEX_TYPE, 0));
}
TEST(CApiTest, TestGetValueFromConfig) {
nlohmann::json cfg = nlohmann::json::parse(
R"({"a" : 100, "b" : true, "c" : "true", "d" : 1.234})");
auto a_value = GetValueFromConfig<int64_t>(cfg, "a");
ASSERT_EQ(a_value.value(), 100);
auto b_value = GetValueFromConfig<bool>(cfg, "b");
ASSERT_TRUE(b_value.value());
auto c_value = GetValueFromConfig<bool>(cfg, "c");
ASSERT_TRUE(c_value.value());
auto d_value = GetValueFromConfig<double>(cfg, "d");
ASSERT_NEAR(d_value.value(), 1.234, 0.001);
try {
GetValueFromConfig<std::string>(cfg, "d");
} catch (const std::exception& e) {
std::cout << e.what() << std::endl;
ASSERT_EQ(std::string(e.what()).find("get value from config for key") !=
std::string::npos,
true);
}
}