From 5198a446aa5d877822eb550984e1bd7483c63e7c Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Tue, 10 Oct 2023 21:37:02 +0200 Subject: [PATCH] Added support for various containers --- include/rfl/parsing/Parser.hpp | 33 +++++++++++++++++++++++------ include/rfl/parsing/is_map_like.hpp | 6 ++++++ include/rfl/parsing/is_set_like.hpp | 32 ++++++++++++++++++++++++++++ tests/test_multimap.hpp | 30 ++++++++++++++++++++++++++ tests/test_multiset.hpp | 28 ++++++++++++++++++++++++ tests/test_set.hpp | 28 ++++++++++++++++++++++++ tests/test_unordered_multimap.hpp | 33 +++++++++++++++++++++++++++++ tests/test_unordered_multiset.hpp | 31 +++++++++++++++++++++++++++ tests/test_unordered_set.hpp | 30 ++++++++++++++++++++++++++ tests/tests.cpp | 12 +++++++++++ 10 files changed, 257 insertions(+), 6 deletions(-) create mode 100644 include/rfl/parsing/is_set_like.hpp create mode 100644 tests/test_multimap.hpp create mode 100644 tests/test_multiset.hpp create mode 100644 tests/test_set.hpp create mode 100644 tests/test_unordered_multimap.hpp create mode 100644 tests/test_unordered_multiset.hpp create mode 100644 tests/test_unordered_set.hpp diff --git a/include/rfl/parsing/Parser.hpp b/include/rfl/parsing/Parser.hpp index 3967108b..1d84a2db 100644 --- a/include/rfl/parsing/Parser.hpp +++ b/include/rfl/parsing/Parser.hpp @@ -37,6 +37,7 @@ #include "rfl/parsing/is_forward_list.hpp" #include "rfl/parsing/is_map_like.hpp" #include "rfl/parsing/is_required.hpp" +#include "rfl/parsing/is_set_like.hpp" #include "rfl/to_named_tuple.hpp" namespace rfl { @@ -967,14 +968,16 @@ struct VectorParser { vec.emplace_front(get_elem(*it)); } } else if constexpr (is_map_like()) { - for (auto it = input_vars.begin(); it != input_vars.end(); - ++it) { - vec.insert(get_pair(*it)); + for (auto& v : input_vars) { + vec.insert(get_pair(v)); + } + } else if constexpr (is_set_like()) { + for (auto& v : input_vars) { + vec.insert(get_elem(v)); } } else { - for (auto it = input_vars.begin(); it != input_vars.end(); - ++it) { - vec.emplace_back(get_elem(*it)); + for (auto& v : input_vars) { + vec.emplace_back(get_elem(v)); } } return vec; @@ -1020,6 +1023,14 @@ template struct Parser> : public VectorParser> {}; +template +struct Parser> + : public VectorParser> {}; + +template +struct Parser> + : public VectorParser> {}; + template struct Parser> : public VectorParser> {}; @@ -1028,6 +1039,16 @@ template struct Parser> : public VectorParser> {}; +template +struct Parser> + : public VectorParser> {}; + +template +struct Parser> + : public VectorParser> {}; + template struct Parser> : public VectorParser> {}; diff --git a/include/rfl/parsing/is_map_like.hpp b/include/rfl/parsing/is_map_like.hpp index c0679bfc..34249982 100644 --- a/include/rfl/parsing/is_map_like.hpp +++ b/include/rfl/parsing/is_map_like.hpp @@ -17,9 +17,15 @@ class is_map_like : public std::false_type {}; template class is_map_like> : public std::true_type {}; +template +class is_map_like> : public std::true_type {}; + template class is_map_like> : public std::true_type {}; +template +class is_map_like> : public std::true_type {}; + } // namespace parsing } // namespace rfl diff --git a/include/rfl/parsing/is_set_like.hpp b/include/rfl/parsing/is_set_like.hpp new file mode 100644 index 00000000..9801c750 --- /dev/null +++ b/include/rfl/parsing/is_set_like.hpp @@ -0,0 +1,32 @@ +#ifndef RFL_PARSING_IS_SET_LIKE_HPP_ +#define RFL_PARSING_IS_SET_LIKE_HPP_ + +#include +#include +#include + +namespace rfl { +namespace parsing { + +template +class is_set_like; + +template +class is_set_like : public std::false_type {}; + +template +class is_set_like> : public std::true_type {}; + +template +class is_set_like> : public std::true_type {}; + +template +class is_set_like> : public std::true_type {}; + +template +class is_set_like> : public std::true_type {}; + +} // namespace parsing +} // namespace rfl + +#endif diff --git a/tests/test_multimap.hpp b/tests/test_multimap.hpp new file mode 100644 index 00000000..6746613e --- /dev/null +++ b/tests/test_multimap.hpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_multimap() { + std::cout << "test_multimap" << std::endl; + + struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name = "Simpson"; + rfl::Field<"children", std::unique_ptr>> + children = rfl::default_value; + }; + + auto children = std::make_unique>(); + children->insert(std::make_pair(1, Person{.first_name = "Bart"})); + children->insert(std::make_pair(2, Person{.first_name = "Lisa"})); + children->insert(std::make_pair(3, Person{.first_name = "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","children":[[1,{"firstName":"Bart","lastName":"Simpson"}],[2,{"firstName":"Lisa","lastName":"Simpson"}],[3,{"firstName":"Maggie","lastName":"Simpson"}]]})"); +} diff --git a/tests/test_multiset.hpp b/tests/test_multiset.hpp new file mode 100644 index 00000000..21d7062e --- /dev/null +++ b/tests/test_multiset.hpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_multiset() { + std::cout << "test_multiset" << std::endl; + + struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name = "Simpson"; + rfl::Field<"children", std::unique_ptr>> + children = rfl::default_value; + }; + + auto children = std::make_unique>( + std::multiset({"Bart", "Lisa", "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","children":["Bart","Lisa","Maggie"]})"); +} diff --git a/tests/test_set.hpp b/tests/test_set.hpp new file mode 100644 index 00000000..97e3834b --- /dev/null +++ b/tests/test_set.hpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_set() { + std::cout << "test_set" << std::endl; + + struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name = "Simpson"; + rfl::Field<"children", std::unique_ptr>> + children = rfl::default_value; + }; + + auto children = std::make_unique>( + std::set({"Bart", "Lisa", "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","children":["Bart","Lisa","Maggie"]})"); +} diff --git a/tests/test_unordered_multimap.hpp b/tests/test_unordered_multimap.hpp new file mode 100644 index 00000000..757118df --- /dev/null +++ b/tests/test_unordered_multimap.hpp @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_unordered_multimap() { + std::cout << "test_unordered_multimap" << std::endl; + + struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name = "Simpson"; + rfl::Field<"children", + std::unique_ptr>> + children = rfl::default_value; + }; + + auto children = std::make_unique>(); + children->insert(std::make_pair(1, Person{.first_name = "Bart"})); + children->insert(std::make_pair(2, Person{.first_name = "Lisa"})); + children->insert(std::make_pair(3, Person{.first_name = "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + // Unordered maps are unpredictable. We therefore only make sure that this + // compiles. + const auto json_string = rfl::json::write(homer); + const auto homer2 = rfl::json::read(json_string); + std::cout << "OK" << std::endl << std::endl; +} diff --git a/tests/test_unordered_multiset.hpp b/tests/test_unordered_multiset.hpp new file mode 100644 index 00000000..11591da4 --- /dev/null +++ b/tests/test_unordered_multiset.hpp @@ -0,0 +1,31 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_unordered_multiset() { + std::cout << "test_unordered_multiset" << std::endl; + + struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name = "Simpson"; + rfl::Field<"children", + std::unique_ptr>> + children = rfl::default_value; + }; + + auto children = std::make_unique>( + std::unordered_multiset({"Bart", "Lisa", "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + // Unordered multisets are unpredictable. We therefore only make sure that + // this compiles. + const auto json_string = rfl::json::write(homer); + const auto homer2 = rfl::json::read(json_string); + std::cout << "OK" << std::endl << std::endl; +} diff --git a/tests/test_unordered_set.hpp b/tests/test_unordered_set.hpp new file mode 100644 index 00000000..f4602a64 --- /dev/null +++ b/tests/test_unordered_set.hpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_unordered_set() { + std::cout << "test_unordered_set" << std::endl; + + struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name = "Simpson"; + rfl::Field<"children", std::unique_ptr>> + children = rfl::default_value; + }; + + auto children = std::make_unique>( + std::unordered_set({"Bart", "Lisa", "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + // Unordered sets are unpredictable. We therefore only make sure that this + // compiles. + const auto json_string = rfl::json::write(homer); + const auto homer2 = rfl::json::read(json_string); + std::cout << "OK" << std::endl << std::endl; +} diff --git a/tests/tests.cpp b/tests/tests.cpp index 8e458819..c196fea9 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -14,17 +14,23 @@ #include "test_list.hpp" #include "test_literal.hpp" #include "test_map.hpp" +#include "test_multimap.hpp" +#include "test_multiset.hpp" #include "test_optional_fields.hpp" #include "test_readme_example.hpp" #include "test_ref.hpp" #include "test_replace.hpp" #include "test_replace_flatten.hpp" #include "test_replace_with_other_struct.hpp" +#include "test_set.hpp" #include "test_tagged_union.hpp" #include "test_timestamp.hpp" #include "test_unique_ptr.hpp" #include "test_unique_ptr2.hpp" #include "test_unordered_map.hpp" +#include "test_unordered_multimap.hpp" +#include "test_unordered_multiset.hpp" +#include "test_unordered_set.hpp" #include "test_variant.hpp" int main() { @@ -47,6 +53,12 @@ int main() { test_list(); test_map(); test_unordered_map(); + test_set(); + test_unordered_set(); + test_multimap(); + test_unordered_multimap(); + test_multiset(); + test_unordered_multiset(); test_custom_class1(); test_custom_class2();