mirror of https://github.com/milvus-io/milvus.git
fix:fix GetValueFromConfig for bool type (#39526)
#39525 Signed-off-by: luzhang <luzhang@zilliz.com> Co-authored-by: luzhang <luzhang@zilliz.com>pull/39586/head
parent
6d8441ad7e
commit
8117d59f85
internal/core
src/index
unittest
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue