mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 10:46:18 +08:00
Added tests for various containers
This commit is contained in:
@@ -34,6 +34,8 @@
|
||||
#include "rfl/internal/no_duplicate_field_names.hpp"
|
||||
#include "rfl/internal/to_ptr_named_tuple.hpp"
|
||||
#include "rfl/named_tuple_t.hpp"
|
||||
#include "rfl/parsing/is_forward_list.hpp"
|
||||
#include "rfl/parsing/is_map_like.hpp"
|
||||
#include "rfl/parsing/is_required.hpp"
|
||||
#include "rfl/to_named_tuple.hpp"
|
||||
|
||||
@@ -533,9 +535,10 @@ struct Parser<ReaderType, WriterType, std::pair<FirstType, SecondType>> {
|
||||
static OutputVarType write(
|
||||
const WriterType& _w,
|
||||
const std::pair<FirstType, SecondType>& _p) noexcept {
|
||||
const auto tup = std::make_tuple(_p.first, _p.second);
|
||||
return Parser<ReaderType, WriterType,
|
||||
std::tuple<FirstType, SecondType>>::write(_w, tup);
|
||||
const auto tup = std::make_tuple(&_p.first, &_p.second);
|
||||
return Parser<
|
||||
ReaderType, WriterType,
|
||||
std::tuple<const FirstType*, const SecondType*>>::write(_w, tup);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -699,7 +702,8 @@ struct Parser<ReaderType, WriterType, std::tuple<Ts...>> {
|
||||
AlreadyExtracted&&... _already_extracted) noexcept {
|
||||
constexpr size_t i = sizeof...(AlreadyExtracted);
|
||||
if constexpr (i == sizeof...(Ts)) {
|
||||
return std::tuple<Ts...>(std::make_tuple(_already_extracted...));
|
||||
return std::tuple<Ts...>(
|
||||
std::make_tuple(std::move(_already_extracted)...));
|
||||
} else {
|
||||
const auto extract_next = [&](auto&& new_entry) {
|
||||
return extract_field_by_field(_r, std::move(_vec),
|
||||
@@ -943,12 +947,35 @@ struct VectorParser {
|
||||
.value();
|
||||
};
|
||||
|
||||
const auto get_pair = [&](auto& _v) {
|
||||
if constexpr (is_map_like<VecType>()) {
|
||||
using K = std::decay_t<typename T::first_type>;
|
||||
using V = std::decay_t<typename T::second_type>;
|
||||
return Parser<ReaderType, WriterType,
|
||||
std::decay_t<std::pair<K, V>>>::read(_r, &_v)
|
||||
.value();
|
||||
}
|
||||
};
|
||||
|
||||
const auto to_container =
|
||||
[&](InputArrayType&& _arr) -> Result<VecType> {
|
||||
auto input_vars = _r.to_vec(&_arr);
|
||||
VecType vec;
|
||||
for (auto it = input_vars.begin(); it != input_vars.end(); ++it) {
|
||||
vec.emplace_back(get_elem(*it));
|
||||
if constexpr (is_forward_list<VecType>()) {
|
||||
for (auto it = input_vars.rbegin(); it != input_vars.rend();
|
||||
++it) {
|
||||
vec.emplace_front(get_elem(*it));
|
||||
}
|
||||
} else if constexpr (is_map_like<VecType>()) {
|
||||
for (auto it = input_vars.begin(); it != input_vars.end();
|
||||
++it) {
|
||||
vec.insert(get_pair(*it));
|
||||
}
|
||||
} else {
|
||||
for (auto it = input_vars.begin(); it != input_vars.end();
|
||||
++it) {
|
||||
vec.emplace_back(get_elem(*it));
|
||||
}
|
||||
}
|
||||
return vec;
|
||||
};
|
||||
@@ -968,9 +995,9 @@ struct VectorParser {
|
||||
static OutputVarType write(const WriterType& _w,
|
||||
const VecType& _vec) noexcept {
|
||||
auto arr = _w.new_array();
|
||||
for (auto it = _vec.begin(); it != _vec.end(); ++it) {
|
||||
for (const auto& v : _vec) {
|
||||
_w.add(
|
||||
Parser<ReaderType, WriterType, std::decay_t<T>>::write(_w, *it),
|
||||
Parser<ReaderType, WriterType, std::decay_t<T>>::write(_w, v),
|
||||
&arr);
|
||||
}
|
||||
return OutputVarType(arr);
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef RFL_PARSING_IS_FORWARD_LIST_HPP_
|
||||
#define RFL_PARSING_IS_FORWARD_LIST_HPP_
|
||||
|
||||
#include <forward_list>
|
||||
#include <type_traits>
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class T>
|
||||
class is_forward_list;
|
||||
|
||||
template <class T>
|
||||
class is_forward_list : public std::false_type {};
|
||||
|
||||
template <class T>
|
||||
class is_forward_list<std::forward_list<T>> : public std::true_type {};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef RFL_PARSING_IS_MAP_LIKE_HPP_
|
||||
#define RFL_PARSING_IS_MAP_LIKE_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class T>
|
||||
class is_map_like;
|
||||
|
||||
template <class T>
|
||||
class is_map_like : public std::false_type {};
|
||||
|
||||
template <class K, class V>
|
||||
class is_map_like<std::map<K, V>> : public std::true_type {};
|
||||
|
||||
template <class K, class V>
|
||||
class is_map_like<std::unordered_map<K, V>> : public std::true_type {};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_deque() {
|
||||
std::cout << "test_deque" << 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<std::deque<Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::deque<Person>>();
|
||||
children->emplace_back(Person{.first_name = "Bart"});
|
||||
children->emplace_back(Person{.first_name = "Lisa"});
|
||||
children->emplace_back(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":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})");
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_forward_list() {
|
||||
std::cout << "test_forward_list" << 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<std::forward_list<Person>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::forward_list<Person>>();
|
||||
children->emplace_front(Person{.first_name = "Maggie"});
|
||||
children->emplace_front(Person{.first_name = "Lisa"});
|
||||
children->emplace_front(Person{.first_name = "Bart"});
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(
|
||||
homer,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})");
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_list() {
|
||||
std::cout << "test_list" << 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<std::list<Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::list<Person>>();
|
||||
children->emplace_back(Person{.first_name = "Bart"});
|
||||
children->emplace_back(Person{.first_name = "Lisa"});
|
||||
children->emplace_back(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":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})");
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_map() {
|
||||
std::cout << "test_map" << 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<std::map<int, Person>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::map<int, Person>>();
|
||||
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"}]]})");
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_unordered_map() {
|
||||
std::cout << "test_unordered_map" << 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<std::unordered_map<int, Person>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::unordered_map<int, Person>>();
|
||||
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<Person>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
@@ -7,9 +7,13 @@
|
||||
#include "test_custom_class3.hpp"
|
||||
#include "test_custom_class4.hpp"
|
||||
#include "test_default_values.hpp"
|
||||
#include "test_deque.hpp"
|
||||
#include "test_field_variant.hpp"
|
||||
#include "test_flatten.hpp"
|
||||
#include "test_forward_list.hpp"
|
||||
#include "test_list.hpp"
|
||||
#include "test_literal.hpp"
|
||||
#include "test_map.hpp"
|
||||
#include "test_optional_fields.hpp"
|
||||
#include "test_readme_example.hpp"
|
||||
#include "test_ref.hpp"
|
||||
@@ -20,6 +24,7 @@
|
||||
#include "test_timestamp.hpp"
|
||||
#include "test_unique_ptr.hpp"
|
||||
#include "test_unique_ptr2.hpp"
|
||||
#include "test_unordered_map.hpp"
|
||||
#include "test_variant.hpp"
|
||||
|
||||
int main() {
|
||||
@@ -37,6 +42,11 @@ int main() {
|
||||
test_array();
|
||||
test_timestamp();
|
||||
test_flatten();
|
||||
test_deque();
|
||||
test_forward_list();
|
||||
test_list();
|
||||
test_map();
|
||||
test_unordered_map();
|
||||
|
||||
test_custom_class1();
|
||||
test_custom_class2();
|
||||
|
||||
Reference in New Issue
Block a user