Treat numeric map types as keys; #14

This commit is contained in:
Patrick Urbanke
2023-11-20 22:10:02 +01:00
parent 958a124e75
commit 7cdd1acc32
5 changed files with 170 additions and 44 deletions
+33 -2
View File
@@ -2,8 +2,10 @@
The serialization and deserialization of containers is handled as follows:
`std::map<std::string, ...>` and `std::unordered_map<std::string, ...>` are treated
as objects.
`std::map<std::string, ...>`, `std::unordered_map<std::string, ...>`,
maps with any numeric types (integral and floating points) as keys,
and any objects where the `ReflectionType` is either a `std::string` or
a numeric type are treated as objects.
Consider the following map:
@@ -19,6 +21,35 @@ This will be represented as follows:
{"firstName":"Homer","lastName":"Simpson"}
```
The same is true for numeric types:
```cpp
std::map<int, std::string> homer;
homer[1] = "Homer";
homer[2] = "Simpson";
```
This will be represented as follows:
```json
{"1":"Homer","2":"Simpson"}
```
If you wrap your keys into a `rfl::Validator`, this will be treated as an object as well:
```cpp
using Between1and3 = rfl::Validator<int, rfl::Minimum<1>, rfl::Maximum<3>>;
std::map<Between1and3, std::string> homer;
homer[1] = "Homer";
homer[2] = "Simpson";
```
This will also be represented as follows:
```json
{"1":"Homer","2":"Simpson"}
```
All other supported standard containers
(other than `std::variant`, `std::optional`, `std::unique_ptr` and `std::shared_ptr`)
will be represented as arrays. Containers for which the `value_type`
+86 -26
View File
@@ -282,28 +282,7 @@ struct MapParser {
using ValueType = std::decay_t<typename MapType::value_type::second_type>;
static Result<MapType> read(const R& _r, const InputVarType& _var) noexcept {
const auto get_pair = [&](auto& _pair) {
const auto to_pair = [&](ValueType&& _val) {
return std::make_pair(KeyType(std::move(_pair.first)), std::move(_val));
};
return Parser<R, W, std::decay_t<ValueType>>::read(_r, _pair.second)
.transform(to_pair)
.value();
};
const auto to_map = [&_r, get_pair](const auto& _obj) -> Result<MapType> {
auto m = _r.to_map(_obj);
MapType res;
try {
for (auto& p : m) {
res.insert(get_pair(p));
}
} catch (std::exception& e) {
return Error(e.what());
}
return res;
};
const auto to_map = [&](const auto& _obj) { return make_map(_r, _obj); };
return _r.to_object(_var).and_then(to_map);
}
@@ -312,13 +291,88 @@ struct MapParser {
for (const auto& [k, v] : _m) {
auto parsed_val = Parser<R, W, std::decay_t<ValueType>>::write(_w, v);
if constexpr (internal::has_reflection_type_v<KeyType>) {
_w.set_field(k.reflection(), std::move(parsed_val), &obj);
using ReflT = typename KeyType::ReflectionType;
if constexpr (std::is_integral_v<ReflT> ||
std::is_floating_point_v<ReflT>) {
_w.set_field(std::to_string(k.reflection()), std::move(parsed_val),
&obj);
} else {
_w.set_field(k.reflection(), std::move(parsed_val), &obj);
}
} else if constexpr (std::is_integral_v<KeyType> ||
std::is_floating_point_v<KeyType>) {
_w.set_field(std::to_string(k), std::move(parsed_val), &obj);
} else {
_w.set_field(k, std::move(parsed_val), &obj);
}
}
return obj;
}
private:
template <class T>
static Result<T> key_to_numeric(auto& _pair) noexcept {
try {
if constexpr (std::is_integral_v<T>) {
return static_cast<T>(std::stoi(_pair.first));
} else if constexpr (std::is_floating_point_v<T>) {
return static_cast<T>(std::stod(_pair.first));
} else {
static_assert(always_false_v<T>, "Unsupported type");
}
} catch (std::exception& e) {
return Error(e.what());
}
}
static Result<std::pair<KeyType, ValueType>> make_key(auto& _pair) noexcept {
const auto to_pair = [&](auto _key) {
return std::make_pair(KeyType(std::move(_key)), std::move(_pair.second));
};
if constexpr (std::is_integral_v<KeyType> ||
std::is_floating_point_v<KeyType>) {
return key_to_numeric<KeyType>(_pair).transform(to_pair);
} else 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>) {
return key_to_numeric<ReflT>(_pair).transform(to_pair);
} else {
return to_pair(_pair.first);
}
} else {
return std::move(_pair);
}
}
static std::pair<KeyType, ValueType> get_pair(const R& _r, auto& _pair) {
const auto to_pair = [&](ValueType&& _val) {
auto pair = std::make_pair(std::move(_pair.first), std::move(_val));
return make_key(pair);
};
return Parser<R, W, std::decay_t<ValueType>>::read(_r, _pair.second)
.and_then(to_pair)
.value();
}
static Result<MapType> make_map(const R& _r, const auto& _obj) noexcept {
auto m = _r.to_map(_obj);
MapType res;
try {
for (auto& p : m) {
res.insert(get_pair(_r, p));
}
} catch (std::exception& e) {
return Error(e.what());
}
return res;
}
};
template <class R, class W, class T>
@@ -1179,9 +1233,15 @@ struct VectorParser {
static constexpr bool treat_as_map() {
if constexpr (is_map_like_not_multimap<VecType>()) {
if constexpr (internal::has_reflection_type_v<typename T::first_type>) {
return std::is_same<
std::decay_t<typename T::first_type::ReflectionType>,
std::string>();
using U = std::decay_t<typename T::first_type::ReflectionType>;
return std::is_same<U, std::string>() || std::is_integral_v<U> ||
std::is_floating_point_v<U>;
// We do not need std::string here, it is already caught by the template
// specialization.
} else if constexpr (std::is_integral_v<typename T::first_type> ||
std::is_floating_point_v<typename T::first_type>) {
return true;
} else {
return false;
}
+16 -16
View File
@@ -7,24 +7,24 @@
#include "write_and_read.hpp"
void test_map() {
std::cout << "test_map" << std::endl;
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;
};
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"}));
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)};
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"}]]})");
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,33 @@
#include <iostream>
#include <map>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>
#include "write_and_read.hpp"
void test_map_with_key_validation() {
std::cout << "test_map_with_key_validation" << std::endl;
using Between1and3 = rfl::Validator<int, rfl::Minimum<1>, rfl::Maximum<3>>;
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<Between1and3, Person>>>
children = rfl::default_value;
};
auto children = std::make_unique<std::map<Between1and3, 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"}}})");
}
+2
View File
@@ -26,6 +26,7 @@
#include "test_literal_map.hpp"
#include "test_literal_unordered_map.hpp"
#include "test_map.hpp"
#include "test_map_with_key_validation.hpp"
#include "test_monster_example.hpp"
#include "test_multimap.hpp"
#include "test_multiset.hpp"
@@ -73,6 +74,7 @@ int main() {
test_forward_list();
test_list();
test_map();
test_map_with_key_validation();
test_unordered_map();
test_set();
test_unordered_set();