diff --git a/src/mpp/Enc.hpp b/src/mpp/Enc.hpp index 894377f33..e2e532e32 100644 --- a/src/mpp/Enc.hpp +++ b/src/mpp/Enc.hpp @@ -544,6 +544,12 @@ encode(CONT &cont, tnt::CStr prefix, } if constexpr(mpp::has_enc_rule_v) { const auto& rule = mpp::get_enc_rule(); return encode(cont, prefix, ais, subst(rule, t), more...); + } else if constexpr(tnt::is_optional_v) { + static_assert(!is_wrapped_family_v && !is_wrapped_raw_v); + if (u.has_value()) + return encode(cont, prefix, ais, u.value(), more...); + else + return encode(cont, prefix, ais, nullptr, more...); } else if constexpr(is_wrapped_raw_v) { if constexpr(std::is_base_of_v) { using V = typename U::type; diff --git a/test/EncDecTest.cpp b/test/EncDecTest.cpp index 5b36fe193..18e6d7731 100644 --- a/test/EncDecTest.cpp +++ b/test/EncDecTest.cpp @@ -1256,7 +1256,8 @@ test_optional() bool ok; TEST_CASE("number"); - mpp::encode(buf, 100, nullptr); + mpp::encode(buf, std::optional(100), std::optional(), + std::optional(42)); auto run = buf.begin(); std::optional opt_num; @@ -1269,11 +1270,18 @@ test_optional() fail_unless(ok); fail_unless(!opt_num.has_value()); + ok = mpp::decode(run, opt_num); + fail_unless(ok); + fail_unless(opt_num.has_value()); + fail_unless(opt_num.value() == 42); + buf.flush(); TEST_CASE("containers with numbers"); int null_idx = 4; - mpp::encode(buf, mpp::as_arr(std::forward_as_tuple(0, 1, 2, 3, nullptr, 5))); + mpp::encode(buf, std::make_optional(mpp::as_arr( + std::forward_as_tuple(0, std::make_optional(1), 2, 3, std::optional(), 5) + ))); mpp::encode(buf, nullptr); std::vector> opt_num_arr; std::set> opt_num_set; @@ -1345,7 +1353,7 @@ test_optional() TEST_CASE("objects"); Body wr; wr.gen(); - mpp::encode(buf, wr, nullptr); + mpp::encode(buf, std::optional(wr), std::optional()); run = buf.begin(); std::optional rd;