draconisplusplus/include/rfl/parsing/MapParser.hpp

100 lines
3.6 KiB
C++
Raw Normal View History

2024-05-31 22:59:00 -04:00
#ifndef RFL_PARSING_MAPPARSER_HPP_
#define RFL_PARSING_MAPPARSER_HPP_
#include <map>
#include <stdexcept>
#include <string>
#include <type_traits>
#include "../Ref.hpp"
#include "../Result.hpp"
#include "../always_false.hpp"
#include "MapReader.hpp"
#include "Parent.hpp"
#include "Parser_base.hpp"
#include "schema/Type.hpp"
#include "to_single_error_message.hpp"
namespace rfl {
2024-06-08 14:10:59 -04:00
namespace parsing {
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
template <class R, class W, class MapType, class ProcessorsType>
requires AreReaderAndWriter<R, W, MapType>
struct MapParser {
public:
using InputObjectType = typename R::InputObjectType;
using InputVarType = typename R::InputVarType;
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
using OutputObjectType = typename W::OutputObjectType;
using OutputVarType = typename W::OutputVarType;
2024-05-31 22:59:00 -04:00
2024-06-16 00:13:15 -04:00
using KeyType = std::remove_cvref_t<typename MapType::value_type::first_type>;
using ValueType = std::remove_cvref_t<typename MapType::value_type::second_type>;
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
using ParentType = Parent<W>;
2024-05-31 22:59:00 -04:00
2024-06-16 00:13:15 -04:00
static Result<MapType> read(const R& _r, const InputVarType& _var) noexcept {
const auto to_map = [&](auto obj) -> Result<MapType> { return make_map(_r, obj); };
2024-06-08 14:10:59 -04:00
return _r.to_object(_var).and_then(to_map);
}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
template <class P>
2024-06-16 00:13:15 -04:00
static void write(const W& _w, const MapType& _m, const P& _parent) noexcept {
2024-06-08 14:10:59 -04:00
auto obj = ParentType::add_object(_w, _m.size(), _parent);
for (const auto& [k, v] : _m) {
if constexpr (internal::has_reflection_type_v<KeyType>) {
using ReflT = typename KeyType::ReflectionType;
2024-05-31 22:59:00 -04:00
2024-06-16 00:13:15 -04:00
if constexpr (std::is_integral_v<ReflT> || std::is_floating_point_v<ReflT>) {
2024-06-08 14:10:59 -04:00
const auto name = std::to_string(k.reflection());
2024-06-16 00:13:15 -04:00
const auto new_parent = typename ParentType::Object { name, &obj };
Parser<R, W, std::remove_cvref_t<ValueType>, ProcessorsType>::write(
_w, v, new_parent
);
2024-06-08 14:10:59 -04:00
} else {
const auto name = k.reflection();
2024-06-16 00:13:15 -04:00
const auto new_parent = typename ParentType::Object { name, &obj };
Parser<R, W, std::remove_cvref_t<ValueType>, ProcessorsType>::write(
_w, v, new_parent
);
2024-06-08 14:10:59 -04:00
}
2024-05-31 22:59:00 -04:00
2024-06-16 00:13:15 -04:00
} else if constexpr (std::is_integral_v<KeyType> || std::is_floating_point_v<KeyType>) {
2024-06-08 14:10:59 -04:00
const auto name = std::to_string(k);
2024-06-16 00:13:15 -04:00
const auto new_parent = typename ParentType::Object { name, &obj };
Parser<R, W, std::remove_cvref_t<ValueType>, ProcessorsType>::write(_w, v, new_parent);
2024-06-08 14:10:59 -04:00
} else {
2024-06-16 00:13:15 -04:00
const auto new_parent = typename ParentType::Object { k, &obj };
Parser<R, W, std::remove_cvref_t<ValueType>, ProcessorsType>::write(_w, v, new_parent);
2024-06-08 14:10:59 -04:00
}
}
_w.end_object(&obj);
2024-05-31 22:59:00 -04:00
}
2024-06-16 00:13:15 -04:00
static schema::Type to_schema(std::map<std::string, schema::Type>* _definitions) {
return schema::Type { schema::Type::StringMap {
Ref<schema::Type>::make(Parser<R, W, ValueType, ProcessorsType>::to_schema(_definitions)
) } };
2024-06-08 14:10:59 -04:00
}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
private:
2024-06-16 00:13:15 -04:00
static Result<MapType> make_map(const R& _r, const InputObjectType& _obj) {
2024-06-08 15:53:06 -04:00
MapType map;
2024-06-08 14:10:59 -04:00
std::vector<Error> errors;
2024-06-16 00:13:15 -04:00
const auto map_reader = MapReader<R, W, MapType, ProcessorsType>(&_r, &map, &errors);
const auto err = _r.read_object(map_reader, _obj);
2024-06-09 18:55:00 -04:00
if (err) {
return *err;
}
if (errors.size() != 0) {
return to_single_error_message(errors);
}
2024-06-08 14:10:59 -04:00
return map;
}
};
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
} // namespace parsing
} // namespace rfl
2024-05-31 22:59:00 -04:00
#endif