Skip to content

Commit

Permalink
Add check optional exist
Browse files Browse the repository at this point in the history
  • Loading branch information
fantasy-peak committed Apr 27, 2024
1 parent 9724ab3 commit 550cc3c
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions include/yaml_cpp_struct.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ struct convert<std::variant<T...>> {
using ToType = std::remove_reference_t<std::decay_t<decltype(value)>>; \
if constexpr (yaml_cpp_struct::is_optional<ToType>()) { \
try { \
value = yaml_cpp_struct::node_as<ToType>(node[name]); \
if (node[name]) \
value = yaml_cpp_struct::node_as<ToType>(node[name]); \
} catch (const std::runtime_error&) { \
} \
} \
Expand Down Expand Up @@ -338,10 +339,15 @@ struct convert<std::variant<T...>> {
static bool decode(const Node& node, T& rhs) { \
visit_struct::for_each(rhs, [&](const char* name, auto& value) { \
using ToType = std::remove_reference_t<std::decay_t<decltype(value)>>; \
try { \
value = yaml_cpp_struct::node_as<ToType>(node[name]); \
} catch (const std::runtime_error&) { \
if constexpr (yaml_cpp_struct::is_optional<ToType>()) { \
if (node[name]) \
value = yaml_cpp_struct::node_as<ToType>(node[name]); \
} \
else \
try { \
value = yaml_cpp_struct::node_as<ToType>(node[name]); \
} catch (const std::runtime_error&) { \
} \
}); \
return true; \
} \
Expand Down

0 comments on commit 550cc3c

Please sign in to comment.