mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-02 16:16:19 +08:00
added a new method of implementing a custom parser using a Reflector … (#142)
Co-authored-by: Daedie <daedie@gmail.com>
This commit is contained in:
+22
-1
@@ -1,7 +1,28 @@
|
||||
# Custom parsers
|
||||
|
||||
If you absolutely do not want to make any changes to your original classes whatsoever,
|
||||
you can implement a custom parser using `rfl::parsing::CustomParser`.
|
||||
You can create a Reflector template specialization for your type:
|
||||
|
||||
```cpp
|
||||
namespace rfl {
|
||||
template <>
|
||||
struct Reflector<Person> {
|
||||
struct ReflType {
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
};
|
||||
static constexpr Person to(const ReflType& v) noexcept {
|
||||
return {v.first_name, v.last_name};
|
||||
}
|
||||
|
||||
static constexpr ReflType from(const Person& v) {
|
||||
return {v.first_name, v.last_name};
|
||||
}
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
Alternatively, you can implement a custom parser using `rfl::parsing::CustomParser`.
|
||||
|
||||
In order to do so, you must do the following:
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
#include <type_traits>
|
||||
|
||||
namespace rfl {
|
||||
template <typename T>
|
||||
struct Reflector;
|
||||
namespace internal {
|
||||
|
||||
template <typename Wrapper>
|
||||
@@ -23,6 +25,13 @@ struct has_refl_m<Wrapper, std::void_t<reflection_method_t<Wrapper>>>
|
||||
template <typename Wrapper>
|
||||
constexpr bool has_reflection_method_v = has_refl_m<Wrapper>::value;
|
||||
|
||||
template <typename Type>
|
||||
concept has_reflector = requires(Type&& item) {
|
||||
Reflector<Type>::from(item);
|
||||
} && requires(const typename Reflector<Type>::ReflType& item) {
|
||||
Reflector<Type>::to(item);
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
|
||||
@@ -40,7 +40,17 @@ struct Parser {
|
||||
|
||||
/// Expresses the variables as type T.
|
||||
static Result<T> read(const R& _r, const InputVarType& _var) noexcept {
|
||||
if constexpr (R::template has_custom_constructor<T>) {
|
||||
if constexpr (internal::has_reflector<T>) {
|
||||
const auto wrap_in_t = [](auto _named_tuple) -> Result<T> {
|
||||
try {
|
||||
return Reflector<T>::to(_named_tuple);
|
||||
} catch (std::exception& e) {
|
||||
return Error(e.what());
|
||||
}
|
||||
};
|
||||
return Parser<R, W, typename Reflector<T>::ReflType, ProcessorsType>::read(_r, _var).and_then(
|
||||
wrap_in_t);
|
||||
} else if constexpr (R::template has_custom_constructor<T>) {
|
||||
return _r.template use_custom_constructor<T>(_var);
|
||||
} else {
|
||||
if constexpr (internal::has_reflection_type_v<T>) {
|
||||
@@ -68,7 +78,10 @@ struct Parser {
|
||||
|
||||
template <class P>
|
||||
static void write(const W& _w, const T& _var, const P& _parent) noexcept {
|
||||
if constexpr (internal::has_reflection_type_v<T>) {
|
||||
if constexpr (internal::has_reflector<T>) {
|
||||
Parser<R, W, typename Reflector<T>::ReflType, ProcessorsType>::write(
|
||||
_w, Reflector<T>::from(_var), _parent);
|
||||
} else if constexpr (internal::has_reflection_type_v<T>) {
|
||||
using ReflectionType = std::remove_cvref_t<typename T::ReflectionType>;
|
||||
if constexpr (internal::has_reflection_method_v<T>) {
|
||||
Parser<R, W, ReflectionType, ProcessorsType>::write(
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
#include <cassert>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace {
|
||||
struct Person {
|
||||
std::string first_name = "Homer";
|
||||
std::string last_name = "Simpson";
|
||||
std::vector<Person> children;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
namespace rfl {
|
||||
template <>
|
||||
struct Reflector<Person> {
|
||||
struct ReflType {
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
static Person to(const ReflType& v) noexcept {
|
||||
return {v.first_name, v.last_name, v.children};
|
||||
}
|
||||
|
||||
static ReflType from(const Person& v) {
|
||||
return {v.first_name, v.last_name, v.children};
|
||||
}
|
||||
};
|
||||
} // namespace rfl
|
||||
|
||||
namespace test_reflector {
|
||||
|
||||
TEST(json, test_reflector) {
|
||||
const auto homer =
|
||||
Person{"Homer", "Simpson", {{"Bart", "Simpson"}, {"Lisa", "Simpson"}}};
|
||||
|
||||
write_and_read(
|
||||
homer,
|
||||
R"({"first_name":"Homer","last_name":"Simpson","children":[{"first_name":"Bart","last_name":"Simpson","children":[]},{"first_name":"Lisa","last_name":"Simpson","children":[]}]})");
|
||||
}
|
||||
} // namespace test_reflector
|
||||
Reference in New Issue
Block a user