mirror of
https://gitee.com/milvus-io/milvus.git
synced 2026-01-07 19:31:51 +08:00
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:
parent
4c656e8a7a
commit
418c35630b
@ -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>) {
|
||||
|
||||
@ -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());
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user