File: MapParser.hpp

package info (click to toggle)
reflect-cpp 0.18.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 12,524 kB
  • sloc: cpp: 44,484; python: 131; makefile: 30; sh: 3
file content (165 lines) | stat: -rw-r--r-- 5,622 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#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 "schemaful/IsSchemafulReader.hpp"
#include "schemaful/IsSchemafulWriter.hpp"
#include "to_single_error_message.hpp"

namespace rfl {
namespace parsing {

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;

  using KeyType = std::remove_cvref_t<typename MapType::value_type::first_type>;
  using ValueType =
      std::remove_cvref_t<typename MapType::value_type::second_type>;

  using ParentType = Parent<W>;

  static Result<MapType> read(const R& _r, const InputVarType& _var) noexcept {
    const auto to_map = [&](auto obj) -> Result<MapType> {
      return make_map(_r, obj);
    };
    if constexpr (schemaful::IsSchemafulReader<R>) {
      return _r.to_map(_var).and_then(to_map);
    } else {
      return _r.to_object(_var).and_then(to_map);
    }
  }

  template <class P>
  static void write(const W& _w, const MapType& _m, const P& _parent) noexcept {
    if constexpr (schemaful::IsSchemafulWriter<W>) {
      write_map(_w, _m, _parent);
    } else {
      write_object(_w, _m, _parent);
    }
  }

  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))}};
  }

 private:
  static Result<MapType> make_map(const R& _r, const auto& _obj_or_map) {
    MapType map;
    std::vector<Error> errors;
    const auto map_reader =
        MapReader<R, W, MapType, ProcessorsType>(&_r, &map, &errors);
    if constexpr (schemaful::IsSchemafulReader<R>) {
      const auto err = _r.read_map(map_reader, _obj_or_map);
      if (err) {
        return error(*err);
      }
    } else {
      const auto err = _r.read_object(map_reader, _obj_or_map);
      if (err) {
        return error(*err);
      }
    }
    if (errors.size() != 0) {
      return error(to_single_error_message(errors));
    }
    return map;
  }

  template <class P>
  static void write_map(const W& _w, const MapType& _m,
                        const P& _parent) noexcept {
    auto m = ParentType::add_map(_w, _m.size(), _parent);

    using ParentMapType =
        typename ParentType::template Map<typename W::OutputMapType>;

    for (const auto& [k, v] : _m) {
      if constexpr (internal::has_reflection_type_v<KeyType>) {
        using ReflT = typename KeyType::ReflectionType;

        if constexpr (std::is_integral_v<ReflT> ||
                      std::is_floating_point_v<ReflT>) {
          const auto name = std::to_string(k.reflection());
          const auto new_parent = ParentMapType{name, &m};
          Parser<R, W, std::remove_cvref_t<ValueType>, ProcessorsType>::write(
              _w, v, new_parent);
        } else {
          const auto name = k.reflection();
          const auto new_parent = ParentMapType{name, &m};
          Parser<R, W, std::remove_cvref_t<ValueType>, ProcessorsType>::write(
              _w, v, new_parent);
        }

      } else if constexpr (std::is_integral_v<KeyType> ||
                           std::is_floating_point_v<KeyType>) {
        const auto name = std::to_string(k);
        const auto new_parent = ParentMapType{name, &m};
        Parser<R, W, std::remove_cvref_t<ValueType>, ProcessorsType>::write(
            _w, v, new_parent);
      } else {
        const auto new_parent = ParentMapType{k, &m};
        Parser<R, W, std::remove_cvref_t<ValueType>, ProcessorsType>::write(
            _w, v, new_parent);
      }
    }
    _w.end_map(&m);
  }

  template <class P>
  static void write_object(const W& _w, const MapType& _m,
                           const P& _parent) noexcept {
    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;

        if constexpr (std::is_integral_v<ReflT> ||
                      std::is_floating_point_v<ReflT>) {
          const auto name = std::to_string(k.reflection());
          const auto new_parent = typename ParentType::Object{name, &obj};
          Parser<R, W, std::remove_cvref_t<ValueType>, ProcessorsType>::write(
              _w, v, new_parent);
        } else {
          const auto name = k.reflection();
          const auto new_parent = typename ParentType::Object{name, &obj};
          Parser<R, W, std::remove_cvref_t<ValueType>, ProcessorsType>::write(
              _w, v, new_parent);
        }

      } else if constexpr (std::is_integral_v<KeyType> ||
                           std::is_floating_point_v<KeyType>) {
        const auto name = std::to_string(k);
        const auto new_parent = typename ParentType::Object{name, &obj};
        Parser<R, W, std::remove_cvref_t<ValueType>, ProcessorsType>::write(
            _w, v, new_parent);
      } else {
        const auto new_parent = typename ParentType::Object{k, &obj};
        Parser<R, W, std::remove_cvref_t<ValueType>, ProcessorsType>::write(
            _w, v, new_parent);
      }
    }
    _w.end_object(&obj);
  }
};

}  // namespace parsing
}  // namespace rfl

#endif