fix: GetValueFromConfig return nullopt instead of exception (#41711)

pr: #41709

Signed-off-by: luzhang <luzhang@zilliz.com>
Co-authored-by: luzhang <luzhang@zilliz.com>
This commit is contained in:
zhagnlu 2025-05-09 11:20:54 +08:00 committed by GitHub
parent 4c656e8a7a
commit 418c35630b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 1 deletions

View File

@ -84,6 +84,9 @@ template <typename T>
inline std::optional<T>
GetValueFromConfig(const Config& cfg, const std::string& key) {
if (cfg.contains(key)) {
if (cfg.at(key).is_null()) {
return std::nullopt;
}
try {
// compatibility for boolean string
if constexpr (std::is_same_v<T, bool>) {

View File

@ -5285,7 +5285,7 @@ TEST(CApiTest, IsLoadWithDisk) {
TEST(CApiTest, TestGetValueFromConfig) {
nlohmann::json cfg = nlohmann::json::parse(
R"({"a" : 100, "b" : true, "c" : "true", "d" : 1.234})");
R"({"a" : 100, "b" : true, "c" : "true", "d" : 1.234, "e" : null})");
auto a_value = GetValueFromConfig<int64_t>(cfg, "a");
ASSERT_EQ(a_value.value(), 100);
@ -5306,4 +5306,7 @@ TEST(CApiTest, TestGetValueFromConfig) {
std::string::npos,
true);
}
auto e_value = GetValueFromConfig<std::string>(cfg, "e");
ASSERT_FALSE(e_value.has_value());
}