Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Old optional #67

Closed
wants to merge 3 commits into from
Closed

Conversation

drewdzzz
Copy link
Collaborator

The patch populates mpp::encode and mpp::decode functions with optional objects support. An empty optional is encoded as MP_NIL, and MP_NIL is decoded as an empty optional. Otherwise, underlying objects are encoded/decoded just like without optional.

@drewdzzz drewdzzz force-pushed the support_obj_in_optional branch from 1c3d6a7 to 7f8d4c4 Compare November 20, 2023 14:13
The method is used to get the container which contains the value the
path points to, so let's rename it to parent_container.
@drewdzzz drewdzzz force-pushed the support_obj_in_optional branch from 7f8d4c4 to 5976755 Compare November 20, 2023 14:30
If MsgPack contains MP_NIL, the optional is reset. Otherwise, the
underlying value is decoded just like without optional. The value is
emplaced with default constructor if the optional is empty.
@drewdzzz drewdzzz force-pushed the support_obj_in_optional branch from 5976755 to 8a2048a Compare November 20, 2023 14:34
If passed optional is not empty, underlying value is encoded - optional
itself is not present in the resulting MsgPack. If the optional is empty,
it is encoded as MP_NIL.
@drewdzzz drewdzzz force-pushed the support_obj_in_optional branch from 8a2048a to e24abec Compare November 20, 2023 14:56
Copy link
Member

@CuriousGeorgiy CuriousGeorgiy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me overall.

The only thing left unclear to me yet is how results are 'sinked' to destinations, and why container types are filled with 'contained' or, in your notion, 'transparent' values, e.g., int of std::optional<int> (or is that simply an optimization?).

You haven't mentioned this in your commit message, but as far as I understand, the trade-off under question is introducing an intermediate jump, i.e., jump_read_optional.

TBH, I didn't review the encoding test cases thoroughly, I think we need to setup a coverage workflow.

src/mpp/Dec.hpp Show resolved Hide resolved
{
if constexpr (TYPE == PIT_STATIC_L0) {
return std::get<POS>(std::tie(t...)).get();
} else if constexpr (is_path_item_static(PI)) {
return tnt::get<POS>(prev(t...));
return tnt::get<POS>(parent_container(t...));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: looks like this hunk belongs to first patch.

@@ -645,8 +705,7 @@ struct JumpsBuilder {
if constexpr (path_item_type(LAST) == PIT_DYN_SKIP) {
return getFamiliesByRules<all_rules_t>();
} else if constexpr (path_item_type(LAST) == PIT_DYN_KEY) {
using R = decltype(path_resolve(PATH{},
std::declval<T>()...));
using R = decltype(path_resolve(PATH{}, std::declval<T>()...));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: look's like an unnecessary hunk.

@@ -659,8 +718,7 @@ struct JumpsBuilder {
return keyMapKeyFamiliesFlat<DST>(is{});
}
} else {
using R = decltype(path_resolve(PATH{},
std::declval<T>()...));
using R = decltype(path_resolve(PATH{}, std::declval<T>()...));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: look's like an unnecessary hunk.

src/mpp/Dec.hpp Show resolved Hide resolved
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<int>(), 5)
)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: no sure it's worth it, but I think adding separate test cases would be cleaner than wrapping around existing ones.

Same comment for objects test case below.

class PATH, class BUF, class... T>
bool jump_read_optional(BUF& buf, T... t)
{
auto&& optional_dst = unwrap(path_resolve(PATH{}, t...));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add some kind of assert about the type?

@@ -1214,11 +1238,206 @@ test_object_codec()

auto itr = buf.begin();
mpp::decode(itr, rd);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: looks like an unnecessary hunk.

fail_unless(rd == wr);

mpp::decode(itr, rds);
fail_unless(rds.count(wr) > 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this test case related to the patch?

src/mpp/Dec.hpp Show resolved Hide resolved
return Res::get(t...);
}

/** Resolve path, semi-transparent object are invisible. */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the comments should be the other way around, i.e., visible for path_resolve and invisible for path_resolve_transparent.

@drewdzzz drewdzzz added the do not merge Not ready to be merged label Nov 22, 2023
@drewdzzz drewdzzz changed the title Support optional in decoder and encoder Old optional Nov 22, 2023
@drewdzzz drewdzzz closed this Nov 22, 2023
@drewdzzz
Copy link
Collaborator Author

Another implementation of optional has been merged just now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do not merge Not ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants