mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 10:46:18 +08:00
This commit is contained in:
@@ -34,6 +34,7 @@ jobs:
|
||||
./build/tests/bson/reflect-cpp-bson-tests
|
||||
./build/tests/cbor/reflect-cpp-cbor-tests
|
||||
./build/tests/flexbuffers/reflect-cpp-flexbuffers-tests
|
||||
./build/tests/generic/reflect-cpp-generic-tests
|
||||
./build/tests/json/reflect-cpp-json-tests
|
||||
./build/tests/json_c_arrays_and_inheritance/reflect-cpp-json-c-arrays-and-inheritance-tests
|
||||
./build/tests/msgpack/reflect-cpp-msgpack-tests
|
||||
|
||||
@@ -34,6 +34,7 @@ jobs:
|
||||
./build/tests/bson/reflect-cpp-bson-tests
|
||||
./build/tests/cbor/reflect-cpp-cbor-tests
|
||||
./build/tests/flexbuffers/reflect-cpp-flexbuffers-tests
|
||||
./build/tests/generic/reflect-cpp-generic-tests
|
||||
./build/tests/json/reflect-cpp-json-tests
|
||||
./build/tests/json_c_arrays_and_inheritance/reflect-cpp-json-c-arrays-and-inheritance-tests
|
||||
./build/tests/msgpack/reflect-cpp-msgpack-tests
|
||||
|
||||
@@ -27,6 +27,7 @@ jobs:
|
||||
./build/tests/bson/reflect-cpp-bson-tests
|
||||
./build/tests/cbor/reflect-cpp-cbor-tests
|
||||
./build/tests/flexbuffers/reflect-cpp-flexbuffers-tests
|
||||
./build/tests/generic/reflect-cpp-generic-tests
|
||||
./build/tests/json/reflect-cpp-json-tests
|
||||
./build/tests/json_c_arrays_and_inheritance/reflect-cpp-json-c-arrays-and-inheritance-tests
|
||||
./build/tests/msgpack/reflect-cpp-msgpack-tests
|
||||
|
||||
@@ -29,6 +29,7 @@ jobs:
|
||||
./build/tests/bson/reflect-cpp-bson-tests
|
||||
./build/tests/cbor/reflect-cpp-cbor-tests
|
||||
./build/tests/flexbuffers/reflect-cpp-flexbuffers-tests
|
||||
./build/tests/generic/reflect-cpp-generic-tests
|
||||
./build/tests/json/reflect-cpp-json-tests
|
||||
./build/tests/json_c_arrays_and_inheritance/reflect-cpp-json-c-arrays-and-inheritance-tests
|
||||
./build/tests/msgpack/reflect-cpp-msgpack-tests
|
||||
|
||||
@@ -26,6 +26,7 @@ jobs:
|
||||
./build/tests/bson/reflect-cpp-bson-tests
|
||||
./build/tests/cbor/reflect-cpp-cbor-tests
|
||||
./build/tests/flexbuffers/reflect-cpp-flexbuffers-tests
|
||||
./build/tests/generic/reflect-cpp-generic-tests
|
||||
./build/tests/json/reflect-cpp-json-tests
|
||||
./build/tests/json_c_arrays_and_inheritance/reflect-cpp-json-c-arrays-and-inheritance-tests
|
||||
./build/tests/msgpack/reflect-cpp-msgpack-tests
|
||||
|
||||
@@ -24,6 +24,7 @@ jobs:
|
||||
.\build\tests\bson\Release\reflect-cpp-bson-tests.exe
|
||||
.\build\tests\cbor\Release\reflect-cpp-cbor-tests.exe
|
||||
.\build\tests\flexbuffers\Release\reflect-cpp-flexbuffers-tests.exe
|
||||
.\build\tests\generic\Release\reflect-cpp-generic-tests.exe
|
||||
.\build\tests\msgpack\Release\reflect-cpp-msgpack-tests.exe
|
||||
.\build\tests\toml\Release\reflect-cpp-toml-tests.exe
|
||||
.\build\tests\xml\Release\reflect-cpp-xml-tests.exe
|
||||
|
||||
@@ -529,8 +529,8 @@ The following compilers are supported:
|
||||
|
||||
Simply copy the contents of the folders `include` and `src` into your source repository or add it to your include path
|
||||
and also add `src/reflectcpp.cpp` and `src/reflectcpp_json.cpp` and `src/yyjson.c` to your source files for compilation.
|
||||
If you want to link to your own version of YYJSON, then only copy `src/reflectcpp.cpp` and `src/reflectcpp_json.cpp`.
|
||||
If you don't need JSON support, then only copy `src/reflectcpp.cpp`.
|
||||
If you want to link to your own version of YYJSON, then only add `src/reflectcpp.cpp` and `src/reflectcpp_json.cpp`.
|
||||
If you don't need JSON support, then only add `src/reflectcpp.cpp`.
|
||||
|
||||
If you need support for other serialization formats like flexbuffers or XML, you should also add `src/reflectcpp_<format>.cpp`
|
||||
and include and link the respective libraries, as listed in the section on serialization formats.
|
||||
|
||||
+61
-5
@@ -8,7 +8,7 @@ have `rfl::Generic`.
|
||||
`rfl::Generic` is a convenience wrapper around the following type:
|
||||
|
||||
```cpp
|
||||
std::variant<bool, int, double, std::string, Object, Array>;
|
||||
std::variant<bool, int, double, std::string, Object, Array, std::nullopt_t>;
|
||||
```
|
||||
|
||||
`Object` and `Array` are defined as follows:
|
||||
@@ -53,14 +53,14 @@ The resulting JSON strings looks as follows:
|
||||
|
||||
`rfl::Generic` contains some convenience methods that allow you to handle parsed data:
|
||||
|
||||
You can retrieve the underlying `std::variant` using `.get()`. This then allows you to handle allow possible six cases using the [visitor pattern](https://en.cppreference.com/w/cpp/utility/variant/visit).
|
||||
You can retrieve the underlying `std::variant` using `.get()`. This then allows you to handle allow possible seven cases using the [visitor pattern](https://en.cppreference.com/w/cpp/utility/variant/visit).
|
||||
|
||||
If you have a guess what the types of a particular field might be, you can use any of the six convenience methods `.to_array()`, `.to_bool()`, `.to_double()`, `.to_int()`, `.to_object()`, `.to_string()`. Each
|
||||
If you have a guess what the types of a particular field might be, you can use any of the seven convenience methods `.to_array()`, `.to_bool()`, `.to_double()`, `.to_int()`, `.to_null()`, `.to_object()`, `.to_string()`. Each
|
||||
of these methods will return an `rfl::Result<...>` with the corresponding type, if the `rfl::Generic` does indeed contain such a type.
|
||||
|
||||
If you prefer, you can also use the following six convenience *functions*:
|
||||
If you prefer, you can also use the following seven convenience *functions*:
|
||||
|
||||
`rfl::to_array(...)`, `rfl::to_bool(...)`, `rfl::to_double(...)`, `rfl::to_int(...)`, `rfl::to_object(...)`, `rfl::to_string(...)`.
|
||||
`rfl::to_array(...)`, `rfl::to_bool(...)`, `rfl::to_double(...)`, `rfl::to_int(...)`, `rfl::to_null`, `rfl::to_object(...)`, `rfl::to_string(...)`.
|
||||
|
||||
These functions are particularly useful for monadic error handling:
|
||||
|
||||
@@ -85,3 +85,59 @@ int age = homer.get("age").and_then(rfl::to_int).value();
|
||||
This will throw an exception if any of our assumptions are false.
|
||||
|
||||
Please refer to the part of the documentation on `rfl::Result` to learn more about monadic error handling.
|
||||
|
||||
## `rfl::to_generic` and `rfl::from_generic`
|
||||
|
||||
You can transform any struct that can be converted to a JSON to the equivalent generic instead.
|
||||
|
||||
In other words:
|
||||
|
||||
```cpp
|
||||
assert(rfl::json::write(my_struct) == rfl::json::write(rfl::to_generic(my_struct)));
|
||||
```
|
||||
|
||||
And:
|
||||
|
||||
```cpp
|
||||
assert(rfl::json::read<rfl::Generic>(rfl::json::write(my_struct)).value() == rfl::to_generic(my_struct));
|
||||
```
|
||||
|
||||
What is more, you can apply any processors you want to:
|
||||
|
||||
```cpp
|
||||
assert(rfl::json::write<rfl::SnakeCaseToCamelCase>(my_struct) == rfl::json::write(rfl::to_generic<rfl::SnakeCaseToCamelCase>(my_struct)));
|
||||
|
||||
assert(rfl::json::read<rfl::Generic>(rfl::json::write<rfl::SnakeCaseToCamelCase>(my_struct)).value() == rfl::to_generic<rfl::SnakeCaseToCamelCase>(my_struct));
|
||||
```
|
||||
|
||||
Conversely, the same statements hold for reading. In other words:
|
||||
|
||||
```cpp
|
||||
assert(rfl::json::read<MyStruct>(my_json_string).value() == rfl::from_generic<MyStruct>(rfl::json::read<rfl::Generic>(my_json_string).value()).value());
|
||||
```
|
||||
|
||||
Likewise, you can apply processors:
|
||||
|
||||
```cpp
|
||||
assert(rfl::json::read<MyStruct, rfl::SnakeCaseToCamelCase>(my_json_string).value() == rfl::from_generic<MyStruct, rfl::SnakeCaseToCamelCase>(rfl::json::read<rfl::Generic>(my_json_string).value()).value());
|
||||
```
|
||||
|
||||
In fact, you can think of `rfl::from_generic` and `rfl::to_generic` as manifestations of the reader and writer interface. You could as well write `rfl::generic::read<...>(...)` and `rfl::generic::write(...)`.
|
||||
|
||||
This can be used to dynamically build structs:
|
||||
|
||||
```cpp
|
||||
struct Person {
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
int age;
|
||||
};
|
||||
|
||||
auto bart = rfl::Generic::Object();
|
||||
bart["first_name"] = "Bart";
|
||||
bart["last_name"] = "Simpson";
|
||||
bart["age"] = 10;
|
||||
|
||||
const auto person = rfl::from_generic<Person>(bart).value();
|
||||
```
|
||||
|
||||
|
||||
@@ -56,6 +56,7 @@
|
||||
#include "rfl/extract_discriminators.hpp"
|
||||
#include "rfl/field_type.hpp"
|
||||
#include "rfl/fields.hpp"
|
||||
#include "rfl/from_generic.hpp"
|
||||
#include "rfl/from_named_tuple.hpp"
|
||||
#include "rfl/get.hpp"
|
||||
#include "rfl/make_from_tuple.hpp"
|
||||
@@ -66,6 +67,7 @@
|
||||
#include "rfl/patterns.hpp"
|
||||
#include "rfl/remove_fields.hpp"
|
||||
#include "rfl/replace.hpp"
|
||||
#include "rfl/to_generic.hpp"
|
||||
#include "rfl/to_named_tuple.hpp"
|
||||
#include "rfl/to_view.hpp"
|
||||
#include "rfl/tuple_cat.hpp"
|
||||
|
||||
+43
-12
@@ -1,21 +1,28 @@
|
||||
#ifndef RFL_GENERIC_HPP_
|
||||
#define RFL_GENERIC_HPP_
|
||||
|
||||
#include <optional>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include "Object.hpp"
|
||||
#include "Result.hpp"
|
||||
#include "Variant.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
class Generic {
|
||||
public:
|
||||
constexpr static std::nullopt_t Null = std::nullopt;
|
||||
|
||||
using Array = std::vector<Generic>;
|
||||
using Object = rfl::Object<Generic>;
|
||||
using ReflectionType =
|
||||
std::variant<bool, int, double, std::string, Object, Array>;
|
||||
using VariantType = std::variant<bool, int, double, std::string, Object,
|
||||
Array, std::nullopt_t>;
|
||||
using ReflectionType = std::optional<
|
||||
std::variant<bool, int, double, std::string, Object, Array>>;
|
||||
|
||||
Generic();
|
||||
|
||||
@@ -23,34 +30,39 @@ class Generic {
|
||||
|
||||
Generic(const Generic& _other);
|
||||
|
||||
Generic(const VariantType& _value);
|
||||
|
||||
Generic(VariantType&& _value) noexcept;
|
||||
|
||||
Generic(const ReflectionType& _value);
|
||||
|
||||
Generic(ReflectionType&& _value) noexcept;
|
||||
|
||||
template <class T,
|
||||
typename std::enable_if<std::is_convertible_v<T, ReflectionType>,
|
||||
typename std::enable_if<std::is_convertible_v<T, VariantType>,
|
||||
bool>::type = true>
|
||||
Generic(const T& _value) : value_(_value) {}
|
||||
|
||||
template <class T,
|
||||
typename std::enable_if<std::is_convertible_v<T, ReflectionType>,
|
||||
typename std::enable_if<std::is_convertible_v<T, VariantType>,
|
||||
bool>::type = true>
|
||||
Generic(T&& _value) noexcept : value_(std::forward<T>(_value)) {}
|
||||
|
||||
~Generic();
|
||||
|
||||
/// Returns the underlying object.
|
||||
const ReflectionType& get() const { return value_; }
|
||||
const VariantType& get() const { return value_; }
|
||||
|
||||
/// Whether the object contains the null value.
|
||||
bool is_null() const noexcept;
|
||||
|
||||
/// Assigns the underlying object.
|
||||
Generic& operator=(const ReflectionType& _value);
|
||||
Generic& operator=(const VariantType& _value);
|
||||
|
||||
/// Assigns the underlying object.
|
||||
Generic& operator=(ReflectionType&& _value) noexcept;
|
||||
Generic& operator=(VariantType&& _value) noexcept;
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class T,
|
||||
typename std::enable_if<std::is_convertible_v<T, ReflectionType>,
|
||||
typename std::enable_if<std::is_convertible_v<T, VariantType>,
|
||||
bool>::type = true>
|
||||
auto& operator=(const T& _value) {
|
||||
value_ = _value;
|
||||
@@ -64,7 +76,7 @@ class Generic {
|
||||
Generic& operator=(Generic&& _other);
|
||||
|
||||
/// Returns the underlying object, necessary for the serialization to work.
|
||||
const ReflectionType& reflection() const { return value_; };
|
||||
ReflectionType reflection() const noexcept;
|
||||
|
||||
/// Casts the underlying value to an rfl::Generic::Array or returns an
|
||||
/// rfl::Error, if the underlying value is not an rfl::Generic::Array.
|
||||
@@ -86,12 +98,25 @@ class Generic {
|
||||
/// rfl::Error, if the underlying value is not an rfl::Generic::Object.
|
||||
Result<Object> to_object() const noexcept;
|
||||
|
||||
/// Casts the underlying value to rfl::Generic::Null or returns an
|
||||
/// rfl::Error, if the underlying value is not rfl::Generic::Null.
|
||||
Result<std::nullopt_t> to_null() const noexcept;
|
||||
|
||||
/// Casts the underlying value to a string or returns an rfl::Error, if the
|
||||
/// underlying value is not a string.
|
||||
Result<std::string> to_string() const noexcept;
|
||||
|
||||
/// Returns the underlying variant.
|
||||
VariantType& variant() noexcept { return value_; };
|
||||
|
||||
/// Returns the underlying variant.
|
||||
const VariantType& variant() const noexcept { return value_; };
|
||||
|
||||
private:
|
||||
ReflectionType value_;
|
||||
static VariantType from_reflection_type(const ReflectionType& _r) noexcept;
|
||||
|
||||
private:
|
||||
VariantType value_;
|
||||
};
|
||||
|
||||
/// Casts the underlying value to an rfl::Generic::Array or returns an
|
||||
@@ -120,6 +145,12 @@ inline Result<Generic::Object> to_object(const Generic& _g) noexcept {
|
||||
return _g.to_object();
|
||||
}
|
||||
|
||||
/// Casts the underlying value to a double or returns an rfl::Error, if the
|
||||
/// underlying value is not a double.
|
||||
inline Result<std::nullopt_t> to_null(const Generic& _g) noexcept {
|
||||
return _g.to_null();
|
||||
}
|
||||
|
||||
/// Casts the underlying value to a string or returns an rfl::Error, if the
|
||||
/// underlying value is not a string.
|
||||
inline Result<std::string> to_string(const Generic& _g) noexcept {
|
||||
|
||||
@@ -128,6 +128,16 @@ class Object {
|
||||
insert(std::make_pair(std::move(_k), std::move(_v)));
|
||||
}
|
||||
|
||||
/// Inserts a new element at the end.
|
||||
void insert(const std::string_view& _k, const T& _v) {
|
||||
insert(std::make_pair(std::string(_k), _v));
|
||||
}
|
||||
|
||||
/// Inserts a new element at the end.
|
||||
void insert(const std::string_view& _k, T&& _v) {
|
||||
insert(std::make_pair(std::string(_k), std::move(_v)));
|
||||
}
|
||||
|
||||
/// Alias for insert that primarily exists for compatability with standard
|
||||
/// containers.
|
||||
template <class... Args>
|
||||
|
||||
+31
-12
@@ -14,6 +14,7 @@
|
||||
#include "internal/nth_element_t.hpp"
|
||||
#include "internal/variant/find_max_size.hpp"
|
||||
#include "internal/variant/is_alternative_type.hpp"
|
||||
#include "internal/variant/is_convertible_to.hpp"
|
||||
#include "internal/variant/result_t.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -40,6 +41,9 @@ class Variant {
|
||||
template <IndexType _i>
|
||||
using Index = std::integral_constant<IndexType, _i>;
|
||||
|
||||
template <class T>
|
||||
struct TypeWrapper {};
|
||||
|
||||
public:
|
||||
Variant() {
|
||||
using FirstAlternative = internal::nth_element_t<0, AlternativeTypes...>;
|
||||
@@ -233,6 +237,21 @@ class Variant {
|
||||
_other.visit(copy_one);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void copy_from_other_type(const T& _t) {
|
||||
bool set = false;
|
||||
const auto copy_one = [&, this]<class AltType>(const T& _t,
|
||||
const TypeWrapper<AltType>) {
|
||||
if constexpr (std::is_convertible_v<T, AltType>) {
|
||||
if (!set) {
|
||||
move_from_type(AltType(_t));
|
||||
set = true;
|
||||
}
|
||||
}
|
||||
};
|
||||
(copy_one(_t, TypeWrapper<AlternativeTypes>{}), ...);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void copy_from_type(const T& _t) noexcept {
|
||||
using CurrentType = std::remove_cvref_t<decltype(_t)>;
|
||||
@@ -442,7 +461,7 @@ class Variant {
|
||||
};
|
||||
|
||||
template <class T, class... Types>
|
||||
constexpr T* get_if(Variant<Types...>& _v) noexcept {
|
||||
constexpr T* get_if(Variant<Types...>* _v) noexcept {
|
||||
const auto get = [](auto& _v) -> T* {
|
||||
using Type = std::remove_cvref_t<decltype(_v)>;
|
||||
if constexpr (std::is_same<Type, std::remove_cvref_t<T>>()) {
|
||||
@@ -451,11 +470,11 @@ constexpr T* get_if(Variant<Types...>& _v) noexcept {
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
return _v.visit(get);
|
||||
return _v->visit(get);
|
||||
}
|
||||
|
||||
template <class T, class... Types>
|
||||
constexpr const T* get_if(const Variant<Types...>& _v) noexcept {
|
||||
constexpr const T* get_if(const Variant<Types...>* _v) noexcept {
|
||||
const auto get = [](const auto& _v) -> const T* {
|
||||
using Type = std::remove_cvref_t<decltype(_v)>;
|
||||
if constexpr (std::is_same<Type, std::remove_cvref_t<T>>()) {
|
||||
@@ -464,24 +483,24 @@ constexpr const T* get_if(const Variant<Types...>& _v) noexcept {
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
return _v.visit(get);
|
||||
return _v->visit(get);
|
||||
}
|
||||
|
||||
template <int _i, class... Types>
|
||||
constexpr auto* get_if(Variant<Types...>& _v) noexcept {
|
||||
constexpr auto* get_if(Variant<Types...>* _v) noexcept {
|
||||
using T = internal::nth_element_t<_i, Types...>;
|
||||
return get_if<T>(_v);
|
||||
}
|
||||
|
||||
template <int _i, class... Types>
|
||||
constexpr auto* get_if(const Variant<Types...>& _v) noexcept {
|
||||
constexpr auto* get_if(const Variant<Types...>* _v) noexcept {
|
||||
using T = internal::nth_element_t<_i, Types...>;
|
||||
return get_if<T>(_v);
|
||||
}
|
||||
|
||||
template <class T, class... Types>
|
||||
constexpr T& get(Variant<Types...>& _v) {
|
||||
auto ptr = get_if<T>(_v);
|
||||
auto ptr = get_if<T>(&_v);
|
||||
if (!ptr) {
|
||||
throw std::runtime_error("Variant does not contain signified type.");
|
||||
}
|
||||
@@ -490,7 +509,7 @@ constexpr T& get(Variant<Types...>& _v) {
|
||||
|
||||
template <class T, class... Types>
|
||||
constexpr T&& get(Variant<Types...>&& _v) {
|
||||
auto ptr = get_if<T>(_v);
|
||||
auto ptr = get_if<T>(&_v);
|
||||
if (!ptr) {
|
||||
throw std::runtime_error("Variant does not contain signified type.");
|
||||
}
|
||||
@@ -499,7 +518,7 @@ constexpr T&& get(Variant<Types...>&& _v) {
|
||||
|
||||
template <class T, class... Types>
|
||||
constexpr const T& get(const Variant<Types...>& _v) {
|
||||
auto ptr = get_if<T>(_v);
|
||||
auto ptr = get_if<T>(&_v);
|
||||
if (!ptr) {
|
||||
throw std::runtime_error("Variant does not contain signified type.");
|
||||
}
|
||||
@@ -508,7 +527,7 @@ constexpr const T& get(const Variant<Types...>& _v) {
|
||||
|
||||
template <int _i, class... Types>
|
||||
constexpr auto& get(Variant<Types...>& _v) {
|
||||
auto ptr = get_if<_i>(_v);
|
||||
auto ptr = get_if<_i>(&_v);
|
||||
if (!ptr) {
|
||||
throw std::runtime_error("Variant does not contain signified type.");
|
||||
}
|
||||
@@ -517,7 +536,7 @@ constexpr auto& get(Variant<Types...>& _v) {
|
||||
|
||||
template <int _i, class... Types>
|
||||
constexpr auto&& get(Variant<Types...>&& _v) {
|
||||
auto ptr = get_if<_i>(_v);
|
||||
auto ptr = get_if<_i>(&_v);
|
||||
if (!ptr) {
|
||||
throw std::runtime_error("Variant does not contain signified type.");
|
||||
}
|
||||
@@ -526,7 +545,7 @@ constexpr auto&& get(Variant<Types...>&& _v) {
|
||||
|
||||
template <int _i, class... Types>
|
||||
constexpr const auto& get(const Variant<Types...>& _v) {
|
||||
auto ptr = get_if<_i>(_v);
|
||||
auto ptr = get_if<_i>(&_v);
|
||||
if (!ptr) {
|
||||
throw std::runtime_error("Variant does not contain signified type.");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef RFL_FROM_GENERIC_HPP_
|
||||
#define RFL_FROM_GENERIC_HPP_
|
||||
|
||||
#include "Generic.hpp"
|
||||
#include "generic/read.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// Generates the struct T from a named tuple.
|
||||
template <class T, class... Ps>
|
||||
auto from_generic(const Generic& _g) {
|
||||
return rfl::generic::read<T, Ps...>(_g);
|
||||
}
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef GENERIC_PARSER_HPP_
|
||||
#define GENERIC_PARSER_HPP_
|
||||
|
||||
#include "../parsing/Parser.hpp"
|
||||
#include "Reader.hpp"
|
||||
#include "Writer.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace generic {
|
||||
|
||||
template <class T, class ProcessorsType>
|
||||
using Parser = parsing::Parser<Reader, Writer, T, ProcessorsType>;
|
||||
|
||||
}
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,89 @@
|
||||
#ifndef GENERIC_READER_HPP_
|
||||
#define GENERIC_READER_HPP_
|
||||
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "../Bytestring.hpp"
|
||||
#include "../Generic.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
|
||||
namespace rfl::generic {
|
||||
|
||||
struct Reader {
|
||||
using InputArrayType = Generic::Array;
|
||||
using InputObjectType = Generic::Object;
|
||||
using InputVarType = Generic;
|
||||
|
||||
template <class T>
|
||||
static constexpr bool has_custom_constructor = false;
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_array(
|
||||
const size_t _idx, const InputArrayType& _arr) const noexcept;
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_object(
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept;
|
||||
|
||||
bool is_empty(const InputVarType& _var) const noexcept;
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
return _var.to_string();
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
return _var.to_bool();
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
return _var.to_double().transform(
|
||||
[](const auto& _v) { return static_cast<T>(_v); });
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
return _var.to_int().transform(
|
||||
[](const auto& _v) { return static_cast<T>(_v); });
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
}
|
||||
|
||||
template <class ArrayReader>
|
||||
std::optional<Error> read_array(const ArrayReader& _array_reader,
|
||||
const InputArrayType& _arr) const noexcept {
|
||||
for (const auto& v : _arr) {
|
||||
const auto err = _array_reader.read(InputVarType(v));
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
template <class ObjectReader>
|
||||
std::optional<Error> read_object(const ObjectReader& _object_reader,
|
||||
const InputObjectType& _obj) const noexcept {
|
||||
for (const auto& [k, v] : _obj) {
|
||||
_object_reader.read(std::string_view(k), v);
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
rfl::Result<InputArrayType> to_array(const InputVarType& _var) const noexcept;
|
||||
|
||||
rfl::Result<InputObjectType> to_object(
|
||||
const InputVarType& _var) const noexcept;
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> use_custom_constructor(
|
||||
const InputVarType _var) const noexcept {
|
||||
return rfl::Error("Not supported for generic types");
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace rfl::generic
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,117 @@
|
||||
#ifndef GENERIC_WRITER_HPP_
|
||||
#define GENERIC_WRITER_HPP_
|
||||
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "../Generic.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../Variant.hpp"
|
||||
#include "../always_false.hpp"
|
||||
|
||||
namespace rfl::generic {
|
||||
|
||||
struct Writer {
|
||||
struct OutputArray {
|
||||
Generic::Array* val_;
|
||||
};
|
||||
|
||||
struct OutputObject {
|
||||
Generic::Object* val_;
|
||||
};
|
||||
|
||||
using OutputArrayType = OutputArray;
|
||||
using OutputObjectType = OutputObject;
|
||||
using OutputVarType = Generic;
|
||||
|
||||
Writer() {}
|
||||
|
||||
~Writer() = default;
|
||||
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept;
|
||||
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept;
|
||||
|
||||
OutputVarType null_as_root() const noexcept;
|
||||
|
||||
template <class T>
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
root_ = to_generic(_var);
|
||||
return root_;
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputArrayType add_array_to_object(const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
OutputObjectType add_object_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
const auto g = to_generic(_var);
|
||||
_parent->val_->push_back(g);
|
||||
return g;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_object(const std::string_view& _name,
|
||||
const T& _var,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
const auto g = to_generic(_var);
|
||||
_parent->val_->insert(_name, g);
|
||||
return g;
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputVarType add_null_to_object(const std::string_view& _name,
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
void end_array(OutputArrayType* _arr) const noexcept {}
|
||||
|
||||
void end_object(OutputObjectType* _obj) const noexcept {}
|
||||
|
||||
OutputVarType& root() { return root_; }
|
||||
|
||||
private:
|
||||
template <class T>
|
||||
OutputVarType to_generic(const T& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
return OutputVarType(_var);
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
return OutputVarType(_var);
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
return OutputVarType(static_cast<double>(_var));
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
return OutputVarType(static_cast<int>(_var));
|
||||
} else {
|
||||
static_assert(always_false_v<T>, "Unsupported type");
|
||||
}
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
private:
|
||||
mutable OutputVarType root_;
|
||||
};
|
||||
|
||||
} // namespace rfl::generic
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef GENERIC_READ_HPP_
|
||||
#define GENERIC_READ_HPP_
|
||||
|
||||
#include <istream>
|
||||
#include <vector>
|
||||
|
||||
#include "../Generic.hpp"
|
||||
#include "../Processors.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "Parser.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace generic {
|
||||
|
||||
/// Parses an object from a generic type.
|
||||
template <class T, class... Ps>
|
||||
auto read(const Generic& _g) {
|
||||
const auto r = Reader();
|
||||
return Parser<T, Processors<Ps...>>::read(r, _g);
|
||||
}
|
||||
|
||||
} // namespace generic
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef GENERIC_WRITE_HPP_
|
||||
#define GENERIC_WRITE_HPP_
|
||||
|
||||
#include <cstddef>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
#include "../Generic.hpp"
|
||||
#include "../parsing/Parent.hpp"
|
||||
#include "Parser.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace generic {
|
||||
|
||||
/// Writes an object to a generic.
|
||||
template <class... Ps>
|
||||
Generic write(const auto& _t) {
|
||||
using T = std::remove_cvref_t<decltype(_t)>;
|
||||
using ParentType = parsing::Parent<Writer>;
|
||||
auto w = Writer();
|
||||
Parser<T, Processors<Ps...>>::write(w, _t, typename ParentType::Root{});
|
||||
return w.root();
|
||||
}
|
||||
|
||||
} // namespace generic
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef RFL_INTERNAL_VARIANT_IS_CONVERTIBLE_TO_HPP_
|
||||
#define RFL_INTERNAL_VARIANT_IS_CONVERTIBLE_TO_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "../element_index.hpp"
|
||||
|
||||
namespace rfl::internal::variant {
|
||||
|
||||
template <class T, class... AlternativeTypes>
|
||||
static constexpr bool is_convertible_to() {
|
||||
return std::disjunction_v<std::is_convertible<T, AlternativeTypes>...>;
|
||||
}
|
||||
|
||||
} // namespace rfl::internal::variant
|
||||
|
||||
#endif
|
||||
@@ -16,7 +16,6 @@ template <class TupleType, unsigned long _size, int _i>
|
||||
void call_destructor_on_one_tuple_element_if_necessary(const size_t _num_set,
|
||||
TupleType* _tup) {
|
||||
using FieldType = tuple_element_t<_i, TupleType>;
|
||||
using OriginalType = std::remove_cv_t<FieldType>;
|
||||
using ValueType = std::remove_cv_t<std::remove_pointer_t<FieldType>>;
|
||||
if constexpr (!std::is_array_v<ValueType> &&
|
||||
std::is_destructible_v<ValueType>) {
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef RFL_TO_GENERIC_HPP_
|
||||
#define RFL_TO_GENERIC_HPP_
|
||||
|
||||
#include "Generic.hpp"
|
||||
#include "generic/write.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// Generates a generic that is equivalent to the struct _t.
|
||||
template <class... Ps>
|
||||
Generic to_generic(const auto& _t) {
|
||||
return rfl::generic::write<Ps...>(_t);
|
||||
}
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -30,4 +30,6 @@ SOFTWARE.
|
||||
// compilation.
|
||||
|
||||
#include "rfl/Generic.cpp"
|
||||
#include "rfl/generic/Reader.cpp"
|
||||
#include "rfl/generic/Writer.cpp"
|
||||
#include "rfl/parsing/schema/Type.cpp"
|
||||
|
||||
+37
-8
@@ -34,19 +34,33 @@ Generic::Generic(Generic&& _other) noexcept = default;
|
||||
|
||||
Generic::Generic(const Generic& _other) = default;
|
||||
|
||||
Generic::Generic(const ReflectionType& _value) : value_(_value) {}
|
||||
Generic::Generic(const VariantType& _value) : value_(_value) {}
|
||||
|
||||
Generic::Generic(ReflectionType&& _value) noexcept
|
||||
: value_(std::move(_value)) {}
|
||||
Generic::Generic(VariantType&& _value) noexcept : value_(std::move(_value)) {}
|
||||
|
||||
Generic::Generic(const ReflectionType& _r) : value_(from_reflection_type(_r)) {}
|
||||
|
||||
Generic::~Generic() = default;
|
||||
|
||||
Generic& Generic::operator=(const ReflectionType& _value) {
|
||||
Generic::VariantType Generic::from_reflection_type(
|
||||
const ReflectionType& _r) noexcept {
|
||||
if (!_r) {
|
||||
return std::nullopt;
|
||||
} else {
|
||||
return std::visit([](const auto& _v) -> VariantType { return _v; }, *_r);
|
||||
}
|
||||
}
|
||||
|
||||
bool Generic::is_null() const noexcept {
|
||||
return std::get_if<std::nullopt_t>(&value_) && true;
|
||||
}
|
||||
|
||||
Generic& Generic::operator=(const VariantType& _value) {
|
||||
value_ = _value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Generic& Generic::operator=(ReflectionType&& _value) noexcept {
|
||||
Generic& Generic::operator=(VariantType&& _value) noexcept {
|
||||
value_ = std::move(_value);
|
||||
return *this;
|
||||
}
|
||||
@@ -55,6 +69,11 @@ Generic& Generic::operator=(const Generic& _other) = default;
|
||||
|
||||
Generic& Generic::operator=(Generic&& _other) = default;
|
||||
|
||||
Generic::ReflectionType Generic::reflection() const noexcept {
|
||||
return std::visit([](const auto& _v) -> ReflectionType { return _v; },
|
||||
value_);
|
||||
}
|
||||
|
||||
Result<Generic::Array> Generic::to_array() const noexcept {
|
||||
if (const auto* ptr = std::get_if<Array>(&value_)) {
|
||||
return *ptr;
|
||||
@@ -66,7 +85,7 @@ Result<Generic::Array> Generic::to_array() const noexcept {
|
||||
}
|
||||
|
||||
Result<bool> Generic::to_bool() const noexcept {
|
||||
if (const auto* ptr = std::get_if<bool>(&value_)) {
|
||||
if (const bool* ptr = std::get_if<bool>(&value_)) {
|
||||
return *ptr;
|
||||
} else {
|
||||
return Error(
|
||||
@@ -75,7 +94,7 @@ Result<bool> Generic::to_bool() const noexcept {
|
||||
}
|
||||
|
||||
Result<double> Generic::to_double() const noexcept {
|
||||
if (const auto* ptr = std::get_if<double>(&value_)) {
|
||||
if (const double* ptr = std::get_if<double>(&value_)) {
|
||||
return *ptr;
|
||||
} else {
|
||||
return Error(
|
||||
@@ -84,7 +103,7 @@ Result<double> Generic::to_double() const noexcept {
|
||||
}
|
||||
|
||||
Result<int> Generic::to_int() const noexcept {
|
||||
if (const auto* ptr = std::get_if<int>(&value_)) {
|
||||
if (const int* ptr = std::get_if<int>(&value_)) {
|
||||
return *ptr;
|
||||
} else {
|
||||
return Error(
|
||||
@@ -102,6 +121,16 @@ Result<Generic::Object> Generic::to_object() const noexcept {
|
||||
}
|
||||
}
|
||||
|
||||
Result<std::nullopt_t> Generic::to_null() const noexcept {
|
||||
if (const auto* ptr = std::get_if<std::nullopt_t>(&value_)) {
|
||||
return *ptr;
|
||||
} else {
|
||||
return Error(
|
||||
"rfl::Generic: Could not cast the underlying value to "
|
||||
"rfl::Generic::Null.");
|
||||
}
|
||||
}
|
||||
|
||||
Result<std::string> Generic::to_string() const noexcept {
|
||||
if (const auto* ptr = std::get_if<std::string>(&value_)) {
|
||||
return *ptr;
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023-2024 Code17 GmbH
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
#include "rfl/generic/Reader.hpp"
|
||||
|
||||
namespace rfl::generic {
|
||||
|
||||
rfl::Result<Reader::InputVarType> Reader::get_field_from_array(
|
||||
const size_t _idx, const InputArrayType& _arr) const noexcept {
|
||||
if (_idx >= _arr.size()) {
|
||||
return rfl::Error("Index " + std::to_string(_idx) + " of of bounds.");
|
||||
}
|
||||
return _arr[_idx];
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputVarType> Reader::get_field_from_object(
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept {
|
||||
return _obj.get(_name);
|
||||
}
|
||||
|
||||
bool Reader::is_empty(const InputVarType& _var) const noexcept {
|
||||
return _var.is_null();
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputArrayType> Reader::to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
return _var.to_array();
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputObjectType> Reader::to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
return _var.to_object();
|
||||
}
|
||||
|
||||
} // namespace rfl::generic
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023-2024 Code17 GmbH
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
#include "rfl/generic/Writer.hpp"
|
||||
|
||||
namespace rfl::generic {
|
||||
|
||||
Writer::OutputArrayType Writer::array_as_root(
|
||||
const size_t _size) const noexcept {
|
||||
root_ = Generic::Array();
|
||||
return OutputArray{get_if<Generic::Array>(&root_.variant())};
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::object_as_root(
|
||||
const size_t _size) const noexcept {
|
||||
root_ = Generic::Object();
|
||||
return OutputObject{get_if<Generic::Object>(&root_.variant())};
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::null_as_root() const noexcept {
|
||||
root_ = Generic::Null;
|
||||
return root_;
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::add_array_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
_parent->val_->push_back(Generic(Generic::Array()));
|
||||
return OutputArrayType{
|
||||
get_if<Generic::Array>(&_parent->val_->rbegin()->variant())};
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::add_array_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
_parent->val_->insert(_name, Generic(Generic::Array()));
|
||||
return OutputArrayType{
|
||||
get_if<Generic::Array>(&_parent->val_->rbegin()->second.variant())};
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
_parent->val_->push_back(Generic(Generic::Object()));
|
||||
return OutputObjectType{
|
||||
get_if<Generic::Object>(&_parent->val_->rbegin()->variant())};
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::add_object_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
_parent->val_->insert(_name, Generic(Generic::Object()));
|
||||
return OutputObjectType{
|
||||
get_if<Generic::Object>(&_parent->val_->rbegin()->second.variant())};
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::add_null_to_array(
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
_parent->val_->push_back(Generic(Generic::Null));
|
||||
return Generic::Null;
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::add_null_to_object(
|
||||
const std::string_view& _name, OutputObjectType* _parent) const noexcept {
|
||||
_parent->val_->insert(_name, Generic(Generic::Null));
|
||||
return Generic::Null;
|
||||
}
|
||||
|
||||
} // namespace rfl::generic
|
||||
@@ -7,6 +7,7 @@ else()
|
||||
endif()
|
||||
|
||||
if (REFLECTCPP_JSON)
|
||||
add_subdirectory(generic)
|
||||
add_subdirectory(json)
|
||||
add_subdirectory(json_c_arrays_and_inheritance)
|
||||
endif ()
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
project(reflect-cpp-generic-tests)
|
||||
|
||||
file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp")
|
||||
|
||||
add_executable(
|
||||
reflect-cpp-generic-tests
|
||||
${SOURCES}
|
||||
"${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/src/gtest_main.cc"
|
||||
)
|
||||
|
||||
target_include_directories(reflect-cpp-generic-tests SYSTEM PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include")
|
||||
|
||||
target_link_libraries(
|
||||
reflect-cpp-generic-tests
|
||||
PRIVATE
|
||||
reflectcpp
|
||||
"${REFLECT_CPP_GTEST_LIB}"
|
||||
)
|
||||
@@ -0,0 +1,46 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_add_struct_name {
|
||||
|
||||
using Age = rfl::Validator<unsigned int, rfl::Minimum<0>, rfl::Maximum<130>>;
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::string town = "Springfield";
|
||||
rfl::Timestamp<"%Y-%m-%d"> birthday;
|
||||
Age age;
|
||||
rfl::Email email;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
|
||||
TEST(generic, test_add_struct_name) {
|
||||
const auto bart = Person{.first_name = "Bart",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 10,
|
||||
.email = "bart@simpson.com"};
|
||||
|
||||
const auto lisa = Person{.first_name = "Lisa",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 8,
|
||||
.email = "lisa@simpson.com"};
|
||||
|
||||
const auto maggie = Person{.first_name = "Maggie",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 0,
|
||||
.email = "maggie@simpson.com"};
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 45,
|
||||
.email = "homer@simpson.com",
|
||||
.children = std::vector<Person>({bart, lisa, maggie})};
|
||||
|
||||
write_and_read<rfl::AddStructName<"type">>(homer);
|
||||
}
|
||||
} // namespace test_add_struct_name
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_array {
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::unique_ptr<std::array<Person, 3>> children = nullptr;
|
||||
};
|
||||
|
||||
TEST(generic, test_array) {
|
||||
auto bart = Person{.first_name = "Bart"};
|
||||
|
||||
auto lisa = Person{.first_name = "Lisa"};
|
||||
|
||||
auto maggie = Person{.first_name = "Maggie"};
|
||||
|
||||
const auto homer = Person{
|
||||
.first_name = "Homer",
|
||||
.children = std::make_unique<std::array<Person, 3>>(std::array<Person, 3>{
|
||||
std::move(bart), std::move(lisa), std::move(maggie)})};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_array
|
||||
@@ -0,0 +1,44 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_box {
|
||||
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
using Tag = rfl::Literal<"Leaf">;
|
||||
double value;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
using Tag = rfl::Literal<"Node">;
|
||||
rfl::Rename<"criticalValue", double> critical_value;
|
||||
rfl::Box<DecisionTree> lesser;
|
||||
rfl::Box<DecisionTree> greater;
|
||||
};
|
||||
|
||||
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
|
||||
|
||||
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
|
||||
};
|
||||
|
||||
TEST(generic, test_box) {
|
||||
auto leaf1 = DecisionTree::Leaf{.value = 3.0};
|
||||
|
||||
auto leaf2 = DecisionTree::Leaf{.value = 5.0};
|
||||
|
||||
auto node = DecisionTree::Node{
|
||||
.critical_value = 10.0,
|
||||
.lesser = rfl::make_box<DecisionTree>(DecisionTree{leaf1}),
|
||||
.greater = rfl::make_box<DecisionTree>(DecisionTree{leaf2})};
|
||||
|
||||
const DecisionTree tree{.leaf_or_node = std::move(node)};
|
||||
|
||||
write_and_read(tree);
|
||||
|
||||
}
|
||||
} // namespace test_box
|
||||
@@ -0,0 +1,49 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_combined_processors {
|
||||
|
||||
using Age = rfl::Validator<unsigned int, rfl::Minimum<0>, rfl::Maximum<130>>;
|
||||
|
||||
struct Person {
|
||||
std::string first_name;
|
||||
std::string last_name = "Simpson";
|
||||
std::string town = "Springfield";
|
||||
rfl::Timestamp<"%Y-%m-%d"> birthday;
|
||||
Age age;
|
||||
rfl::Email email;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
|
||||
TEST(generic, test_combined_processors) {
|
||||
const auto bart = Person{.first_name = "Bart",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 10,
|
||||
.email = "bart@simpson.com"};
|
||||
|
||||
const auto lisa = Person{.first_name = "Lisa",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 8,
|
||||
.email = "lisa@simpson.com"};
|
||||
|
||||
const auto maggie = Person{.first_name = "Maggie",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 0,
|
||||
.email = "maggie@simpson.com"};
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 45,
|
||||
.email = "homer@simpson.com",
|
||||
.children = std::vector<Person>({bart, lisa, maggie})};
|
||||
|
||||
using Processors =
|
||||
rfl::Processors<rfl::SnakeCaseToCamelCase, rfl::AddStructName<"type">>;
|
||||
|
||||
write_and_read<Processors>(homer);
|
||||
}
|
||||
} // namespace test_combined_processors
|
||||
@@ -0,0 +1,36 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_custom_class1 {
|
||||
|
||||
struct Person {
|
||||
struct PersonImpl {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::vector<Person> children;
|
||||
};
|
||||
|
||||
using ReflectionType = PersonImpl;
|
||||
|
||||
Person(const PersonImpl& _impl) : impl(_impl) {}
|
||||
|
||||
Person(const std::string& _first_name)
|
||||
: impl(PersonImpl{.first_name = _first_name}) {}
|
||||
|
||||
const ReflectionType& reflection() const { return impl; };
|
||||
|
||||
private:
|
||||
PersonImpl impl;
|
||||
};
|
||||
|
||||
TEST(generic, test_custom_class1) {
|
||||
const auto bart = Person("Bart");
|
||||
|
||||
write_and_read(bart);
|
||||
}
|
||||
} // namespace test_custom_class1
|
||||
@@ -0,0 +1,64 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_custom_class3 {
|
||||
|
||||
struct Person {
|
||||
Person(const std::string& _first_name, const std::string& _last_name,
|
||||
const int _age)
|
||||
: first_name_(_first_name), last_name_(_last_name), age_(_age) {}
|
||||
|
||||
const auto& first_name() const { return first_name_; }
|
||||
|
||||
const auto& last_name() const { return last_name_; }
|
||||
|
||||
auto age() const { return age_; }
|
||||
|
||||
private:
|
||||
std::string first_name_;
|
||||
std::string last_name_;
|
||||
int age_;
|
||||
};
|
||||
|
||||
struct PersonImpl {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name;
|
||||
int age;
|
||||
|
||||
static PersonImpl from_class(const Person& _p) noexcept {
|
||||
return PersonImpl{.first_name = _p.first_name(),
|
||||
.last_name = _p.last_name(),
|
||||
.age = _p.age()};
|
||||
}
|
||||
|
||||
Person to_class() const { return Person(first_name(), last_name(), age); }
|
||||
};
|
||||
} // namespace test_custom_class3
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class ReaderType, class WriterType, class ProcessorsType>
|
||||
struct Parser<ReaderType, WriterType, test_custom_class3::Person,
|
||||
ProcessorsType>
|
||||
: public CustomParser<ReaderType, WriterType, ProcessorsType,
|
||||
test_custom_class3::Person,
|
||||
test_custom_class3::PersonImpl> {};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
namespace test_custom_class3 {
|
||||
|
||||
TEST(generic, test_custom_class3) {
|
||||
const auto bart = Person("Bart", "Simpson", 10);
|
||||
|
||||
write_and_read(bart);
|
||||
}
|
||||
|
||||
} // namespace test_custom_class3
|
||||
@@ -0,0 +1,65 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_custom_class4 {
|
||||
|
||||
struct Person {
|
||||
Person(const std::string& _first_name,
|
||||
const rfl::Box<std::string>& _last_name, int _age)
|
||||
: first_name_(_first_name),
|
||||
last_name_(rfl::make_box<std::string>(*_last_name)),
|
||||
age_(_age) {}
|
||||
|
||||
const auto& first_name() const { return first_name_; }
|
||||
|
||||
const auto& last_name() const { return last_name_; }
|
||||
|
||||
auto age() const { return age_; }
|
||||
|
||||
private:
|
||||
std::string first_name_;
|
||||
rfl::Box<std::string> last_name_;
|
||||
int age_;
|
||||
};
|
||||
|
||||
struct PersonImpl {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", rfl::Box<std::string>> last_name;
|
||||
rfl::Field<"age", int> age;
|
||||
|
||||
static PersonImpl from_class(const Person& _p) noexcept {
|
||||
return PersonImpl{.first_name = _p.first_name(),
|
||||
.last_name = rfl::make_box<std::string>(*_p.last_name()),
|
||||
.age = _p.age()};
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace test_custom_class4
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class ReaderType, class WriterType, class ProcessorsType>
|
||||
struct Parser<ReaderType, WriterType, test_custom_class4::Person,
|
||||
ProcessorsType>
|
||||
: public CustomParser<ReaderType, WriterType, ProcessorsType,
|
||||
test_custom_class4::Person,
|
||||
test_custom_class4::PersonImpl> {};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
namespace test_custom_class4 {
|
||||
|
||||
TEST(generic, test_custom_class4) {
|
||||
const auto bart = test_custom_class4::Person(
|
||||
"Bart", rfl::make_box<std::string>("Simpson"), 10);
|
||||
|
||||
write_and_read(bart);
|
||||
}
|
||||
} // namespace test_custom_class4
|
||||
@@ -0,0 +1,27 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_default_values {
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::vector<Person> children;
|
||||
};
|
||||
|
||||
TEST(generic, test_default_values) {
|
||||
const auto bart = Person{.first_name = "Bart"};
|
||||
const auto lisa = Person{.first_name = "Lisa"};
|
||||
const auto maggie = Person{.first_name = "Maggie"};
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer",
|
||||
.children = std::vector<Person>({bart, lisa, maggie})};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_default_values
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_deque {
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::unique_ptr<std::deque<Person>> children;
|
||||
};
|
||||
|
||||
TEST(generic, test_default_values) {
|
||||
auto children = std::make_unique<std::deque<Person>>();
|
||||
children->emplace_back(Person{.first_name = "Bart"});
|
||||
children->emplace_back(Person{.first_name = "Lisa"});
|
||||
children->emplace_back(Person{.first_name = "Maggie"});
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
|
||||
}
|
||||
} // namespace test_deque
|
||||
@@ -0,0 +1,24 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_enum {
|
||||
|
||||
enum class Color { red, green, blue, yellow };
|
||||
|
||||
struct Circle {
|
||||
float radius;
|
||||
Color color;
|
||||
};
|
||||
|
||||
TEST(generic, test_enum) {
|
||||
const auto circle = Circle{.radius = 2.0, .color = Color::green};
|
||||
|
||||
write_and_read(circle);
|
||||
}
|
||||
|
||||
} // namespace test_enum
|
||||
@@ -0,0 +1,34 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_field_variant {
|
||||
|
||||
struct Circle {
|
||||
double radius;
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
double height;
|
||||
double width;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
double width;
|
||||
};
|
||||
|
||||
using Shapes = rfl::Variant<rfl::Field<"circle", Circle>,
|
||||
rfl::Field<"rectangle", Rectangle>,
|
||||
rfl::Field<"square", rfl::Box<Square>>>;
|
||||
|
||||
TEST(generic, test_field_variant) {
|
||||
const Shapes r =
|
||||
rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5});
|
||||
|
||||
write_and_read(r);
|
||||
}
|
||||
} // namespace test_field_variant
|
||||
@@ -0,0 +1,35 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_flag_enum {
|
||||
|
||||
enum class Color {
|
||||
red = 256,
|
||||
green = 512,
|
||||
blue = 1024,
|
||||
yellow = 2048,
|
||||
orange = (256 | 2048) // red + yellow = orange
|
||||
};
|
||||
|
||||
inline Color operator|(Color c1, Color c2) {
|
||||
return static_cast<Color>(static_cast<int>(c1) | static_cast<int>(c2));
|
||||
}
|
||||
|
||||
struct Circle {
|
||||
float radius;
|
||||
Color color;
|
||||
};
|
||||
|
||||
TEST(generic, test_flag_enum) {
|
||||
const auto circle =
|
||||
Circle{.radius = 2.0, .color = Color::blue | Color::orange};
|
||||
|
||||
write_and_read(circle);
|
||||
}
|
||||
|
||||
} // namespace test_flag_enum
|
||||
@@ -0,0 +1,34 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_flag_enum_with_int {
|
||||
|
||||
enum class Color {
|
||||
red = 256,
|
||||
green = 512,
|
||||
blue = 1024,
|
||||
yellow = 2048,
|
||||
orange = (256 | 2048) // red + yellow = orange
|
||||
};
|
||||
|
||||
inline Color operator|(Color c1, Color c2) {
|
||||
return static_cast<Color>(static_cast<int>(c1) | static_cast<int>(c2));
|
||||
}
|
||||
|
||||
struct Circle {
|
||||
float radius;
|
||||
Color color;
|
||||
};
|
||||
|
||||
TEST(generic, test_flag_enum_with_int) {
|
||||
const auto circle = Circle{.radius = 2.0, .color = static_cast<Color>(10000)};
|
||||
|
||||
write_and_read(circle);
|
||||
}
|
||||
|
||||
} // namespace test_flag_enum_with_int
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_flatten {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", rfl::Box<std::string>> last_name;
|
||||
rfl::Field<"age", int> age;
|
||||
};
|
||||
|
||||
struct Employee {
|
||||
rfl::Flatten<Person> person;
|
||||
rfl::Field<"employer", rfl::Box<std::string>> employer;
|
||||
rfl::Field<"salary", float> salary;
|
||||
};
|
||||
|
||||
TEST(generic, test_flatten) {
|
||||
const auto employee = Employee{
|
||||
.person = Person{.first_name = "Homer",
|
||||
.last_name = rfl::make_box<std::string>("Simpson"),
|
||||
.age = 45},
|
||||
.employer = rfl::make_box<std::string>("Mr. Burns"),
|
||||
.salary = 60000.0};
|
||||
|
||||
write_and_read(employee);
|
||||
}
|
||||
} // namespace test_flatten
|
||||
@@ -0,0 +1,34 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_flatten_anonymous {
|
||||
|
||||
struct Person {
|
||||
std::string first_name;
|
||||
rfl::Box<std::string> last_name;
|
||||
int age;
|
||||
};
|
||||
|
||||
struct Employee {
|
||||
rfl::Flatten<Person> person;
|
||||
rfl::Box<std::string> employer;
|
||||
float salary;
|
||||
};
|
||||
|
||||
TEST(generic, test_flatten_anonymous) {
|
||||
const auto employee = Employee{
|
||||
.person = Person{.first_name = "Homer",
|
||||
.last_name = rfl::make_box<std::string>("Simpson"),
|
||||
.age = 45},
|
||||
.employer = rfl::make_box<std::string>("Mr. Burns"),
|
||||
.salary = 60000.0};
|
||||
|
||||
write_and_read(employee);
|
||||
}
|
||||
|
||||
} // namespace test_flatten_anonymous
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_forward_list {
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::unique_ptr<std::forward_list<Person>> children;
|
||||
};
|
||||
|
||||
TEST(generic, test_forward_list) {
|
||||
auto children = std::make_unique<std::forward_list<Person>>();
|
||||
children->emplace_front(Person{.first_name = "Maggie"});
|
||||
children->emplace_front(Person{.first_name = "Lisa"});
|
||||
children->emplace_front(Person{.first_name = "Bart"});
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
|
||||
}
|
||||
} // namespace test_forward_list
|
||||
@@ -0,0 +1,25 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_literal {
|
||||
|
||||
using FirstName = rfl::Literal<"Homer", "Marge", "Bart", "Lisa", "Maggie">;
|
||||
using LastName = rfl::Literal<"Simpson">;
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", FirstName> first_name;
|
||||
rfl::Rename<"lastName", LastName> last_name;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
|
||||
TEST(generic, test_literal) {
|
||||
const auto bart = Person{.first_name = FirstName::make<"Bart">()};
|
||||
|
||||
write_and_read(bart);
|
||||
}
|
||||
} // namespace test_literal
|
||||
@@ -0,0 +1,23 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_literal_map {
|
||||
|
||||
using FieldName = rfl::Literal<"firstName", "lastName">;
|
||||
|
||||
TEST(generic, test_literal_map) {
|
||||
std::map<FieldName, std::unique_ptr<std::string>> homer;
|
||||
homer.insert(std::make_pair(FieldName::make<"firstName">(),
|
||||
std::make_unique<std::string>("Homer")));
|
||||
homer.insert(std::make_pair(FieldName::make<"lastName">(),
|
||||
std::make_unique<std::string>("Simpson")));
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_literal_map
|
||||
@@ -0,0 +1,27 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_map {
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::map<std::string, Person> children;
|
||||
};
|
||||
|
||||
TEST(generic, test_map) {
|
||||
auto children = std::map<std::string, Person>();
|
||||
children.insert(std::make_pair("child1", Person{.first_name = "Bart"}));
|
||||
children.insert(std::make_pair("child2", Person{.first_name = "Lisa"}));
|
||||
children.insert(std::make_pair("child3", Person{.first_name = "Maggie"}));
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_map
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_map_with_key_validation {
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::unique_ptr<std::map<rfl::AlphaNumeric, Person>> children;
|
||||
};
|
||||
|
||||
TEST(generic, test_map_with_key_validation) {
|
||||
auto children = std::make_unique<std::map<rfl::AlphaNumeric, Person>>();
|
||||
|
||||
children->insert(std::make_pair("Bart", Person{.first_name = "Bart"}));
|
||||
children->insert(std::make_pair("Lisa", Person{.first_name = "Lisa"}));
|
||||
children->insert(std::make_pair("Maggie", Person{.first_name = "Maggie"}));
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_map_with_key_validation
|
||||
@@ -0,0 +1,59 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_monster_example {
|
||||
|
||||
using Color = rfl::Literal<"Red", "Green", "Blue">;
|
||||
|
||||
struct Weapon {
|
||||
std::string name;
|
||||
short damage;
|
||||
};
|
||||
|
||||
using Equipment = rfl::Variant<rfl::Field<"weapon", Weapon>>;
|
||||
|
||||
struct Vec3 {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
};
|
||||
|
||||
struct Monster {
|
||||
Vec3 pos;
|
||||
short mana = 150;
|
||||
short hp = 100;
|
||||
std::string name;
|
||||
bool friendly = false;
|
||||
std::vector<int> inventory;
|
||||
Color color = Color::make<"Blue">();
|
||||
std::vector<Weapon> weapons;
|
||||
Equipment equipped;
|
||||
std::vector<Vec3> path;
|
||||
};
|
||||
|
||||
TEST(generic, test_monster_example) {
|
||||
const auto sword = Weapon{.name = "Sword", .damage = 3};
|
||||
const auto axe = Weapon{.name = "Axe", .damage = 5};
|
||||
|
||||
const auto weapons = std::vector<Weapon>({sword, axe});
|
||||
|
||||
const auto position = Vec3{1.0f, 2.0f, 3.0f};
|
||||
|
||||
const auto inventory = std::vector<int>({0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
|
||||
|
||||
const auto orc = Monster{.pos = position,
|
||||
.mana = 150,
|
||||
.hp = 80,
|
||||
.name = "MyMonster",
|
||||
.inventory = inventory,
|
||||
.color = Color::make<"Red">(),
|
||||
.weapons = weapons,
|
||||
.equipped = rfl::make_field<"weapon">(axe)};
|
||||
|
||||
write_and_read(orc);
|
||||
}
|
||||
} // namespace test_monster_example
|
||||
@@ -0,0 +1,46 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_readme_example {
|
||||
|
||||
using Age = rfl::Validator<unsigned int, rfl::Minimum<0>, rfl::Maximum<130>>;
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::string town = "Springfield";
|
||||
rfl::Timestamp<"%Y-%m-%d"> birthday;
|
||||
Age age;
|
||||
rfl::Email email;
|
||||
std::vector<Person> child;
|
||||
};
|
||||
|
||||
TEST(generic, test_readme_example) {
|
||||
const auto bart = Person{.first_name = "Bart",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 10,
|
||||
.email = "bart@simpson.com"};
|
||||
|
||||
const auto lisa = Person{.first_name = "Lisa",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 8,
|
||||
.email = "lisa@simpson.com"};
|
||||
|
||||
const auto maggie = Person{.first_name = "Maggie",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 0,
|
||||
.email = "maggie@simpson.com"};
|
||||
|
||||
const auto homer = Person{.first_name = "Homer",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 45,
|
||||
.email = "homer@simpson.com",
|
||||
.child = std::vector<Person>({bart, lisa, maggie})};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_readme_example
|
||||
@@ -0,0 +1,22 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_readme_example2 {
|
||||
|
||||
struct Person {
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
int age;
|
||||
};
|
||||
|
||||
TEST(generic, test_readme_example2) {
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .last_name = "Simpson", .age = 45};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_readme_example2
|
||||
@@ -0,0 +1,44 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_ref {
|
||||
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
using Tag = rfl::Literal<"Leaf">;
|
||||
double value;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
using Tag = rfl::Literal<"Node">;
|
||||
rfl::Rename<"criticalValue", double> critical_value;
|
||||
rfl::Ref<DecisionTree> lesser;
|
||||
rfl::Ref<DecisionTree> greater;
|
||||
};
|
||||
|
||||
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
|
||||
|
||||
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
|
||||
};
|
||||
|
||||
TEST(generic, test_ref) {
|
||||
const auto leaf1 = DecisionTree::Leaf{.value = 3.0};
|
||||
|
||||
const auto leaf2 = DecisionTree::Leaf{.value = 5.0};
|
||||
|
||||
auto node = DecisionTree::Node{
|
||||
.critical_value = 10.0,
|
||||
.lesser = rfl::make_ref<DecisionTree>(DecisionTree{leaf1}),
|
||||
.greater = rfl::make_ref<DecisionTree>(DecisionTree{leaf2})};
|
||||
|
||||
const DecisionTree tree{.leaf_or_node = std::move(node)};
|
||||
|
||||
write_and_read(tree);
|
||||
|
||||
}
|
||||
} // namespace test_ref
|
||||
@@ -0,0 +1,25 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_set {
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::unique_ptr<std::set<std::string>> children;
|
||||
};
|
||||
|
||||
TEST(generic, test_set) {
|
||||
auto children = std::make_unique<std::set<std::string>>(
|
||||
std::set<std::string>({"Bart", "Lisa", "Maggie"}));
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_set
|
||||
@@ -0,0 +1,37 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_size {
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name;
|
||||
rfl::Timestamp<"%Y-%m-%d"> birthday;
|
||||
rfl::Validator<std::vector<Person>,
|
||||
rfl::Size<rfl::AnyOf<rfl::EqualTo<0>, rfl::EqualTo<3>>>>
|
||||
children;
|
||||
};
|
||||
|
||||
TEST(generic, test_size) {
|
||||
const auto bart = Person{
|
||||
.first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19"};
|
||||
|
||||
const auto lisa = Person{
|
||||
.first_name = "Lisa", .last_name = "Simpson", .birthday = "1987-04-19"};
|
||||
|
||||
const auto maggie = Person{
|
||||
.first_name = "Maggie", .last_name = "Simpson", .birthday = "1987-04-19"};
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer",
|
||||
.last_name = "Simpson",
|
||||
.birthday = "1987-04-19",
|
||||
.children = std::vector<Person>({bart, lisa, maggie})};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_size
|
||||
@@ -0,0 +1,34 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_snake_case_to_camel_case {
|
||||
|
||||
struct Person {
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
rfl::Timestamp<"%Y-%m-%d"> birthday;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
|
||||
TEST(generic, test_snake_case_to_camel_case) {
|
||||
const auto bart = Person{
|
||||
.first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19"};
|
||||
|
||||
const auto lisa = Person{
|
||||
.first_name = "Lisa", .last_name = "Simpson", .birthday = "1987-04-19"};
|
||||
|
||||
const auto maggie = Person{
|
||||
.first_name = "Maggie", .last_name = "Simpson", .birthday = "1987-04-19"};
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer",
|
||||
.last_name = "Simpson",
|
||||
.birthday = "1987-04-19",
|
||||
.children = std::vector<Person>({bart, lisa, maggie})};
|
||||
|
||||
write_and_read<rfl::SnakeCaseToCamelCase>(homer);
|
||||
}
|
||||
} // namespace test_snake_case_to_camel_case
|
||||
@@ -0,0 +1,34 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_snake_case_to_pascal_case {
|
||||
|
||||
struct Person {
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
rfl::Timestamp<"%Y-%m-%d"> birthday;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
|
||||
TEST(bson, test_snake_case_to_pascal_case) {
|
||||
const auto bart = Person{
|
||||
.first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19"};
|
||||
|
||||
const auto lisa = Person{
|
||||
.first_name = "Lisa", .last_name = "Simpson", .birthday = "1987-04-19"};
|
||||
|
||||
const auto maggie = Person{
|
||||
.first_name = "Maggie", .last_name = "Simpson", .birthday = "1987-04-19"};
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer",
|
||||
.last_name = "Simpson",
|
||||
.birthday = "1987-04-19",
|
||||
.children = std::vector<Person>({bart, lisa, maggie})};
|
||||
|
||||
write_and_read<rfl::SnakeCaseToPascalCase>(homer);
|
||||
}
|
||||
} // namespace test_snake_case_to_pascal_case
|
||||
@@ -0,0 +1,19 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_string_map {
|
||||
TEST(generic, test_string_map) {
|
||||
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);
|
||||
}
|
||||
} // namespace test_string_map
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_tagged_union {
|
||||
|
||||
struct Circle {
|
||||
double radius;
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
double height;
|
||||
double width;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
double width;
|
||||
};
|
||||
|
||||
using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>;
|
||||
|
||||
TEST(generic, test_tagged_union) {
|
||||
const Shapes r = Rectangle{.height = 10, .width = 5};
|
||||
write_and_read(r);
|
||||
}
|
||||
} // namespace test_tagged_union
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <ctime>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_timestamp {
|
||||
|
||||
using TS = rfl::Timestamp<"%Y-%m-%d">;
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
TS birthday;
|
||||
};
|
||||
|
||||
TEST(generic, test_timestamp) {
|
||||
const auto result = TS::from_string("nonsense");
|
||||
|
||||
if (result) {
|
||||
std::cout << "Failed: Expected an error, but got none." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
const auto bart = Person{.first_name = "Bart", .birthday = "1987-04-19"};
|
||||
|
||||
write_and_read(bart);
|
||||
}
|
||||
} // namespace test_timestamp
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_unique_ptr {
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::unique_ptr<std::vector<Person>> children;
|
||||
};
|
||||
|
||||
TEST(generic, test_unique_ptr) {
|
||||
auto children = std::make_unique<std::vector<Person>>();
|
||||
children->emplace_back(Person{.first_name = "Bart"});
|
||||
children->emplace_back(Person{.first_name = "Lisa"});
|
||||
children->emplace_back(Person{.first_name = "Maggie"});
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_unique_ptr
|
||||
@@ -0,0 +1,43 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_unique_ptr2 {
|
||||
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
using Tag = rfl::Literal<"Leaf">;
|
||||
double value;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
using Tag = rfl::Literal<"Node">;
|
||||
rfl::Rename<"criticalValue", double> critical_value;
|
||||
std::unique_ptr<DecisionTree> lesser;
|
||||
std::unique_ptr<DecisionTree> greater;
|
||||
};
|
||||
|
||||
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
|
||||
|
||||
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
|
||||
};
|
||||
|
||||
TEST(generic, test_unique_ptr2) {
|
||||
auto leaf1 = DecisionTree::Leaf{.value = 3.0};
|
||||
|
||||
auto leaf2 = DecisionTree::Leaf{.value = 5.0};
|
||||
|
||||
auto node = DecisionTree::Node{
|
||||
.critical_value = 10.0,
|
||||
.lesser = std::make_unique<DecisionTree>(DecisionTree{leaf1}),
|
||||
.greater = std::make_unique<DecisionTree>(DecisionTree{leaf2})};
|
||||
|
||||
const DecisionTree tree{.leaf_or_node = std::move(node)};
|
||||
|
||||
write_and_read(tree);
|
||||
}
|
||||
} // namespace test_unique_ptr2
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_variant {
|
||||
|
||||
struct Circle {
|
||||
double radius;
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
double height;
|
||||
double width;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
double width;
|
||||
};
|
||||
|
||||
using Shapes = std::variant<Circle, Rectangle, std::unique_ptr<Square>>;
|
||||
|
||||
TEST(generic, test_variant) {
|
||||
const Shapes r = Rectangle{.height = 10, .width = 5};
|
||||
|
||||
write_and_read(r);
|
||||
}
|
||||
} // namespace test_variant
|
||||
@@ -0,0 +1,21 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
struct TestStruct {
|
||||
std::string theNormalString;
|
||||
std::wstring theWiderString;
|
||||
};
|
||||
|
||||
namespace test_wstring {
|
||||
TEST(generic, test_wstring) {
|
||||
const auto test = TestStruct{.theNormalString = "The normal string",
|
||||
.theWiderString = L"The wider string"};
|
||||
|
||||
write_and_read(test);
|
||||
}
|
||||
} // namespace test_wstring
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef WRITE_AND_READ_
|
||||
#define WRITE_AND_READ_
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
|
||||
template <class... Ps>
|
||||
void write_and_read(const auto& _struct) {
|
||||
using T = std::remove_cvref_t<decltype(_struct)>;
|
||||
const auto serialized1 = rfl::to_generic<Ps...>(_struct);
|
||||
EXPECT_EQ(rfl::json::write(serialized1), rfl::json::write<Ps...>(_struct));
|
||||
const auto res = rfl::from_generic<T, Ps...>(serialized1);
|
||||
EXPECT_TRUE(res && true) << "Test failed on read. Error: "
|
||||
<< res.error().value().what();
|
||||
EXPECT_EQ(rfl::json::write(serialized1),
|
||||
rfl::json::write<Ps...>(res.value()));
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -68,7 +68,7 @@ TEST(json, test_json_schema) {
|
||||
Person>>();
|
||||
|
||||
const std::string expected =
|
||||
R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/definitions/test_json_schema__Person","description":"JSON schema that describes the required attributes for the person class.","definitions":{"rfl__Generic":{"anyOf":[{"type":"boolean"},{"type":"integer"},{"type":"number"},{"type":"string"},{"type":"object","additionalProperties":{"$ref":"#/definitions/rfl__Generic"}},{"type":"array","items":{"$ref":"#/definitions/rfl__Generic"}}]},"test_json_schema__Circle":{"type":"object","properties":{"radius":{"type":"number"}},"required":["radius"]},"test_json_schema__Circle__tagged":{"type":"object","properties":{"radius":{"type":"number"},"shape":{"type":"string","enum":["Circle"]}},"required":["radius","shape"]},"test_json_schema__Person":{"type":"object","properties":{"age":{"allOf":[{"minimum":0,"type":"integer"},{"maximum":130,"type":"integer"}]},"children":{"type":"array","description":"The person's children. Pass an empty array for no children.","items":{"$ref":"#/definitions/test_json_schema__Person"}},"color":{"type":"string","enum":["red","green","blue"]},"email":{"type":"string","description":"Must be a proper email in the form xxx@xxx.xxx.","pattern":"^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$"},"fieldVariant":{"anyOf":[{"type":"object","properties":{"rectangle":{"$ref":"#/definitions/test_json_schema__Rectangle"}},"required":["rectangle"]},{"type":"object","properties":{"square":{"$ref":"#/definitions/test_json_schema__Square"}},"required":["square"]},{"type":"object","properties":{"circle":{"$ref":"#/definitions/test_json_schema__Circle"}},"required":["circle"]}]},"firstName":{"type":"string"},"generic":{"$ref":"#/definitions/rfl__Generic"},"lastName":{"type":"string"},"salary":{"type":"number"},"taggedUnion":{"anyOf":[{"$ref":"#/definitions/test_json_schema__Rectangle__tagged"},{"$ref":"#/definitions/test_json_schema__Square__tagged"},{"$ref":"#/definitions/test_json_schema__Circle__tagged"}]},"town":{"type":"string"},"tuple":{"type":"array","prefixItems":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/definitions/test_json_schema__Person"}},{"type":"integer"}],"items":false},"variant":{"anyOf":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/definitions/test_json_schema__Person"}},{"type":"integer"}]},"with_extra_fields":{"$ref":"#/definitions/test_json_schema__WithExtraFields"}},"required":["age","children","color","email","fieldVariant","firstName","generic","lastName","salary","taggedUnion","town","tuple","variant","with_extra_fields"]},"test_json_schema__Rectangle":{"type":"object","properties":{"height":{"type":"number"},"width":{"type":"number"}},"required":["height","width"]},"test_json_schema__Rectangle__tagged":{"type":"object","properties":{"height":{"type":"number"},"shape":{"type":"string","enum":["Rectangle"]},"width":{"type":"number"}},"required":["height","shape","width"]},"test_json_schema__Square":{"type":"object","properties":{"width":{"type":"number"}},"required":["width"]},"test_json_schema__Square__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Square"]},"width":{"type":"number"}},"required":["shape","width"]},"test_json_schema__WithExtraFields":{"type":"object","properties":{"f1":{"type":"string"},"f2":{"type":"string"}},"required":["f1","f2"],"additionalProperties":{"type":"string"}}}})";
|
||||
R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/definitions/test_json_schema__Person","description":"JSON schema that describes the required attributes for the person class.","definitions":{"rfl__Generic":{"anyOf":[{"anyOf":[{"type":"boolean"},{"type":"integer"},{"type":"number"},{"type":"string"},{"type":"object","additionalProperties":{"$ref":"#/definitions/rfl__Generic"}},{"type":"array","items":{"$ref":"#/definitions/rfl__Generic"}}]},{"type":"null"}]},"test_json_schema__Circle":{"type":"object","properties":{"radius":{"type":"number"}},"required":["radius"]},"test_json_schema__Circle__tagged":{"type":"object","properties":{"radius":{"type":"number"},"shape":{"type":"string","enum":["Circle"]}},"required":["radius","shape"]},"test_json_schema__Person":{"type":"object","properties":{"age":{"allOf":[{"minimum":0,"type":"integer"},{"maximum":130,"type":"integer"}]},"children":{"type":"array","description":"The person's children. Pass an empty array for no children.","items":{"$ref":"#/definitions/test_json_schema__Person"}},"color":{"type":"string","enum":["red","green","blue"]},"email":{"type":"string","description":"Must be a proper email in the form xxx@xxx.xxx.","pattern":"^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$"},"fieldVariant":{"anyOf":[{"type":"object","properties":{"rectangle":{"$ref":"#/definitions/test_json_schema__Rectangle"}},"required":["rectangle"]},{"type":"object","properties":{"square":{"$ref":"#/definitions/test_json_schema__Square"}},"required":["square"]},{"type":"object","properties":{"circle":{"$ref":"#/definitions/test_json_schema__Circle"}},"required":["circle"]}]},"firstName":{"type":"string"},"generic":{"$ref":"#/definitions/rfl__Generic"},"lastName":{"type":"string"},"salary":{"type":"number"},"taggedUnion":{"anyOf":[{"$ref":"#/definitions/test_json_schema__Rectangle__tagged"},{"$ref":"#/definitions/test_json_schema__Square__tagged"},{"$ref":"#/definitions/test_json_schema__Circle__tagged"}]},"town":{"type":"string"},"tuple":{"type":"array","prefixItems":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/definitions/test_json_schema__Person"}},{"type":"integer"}],"items":false},"variant":{"anyOf":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/definitions/test_json_schema__Person"}},{"type":"integer"}]},"with_extra_fields":{"$ref":"#/definitions/test_json_schema__WithExtraFields"}},"required":["age","children","color","email","fieldVariant","firstName","generic","lastName","salary","taggedUnion","town","tuple","variant","with_extra_fields"]},"test_json_schema__Rectangle":{"type":"object","properties":{"height":{"type":"number"},"width":{"type":"number"}},"required":["height","width"]},"test_json_schema__Rectangle__tagged":{"type":"object","properties":{"height":{"type":"number"},"shape":{"type":"string","enum":["Rectangle"]},"width":{"type":"number"}},"required":["height","shape","width"]},"test_json_schema__Square":{"type":"object","properties":{"width":{"type":"number"}},"required":["width"]},"test_json_schema__Square__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Square"]},"width":{"type":"number"}},"required":["shape","width"]},"test_json_schema__WithExtraFields":{"type":"object","properties":{"f1":{"type":"string"},"f2":{"type":"string"}},"required":["f1","f2"],"additionalProperties":{"type":"string"}}}})";
|
||||
|
||||
EXPECT_EQ(json_schema, expected);
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ using Shapes = rfl::Variant<Circle, Rectangle, std::unique_ptr<Square>>;
|
||||
|
||||
TEST(json, test_rfl_variant_get_if) {
|
||||
const Shapes r = Rectangle{.height = 10, .width = 5};
|
||||
EXPECT_TRUE(rfl::get_if<1>(r) && true);
|
||||
EXPECT_FALSE(rfl::get_if<0>(r) && true);
|
||||
EXPECT_TRUE(rfl::get_if<1>(&r) && true);
|
||||
EXPECT_FALSE(rfl::get_if<0>(&r) && true);
|
||||
}
|
||||
} // namespace test_rfl_variant_get_if
|
||||
|
||||
Reference in New Issue
Block a user