Skip to content

Commit 6cdabd9

Browse files
zhagnluluzhang
andauthored
fix:fix GetValueFromConfig for bool type (#39527)
pr: #39526 Signed-off-by: luzhang <luzhang@zilliz.com> Co-authored-by: luzhang <luzhang@zilliz.com>
1 parent 30411d6 commit 6cdabd9

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

internal/core/src/index/Utils.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,23 @@ void inline CheckParameter(Config& conf,
8282
template <typename T>
8383
inline std::optional<T>
8484
GetValueFromConfig(const Config& cfg, const std::string& key) {
85-
// cfg value are all string type
8685
if (cfg.contains(key)) {
87-
if constexpr (std::is_same_v<T, bool>) {
88-
return boost::algorithm::to_lower_copy(
89-
cfg.at(key).get<std::string>()) == "true";
86+
try {
87+
// compatibility for boolean string
88+
if constexpr (std::is_same_v<T, bool>) {
89+
if (cfg.at(key).is_boolean()) {
90+
return cfg.at(key).get<bool>();
91+
}
92+
return boost::algorithm::to_lower_copy(
93+
cfg.at(key).get<std::string>()) == "true";
94+
}
95+
return cfg.at(key).get<T>();
96+
} catch (std::exception& e) {
97+
PanicInfo(ErrorCode::UnexpectedError,
98+
"get value from config for key {} failed, error: {}",
99+
key,
100+
e.what());
90101
}
91-
return cfg.at(key).get<T>();
92102
}
93103
return std::nullopt;
94104
}

internal/core/unittest/test_c_api.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5281,3 +5281,28 @@ TEST(CApiTest, RANGE_SEARCH_WITH_RADIUS_AND_RANGE_FILTER_WHEN_IP_BFLOAT16) {
52815281
TEST(CApiTest, IsLoadWithDisk) {
52825282
ASSERT_TRUE(IsLoadWithDisk(INVERTED_INDEX_TYPE, 0));
52835283
}
5284+
5285+
TEST(CApiTest, TestGetValueFromConfig) {
5286+
nlohmann::json cfg = nlohmann::json::parse(
5287+
R"({"a" : 100, "b" : true, "c" : "true", "d" : 1.234})");
5288+
auto a_value = GetValueFromConfig<int64_t>(cfg, "a");
5289+
ASSERT_EQ(a_value.value(), 100);
5290+
5291+
auto b_value = GetValueFromConfig<bool>(cfg, "b");
5292+
ASSERT_TRUE(b_value.value());
5293+
5294+
auto c_value = GetValueFromConfig<bool>(cfg, "c");
5295+
ASSERT_TRUE(c_value.value());
5296+
5297+
auto d_value = GetValueFromConfig<double>(cfg, "d");
5298+
ASSERT_NEAR(d_value.value(), 1.234, 0.001);
5299+
5300+
try {
5301+
GetValueFromConfig<std::string>(cfg, "d");
5302+
} catch (const std::exception& e) {
5303+
std::cout << e.what() << std::endl;
5304+
ASSERT_EQ(std::string(e.what()).find("get value from config for key") !=
5305+
std::string::npos,
5306+
true);
5307+
}
5308+
}

0 commit comments

Comments
 (0)