mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-07 03:06:18 +08:00
Added tests for string map and string unordered map
This commit is contained in:
@@ -272,8 +272,10 @@ struct Parser<ReaderType, WriterType, Literal<_fields...>> {
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
template <class ReaderType, class WriterType, class ValueType>
|
||||
struct Parser<ReaderType, WriterType, std::map<std::string, ValueType>> {
|
||||
/// Used for maps for which the key type is std::string. These are represented
|
||||
/// as objects.
|
||||
template <class ReaderType, class WriterType, class MapType>
|
||||
struct MapParser {
|
||||
public:
|
||||
using InputObjectType = typename ReaderType::InputObjectType;
|
||||
using InputVarType = typename ReaderType::InputVarType;
|
||||
@@ -281,12 +283,13 @@ struct Parser<ReaderType, WriterType, std::map<std::string, ValueType>> {
|
||||
using OutputObjectType = typename WriterType::OutputObjectType;
|
||||
using OutputVarType = typename WriterType::OutputVarType;
|
||||
|
||||
/// Expresses the variables as a std::map.
|
||||
static Result<std::map<std::string, ValueType>> read(
|
||||
const ReaderType& _r, InputVarType* _var) noexcept {
|
||||
using ValueType = std::decay_t<typename MapType::value_type::second_type>;
|
||||
|
||||
static Result<MapType> read(const ReaderType& _r,
|
||||
InputVarType* _var) noexcept {
|
||||
const auto get_pair = [&](auto _pair) {
|
||||
const auto to_pair = [&](const ValueType& _val) {
|
||||
return std::make_pair(_pair.first, _val);
|
||||
const auto to_pair = [&](ValueType&& _val) {
|
||||
return std::make_pair(_pair.first, std::move(_val));
|
||||
};
|
||||
return Parser<ReaderType, WriterType,
|
||||
std::decay_t<ValueType>>::read(_r, &_pair.second)
|
||||
@@ -294,25 +297,24 @@ struct Parser<ReaderType, WriterType, std::map<std::string, ValueType>> {
|
||||
.value();
|
||||
};
|
||||
|
||||
const auto to_map = [&_r, get_pair](auto _obj) {
|
||||
using namespace std::ranges::views;
|
||||
const auto to_map = [&_r, get_pair](auto _obj) -> Result<MapType> {
|
||||
auto m = _r.to_map(&_obj);
|
||||
auto range = m | transform(get_pair);
|
||||
MapType res;
|
||||
try {
|
||||
return std::map<std::string, ValueType>(range.begin(),
|
||||
range.end());
|
||||
for (const auto& p : m) {
|
||||
res.insert(get_pair(p));
|
||||
}
|
||||
} catch (std::exception& e) {
|
||||
return Error(e.what());
|
||||
}
|
||||
return res;
|
||||
};
|
||||
|
||||
return _r.to_object(_var).and_then(to_map);
|
||||
}
|
||||
|
||||
/// Transform a std::map into an object
|
||||
static OutputVarType write(
|
||||
const WriterType& _w,
|
||||
const std::map<std::string, ValueType>& _m) noexcept {
|
||||
static OutputVarType write(const WriterType& _w,
|
||||
const MapType& _m) noexcept {
|
||||
auto obj = _w.new_object();
|
||||
for (const auto& [k, v] : _m) {
|
||||
_w.set_field(
|
||||
@@ -325,6 +327,15 @@ struct Parser<ReaderType, WriterType, std::map<std::string, ValueType>> {
|
||||
}
|
||||
};
|
||||
|
||||
template <class ReaderType, class WriterType, class T>
|
||||
struct Parser<ReaderType, WriterType, std::map<std::string, T>>
|
||||
: public MapParser<ReaderType, WriterType, std::map<std::string, T>> {};
|
||||
|
||||
template <class ReaderType, class WriterType, class T>
|
||||
struct Parser<ReaderType, WriterType, std::unordered_map<std::string, T>>
|
||||
: public MapParser<ReaderType, WriterType,
|
||||
std::unordered_map<std::string, T>> {};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
template <class ReaderType, class WriterType, class... FieldTypes>
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_string_map() {
|
||||
std::cout << "test_string_map" << std::endl;
|
||||
|
||||
std::map<std::string, std::unique_ptr<std::string>> homer;
|
||||
homer.insert(
|
||||
std::make_pair("firstName", std::make_unique<std::string>("Homer")));
|
||||
homer.insert(
|
||||
std::make_pair("lastName", std::make_unique<std::string>("Simpson")));
|
||||
|
||||
write_and_read(homer, R"({"firstName":"Homer","lastName":"Simpson"})");
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_string_unordered_map() {
|
||||
std::cout << "test_string_unordered_map" << std::endl;
|
||||
|
||||
using Map = std::unordered_map<std::string, std::unique_ptr<std::string>>;
|
||||
Map homer;
|
||||
homer.insert(
|
||||
std::make_pair("firstName", std::make_unique<std::string>("Homer")));
|
||||
homer.insert(
|
||||
std::make_pair("lastName", std::make_unique<std::string>("Simpson")));
|
||||
|
||||
// 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<Map>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
@@ -23,6 +23,8 @@
|
||||
#include "test_replace_flatten.hpp"
|
||||
#include "test_replace_with_other_struct.hpp"
|
||||
#include "test_set.hpp"
|
||||
#include "test_string_map.hpp"
|
||||
#include "test_string_unordered_map.hpp"
|
||||
#include "test_tagged_union.hpp"
|
||||
#include "test_timestamp.hpp"
|
||||
#include "test_unique_ptr.hpp"
|
||||
@@ -59,6 +61,8 @@ int main() {
|
||||
test_unordered_multimap();
|
||||
test_multiset();
|
||||
test_unordered_multiset();
|
||||
test_string_map();
|
||||
test_string_unordered_map();
|
||||
|
||||
test_custom_class1();
|
||||
test_custom_class2();
|
||||
|
||||
Reference in New Issue
Block a user