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

mpp: introduce object encoding/decoding #63

Merged
merged 5 commits into from
Nov 7, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
mpp: fix encoding of tuples of pairs
By default tuples of pairs are interpeted as MP_MAP type and are
encoded correspondingly. But by mistake is this case all keys were
encoded before all values.

Fix it by reordering keys and values so that key-value pairs are
encoded sequentially.

Add a test for that case.
alyapunov committed Nov 7, 2023
commit 4cb1b14b7af1fd4c447c0bf8e0fcbb95cd172c75
14 changes: 11 additions & 3 deletions src/mpp/Enc.hpp
Original file line number Diff line number Diff line change
@@ -557,10 +557,18 @@ encode(CONT &cont, tnt::CStr<C...> prefix,
const V& v = u.v;
if constexpr(tnt::is_tuplish_v<V>) {
tnt::iseq<> is;
constexpr size_t N = sizeof...(I);
[[maybe_unused]] auto reorder =
std::tie(tnt::get<I>(v).first...,
tnt::get<I>(v).second...);
[[maybe_unused]] auto reindex =
[](size_t i, size_t n) {
return i / 2 + n * (i % 2);
};
return encode(cont, prefix, is,
tnt::get<I>(v).first...,
tnt::get<I>(v).second...,
more...);
tnt::get<reindex(I, N)>(reorder)...,
tnt::get<reindex(I + N, N)>(reorder)...,
more...);
} else if constexpr(tnt::is_const_iterable_v<V>) {
auto itr = std::begin(v);
auto e = std::end(v);
4 changes: 2 additions & 2 deletions test/EncDecTest.cpp
Original file line number Diff line number Diff line change
@@ -425,8 +425,8 @@ test_basic()
std::forward_as_tuple(10, true, 11, "val",
12, std::make_tuple(1, 2, 3))));
// String keys.
mpp::encode(buf, mpp::as_map(std::make_tuple("key1", "val1",
"key2", "val2")));
mpp::encode(buf, std::make_tuple(std::make_pair("key1", "val1"),
std::make_pair("key2", "val2")));
// Mixed keys.
mpp::encode(buf, mpp::as_map(std::make_tuple(1, "val1",
"key2", "val2")));