Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 4 additions & 3 deletions include/rfl/capnproto/Writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,11 @@ class RFL_API Writer {
template <class T>
OutputVarType add_value_to_map(const std::string_view& _name, const T& _var,
OutputMapType* _parent) const {
auto entries =
OutputArrayType{_parent->val_.get("entries").as<capnp::DynamicList>()};
auto entries = OutputArrayType{
.val_ = _parent->val_.get("entries").as<capnp::DynamicList>(),
.ix_ = _parent->ix_++};
auto new_entry = add_object_to_array(2, &entries);
add_value_to_object("key", _name, &new_entry);
add_value_to_object("key", std::string(_name), &new_entry);
return add_value_to_object("value", _var, &new_entry);
}

Expand Down
25 changes: 25 additions & 0 deletions tests/capnproto/test_map_value_types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <map>
#include <rfl.hpp>
#include <string>

#include "write_and_read.hpp"

namespace test_map_value_types {

struct PersonWithStringMap {
rfl::Rename<"firstName", std::string> first_name;
std::map<std::string, std::string> nicknames;
};

TEST(capnproto, test_map_with_string_values) {
auto nicknames = std::map<std::string, std::string>();
nicknames["Bart"] = "El Barto";
nicknames["Lisa"] = "Lis";
nicknames["Maggie"] = "Mags";

const auto homer = PersonWithStringMap{.first_name = "Homer",
.nicknames = std::move(nicknames)};

write_and_read(homer);
}
} // namespace test_map_value_types
Loading