diff --git a/include/rfl/capnproto/Writer.hpp b/include/rfl/capnproto/Writer.hpp index c93f9621..6764dcd8 100644 --- a/include/rfl/capnproto/Writer.hpp +++ b/include/rfl/capnproto/Writer.hpp @@ -186,10 +186,11 @@ class RFL_API Writer { template OutputVarType add_value_to_map(const std::string_view& _name, const T& _var, OutputMapType* _parent) const { - auto entries = - OutputArrayType{_parent->val_.get("entries").as()}; + auto entries = OutputArrayType{ + .val_ = _parent->val_.get("entries").as(), + .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); } diff --git a/tests/capnproto/test_map_value_types.cpp b/tests/capnproto/test_map_value_types.cpp new file mode 100644 index 00000000..f53c375f --- /dev/null +++ b/tests/capnproto/test_map_value_types.cpp @@ -0,0 +1,25 @@ +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_map_value_types { + +struct PersonWithStringMap { + rfl::Rename<"firstName", std::string> first_name; + std::map nicknames; +}; + +TEST(capnproto, test_map_with_string_values) { + auto nicknames = std::map(); + 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