Skip to content
Merged
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
10 changes: 10 additions & 0 deletions include/rfl/capnproto/schema/CapnProtoTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace rfl::capnproto::schema {

struct CapnProtoTypes {
bool has_maps_ = false;
std::map<std::string, schema::Type> scalar_maps_;
std::map<std::string, schema::Type> structs_;
std::map<std::string, schema::Type> enums_;
std::map<std::string, schema::Type> tuples_;
Expand Down Expand Up @@ -40,6 +41,15 @@ std::ostream& operator<<(std::ostream& _os, const CapnProtoTypes& _cnp_types) {
if (_cnp_types.has_maps_) {
_os << MAP_DEFINITION << std::endl << std::endl;
}
for (const auto& [name, value_type] : _cnp_types.scalar_maps_) {
_os << "struct " << name << " {\n"
<< " entries @0 :List(Entry);\n"
<< " struct Entry {\n"
<< " key @0 :Text;\n"
<< " value @1 :" << value_type << ";\n"
<< " }\n"
<< "}\n\n";
}
_os << _cnp_types.structs_ << _cnp_types.enums_ << _cnp_types.tuples_
<< _cnp_types.unions_;
return _os;
Expand Down
46 changes: 40 additions & 6 deletions src/rfl/capnproto/to_schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,45 @@ schema::Type reference_to_capnproto_schema_type(
}
}

schema::Type string_map_to_capnproto_schema_type(
const parsing::schema::Type::StringMap& _map,
const std::map<std::string, parsing::schema::Type>& _definitions,
schema::CapnProtoTypes* _cnp_types) {
auto value_type = type_to_capnproto_schema_type(
*_map.value_type_, _definitions, Parent::is_not_a_struct, _cnp_types);
const bool is_scalar = value_type.reflection().visit([&](const auto& _value) {
using T = std::remove_cvref_t<decltype(_value)>;
using Type = schema::Type;
if constexpr (std::is_same_v<T, Type::Reference>) {
const auto it = _definitions.find(_value.type_name);
return _cnp_types->enums_.contains(_value.type_name) ||
(it != _definitions.end() &&
it->second.variant_.visit([](const auto& _definition) {
return std::is_same_v<
std::remove_cvref_t<decltype(_definition)>,
parsing::schema::Type::Literal>;
}));
} else {
return !std::is_same_v<T, Type::Data> && !std::is_same_v<T, Type::Text> &&
!std::is_same_v<T, Type::List> && !std::is_same_v<T, Type::Map> &&
!std::is_same_v<T, Type::Struct> &&
!std::is_same_v<T, Type::Union>;
}
});
if (is_scalar) {
// Cap'n Proto generic parameters must be pointer types. Keep scalar values
// directly in a concrete Entry struct instead of instantiating Map(Value).
const auto name = std::string("ScalarMap") +
std::to_string(_cnp_types->scalar_maps_.size() + 1);
_cnp_types->scalar_maps_.emplace(name, std::move(value_type));
return schema::Type{.value = schema::Type::Reference{name}};
}
_cnp_types->has_maps_ = true;
return schema::Type{
.value = schema::Type::Map{
.type = Ref<schema::Type>::make(std::move(value_type))}};
}

schema::Type type_to_capnproto_schema_type(
const parsing::schema::Type& _type,
const std::map<std::string, parsing::schema::Type>& _definitions,
Expand Down Expand Up @@ -223,12 +262,7 @@ schema::Type type_to_capnproto_schema_type(
_cnp_types);

} else if constexpr (std::is_same<T, Type::StringMap>()) {
_cnp_types->has_maps_ = true;
return schema::Type{
.value = schema::Type::Map{
.type = Ref<schema::Type>::make(type_to_capnproto_schema_type(
*_t.value_type_, _definitions, Parent::is_not_a_struct,
_cnp_types))}};
return string_map_to_capnproto_schema_type(_t, _definitions, _cnp_types);

} else if constexpr (std::is_same<T, Type::Tuple>()) {
return type_to_capnproto_schema_type(
Expand Down
80 changes: 80 additions & 0 deletions tests/capnproto/test_map_value_types.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#include <cstdint>
#include <limits>
#include <map>
#include <optional>
#include <rfl.hpp>
#include <string>
#include <unordered_map>
#include <vector>

#include "write_and_read.hpp"

Expand All @@ -22,4 +27,79 @@ TEST(capnproto, test_map_with_string_values) {

write_and_read(homer);
}

template <class Map>
struct MapValues {
Map values;
};

template <class Map>
void check_map_values(const Map& _values) {
using T = MapValues<Map>;
const auto serialized = rfl::capnproto::write(T{.values = _values});
const auto restored = rfl::capnproto::read<T>(serialized);
ASSERT_TRUE(restored) << restored.error().what();
EXPECT_EQ(restored.value().values, _values);
}

TEST(capnproto, test_map_with_integer_values) {
check_map_values(std::map<std::string, int>{});
check_map_values(std::map<std::string, int>{
{"negative", -42}, {"zero", 0}, {"positive", 42}});
check_map_values(std::map<std::string, unsigned int>{
{"zero", 0}, {"maximum", std::numeric_limits<unsigned int>::max()}});
check_map_values(std::unordered_map<std::string, std::int64_t>{});
check_map_values(std::unordered_map<std::string, std::int64_t>{
{"minimum", std::numeric_limits<std::int64_t>::min()},
{"large", std::int64_t{1} << 40},
{"maximum", std::numeric_limits<std::int64_t>::max()}});
check_map_values(std::map<std::string, std::uint64_t>{
{"zero", 0}, {"maximum", std::numeric_limits<std::uint64_t>::max()}});
}

TEST(capnproto, test_map_with_bool_and_floating_point_values) {
check_map_values(
std::map<std::string, bool>{{"false", false}, {"true", true}});
check_map_values(
std::map<std::string, float>{{"negative", -1.25f}, {"positive", 2.5f}});
check_map_values(std::unordered_map<std::string, double>{{"negative", -1.25},
{"positive", 2.5}});
}

enum class Color { red, green, blue };

TEST(capnproto, test_map_with_enum_values) {
check_map_values(std::map<std::string, Color>{{"first", Color::red},
{"second", Color::blue}});
using Literal = rfl::Literal<"red", "green", "blue">;
check_map_values(std::unordered_map<std::string, Literal>{
{"first", Literal::make<"red">()}, {"second", Literal::make<"blue">()}});
}

struct MixedMaps {
std::map<std::string, int> integers;
std::map<std::string, bool> booleans;
std::map<std::string, Color> colors;
std::map<std::string, std::string> strings;
std::map<std::string, std::vector<int>> lists;
std::map<std::string, std::optional<int>> optionals;
std::map<std::string, std::map<std::string, int>> nested;

bool operator==(const MixedMaps&) const = default;
};

TEST(capnproto, test_mixed_and_nested_map_value_types) {
const auto original =
MixedMaps{.integers = {{"negative", -42}},
.booleans = {{"true", true}},
.colors = {{"red", Color::red}},
.strings = {{"first", "hello"}},
.lists = {{"first", {1, 2, 3}}},
.optionals = {{"present", 42}, {"absent", std::nullopt}},
.nested = {{"first", {{"second", 42}}}}};
const auto serialized = rfl::capnproto::write(original);
const auto restored = rfl::capnproto::read<MixedMaps>(serialized);
ASSERT_TRUE(restored) << restored.error().what();
EXPECT_EQ(restored.value(), original);
}
} // namespace test_map_value_types
Loading