Added more features to the automated field name retrieval

This commit is contained in:
Patrick Urbanke
2023-11-27 19:51:30 +01:00
parent 5e6682df5d
commit ac244c082d
67 changed files with 6538 additions and 6136 deletions
+1
View File
@@ -12,6 +12,7 @@
#include "rfl/Pattern.hpp"
#include "rfl/PatternValidator.hpp"
#include "rfl/Ref.hpp"
#include "rfl/Rename.hpp"
#include "rfl/Size.hpp"
#include "rfl/TaggedUnion.hpp"
#include "rfl/Timestamp.hpp"
+140
View File
@@ -0,0 +1,140 @@
#ifndef RFL_RENAME_HPP_
#define RFL_RENAME_HPP_
#include <algorithm>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <utility>
#include "rfl/Literal.hpp"
#include "rfl/default.hpp"
#include "rfl/internal/StringLiteral.hpp"
namespace rfl {
/// Used to define a field in the NamedTuple.
template <internal::StringLiteral _name, class T>
struct Rename {
/// The underlying type.
using Type = T;
/// The name of the field.
using Name = rfl::Literal<_name>;
Rename() : value_(Type()) {}
Rename(const Type& _value) : value_(_value) {}
Rename(Type&& _value) noexcept : value_(std::move(_value)) {}
Rename(Rename<_name, T>&& _field) noexcept = default;
Rename(const Rename<_name, Type>& _field) = default;
template <class U>
Rename(const Rename<_name, U>& _field) : value_(_field.get()) {}
template <class U>
Rename(Rename<_name, U>&& _field) : value_(_field.get()) {}
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>,
bool>::type = true>
Rename(const U& _value) : value_(_value) {}
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>,
bool>::type = true>
Rename(U&& _value) noexcept : value_(std::forward<U>(_value)) {}
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>,
bool>::type = true>
Rename(const Rename<_name, U>& _field) : value_(_field.value()) {}
/// Assigns the underlying object to its default value.
template <class U = Type,
typename std::enable_if<std::is_default_constructible_v<U>,
bool>::type = true>
Rename(const Default& _default) : value_(Type()) {}
~Rename() = default;
/// The name of the field, for internal use.
constexpr static const internal::StringLiteral name_ = _name;
/// Returns the underlying object.
const Type& get() const { return value_; }
/// Returns the underlying object.
Type& operator()() { return value_; }
/// Returns the underlying object.
const Type& operator()() const { return value_; }
/// Assigns the underlying object.
auto& operator=(const Type& _value) {
value_ = _value;
return *this;
}
/// Assigns the underlying object.
auto& operator=(Type&& _value) noexcept {
value_ = std::move(_value);
return *this;
}
/// Assigns the underlying object.
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>,
bool>::type = true>
auto& operator=(const U& _value) {
value_ = _value;
return *this;
}
/// Assigns the underlying object to its default value.
template <class U = Type,
typename std::enable_if<std::is_default_constructible_v<U>,
bool>::type = true>
auto& operator=(const Default& _default) {
value_ = Type();
return *this;
}
/// Assigns the underlying object.
Rename<_name, T>& operator=(const Rename<_name, T>& _field) = default;
/// Assigns the underlying object.
Rename<_name, T>& operator=(Rename<_name, T>&& _field) = default;
/// Assigns the underlying object.
template <class U>
auto& operator=(const Rename<_name, U>& _field) {
value_ = _field.get();
return *this;
}
/// Assigns the underlying object.
template <class U>
auto& operator=(Rename<_name, U>&& _field) {
value_ = std::forward<T>(_field.value_);
return *this;
}
/// Assigns the underlying object.
void set(const Type& _value) { value_ = _value; }
/// Assigns the underlying object.
void set(Type&& _value) { value_ = std::move(_value); }
/// Returns the underlying object.
Type& value() { return value_; }
/// Returns the underlying object.
const Type& value() const { return value_; }
/// The underlying value.
Type value_;
};
} // namespace rfl
#endif
+20
View File
@@ -0,0 +1,20 @@
#ifndef RFL_INTERNAL_FIELD_TUPLE_T_HPP_
#define RFL_INTERNAL_FIELD_TUPLE_T_HPP_
#include <functional>
#include <tuple>
#include <type_traits>
#include "rfl/internal/copy_to_field_tuple.hpp"
namespace rfl {
namespace internal {
template <class T>
using field_tuple_t =
typename std::invoke_result<decltype(copy_to_field_tuple<T>), T>::type;
}
} // namespace rfl
#endif
+23
View File
@@ -0,0 +1,23 @@
#ifndef RFL_INTERNAL_FIELDS_HPP_
#define RFL_INTERNAL_FIELDS_HPP_
#include <array>
#include <cstdint>
#include <string>
#include <string_view>
#include <unordered_map>
namespace rfl {
namespace internal {
template <int N>
struct Fields {
std::array<std::string, N> names_;
std::unordered_map<std::string_view, std::int16_t> indices_;
};
} // namespace internal
} // namespace rfl
#endif
File diff suppressed because it is too large Load Diff
+35
View File
@@ -0,0 +1,35 @@
#ifndef RFL_INTERNAL_ISRENAME_HPP_
#define RFL_INTERNAL_ISRENAME_HPP_
#include <tuple>
#include <type_traits>
#include <utility>
#include "rfl/Rename.hpp"
#include "rfl/internal/StringLiteral.hpp"
namespace rfl {
namespace internal {
template <class T>
class is_rename;
template <class T>
class is_rename : public std::false_type {};
template <StringLiteral _name, class Type>
class is_rename<Rename<_name, Type>> : public std::true_type {};
template <StringLiteral _name, class Type>
class is_rename<Rename<_name, Type>*> : public std::true_type {};
template <StringLiteral _name, class Type>
class is_rename<const Rename<_name, Type>*> : public std::true_type {};
template <class T>
constexpr bool is_rename_v = is_rename<T>::value;
} // namespace internal
} // namespace rfl
#endif
File diff suppressed because it is too large Load Diff
+30 -28
View File
@@ -16,47 +16,49 @@ namespace internal {
template <class PtrFieldTuple, class... Args>
auto flatten_ptr_field_tuple(const PtrFieldTuple& _t, Args&&... _args) {
constexpr auto i = sizeof...(Args);
if constexpr (i == std::tuple_size_v<std::decay_t<PtrFieldTuple>>) {
return std::tuple_cat(std::forward<Args>(_args)...);
constexpr auto i = sizeof...(Args);
if constexpr (i == std::tuple_size_v<std::decay_t<PtrFieldTuple>>) {
return std::tuple_cat(std::forward<Args>(_args)...);
} else {
using T = std::tuple_element_t<i, std::decay_t<PtrFieldTuple>>;
if constexpr (internal::is_flatten_field<T>::value) {
const auto subtuple =
internal::to_ptr_field_tuple(*std::get<i>(_t).get());
return flatten_ptr_field_tuple(_t, std::forward<Args>(_args)...,
flatten_ptr_field_tuple(subtuple));
} else {
using T = std::tuple_element_t<i, std::decay_t<PtrFieldTuple>>;
if constexpr (internal::is_flatten_field<T>::value) {
const auto subtuple =
internal::to_ptr_field_tuple(*std::get<i>(_t).get());
return flatten_ptr_field_tuple(_t, std::forward<Args>(_args)...,
flatten_ptr_field_tuple(subtuple));
} else {
return flatten_ptr_field_tuple(_t, std::forward<Args>(_args)...,
std::make_tuple(std::get<i>(_t)));
}
return flatten_ptr_field_tuple(_t, std::forward<Args>(_args)...,
std::make_tuple(std::get<i>(_t)));
}
}
}
template <class PtrFieldTuple>
auto field_tuple_to_named_tuple(const PtrFieldTuple& _ptr_field_tuple) {
const auto ft_to_nt = []<class... Fields>(const Fields&... _fields) {
return make_named_tuple(_fields...);
};
const auto ft_to_nt = []<class... Fields>(const Fields&... _fields) {
return make_named_tuple(_fields...);
};
if constexpr (!has_flatten_fields<std::decay_t<PtrFieldTuple>>()) {
return std::apply(ft_to_nt, std::move(_ptr_field_tuple));
} else {
const auto flattened_tuple = flatten_ptr_field_tuple(_ptr_field_tuple);
return std::apply(ft_to_nt, flattened_tuple);
}
if constexpr (!has_flatten_fields<std::decay_t<PtrFieldTuple>>()) {
return std::apply(ft_to_nt, std::move(_ptr_field_tuple));
} else {
const auto flattened_tuple = flatten_ptr_field_tuple(_ptr_field_tuple);
return std::apply(ft_to_nt, flattened_tuple);
}
}
/// Generates a named tuple that contains pointers to the original values in
/// the struct.
template <class T>
auto to_ptr_named_tuple(const T& _t) {
if constexpr (is_named_tuple_v<std::decay_t<T>>) {
return nt_to_ptr_named_tuple(_t);
} else {
const auto ptr_field_tuple = to_ptr_field_tuple(_t);
return field_tuple_to_named_tuple(ptr_field_tuple);
}
if constexpr (std::is_pointer_v<std::decay_t<T>>) {
return to_ptr_named_tuple(*_t);
} else if constexpr (is_named_tuple_v<std::decay_t<T>>) {
return nt_to_ptr_named_tuple(_t);
} else {
const auto ptr_field_tuple = to_ptr_field_tuple(_t);
return field_tuple_to_named_tuple(ptr_field_tuple);
}
}
} // namespace internal
+3 -1
View File
@@ -38,7 +38,9 @@ with open("generated_code4.cpp", "w", encoding="utf-8") as codefile:
template <class T>
auto to_ptr_tuple(const T& _t) {
if constexpr (has_n_fields<T, 0>) {
if constexpr (std::is_pointer_v<std::decay_t<T>>) {
return to_ptr_tuple(*_t);
} else if constexpr (has_n_fields<T, 0>) {
return std::make_tuple();
} else if constexpr (has_n_fields<T, 1>) {
auto& [f1] = _t;
+55 -56
View File
@@ -19,77 +19,76 @@ namespace rfl {
namespace json {
struct Writer {
struct YYJSONOutputArray {
YYJSONOutputArray(yyjson_mut_val* _val) : val_(_val) {}
yyjson_mut_val* val_;
};
struct YYJSONOutputArray {
YYJSONOutputArray(yyjson_mut_val* _val) : val_(_val) {}
yyjson_mut_val* val_;
};
struct YYJSONOutputObject {
YYJSONOutputObject(yyjson_mut_val* _val) : val_(_val) {}
yyjson_mut_val* val_;
};
struct YYJSONOutputObject {
YYJSONOutputObject(yyjson_mut_val* _val) : val_(_val) {}
yyjson_mut_val* val_;
};
struct YYJSONOutputVar {
YYJSONOutputVar(yyjson_mut_val* _val) : val_(_val) {}
struct YYJSONOutputVar {
YYJSONOutputVar(yyjson_mut_val* _val) : val_(_val) {}
YYJSONOutputVar(YYJSONOutputArray _arr) : val_(_arr.val_) {}
YYJSONOutputVar(YYJSONOutputArray _arr) : val_(_arr.val_) {}
YYJSONOutputVar(YYJSONOutputObject _obj) : val_(_obj.val_) {}
YYJSONOutputVar(YYJSONOutputObject _obj) : val_(_obj.val_) {}
yyjson_mut_val* val_;
};
yyjson_mut_val* val_;
};
using OutputArrayType = YYJSONOutputArray;
using OutputObjectType = YYJSONOutputObject;
using OutputVarType = YYJSONOutputVar;
using OutputArrayType = YYJSONOutputArray;
using OutputObjectType = YYJSONOutputObject;
using OutputVarType = YYJSONOutputVar;
Writer(yyjson_mut_doc* _doc) : doc_(_doc) {}
Writer(yyjson_mut_doc* _doc) : doc_(_doc) {}
~Writer() = default;
~Writer() = default;
void add(const OutputVarType _var, OutputArrayType* _arr) const noexcept {
yyjson_mut_arr_add_val(_arr->val_, _var.val_);
void add(const OutputVarType _var, OutputArrayType* _arr) const noexcept {
yyjson_mut_arr_add_val(_arr->val_, _var.val_);
}
OutputVarType empty_var() const noexcept {
return OutputVarType(yyjson_mut_null(doc_));
}
template <class T>
OutputVarType from_basic_type(const T& _var) const noexcept {
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
return OutputVarType(yyjson_mut_strcpy(doc_, _var.c_str()));
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
return OutputVarType(yyjson_mut_bool(doc_, _var));
} else if constexpr (std::is_floating_point<std::decay_t<T>>()) {
return OutputVarType(yyjson_mut_real(doc_, static_cast<double>(_var)));
} else if constexpr (std::is_integral<std::decay_t<T>>()) {
return OutputVarType(yyjson_mut_int(doc_, static_cast<int>(_var)));
} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
}
OutputVarType empty_var() const noexcept {
return OutputVarType(yyjson_mut_null(doc_));
}
OutputArrayType new_array() const noexcept {
return OutputArrayType(yyjson_mut_arr(doc_));
}
template <class T>
OutputVarType from_basic_type(const T& _var) const noexcept {
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
return OutputVarType(yyjson_mut_strcpy(doc_, _var.c_str()));
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
return OutputVarType(yyjson_mut_bool(doc_, _var));
} else if constexpr (std::is_floating_point<std::decay_t<T>>()) {
return OutputVarType(
yyjson_mut_real(doc_, static_cast<double>(_var)));
} else if constexpr (std::is_integral<std::decay_t<T>>()) {
return OutputVarType(yyjson_mut_int(doc_, static_cast<int>(_var)));
} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
}
OutputObjectType new_object() const noexcept {
return OutputObjectType(yyjson_mut_obj(doc_));
}
OutputArrayType new_array() const noexcept {
return OutputArrayType(yyjson_mut_arr(doc_));
}
bool is_empty(const OutputVarType& _var) const noexcept {
return yyjson_mut_is_null(_var.val_);
}
OutputObjectType new_object() const noexcept {
return OutputObjectType(yyjson_mut_obj(doc_));
}
void set_field(const std::string& _name, const OutputVarType& _var,
OutputObjectType* _obj) const noexcept {
yyjson_mut_obj_add(_obj->val_, yyjson_mut_strcpy(doc_, _name.c_str()),
_var.val_);
}
bool is_empty(const OutputVarType& _var) const noexcept {
return yyjson_mut_is_null(_var.val_);
}
void set_field(const std::string& _name, const OutputVarType& _var,
OutputObjectType* _obj) const noexcept {
yyjson_mut_obj_add(_obj->val_, yyjson_mut_strcpy(doc_, _name.c_str()),
_var.val_);
}
yyjson_mut_doc* doc_;
yyjson_mut_doc* doc_;
};
} // namespace json
+78 -43
View File
@@ -1,6 +1,8 @@
#ifndef RFL_PARSING_PARSER_HPP_
#define RFL_PARSING_PARSER_HPP_
#include <math.h>
#include <array>
#include <cstddef>
#include <cstdint>
@@ -29,10 +31,13 @@
#include "rfl/always_false.hpp"
#include "rfl/field_type.hpp"
#include "rfl/from_named_tuple.hpp"
#include "rfl/internal/Fields.hpp"
#include "rfl/internal/Memoization.hpp"
#include "rfl/internal/StringLiteral.hpp"
#include "rfl/internal/all_fields.hpp"
#include "rfl/internal/flattened_ptr_tuple_t.hpp"
#include "rfl/internal/flattened_tuple_t.hpp"
#include "rfl/internal/get_field_names.hpp"
#include "rfl/internal/has_fields.hpp"
#include "rfl/internal/has_reflection_method_v.hpp"
#include "rfl/internal/has_reflection_type_v.hpp"
@@ -44,6 +49,7 @@
#include "rfl/internal/tuple_t.hpp"
#include "rfl/named_tuple_t.hpp"
#include "rfl/parsing/AreReaderAndWriter.hpp"
#include "rfl/parsing/TaggedUnionWrapper.hpp"
#include "rfl/parsing/is_forward_list.hpp"
#include "rfl/parsing/is_map_like.hpp"
#include "rfl/parsing/is_map_like_not_multimap.hpp"
@@ -71,7 +77,7 @@ struct StructParser {
using OutputObjectType = typename W::OutputObjectType;
using OutputVarType = typename W::OutputVarType;
using PtrTupleType = internal::ptr_tuple_t<T>;
using PtrTupleType = internal::flattened_ptr_tuple_t<T>;
using TupleType = internal::flattened_tuple_t<T>;
static constexpr auto tuple_size = std::tuple_size_v<TupleType>;
@@ -90,7 +96,7 @@ struct StructParser {
return _r.template to_fields_array<tuple_size>(fct, _obj);
};
const auto build_tuple = [&](auto _fields_vec) -> TupleType {
const auto build_tuple = [&](auto _fields_vec) -> Result<TupleType> {
return build_tuple_recursively(_r, _fields_vec);
};
@@ -218,7 +224,7 @@ struct StructParser {
using ValueType =
std::decay_t<typename std::tuple_element<_i, PtrTupleType>::type>;
auto value = Parser<R, W, ValueType>::write(_w, rfl::get<_i>(_tup));
auto value = Parser<R, W, ValueType>::write(_w, std::get<_i>(_tup));
const auto& key = std::get<_i>(field_names());
@@ -238,16 +244,14 @@ struct StructParser {
/// There are some objects that we are likely to parse many times,
/// so we only calculate these indices once.
static const auto& field_indices() noexcept {
// return field_indices_.value(set_field_indices<0>);
return field_indices_;
return fields_.value(make_fields).indices_;
}
/// Uses a memoization pattern to retrieve the field names.
/// There are some objects that we are likely to parse many times,
/// so we only calculate these indices once.
static const auto& field_names() noexcept {
// return field_indices_.value(set_field_indices<0>);
return field_names_;
return fields_.value(make_fields).names_;
}
/// Retrieves the value from the object. This is mainly needed to
@@ -262,31 +266,17 @@ struct StructParser {
}
/// Builds the object field by field.
/*template <size_t _i = 0>
static void set_field_indices(
std::unordered_map<std::string_view, std::int16_t>*
_field_indices) noexcept {
if constexpr (_i >= sizeof...(FieldTypes)) {
return;
} else {
using FieldType =
typename std::tuple_element<_i, std::tuple<FieldTypes...>>::type;
const auto name = FieldType::name_.string_view();
(*_field_indices)[name] = static_cast<std::int16_t>(_i);
set_field_indices<_i + 1>(_field_indices);
static void make_fields(internal::Fields<tuple_size>* _fields) noexcept {
_fields->names_ = internal::get_field_names<T>();
for (size_t i = 0; i < tuple_size; ++i) {
_fields->indices_[_fields->names_[i]] = static_cast<std::int16_t>(i);
}
}*/
}
private:
/// Maps each of the field names to an index signifying their order.
/*static inline internal::Memoization<
std::unordered_map<std::string_view, std::int16_t>>
field_indices_;*/
static inline std::array<std::string, tuple_size> field_names_;
static inline std::unordered_map<std::string_view, std::int16_t>
field_indices_;
/// Maps each of the field names to an index signifying their order and
/// vice-versa.
static inline internal::Memoization<internal::Fields<tuple_size>> fields_;
};
// ----------------------------------------------------------------------------
@@ -326,11 +316,7 @@ struct Parser {
return Parser<R, W, NamedTupleType>::read(_r, _var).and_then(
to_struct);
} else {
using TupleType = internal::flattened_tuple_t<T>;
const auto to_struct = [](TupleType&& _t) -> T {
return internal::move_from_tuple<T, TupleType>(std::move(_t));
};
return Parser<R, W, TupleType>::read(_r, _var).transform(to_struct);
return StructParser<R, W, T>::read(_r, _var);
}
} else if constexpr (internal::is_basic_type_v<T>) {
return _r.template to_basic_type<std::decay_t<T>>(_var);
@@ -360,9 +346,7 @@ struct Parser {
using PtrNamedTupleType = std::decay_t<decltype(ptr_named_tuple)>;
return Parser<R, W, PtrNamedTupleType>::write(_w, ptr_named_tuple);
} else {
const auto ptr_tuple = internal::to_flattened_ptr_tuple(_var);
using PtrTupleType = std::decay_t<decltype(ptr_tuple)>;
return Parser<R, W, PtrTupleType>::write(_w, ptr_tuple);
return StructParser<R, W, T>::write(_w, _var);
}
} else if constexpr (internal::is_basic_type_v<T>) {
return _w.from_basic_type(_var);
@@ -862,6 +846,28 @@ struct Parser<R, W, Ref<T>> {
// ----------------------------------------------------------------------------
template <class R, class W, class T, internal::StringLiteral _name>
requires AreReaderAndWriter<R, W, Rename<_name, T>>
struct Parser<R, W, Rename<_name, T>> {
using InputVarType = typename R::InputVarType;
using OutputVarType = typename W::OutputVarType;
static Result<Rename<_name, T>> read(const R& _r,
const InputVarType& _var) noexcept {
const auto to_rename = [](auto&& _t) {
return Rename<_name, T>(std::move(_t));
};
return Parser<R, W, std::decay_t<T>>::read(_r, _var).transform(to_rename);
}
static OutputVarType write(const W& _w,
const Rename<_name, T>& _rename) noexcept {
return Parser<R, W, std::decay_t<T>>::write(_w, _rename.value());
}
};
// ----------------------------------------------------------------------------
template <class R, class W, class T>
requires AreReaderAndWriter<R, W, Result<T>>
struct Parser<R, W, Result<T>> {
@@ -997,9 +1003,10 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>> {
static OutputVarType write(
const W& _w, const TaggedUnion<_discriminator, AlternativeTypes...>&
_tagged_union) noexcept {
using VariantType =
typename TaggedUnion<_discriminator, AlternativeTypes...>::VariantType;
return Parser<R, W, VariantType>::write(_w, _tagged_union.variant_);
const auto handle = [&](const auto& _val) -> OutputVarType {
return write_wrapped(_w, _val);
};
return std::visit(handle, _tagged_union.variant_);
}
private:
@@ -1061,14 +1068,42 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>> {
static inline bool contains_disc_value(
const std::string& _disc_value) noexcept {
if constexpr (!internal::has_reflection_type_v<T>) {
using LiteralType = field_type_t<_discriminator, T>;
using LiteralType = typename T::Tag;
return LiteralType::contains(_disc_value);
} else {
using LiteralType =
field_type_t<_discriminator, typename T::ReflectionType>;
using LiteralType = typename T::ReflectionType::Tag;
return LiteralType::contains(_disc_value);
}
}
template <class T>
static inline auto make_tag() noexcept {
if constexpr (!internal::has_reflection_type_v<T>) {
using LiteralType = typename T::Tag;
return LiteralType::template name_of<0>();
} else {
using LiteralType = typename T::ReflectionType::Tag;
return LiteralType::template name_of<0>();
}
}
/// Writes a wrapped version of the original object, which contains the tag.
template <class T>
static OutputVarType write_wrapped(const W& _w, const T& _val) noexcept {
const auto tag = make_tag<T>();
using TagType = std::decay_t<decltype(tag)>;
if constexpr (internal::has_fields<std::decay_t<T>>()) {
using WrapperType =
TaggedUnionWrapperWithFields<T, TagType, _discriminator>;
const auto wrapper = WrapperType{.tag = tag, .fields = &_val};
return Parser<R, W, WrapperType>::write(_w, wrapper);
} else {
using WrapperType =
TaggedUnionWrapperNoFields<T, TagType, _discriminator>;
const auto wrapper = WrapperType{.tag = tag, .fields = &_val};
return Parser<R, W, WrapperType>::write(_w, wrapper);
}
}
};
// ----------------------------------------------------------------------------
@@ -0,0 +1,28 @@
#ifndef RFL_PARSING_TAGGEDUNIONWRAPPER_HPP_
#define RFL_PARSING_TAGGEDUNIONWRAPPER_HPP_
#include <concepts>
#include "rfl/Flatten.hpp"
#include "rfl/Rename.hpp"
#include "rfl/internal/StringLiteral.hpp"
namespace rfl {
namespace parsing {
template <class T, class LiteralType, internal::StringLiteral _discriminator>
struct TaggedUnionWrapperNoFields {
rfl::Rename<_discriminator, LiteralType> tag;
rfl::Flatten<const T*> fields;
};
template <class T, class LiteralType, internal::StringLiteral _discriminator>
struct TaggedUnionWrapperWithFields {
rfl::Field<_discriminator, LiteralType> tag;
rfl::Flatten<const T*> fields;
};
} // namespace parsing
} // namespace rfl
#endif
+12 -9
View File
@@ -6,20 +6,23 @@
#include "write_and_read.hpp"
void test_all_of() {
namespace test_all_of {
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;
Age age;
};
void test() {
std::cout << "test_all_of" << std::endl;
using Age = rfl::Validator<unsigned int, rfl::Minimum<0>, rfl::Maximum<130>>;
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name;
rfl::Field<"age", Age> age;
};
const auto homer =
Person{.first_name = "Homer", .last_name = "Simpson", .age = 45};
write_and_read(homer,
R"({"firstName":"Homer","lastName":"Simpson","age":45})");
}
} // namespace test_all_of
+5 -1
View File
@@ -7,7 +7,9 @@
#include "write_and_read.hpp"
void test_alphanumeric_map() {
namespace test_alphanumeric_map {
void test() {
std::cout << "test_alphanumeric_map" << std::endl;
std::map<rfl::AlphaNumeric, std::unique_ptr<std::string>> homer;
@@ -18,3 +20,5 @@ void test_alphanumeric_map() {
write_and_read(homer, R"({"firstName":"Homer","lastName":"Simpson"})");
}
} // namespace test_alphanumeric_map
@@ -7,7 +7,8 @@
#include "write_and_read.hpp"
void test_alphanumeric_unordered_map() {
namespace test_alphanumeric_unordered_map {
void test() {
std::cout << "test_alphanumeric_unordered_map" << std::endl;
using Map =
@@ -24,3 +25,4 @@ void test_alphanumeric_unordered_map() {
const auto homer2 = rfl::json::read<Map>(json_string);
std::cout << "OK" << std::endl << std::endl;
}
} // namespace test_alphanumeric_unordered_map
+17 -14
View File
@@ -6,21 +6,23 @@
#include "write_and_read.hpp"
void test_anonymous_fields() {
namespace test_anonymous_fields {
using Age = rfl::Validator<unsigned int,
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
struct Person {
std::string first_name;
std::string last_name;
rfl::Timestamp<"%Y-%m-%d"> birthday;
Age age;
rfl::Email email;
std::vector<Person> children;
};
void test() {
std::cout << "test_anonymous_fields" << std::endl;
using Age = rfl::Validator<unsigned int,
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
struct Person {
std::string first_name;
std::string last_name;
rfl::Timestamp<"%Y-%m-%d"> birthday;
Age age;
rfl::Email email;
std::vector<Person> children;
};
const auto bart = Person{.first_name = "Bart",
.last_name = "Simpson",
.birthday = "1987-04-19",
@@ -49,5 +51,6 @@ void test_anonymous_fields() {
write_and_read(
homer,
R"(["Homer","Simpson","1987-04-19",45,"homer@simpson.com",[["Bart","Simpson","1987-04-19",10,"bart@simpson.com",[]],["Lisa","Simpson","1987-04-19",8,"lisa@simpson.com",[]],["Maggie","Simpson","1987-04-19",0,"maggie@simpson.com",[]]]])");
R"({"first_name":"Homer","last_name":"Simpson","birthday":"1987-04-19","age":45,"email":"homer@simpson.com","children":[{"first_name":"Bart","last_name":"Simpson","birthday":"1987-04-19","age":10,"email":"bart@simpson.com","children":[]},{"first_name":"Lisa","last_name":"Simpson","birthday":"1987-04-19","age":8,"email":"lisa@simpson.com","children":[]},{"first_name":"Maggie","last_name":"Simpson","birthday":"1987-04-19","age":0,"email":"maggie@simpson.com","children":[]}]})");
}
} // namespace test_anonymous_fields
+16 -12
View File
@@ -6,23 +6,27 @@
#include "write_and_read.hpp"
void test_any_of() {
namespace test_any_of {
using Age =
rfl::Validator<unsigned int,
rfl::AnyOf<rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<10>>,
rfl::AllOf<rfl::Minimum<40>, rfl::Maximum<130>>>>;
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name;
Age age;
};
void test() {
std::cout << "test_any_of" << std::endl;
using Age = rfl::Validator<
unsigned int,
rfl::AnyOf<rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<10>>,
rfl::AllOf<rfl::Minimum<40>, rfl::Maximum<130>>>>;
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name;
rfl::Field<"age", Age> age;
};
const auto homer =
Person{.first_name = "Homer", .last_name = "Simpson", .age = 45};
write_and_read(homer,
R"({"firstName":"Homer","lastName":"Simpson","age":45})");
}
} // namespace test_any_of
+23 -22
View File
@@ -7,32 +7,33 @@
#include "write_and_read.hpp"
void test_array() {
std::cout << "test_array" << std::endl;
namespace test_array {
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name = "Simpson";
rfl::Field<"children", std::unique_ptr<std::array<Person, 3>>>
children = rfl::default_value;
};
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;
};
auto bart = Person{.first_name = "Bart"};
void test() {
std::cout << "test_array" << std::endl;
auto lisa = Person{.first_name = "Lisa"};
auto bart = Person{.first_name = "Bart"};
auto maggie = Person{.first_name = "Maggie"};
auto lisa = Person{.first_name = "Lisa"};
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)})};
auto maggie = Person{.first_name = "Maggie"};
write_and_read(
homer,
"{\"firstName\":\"Homer\",\"lastName\":\"Simpson\",\"children\":[{"
"\"firstName\":\"Bart\",\"lastName\":\"Simpson\"},{"
"\"firstName\":\"Lisa\",\"lastName\":\"Simpson\"},{"
"\"firstName\":\"Maggie\",\"lastName\":\"Simpson\"}]}");
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,
"{\"firstName\":\"Homer\",\"lastName\":\"Simpson\",\"children\":[{"
"\"firstName\":\"Bart\",\"lastName\":\"Simpson\"},{"
"\"firstName\":\"Lisa\",\"lastName\":\"Simpson\"},{"
"\"firstName\":\"Maggie\",\"lastName\":\"Simpson\"}]}");
}
} // namespace test_array
+23 -19
View File
@@ -7,30 +7,34 @@
#include "write_and_read.hpp"
void test_as() {
std::cout << "test_as" << std::endl;
namespace test_as {
struct A {
rfl::Field<"f1", std::string> f1;
rfl::Field<"f2", rfl::Box<std::string>> f2;
};
struct A {
rfl::Field<"f1", std::string> f1;
rfl::Field<"f2", rfl::Box<std::string>> f2;
};
struct B {
rfl::Field<"f3", std::string> f3;
rfl::Field<"f4", rfl::Box<std::string>> f4;
};
struct B {
rfl::Field<"f3", std::string> f3;
rfl::Field<"f4", rfl::Box<std::string>> f4;
};
struct C {
rfl::Field<"f1", std::string> f1;
rfl::Field<"f2", rfl::Box<std::string>> f2;
rfl::Field<"f4", rfl::Box<std::string>> f4;
};
struct C {
rfl::Field<"f1", std::string> f1;
rfl::Field<"f2", rfl::Box<std::string>> f2;
rfl::Field<"f4", rfl::Box<std::string>> f4;
};
auto a = A{.f1 = "Hello", .f2 = rfl::make_box<std::string>("World")};
void test() {
std::cout << "test_as" << std::endl;
auto b = B{.f3 = "Hello", .f4 = rfl::make_box<std::string>("World")};
auto a = A{.f1 = "Hello", .f2 = rfl::make_box<std::string>("World")};
const auto c = rfl::as<C>(std::move(a), std::move(b));
auto b = B{.f3 = "Hello", .f4 = rfl::make_box<std::string>("World")};
write_and_read(c, R"({"f1":"Hello","f2":"World","f4":"World"})");
const auto c = rfl::as<C>(std::move(a), std::move(b));
write_and_read(c, R"({"f1":"Hello","f2":"World","f4":"World"})");
}
} // namespace test_as
+29 -26
View File
@@ -7,39 +7,42 @@
#include "write_and_read.hpp"
void test_box() {
std::cout << "test_box" << std::endl;
namespace test_box {
struct DecisionTree {
struct Leaf {
rfl::Field<"type", rfl::Literal<"Leaf">> type = rfl::default_value;
rfl::Field<"value", double> value;
};
struct DecisionTree {
struct Leaf {
using Tag = rfl::Literal<"Leaf">;
rfl::Field<"value", double> value;
};
struct Node {
rfl::Field<"type", rfl::Literal<"Node">> type = rfl::default_value;
rfl::Field<"criticalValue", double> critical_value;
rfl::Field<"left", rfl::Box<DecisionTree>> lesser;
rfl::Field<"right", rfl::Box<DecisionTree>> greater;
};
struct Node {
using Tag = rfl::Literal<"Node">;
rfl::Field<"criticalValue", double> critical_value;
rfl::Field<"left", rfl::Box<DecisionTree>> lesser;
rfl::Field<"right", rfl::Box<DecisionTree>> greater;
};
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
};
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
};
auto leaf1 = DecisionTree::Leaf{.value = 3.0};
void test() {
std::cout << "test_box" << std::endl;
auto leaf2 = DecisionTree::Leaf{.value = 5.0};
auto leaf1 = DecisionTree::Leaf{.value = 3.0};
auto node = DecisionTree::Node{
.critical_value = 10.0,
.lesser = rfl::make_box<DecisionTree>(DecisionTree{leaf1}),
.greater = rfl::make_box<DecisionTree>(DecisionTree{leaf2})};
auto leaf2 = DecisionTree::Leaf{.value = 5.0};
const DecisionTree tree{.leaf_or_node = std::move(node)};
auto node = DecisionTree::Node{
.critical_value = 10.0,
.lesser = rfl::make_box<DecisionTree>(DecisionTree{leaf1}),
.greater = rfl::make_box<DecisionTree>(DecisionTree{leaf2})};
write_and_read(
tree,
R"({"leafOrNode":{"type":"Node","criticalValue":10.0,"left":{"leafOrNode":{"type":"Leaf","value":3.0}},"right":{"leafOrNode":{"type":"Leaf","value":5.0}}}})");
const DecisionTree tree{.leaf_or_node = std::move(node)};
write_and_read(
tree,
R"({"leafOrNode":{"type":"Node","criticalValue":10.0,"left":{"leafOrNode":{"type":"Leaf","value":3.0}},"right":{"leafOrNode":{"type":"Leaf","value":5.0}}}})");
}
} // namespace test_box
+22 -20
View File
@@ -7,32 +7,34 @@
#include "write_and_read.hpp"
void test_custom_class1() {
std::cout << "test_custom_class1" << std::endl;
namespace test_custom_class1 {
struct Person {
struct PersonImpl {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name = "Simpson";
rfl::Field<"children", std::vector<Person>> children =
rfl::default_value;
};
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;
using ReflectionType = PersonImpl;
Person(const PersonImpl& _impl) : impl(_impl) {}
Person(const PersonImpl& _impl) : impl(_impl) {}
Person(const std::string& _first_name)
: impl(PersonImpl{.first_name = _first_name}) {}
Person(const std::string& _first_name)
: impl(PersonImpl{.first_name = _first_name}) {}
const ReflectionType& reflection() const { return impl; };
const ReflectionType& reflection() const { return impl; };
private:
PersonImpl impl;
};
private:
PersonImpl impl;
};
const auto bart = Person("Bart");
void test() {
std::cout << "test_custom_class1" << std::endl;
write_and_read(
bart, R"({"firstName":"Bart","lastName":"Simpson","children":[]})");
const auto bart = Person("Bart");
write_and_read(bart,
R"({"firstName":"Bart","lastName":"Simpson","children":[]})");
}
} // namespace test_custom_class1
+29 -26
View File
@@ -8,35 +8,38 @@
#include "write_and_read.hpp"
void test_custom_class2() {
std::cout << "test_custom_class2" << std::endl;
namespace test_custom_class2 {
struct FiveDigitCode {
using ReflectionType = int;
struct FiveDigitCode {
using ReflectionType = int;
FiveDigitCode(const int _code) : code(_code) {
if (_code < 10000) {
throw std::runtime_error("Less than five digits.");
}
if (_code > 99999) {
throw std::runtime_error("More than five digits.");
}
}
ReflectionType reflection() const { return code; };
private:
int code;
};
const auto result = rfl::json::read<FiveDigitCode>("123");
if (result) {
std::cout << "Failed: Expected an error, but got none." << std::endl;
return;
FiveDigitCode(const int _code) : code(_code) {
if (_code < 10000) {
throw std::runtime_error("Less than five digits.");
}
if (_code > 99999) {
throw std::runtime_error("More than five digits.");
}
}
const auto c = FiveDigitCode(12345);
ReflectionType reflection() const { return code; };
write_and_read(c, "12345");
private:
int code;
};
void test() {
std::cout << "test_custom_class2" << std::endl;
const auto result = rfl::json::read<FiveDigitCode>("123");
if (result) {
std::cout << "Failed: Expected an error, but got none." << std::endl;
return;
}
const auto c = FiveDigitCode(12345);
write_and_read(c, "12345");
}
} // namespace test_custom_class2
+32 -30
View File
@@ -7,57 +7,59 @@
#include "write_and_read.hpp"
namespace tcc3 {
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) {}
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& first_name() const { return first_name_; }
const auto& last_name() const { return last_name_; }
const auto& last_name() const { return last_name_; }
auto age() const { return age_; }
auto age() const { return age_; }
private:
std::string first_name_;
std::string last_name_;
int age_;
private:
std::string first_name_;
std::string last_name_;
int age_;
};
struct PersonImpl {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name;
rfl::Field<"age", int> age;
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()};
}
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()); }
Person to_class() const { return Person(first_name(), last_name(), age); }
};
} // namespace tcc3
} // namespace test_custom_class3
namespace rfl {
namespace parsing {
template <class ReaderType, class WriterType>
struct Parser<ReaderType, WriterType, tcc3::Person>
: public CustomParser<ReaderType, WriterType, tcc3::Person,
tcc3::PersonImpl> {};
struct Parser<ReaderType, WriterType, test_custom_class3::Person>
: public CustomParser<ReaderType, WriterType, test_custom_class3::Person,
test_custom_class3::PersonImpl> {};
} // namespace parsing
} // namespace rfl
void test_custom_class3() {
std::cout << "test_custom_class3" << std::endl;
namespace test_custom_class3 {
const auto bart = tcc3::Person("Bart", "Simpson", 10);
void test() {
std::cout << "test_custom_class3" << std::endl;
write_and_read(bart,
R"({"firstName":"Bart","lastName":"Simpson","age":10})");
const auto bart = Person("Bart", "Simpson", 10);
write_and_read(bart, R"({"firstName":"Bart","lastName":"Simpson","age":10})");
}
} // namespace test_custom_class3
+33 -32
View File
@@ -7,59 +7,60 @@
#include "write_and_read.hpp"
namespace tcc4 {
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) {}
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& first_name() const { return first_name_; }
const auto& last_name() const { return last_name_; }
const auto& last_name() const { return last_name_; }
auto age() const { return age_; }
auto age() const { return age_; }
private:
std::string first_name_;
rfl::Box<std::string> last_name_;
int 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;
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()};
}
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 tcc4
} // namespace test_custom_class4
namespace rfl {
namespace parsing {
template <class ReaderType, class WriterType>
struct Parser<ReaderType, WriterType, tcc4::Person>
: public CustomParser<ReaderType, WriterType, tcc4::Person,
tcc4::PersonImpl> {};
struct Parser<ReaderType, WriterType, test_custom_class4::Person>
: public CustomParser<ReaderType, WriterType, test_custom_class4::Person,
test_custom_class4::PersonImpl> {};
} // namespace parsing
} // namespace rfl
void test_custom_class4() {
std::cout << "test_custom_class4" << std::endl;
namespace test_custom_class4 {
const auto bart =
tcc4::Person("Bart", rfl::make_box<std::string>("Simpson"), 10);
void test() {
std::cout << "test_custom_class4" << std::endl;
write_and_read(bart,
R"({"firstName":"Bart","lastName":"Simpson","age":10})");
const auto bart = test_custom_class4::Person(
"Bart", rfl::make_box<std::string>("Simpson"), 10);
write_and_read(bart, R"({"firstName":"Bart","lastName":"Simpson","age":10})");
}
} // namespace test_custom_class4
+38 -38
View File
@@ -6,47 +6,47 @@
#include "write_and_read.hpp"
// Makes sure that the example in the README works as expected.
// It would be embarrassing if it didn't...
void test_custom_constructor() {
std::cout << "test_custom_constructor" << std::endl;
namespace test_custom_constructor {
struct Person {
static rfl::Result<Person> from_json_obj(
typename rfl::json::Reader::InputVarType _obj) {
/// This only exists for the purpose of the test.
const auto change_first_name = [](auto&& _person) {
return rfl::replace(
std::move(_person),
rfl::Field<"firstName", std::string>("Bart"));
};
const auto from_nt = [](auto&& _nt) {
return rfl::from_named_tuple<Person>(std::move(_nt));
};
return rfl::json::read<rfl::named_tuple_t<Person>>(_obj)
.transform(from_nt)
.transform(change_first_name);
}
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name;
rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday;
struct Person {
static rfl::Result<Person> from_json_obj(
typename rfl::json::Reader::InputVarType _obj) {
/// This only exists for the purpose of the test.
const auto change_first_name = [](auto&& _person) {
return rfl::replace(std::move(_person),
rfl::Field<"firstName", std::string>("Bart"));
};
const auto from_nt = [](auto&& _nt) {
return rfl::from_named_tuple<Person>(std::move(_nt));
};
return rfl::json::read<rfl::named_tuple_t<Person>>(_obj)
.transform(from_nt)
.transform(change_first_name);
}
const auto res = rfl::json::read<Person>(
R"({"firstName":"Homer","lastName":"Simpson","birthday":"1987-04-19"})");
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name;
rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday;
};
if (!res) {
std::cout << "Test failed on read. Error: "
<< res.error().value().what() << std::endl;
return;
}
void test() {
std::cout << "test_custom_constructor" << std::endl;
if (res.value().first_name() != "Bart") {
std::cout << "Expected 'Bart', got '" << res.value().first_name() << "'"
<< std::endl
<< std::endl;
} else {
std::cout << "OK" << std::endl << std::endl;
}
const auto res = rfl::json::read<Person>(
R"({"firstName":"Homer","lastName":"Simpson","birthday":"1987-04-19"})");
if (!res) {
std::cout << "Test failed on read. Error: " << res.error().value().what()
<< std::endl;
return;
}
if (res.value().first_name() != "Bart") {
std::cout << "Expected 'Bart', got '" << res.value().first_name() << "'"
<< std::endl
<< std::endl;
} else {
std::cout << "OK" << std::endl << std::endl;
}
}
} // namespace test_custom_constructor
+19 -18
View File
@@ -7,25 +7,26 @@
#include "write_and_read.hpp"
// Makes sure that we can set default values.
void test_default_values() {
std::cout << "test_default_values" << std::endl;
namespace test_default_values {
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name = "Simpson";
rfl::Field<"children", std::vector<Person>> children =
rfl::default_value;
};
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::vector<Person> children;
};
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})};
void test() {
std::cout << "test_default_values" << std::endl;
write_and_read(
homer,
R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson","children":[]},{"firstName":"Lisa","lastName":"Simpson","children":[]},{"firstName":"Maggie","lastName":"Simpson","children":[]}]})");
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,
R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson","children":[]},{"firstName":"Lisa","lastName":"Simpson","children":[]},{"firstName":"Maggie","lastName":"Simpson","children":[]}]})");
}
} // namespace test_default_values
+19 -17
View File
@@ -6,25 +6,27 @@
#include "write_and_read.hpp"
void test_deque() {
std::cout << "test_deque" << std::endl;
namespace test_deque {
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name = "Simpson";
rfl::Field<"children", std::unique_ptr<std::deque<Person>>> children =
rfl::default_value;
};
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::unique_ptr<std::deque<Person>> children;
};
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"});
void test() {
std::cout << "test_deque" << std::endl;
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
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"});
write_and_read(
homer,
R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})");
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
write_and_read(
homer,
R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})");
}
} // namespace test_deque
+10 -7
View File
@@ -6,14 +6,16 @@
#include "write_and_read.hpp"
void test_email() {
std::cout << "test_email" << std::endl;
namespace test_email {
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name;
rfl::Field<"email", rfl::Email> email;
};
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name;
rfl::Email email;
};
void test() {
std::cout << "test_email" << std::endl;
const auto homer = Person{.first_name = "Homer",
.last_name = "Simpson",
@@ -23,3 +25,4 @@ void test_email() {
homer,
R"({"firstName":"Homer","lastName":"Simpson","email":"homer@simpson.com"})");
}
} // namespace test_email
+26 -23
View File
@@ -6,37 +6,40 @@
#include "write_and_read.hpp"
void test_error_messages() {
std::cout << "test_error_messages" << std::endl;
namespace test_error_messages {
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name;
rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday;
rfl::Field<"children", std::vector<Person>> children;
};
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name;
rfl::Timestamp<"%Y-%m-%d"> birthday;
std::vector<Person> children;
};
const std::string faulty_string =
R"({"firstName":"Homer","lastName":12345,"birthday":"04/19/1987"})";
void test() {
std::cout << "test_error_messages" << std::endl;
const auto result = rfl::json::read<Person>(faulty_string);
const std::string faulty_string =
R"({"firstName":"Homer","lastName":12345,"birthday":"04/19/1987"})";
const std::string expected = R"(Found 3 errors:
const auto result = rfl::json::read<Person>(faulty_string);
const std::string expected = R"(Found 3 errors:
1) Failed to parse field 'lastName': Could not cast to string.
2) Failed to parse field 'birthday': String '04/19/1987' did not match format '%Y-%m-%d'.
3) Field named 'children' not found.)";
if (!result.error()) {
std::cout << "Expected an error, got none." << std::endl << std::endl;
}
if (!result.error()) {
std::cout << "Expected an error, got none." << std::endl << std::endl;
}
if (result.error().value().what() != expected) {
std::cout << "Expected: " << std::endl
<< expected << std::endl
<< "Got:" << std::endl
<< result.error().value().what() << std::endl
<< std::endl;
}
if (result.error().value().what() != expected) {
std::cout << "Expected: " << std::endl
<< expected << std::endl
<< "Got:" << std::endl
<< result.error().value().what() << std::endl
<< std::endl;
}
std::cout << "OK" << std::endl << std::endl;
std::cout << "OK" << std::endl << std::endl;
}
} // namespace test_error_messages
+21 -18
View File
@@ -7,28 +7,31 @@
#include "write_and_read.hpp"
void test_field_variant() {
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>>>;
void test() {
std::cout << "test_field_variant" << std::endl;
struct Circle {
rfl::Field<"radius", double> radius;
};
struct Rectangle {
rfl::Field<"height", double> height;
rfl::Field<"width", double> width;
};
struct Square {
rfl::Field<"width", double> width;
};
using Shapes = rfl::Variant<rfl::Field<"circle", Circle>,
rfl::Field<"rectangle", Rectangle>,
rfl::Field<"square", rfl::Box<Square>>>;
const Shapes r =
rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5});
write_and_read(r, R"({"rectangle":{"height":10.0,"width":5.0}})");
}
} // namespace test_field_variant
+24 -21
View File
@@ -7,29 +7,32 @@
#include "write_and_read.hpp"
void test_flatten() {
std::cout << "test_flatten" << std::endl;
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 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;
};
struct Employee {
rfl::Flatten<Person> person;
rfl::Field<"employer", rfl::Box<std::string>> employer;
rfl::Field<"salary", float> salary;
};
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};
void test() {
std::cout << "test_flatten" << std::endl;
write_and_read(
employee,
R"({"firstName":"Homer","lastName":"Simpson","age":45,"employer":"Mr. Burns","salary":60000.0})");
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,
R"({"firstName":"Homer","lastName":"Simpson","age":45,"employer":"Mr. Burns","salary":60000.0})");
}
} // namespace test_flatten
+20 -14
View File
@@ -7,21 +7,23 @@
#include "write_and_read.hpp"
void test_flatten_anonymous() {
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;
};
void test() {
std::cout << "test_flatten_anonymous" << std::endl;
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;
};
const auto employee = Employee{
.person = Person{.first_name = "Homer",
.last_name = rfl::make_box<std::string>("Simpson"),
@@ -29,5 +31,9 @@ void test_flatten_anonymous() {
.employer = rfl::make_box<std::string>("Mr. Burns"),
.salary = 60000.0};
write_and_read(employee, R"(["Homer","Simpson",45,"Mr. Burns",60000.0])");
write_and_read(
employee,
R"({"first_name":"Homer","last_name":"Simpson","age":45,"employer":"Mr. Burns","salary":60000.0})");
}
} // namespace test_flatten_anonymous
+19 -17
View File
@@ -6,25 +6,27 @@
#include "write_and_read.hpp"
void test_forward_list() {
std::cout << "test_forward_list" << std::endl;
namespace test_forward_list {
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name = "Simpson";
rfl::Field<"children", std::unique_ptr<std::forward_list<Person>>>
children = rfl::default_value;
};
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;
};
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"});
void test() {
std::cout << "test_forward_list" << std::endl;
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
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"});
write_and_read(
homer,
R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})");
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
write_and_read(
homer,
R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})");
}
} // namespace test_forward_list
+19 -17
View File
@@ -6,25 +6,27 @@
#include "write_and_read.hpp"
void test_list() {
std::cout << "test_list" << std::endl;
namespace test_list {
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name = "Simpson";
rfl::Field<"children", std::unique_ptr<std::list<Person>>> children =
rfl::default_value;
};
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::unique_ptr<std::list<Person>> children;
};
auto children = std::make_unique<std::list<Person>>();
children->emplace_back(Person{.first_name = "Bart"});
children->emplace_back(Person{.first_name = "Lisa"});
children->emplace_back(Person{.first_name = "Maggie"});
void test() {
std::cout << "test_list" << std::endl;
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
auto children = std::make_unique<std::list<Person>>();
children->emplace_back(Person{.first_name = "Bart"});
children->emplace_back(Person{.first_name = "Lisa"});
children->emplace_back(Person{.first_name = "Maggie"});
write_and_read(
homer,
R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})");
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
write_and_read(
homer,
R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})");
}
} // namespace test_list
+15 -14
View File
@@ -7,22 +7,23 @@
#include "write_and_read.hpp"
// Makes sure that we can set default values.
void test_literal() {
std::cout << "test_literal" << std::endl;
namespace test_literal {
using FirstName = rfl::Literal<"Homer", "Marge", "Bart", "Lisa", "Maggie">;
using LastName = rfl::Literal<"Simpson">;
using FirstName = rfl::Literal<"Homer", "Marge", "Bart", "Lisa", "Maggie">;
using LastName = rfl::Literal<"Simpson">;
struct Person {
rfl::Field<"firstName", FirstName> first_name;
rfl::Field<"lastName", LastName> last_name = rfl::default_value;
rfl::Field<"children", std::vector<Person>> children =
rfl::default_value;
};
struct Person {
rfl::Rename<"firstName", FirstName> first_name;
rfl::Rename<"lastName", LastName> last_name;
std::vector<Person> children;
};
const auto bart = Person{.first_name = FirstName::make<"Bart">()};
void test() {
std::cout << "test_literal" << std::endl;
write_and_read(
bart, R"({"firstName":"Bart","lastName":"Simpson","children":[]})");
const auto bart = Person{.first_name = FirstName::make<"Bart">()};
write_and_read(bart,
R"({"firstName":"Bart","lastName":"Simpson","children":[]})");
}
} // namespace test_literal
+6 -3
View File
@@ -8,10 +8,12 @@
#include "write_and_read.hpp"
void test_literal_map() {
std::cout << "test_literal_map" << std::endl;
namespace test_literal_map {
using FieldName = rfl::Literal<"firstName", "lastName">;
using FieldName = rfl::Literal<"firstName", "lastName">;
void test() {
std::cout << "test_literal_map" << std::endl;
std::map<FieldName, std::unique_ptr<std::string>> homer;
homer.insert(std::make_pair(FieldName::make<"firstName">(),
@@ -21,3 +23,4 @@ void test_literal_map() {
write_and_read(homer, R"({"firstName":"Homer","lastName":"Simpson"})");
}
} // namespace test_literal_map
+8 -5
View File
@@ -9,13 +9,15 @@
#include "write_and_read.hpp"
void test_literal_unordered_map() {
namespace test_literal_unordered_map {
using FieldName = rfl::Literal<"firstName", "lastName">;
using Map = std::unordered_map<FieldName, std::unique_ptr<std::string>>;
void test() {
std::cout << "test_literal_unordered_map" << std::endl;
using FieldName = rfl::Literal<"firstName", "lastName">;
using Map = std::unordered_map<FieldName, std::unique_ptr<std::string>>;
Map homer;
homer.insert(std::make_pair(FieldName::make<"firstName">(),
@@ -29,3 +31,4 @@ void test_literal_unordered_map() {
const auto homer2 = rfl::json::read<Map>(json_string);
std::cout << "OK" << std::endl << std::endl;
}
} // namespace test_literal_unordered_map
+10 -8
View File
@@ -6,15 +6,16 @@
#include "write_and_read.hpp"
void test_map() {
std::cout << "test_map" << std::endl;
namespace test_map {
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::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::unique_ptr<std::map<int, Person>> children;
};
void test() {
std::cout << "test_map" << std::endl;
auto children = std::make_unique<std::map<int, Person>>();
children->insert(std::make_pair(1, Person{.first_name = "Bart"}));
@@ -28,3 +29,4 @@ void test_map() {
homer,
R"({"firstName":"Homer","lastName":"Simpson","children":{"1":{"firstName":"Bart","lastName":"Simpson"},"2":{"firstName":"Lisa","lastName":"Simpson"},"3":{"firstName":"Maggie","lastName":"Simpson"}}})");
}
} // namespace test_map
+12 -10
View File
@@ -6,18 +6,19 @@
#include "write_and_read.hpp"
void test_map_with_key_validation() {
namespace test_map_with_key_validation {
using Between1and3 = rfl::Validator<int, rfl::Minimum<1>, rfl::Maximum<3>>;
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::unique_ptr<std::map<Between1and3, Person>> children;
};
void test() {
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"}));
@@ -31,3 +32,4 @@ void test_map_with_key_validation() {
homer,
R"({"firstName":"Homer","lastName":"Simpson","children":{"1":{"firstName":"Bart","lastName":"Simpson"},"2":{"firstName":"Lisa","lastName":"Simpson"},"3":{"firstName":"Maggie","lastName":"Simpson"}}})");
}
} // namespace test_map_with_key_validation
+33 -31
View File
@@ -6,37 +6,39 @@
#include "write_and_read.hpp"
void test_monster_example() {
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<std::uint8_t> inventory;
Color color = Color::make<"Blue">();
std::vector<Weapon> weapons;
Equipment equipped;
std::vector<Vec3> path;
};
void test() {
std::cout << "test_monster_example" << std::endl;
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<std::uint8_t> inventory;
Color color = Color::make<"Blue">();
std::vector<Weapon> weapons;
Equipment equipped;
std::vector<Vec3> path;
};
const auto sword = Weapon{.name = "Sword", .damage = 3};
const auto axe = Weapon{.name = "Axe", .damage = 5};
@@ -58,6 +60,6 @@ void test_monster_example() {
write_and_read(
orc,
R"([[1.0,2.0,3.0],150,80,"MyMonster",false,[0,1,2,3,4,5,6,7,8,9],"Red",[["Sword",3],["Axe",5]],{"weapon":["Axe",5]},[]])");
R"({"pos":{"x":1.0,"y":2.0,"z":3.0},"mana":150,"hp":80,"name":"MyMonster","friendly":false,"inventory":[0,1,2,3,4,5,6,7,8,9],"color":"Red","weapons":[{"name":"Sword","damage":3},{"name":"Axe","damage":5}],"equipped":{"weapon":{"name":"Axe","damage":5}},"path":[]})");
}
} // namespace test_monster_example
+17 -14
View File
@@ -7,22 +7,25 @@
#include "write_and_read.hpp"
void test_move_replace() {
std::cout << "test_move_replace" << std::endl;
namespace test_move_replace {
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name;
rfl::Field<"children", rfl::Box<std::vector<Person>>> children;
};
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name;
rfl::Box<std::vector<Person>> children;
};
const auto lisa = Person{.first_name = "Lisa",
.last_name = "Simpson",
.children = rfl::make_box<std::vector<Person>>()};
void test() {
std::cout << "test_move_replace" << std::endl;
const auto maggie =
rfl::replace(lisa, rfl::make_field<"firstName">(std::string("Maggie")));
const auto lisa = Person{.first_name = "Lisa",
.last_name = "Simpson",
.children = rfl::make_box<std::vector<Person>>()};
write_and_read(
maggie, R"({"firstName":"Maggie","lastName":"Simpson","children":[]})");
const auto maggie =
rfl::replace(lisa, rfl::make_field<"firstName">(std::string("Maggie")));
write_and_read(
maggie, R"({"firstName":"Maggie","lastName":"Simpson","children":[]})");
}
} // namespace test_move_replace
+19 -17
View File
@@ -6,25 +6,27 @@
#include "write_and_read.hpp"
void test_multimap() {
std::cout << "test_multimap" << std::endl;
namespace test_multimap {
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name = "Simpson";
rfl::Field<"children", std::unique_ptr<std::multimap<int, Person>>>
children = rfl::default_value;
};
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::unique_ptr<std::multimap<int, Person>> children;
};
auto children = std::make_unique<std::multimap<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"}));
void test() {
std::cout << "test_multimap" << std::endl;
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
auto children = std::make_unique<std::multimap<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"}));
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"}]]})");
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"}]]})");
}
} // namespace test_multimap
+17 -15
View File
@@ -6,23 +6,25 @@
#include "write_and_read.hpp"
void test_multiset() {
std::cout << "test_multiset" << std::endl;
namespace test_multiset {
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name = "Simpson";
rfl::Field<"children", std::unique_ptr<std::multiset<std::string>>>
children = rfl::default_value;
};
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::unique_ptr<std::multiset<std::string>> children;
};
auto children = std::make_unique<std::multiset<std::string>>(
std::multiset<std::string>({"Bart", "Lisa", "Maggie"}));
void test() {
std::cout << "test_multiset" << std::endl;
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
auto children = std::make_unique<std::multiset<std::string>>(
std::multiset<std::string>({"Bart", "Lisa", "Maggie"}));
write_and_read(
homer,
R"({"firstName":"Homer","lastName":"Simpson","children":["Bart","Lisa","Maggie"]})");
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
write_and_read(
homer,
R"({"firstName":"Homer","lastName":"Simpson","children":["Bart","Lisa","Maggie"]})");
}
} // namespace test_multiset
+15 -12
View File
@@ -6,23 +6,26 @@
#include "write_and_read.hpp"
void test_one_of() {
namespace test_one_of {
using Age =
rfl::Validator<unsigned int,
rfl::OneOf<rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<10>>,
rfl::AllOf<rfl::Minimum<40>, rfl::Maximum<130>>>>;
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name;
Age age;
};
void test() {
std::cout << "test_one_of" << std::endl;
using Age = rfl::Validator<
unsigned int,
rfl::OneOf<rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<10>>,
rfl::AllOf<rfl::Minimum<40>, rfl::Maximum<130>>>>;
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name;
rfl::Field<"age", Age> age;
};
const auto homer =
Person{.first_name = "Homer", .last_name = "Simpson", .age = 45};
write_and_read(homer,
R"({"firstName":"Homer","lastName":"Simpson","age":45})");
}
} // namespace test_one_of
+19 -17
View File
@@ -6,27 +6,29 @@
#include "write_and_read.hpp"
void test_optional_fields() {
std::cout << "test_optional_fields" << std::endl;
namespace test_optional_fields {
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name = "Simpson";
rfl::Field<"children", std::optional<std::vector<Person>>> children =
rfl::default_value;
};
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::optional<std::vector<Person>> children;
};
const auto bart = Person{.first_name = "Bart"};
void test() {
std::cout << "test_optional_fields" << std::endl;
const auto lisa = Person{.first_name = "Lisa"};
const auto bart = Person{.first_name = "Bart"};
const auto maggie = Person{.first_name = "Maggie"};
const auto lisa = Person{.first_name = "Lisa"};
const auto homer =
Person{.first_name = "Homer",
.children = std::vector<Person>({bart, lisa, maggie})};
const auto maggie = Person{.first_name = "Maggie"};
write_and_read(
homer,
R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})");
const auto homer =
Person{.first_name = "Homer",
.children = std::vector<Person>({bart, lisa, maggie})};
write_and_read(
homer,
R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})");
}
} // namespace test_optional_fields
+32 -32
View File
@@ -8,26 +8,29 @@
// Makes sure that the example in the README works as expected.
// It would be embarrassing if it didn't...
void test_readme_example() {
namespace test_readme_example {
// Age must be a plausible number, between 0 and 130. This will
// be validated automatically.
using Age = rfl::Validator<unsigned int, rfl::Minimum<0>, rfl::Maximum<130>>;
// "firstName", "lastName" and "children" are the field names
// as they will appear in the JSON. The C++ standard is
// snake case, the JSON standard is camel case, so the names
// will not always be identical.
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name;
rfl::Timestamp<"%Y-%m-%d"> birthday;
Age age;
rfl::Email email;
std::vector<Person> children;
};
void test() {
std::cout << "test_readme_example" << std::endl;
// Age must be a plausible number, between 0 and 130. This will
// be validated automatically.
using Age = rfl::Validator<unsigned int, rfl::Minimum<0>, rfl::Maximum<130>>;
// "firstName", "lastName" and "children" are the field names
// as they will appear in the JSON. The C++ standard is
// snake case, the JSON standard is camel case, so the names
// will not always be identical.
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name;
rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday;
rfl::Field<"age", Age> age;
rfl::Field<"email", rfl::Email> email;
rfl::Field<"children", std::vector<Person>> children;
};
const auto bart = Person{.first_name = "Bart",
.last_name = "Simpson",
.birthday = "1987-04-19",
@@ -35,21 +38,17 @@ void test_readme_example() {
.email = "bart@simpson.com",
.children = std::vector<Person>()};
const auto lisa = Person{
.first_name = "Lisa",
.last_name = "Simpson",
.birthday = "1987-04-19",
.age = 8,
.email = "lisa@simpson.com",
.children = rfl::default_value // same as std::vector<Person>()
};
const auto lisa = Person{.first_name = "Lisa",
.last_name = "Simpson",
.birthday = "1987-04-19",
.age = 8,
.email = "lisa@simpson.com"};
// Returns a deep copy of the original object,
// replacing first_name.
const auto maggie =
rfl::replace(lisa, rfl::make_field<"firstName">(std::string("Maggie")),
rfl::make_field<"email">(std::string("maggie@simpson.com")),
rfl::make_field<"age">(0));
const auto maggie = Person{.first_name = "Maggie",
.last_name = "Simpson",
.birthday = "1987-04-19",
.age = 0,
.email = "maggie@simpson.com"};
const auto homer =
Person{.first_name = "Homer",
@@ -63,3 +62,4 @@ void test_readme_example() {
homer,
R"({"firstName":"Homer","lastName":"Simpson","birthday":"1987-04-19","age":45,"email":"homer@simpson.com","children":[{"firstName":"Bart","lastName":"Simpson","birthday":"1987-04-19","age":10,"email":"bart@simpson.com","children":[]},{"firstName":"Lisa","lastName":"Simpson","birthday":"1987-04-19","age":8,"email":"lisa@simpson.com","children":[]},{"firstName":"Maggie","lastName":"Simpson","birthday":"1987-04-19","age":0,"email":"maggie@simpson.com","children":[]}]})");
}
} // namespace test_readme_example
+29 -26
View File
@@ -7,39 +7,42 @@
#include "write_and_read.hpp"
void test_ref() {
std::cout << "test_ref" << std::endl;
namespace test_ref {
struct DecisionTree {
struct Leaf {
rfl::Field<"type", rfl::Literal<"Leaf">> type = rfl::default_value;
rfl::Field<"value", double> value;
};
struct DecisionTree {
struct Leaf {
using Tag = rfl::Literal<"Leaf">;
rfl::Field<"value", double> value;
};
struct Node {
rfl::Field<"type", rfl::Literal<"Node">> type = rfl::default_value;
rfl::Field<"criticalValue", double> critical_value;
rfl::Field<"left", rfl::Ref<DecisionTree>> lesser;
rfl::Field<"right", rfl::Ref<DecisionTree>> greater;
};
struct Node {
using Tag = rfl::Literal<"Node">;
rfl::Field<"criticalValue", double> critical_value;
rfl::Field<"left", rfl::Ref<DecisionTree>> lesser;
rfl::Field<"right", rfl::Ref<DecisionTree>> greater;
};
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
};
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
};
const auto leaf1 = DecisionTree::Leaf{.value = 3.0};
void test() {
std::cout << "test_ref" << std::endl;
const auto leaf2 = DecisionTree::Leaf{.value = 5.0};
const auto leaf1 = DecisionTree::Leaf{.value = 3.0};
auto node = DecisionTree::Node{
.critical_value = 10.0,
.lesser = rfl::make_ref<DecisionTree>(DecisionTree{leaf1}),
.greater = rfl::make_ref<DecisionTree>(DecisionTree{leaf2})};
const auto leaf2 = DecisionTree::Leaf{.value = 5.0};
const DecisionTree tree{.leaf_or_node = std::move(node)};
auto node = DecisionTree::Node{
.critical_value = 10.0,
.lesser = rfl::make_ref<DecisionTree>(DecisionTree{leaf1}),
.greater = rfl::make_ref<DecisionTree>(DecisionTree{leaf2})};
write_and_read(
tree,
R"({"leafOrNode":{"type":"Node","criticalValue":10.0,"left":{"leafOrNode":{"type":"Leaf","value":3.0}},"right":{"leafOrNode":{"type":"Leaf","value":5.0}}}})");
const DecisionTree tree{.leaf_or_node = std::move(node)};
write_and_read(
tree,
R"({"leafOrNode":{"type":"Node","criticalValue":10.0,"left":{"leafOrNode":{"type":"Leaf","value":3.0}},"right":{"leafOrNode":{"type":"Leaf","value":5.0}}}})");
}
} // namespace test_ref
+56
View File
@@ -0,0 +1,56 @@
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace test_rename {
using Age = rfl::Validator<unsigned int,
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name;
rfl::Timestamp<"%Y-%m-%d"> birthday;
Age age;
rfl::Email email;
std::vector<Person> children;
};
void test() {
std::cout << "test_anonymous_fields" << std::endl;
const auto bart = Person{.first_name = "Bart",
.last_name = "Simpson",
.birthday = "1987-04-19",
.age = 10,
.email = "bart@simpson.com"};
const auto lisa = Person{.first_name = "Lisa",
.last_name = "Simpson",
.birthday = "1987-04-19",
.age = 8,
.email = "lisa@simpson.com"};
const auto maggie = Person{.first_name = "Maggie",
.last_name = "Simpson",
.birthday = "1987-04-19",
.age = 0,
.email = "maggie@simpson.com"};
const auto homer =
Person{.first_name = "Homer",
.last_name = "Simpson",
.birthday = "1987-04-19",
.age = 45,
.email = "homer@simpson.com",
.children = std::vector<Person>({bart, lisa, maggie})};
write_and_read(
homer,
R"({"firstName":"Homer","lastName":"Simpson","birthday":"1987-04-19","age":45,"email":"homer@simpson.com","children":[{"firstName":"Bart","lastName":"Simpson","birthday":"1987-04-19","age":10,"email":"bart@simpson.com","children":[]},{"firstName":"Lisa","lastName":"Simpson","birthday":"1987-04-19","age":8,"email":"lisa@simpson.com","children":[]},{"firstName":"Maggie","lastName":"Simpson","birthday":"1987-04-19","age":0,"email":"maggie@simpson.com","children":[]}]})");
}
} // namespace test_rename
+17 -14
View File
@@ -7,22 +7,25 @@
#include "write_and_read.hpp"
void test_replace() {
std::cout << "test_replace" << std::endl;
namespace test_replace {
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name;
rfl::Field<"children", rfl::Box<std::vector<Person>>> children;
};
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name;
rfl::Field<"children", rfl::Box<std::vector<Person>>> children;
};
auto lisa = Person{.first_name = "Lisa",
.last_name = "Simpson",
.children = rfl::make_box<std::vector<Person>>()};
void test() {
std::cout << "test_replace" << std::endl;
const auto maggie = rfl::replace(
std::move(lisa), rfl::make_field<"firstName">(std::string("Maggie")));
auto lisa = Person{.first_name = "Lisa",
.last_name = "Simpson",
.children = rfl::make_box<std::vector<Person>>()};
write_and_read(
maggie, R"({"firstName":"Maggie","lastName":"Simpson","children":[]})");
const auto maggie = rfl::replace(
std::move(lisa), rfl::make_field<"firstName">(std::string("Maggie")));
write_and_read(
maggie, R"({"firstName":"Maggie","lastName":"Simpson","children":[]})");
}
} // namespace test_replace
+27 -24
View File
@@ -7,33 +7,36 @@
#include "write_and_read.hpp"
void test_replace_flatten() {
std::cout << "test_replace_flatten" << std::endl;
namespace test_replace_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 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;
};
struct Employee {
rfl::Flatten<Person> person;
rfl::Field<"employer", rfl::Box<std::string>> employer;
rfl::Field<"salary", float> salary;
};
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};
void test() {
std::cout << "test_replace_flatten" << std::endl;
auto employee2 =
rfl::replace(std::move(employee), rfl::make_field<"salary">(70000.0),
rfl::make_field<"age">(46));
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(
employee2,
R"({"firstName":"Homer","lastName":"Simpson","age":46,"employer":"Mr. Burns","salary":70000.0})");
auto employee2 =
rfl::replace(std::move(employee), rfl::make_field<"salary">(70000.0),
rfl::make_field<"age">(46));
write_and_read(
employee2,
R"({"firstName":"Homer","lastName":"Simpson","age":46,"employer":"Mr. Burns","salary":70000.0})");
}
} // namespace test_replace_flatten
+25 -22
View File
@@ -7,32 +7,35 @@
#include "write_and_read.hpp"
void test_replace_with_other_struct() {
std::cout << "test_replace_with_other_struct" << std::endl;
namespace test_replace_with_other_struct {
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name;
rfl::Field<"age", int> age;
};
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name;
rfl::Field<"age", int> age;
};
struct Employee {
rfl::Flatten<Person> person;
rfl::Field<"employer", std::string> employer;
rfl::Field<"salary", float> salary;
};
struct Employee {
rfl::Flatten<Person> person;
rfl::Field<"employer", std::string> employer;
rfl::Field<"salary", float> salary;
};
const auto employee = Employee{
.person =
Person{.first_name = "Homer", .last_name = "Simpson", .age = 45},
.employer = std::string("Mr. Burns"),
.salary = 60000.0};
void test() {
std::cout << "test_replace_with_other_struct" << std::endl;
const auto carl = Person{.first_name = "Carl", .last_name = "", .age = 45};
const auto employee = Employee{
.person =
Person{.first_name = "Homer", .last_name = "Simpson", .age = 45},
.employer = std::string("Mr. Burns"),
.salary = 60000.0};
const auto employee2 = rfl::replace(employee, carl);
const auto carl = Person{.first_name = "Carl", .last_name = "", .age = 45};
write_and_read(
employee2,
R"({"firstName":"Carl","lastName":"","age":45,"employer":"Mr. Burns","salary":60000.0})");
const auto employee2 = rfl::replace(employee, carl);
write_and_read(
employee2,
R"({"firstName":"Carl","lastName":"","age":45,"employer":"Mr. Burns","salary":60000.0})");
}
} // namespace test_replace_with_other_struct
+10 -8
View File
@@ -6,15 +6,16 @@
#include "write_and_read.hpp"
void test_result() {
std::cout << "test_result" << std::endl;
namespace test_result {
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name = "Simpson";
rfl::Field<"children", std::vector<rfl::Result<Person>>> children =
rfl::default_value;
};
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::vector<rfl::Result<Person>> children;
};
void test() {
std::cout << "test_result" << std::endl;
const rfl::Result<Person> bart = Person{.first_name = "Bart"};
@@ -30,3 +31,4 @@ void test_result() {
homer,
R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson","children":[]},{"firstName":"Lisa","lastName":"Simpson","children":[]},{"error":"Some error occurred."}]})");
}
} // namespace test_result
+26 -25
View File
@@ -5,21 +5,23 @@
#include <string>
#include <vector>
void test_save_load() {
namespace test_save_load {
using Age = rfl::Validator<unsigned int,
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name;
rfl::Timestamp<"%Y-%m-%d"> birthday;
Age age;
rfl::Email email;
std::vector<Person> children;
};
void test() {
std::cout << "test_save_load" << std::endl;
using Age = rfl::Validator<unsigned int,
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name;
rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday;
rfl::Field<"age", Age> age;
rfl::Field<"email", rfl::Email> email;
rfl::Field<"children", std::vector<Person>> children;
};
const auto bart = Person{.first_name = "Bart",
.last_name = "Simpson",
.birthday = "1987-04-19",
@@ -27,19 +29,17 @@ void test_save_load() {
.email = "bart@simpson.com",
.children = std::vector<Person>()};
const auto lisa = Person{
.first_name = "Lisa",
.last_name = "Simpson",
.birthday = "1987-04-19",
.age = 8,
.email = "lisa@simpson.com",
.children = rfl::default_value // same as std::vector<Person>()
};
const auto lisa = Person{.first_name = "Lisa",
.last_name = "Simpson",
.birthday = "1987-04-19",
.age = 8,
.email = "lisa@simpson.com"};
const auto maggie =
rfl::replace(lisa, rfl::make_field<"firstName">(std::string("Maggie")),
rfl::make_field<"email">(std::string("maggie@simpson.com")),
rfl::make_field<"age">(0));
const auto maggie = Person{.first_name = "Maggie",
.last_name = "Simpson",
.birthday = "1987-04-19",
.age = 0,
.email = "maggie@simpson.com"};
const auto homer1 =
Person{.first_name = "Homer",
@@ -64,3 +64,4 @@ void test_save_load() {
std::cout << "OK" << std::endl << std::endl;
}
} // namespace test_save_load
+17 -15
View File
@@ -6,23 +6,25 @@
#include "write_and_read.hpp"
void test_set() {
std::cout << "test_set" << std::endl;
namespace test_set {
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name = "Simpson";
rfl::Field<"children", std::unique_ptr<std::set<std::string>>>
children = rfl::default_value;
};
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;
};
auto children = std::make_unique<std::set<std::string>>(
std::set<std::string>({"Bart", "Lisa", "Maggie"}));
void test() {
std::cout << "test_set" << std::endl;
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
auto children = std::make_unique<std::set<std::string>>(
std::set<std::string>({"Bart", "Lisa", "Maggie"}));
write_and_read(
homer,
R"({"firstName":"Homer","lastName":"Simpson","children":["Bart","Lisa","Maggie"]})");
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
write_and_read(
homer,
R"({"firstName":"Homer","lastName":"Simpson","children":["Bart","Lisa","Maggie"]})");
}
} // namespace test_set
+18 -27
View File
@@ -6,38 +6,28 @@
#include "write_and_read.hpp"
// Makes sure that the example in the README works as expected.
// It would be embarrassing if it didn't...
void test_size() {
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;
};
void test() {
std::cout << "test_size" << std::endl;
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name;
rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday;
rfl::Field<
"children",
rfl::Validator<std::vector<Person>,
rfl::Size<rfl::AnyOf<rfl::EqualTo<0>, rfl::EqualTo<3>>>>>
children;
};
const auto bart = Person{.first_name = "Bart",
.last_name = "Simpson",
.birthday = "1987-04-19",
.children = std::vector<Person>()};
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",
.children = rfl::default_value // same as std::vector<Person>()
};
.first_name = "Lisa", .last_name = "Simpson", .birthday = "1987-04-19"};
// Returns a deep copy of the original object,
// replacing first_name.
const auto maggie =
rfl::replace(lisa, rfl::make_field<"firstName">(std::string("Maggie")));
const auto maggie = Person{
.first_name = "Maggie", .last_name = "Simpson", .birthday = "1987-04-19"};
const auto homer =
Person{.first_name = "Homer",
@@ -49,3 +39,4 @@ void test_size() {
homer,
R"({"firstName":"Homer","lastName":"Simpson","birthday":"1987-04-19","children":[{"firstName":"Bart","lastName":"Simpson","birthday":"1987-04-19","children":[]},{"firstName":"Lisa","lastName":"Simpson","birthday":"1987-04-19","children":[]},{"firstName":"Maggie","lastName":"Simpson","birthday":"1987-04-19","children":[]}]})");
}
} // namespace test_size
+10 -8
View File
@@ -7,14 +7,16 @@
#include "write_and_read.hpp"
void test_string_map() {
std::cout << "test_string_map" << std::endl;
namespace test_string_map {
void test() {
std::cout << "test_string_map" << std::endl;
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")));
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, R"({"firstName":"Homer","lastName":"Simpson"})");
write_and_read(homer, R"({"firstName":"Homer","lastName":"Simpson"})");
}
} // namespace test_string_map
+15 -13
View File
@@ -7,19 +7,21 @@
#include "write_and_read.hpp"
void test_string_unordered_map() {
std::cout << "test_string_unordered_map" << std::endl;
namespace test_string_unordered_map {
void test() {
std::cout << "test_string_unordered_map" << std::endl;
using Map = std::unordered_map<std::string, std::unique_ptr<std::string>>;
Map 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")));
using Map = std::unordered_map<std::string, std::unique_ptr<std::string>>;
Map 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")));
// Unordered maps are unpredictable. We therefore only make sure that this
// compiles.
const auto json_string = rfl::json::write(homer);
const auto homer2 = rfl::json::read<Map>(json_string);
std::cout << "OK" << std::endl << std::endl;
// Unordered maps are unpredictable. We therefore only make sure that this
// compiles.
const auto json_string = rfl::json::write(homer);
const auto homer2 = rfl::json::read<Map>(json_string);
std::cout << "OK" << std::endl << std::endl;
}
} // namespace test_string_unordered_map
+21 -20
View File
@@ -7,30 +7,31 @@
#include "write_and_read.hpp"
// Makes sure that we can set default values.
void test_tagged_union() {
std::cout << "test_tagged_union" << std::endl;
namespace test_tagged_union {
struct Circle {
rfl::Field<"shape", rfl::Literal<"Circle">> shape = rfl::default_value;
rfl::Field<"radius", double> radius;
};
struct Circle {
using Tag = rfl::Literal<"Circle">;
rfl::Field<"radius", double> radius;
};
struct Rectangle {
rfl::Field<"shape", rfl::Literal<"Rectangle">> shape =
rfl::default_value;
rfl::Field<"height", double> height;
rfl::Field<"width", double> width;
};
struct Rectangle {
using Tag = rfl::Literal<"Rectangle">;
rfl::Field<"height", double> height;
rfl::Field<"width", double> width;
};
struct Square {
rfl::Field<"shape", rfl::Literal<"Square">> shape = rfl::default_value;
rfl::Field<"width", double> width;
};
struct Square {
using Tag = rfl::Literal<"Square">;
rfl::Field<"width", double> width;
};
using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>;
using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>;
const Shapes r = Rectangle{.height = 10, .width = 5};
void test() {
std::cout << "test_tagged_union" << std::endl;
write_and_read(r, R"({"shape":"Rectangle","height":10.0,"width":5.0})");
const Shapes r = Rectangle{.height = 10, .width = 5};
write_and_read(r, R"({"shape":"Rectangle","height":10.0,"width":5.0})");
}
} // namespace test_tagged_union
+20 -17
View File
@@ -7,27 +7,30 @@
#include "write_and_read.hpp"
void test_timestamp() {
std::cout << "test_timestamp" << std::endl;
namespace test_timestamp {
using TS = rfl::Timestamp<"%Y-%m-%d">;
using TS = rfl::Timestamp<"%Y-%m-%d">;
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name = "Simpson";
rfl::Field<"birthday", TS> birthday;
};
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
TS birthday;
};
const auto result = TS::from_string("nonsense");
void test() {
std::cout << "test_timestamp" << std::endl;
if (result) {
std::cout << "Failed: Expected an error, but got none." << std::endl;
return;
}
const auto result = TS::from_string("nonsense");
const auto bart = Person{.first_name = "Bart", .birthday = "1987-04-19"};
if (result) {
std::cout << "Failed: Expected an error, but got none." << std::endl;
return;
}
write_and_read(
bart,
R"({"firstName":"Bart","lastName":"Simpson","birthday":"1987-04-19"})");
const auto bart = Person{.first_name = "Bart", .birthday = "1987-04-19"};
write_and_read(
bart,
R"({"firstName":"Bart","lastName":"Simpson","birthday":"1987-04-19"})");
}
} // namespace test_timestamp
+19 -17
View File
@@ -7,25 +7,27 @@
#include "write_and_read.hpp"
void test_unique_ptr() {
std::cout << "test_unique_ptr" << std::endl;
namespace test_unique_ptr {
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name = "Simpson";
rfl::Field<"children", std::unique_ptr<std::vector<Person>>> children =
rfl::default_value;
};
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::unique_ptr<std::vector<Person>> children;
};
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"});
void test() {
std::cout << "test_unique_ptr" << std::endl;
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
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"});
write_and_read(
homer,
R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})");
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
write_and_read(
homer,
R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})");
}
} // namespace test_unique_ptr
+29 -26
View File
@@ -7,39 +7,42 @@
#include "write_and_read.hpp"
void test_unique_ptr2() {
std::cout << "test_unique_ptr2" << std::endl;
namespace test_unique_ptr2 {
struct DecisionTree {
struct Leaf {
rfl::Field<"type", rfl::Literal<"Leaf">> type = rfl::default_value;
rfl::Field<"value", double> value;
};
struct DecisionTree {
struct Leaf {
using Tag = rfl::Literal<"Leaf">;
rfl::Field<"value", double> value;
};
struct Node {
rfl::Field<"type", rfl::Literal<"Node">> type = rfl::default_value;
rfl::Field<"criticalValue", double> critical_value;
rfl::Field<"left", std::unique_ptr<DecisionTree>> lesser;
rfl::Field<"right", std::unique_ptr<DecisionTree>> greater;
};
struct Node {
using Tag = rfl::Literal<"Node">;
rfl::Field<"criticalValue", double> critical_value;
rfl::Field<"left", std::unique_ptr<DecisionTree>> lesser;
rfl::Field<"right", std::unique_ptr<DecisionTree>> greater;
};
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
};
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
};
auto leaf1 = DecisionTree::Leaf{.value = 3.0};
void test() {
std::cout << "test_unique_ptr2" << std::endl;
auto leaf2 = DecisionTree::Leaf{.value = 5.0};
auto leaf1 = DecisionTree::Leaf{.value = 3.0};
auto node = DecisionTree::Node{
.critical_value = 10.0,
.lesser = std::make_unique<DecisionTree>(DecisionTree{leaf1}),
.greater = std::make_unique<DecisionTree>(DecisionTree{leaf2})};
auto leaf2 = DecisionTree::Leaf{.value = 5.0};
const DecisionTree tree{.leaf_or_node = std::move(node)};
auto node = DecisionTree::Node{
.critical_value = 10.0,
.lesser = std::make_unique<DecisionTree>(DecisionTree{leaf1}),
.greater = std::make_unique<DecisionTree>(DecisionTree{leaf2})};
write_and_read(
tree,
R"({"leafOrNode":{"type":"Node","criticalValue":10.0,"left":{"leafOrNode":{"type":"Leaf","value":3.0}},"right":{"leafOrNode":{"type":"Leaf","value":5.0}}}})");
const DecisionTree tree{.leaf_or_node = std::move(node)};
write_and_read(
tree,
R"({"leafOrNode":{"type":"Node","criticalValue":10.0,"left":{"leafOrNode":{"type":"Leaf","value":3.0}},"right":{"leafOrNode":{"type":"Leaf","value":5.0}}}})");
}
} // namespace test_unique_ptr2
+21 -19
View File
@@ -6,27 +6,29 @@
#include "write_and_read.hpp"
void test_unordered_map() {
std::cout << "test_unordered_map" << std::endl;
namespace test_unordered_map {
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name = "Simpson";
rfl::Field<"children", std::unique_ptr<std::unordered_map<int, Person>>>
children = rfl::default_value;
};
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::unique_ptr<std::unordered_map<int, Person>> children;
};
auto children = std::make_unique<std::unordered_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"}));
void test() {
std::cout << "test_unordered_map" << std::endl;
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
auto children = std::make_unique<std::unordered_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"}));
// Unordered maps are unpredictable. We therefore only make sure that this
// compiles.
const auto json_string = rfl::json::write(homer);
const auto homer2 = rfl::json::read<Person>(json_string);
std::cout << "OK" << std::endl << std::endl;
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
// Unordered maps are unpredictable. We therefore only make sure that this
// compiles.
const auto json_string = rfl::json::write(homer);
const auto homer2 = rfl::json::read<Person>(json_string);
std::cout << "OK" << std::endl << std::endl;
}
} // namespace test_unordered_map
+21 -20
View File
@@ -6,28 +6,29 @@
#include "write_and_read.hpp"
void test_unordered_multimap() {
std::cout << "test_unordered_multimap" << std::endl;
namespace test_unordered_multimap {
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name = "Simpson";
rfl::Field<"children",
std::unique_ptr<std::unordered_multimap<int, Person>>>
children = rfl::default_value;
};
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::unique_ptr<std::unordered_multimap<int, Person>> children;
};
auto children = std::make_unique<std::unordered_multimap<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"}));
void test() {
std::cout << "test_unordered_multimap" << std::endl;
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
auto children = std::make_unique<std::unordered_multimap<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"}));
// Unordered maps are unpredictable. We therefore only make sure that this
// compiles.
const auto json_string = rfl::json::write(homer);
const auto homer2 = rfl::json::read<Person>(json_string);
std::cout << "OK" << std::endl << std::endl;
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
// Unordered maps are unpredictable. We therefore only make sure that this
// compiles.
const auto json_string = rfl::json::write(homer);
const auto homer2 = rfl::json::read<Person>(json_string);
std::cout << "OK" << std::endl << std::endl;
}
} // namespace test_unordered_multimap
+19 -18
View File
@@ -6,26 +6,27 @@
#include "write_and_read.hpp"
void test_unordered_multiset() {
std::cout << "test_unordered_multiset" << std::endl;
namespace test_unordered_multiset {
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name = "Simpson";
rfl::Field<"children",
std::unique_ptr<std::unordered_multiset<std::string>>>
children = rfl::default_value;
};
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::unique_ptr<std::unordered_multiset<std::string>> children;
};
auto children = std::make_unique<std::unordered_multiset<std::string>>(
std::unordered_multiset<std::string>({"Bart", "Lisa", "Maggie"}));
void test() {
std::cout << "test_unordered_multiset" << std::endl;
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
auto children = std::make_unique<std::unordered_multiset<std::string>>(
std::unordered_multiset<std::string>({"Bart", "Lisa", "Maggie"}));
// Unordered multisets are unpredictable. We therefore only make sure that
// this compiles.
const auto json_string = rfl::json::write(homer);
const auto homer2 = rfl::json::read<Person>(json_string);
std::cout << "OK" << std::endl << std::endl;
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
// Unordered multisets are unpredictable. We therefore only make sure that
// this compiles.
const auto json_string = rfl::json::write(homer);
const auto homer2 = rfl::json::read<Person>(json_string);
std::cout << "OK" << std::endl << std::endl;
}
} // namespace test_unordered_multiset
+19 -17
View File
@@ -6,25 +6,27 @@
#include "write_and_read.hpp"
void test_unordered_set() {
std::cout << "test_unordered_set" << std::endl;
namespace test_unordered_set {
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name = "Simpson";
rfl::Field<"children", std::unique_ptr<std::unordered_set<std::string>>>
children = rfl::default_value;
};
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::unique_ptr<std::unordered_set<std::string>> children;
};
auto children = std::make_unique<std::unordered_set<std::string>>(
std::unordered_set<std::string>({"Bart", "Lisa", "Maggie"}));
void test() {
std::cout << "test_unordered_set" << std::endl;
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
auto children = std::make_unique<std::unordered_set<std::string>>(
std::unordered_set<std::string>({"Bart", "Lisa", "Maggie"}));
// Unordered sets are unpredictable. We therefore only make sure that this
// compiles.
const auto json_string = rfl::json::write(homer);
const auto homer2 = rfl::json::read<Person>(json_string);
std::cout << "OK" << std::endl << std::endl;
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
// Unordered sets are unpredictable. We therefore only make sure that this
// compiles.
const auto json_string = rfl::json::write(homer);
const auto homer2 = rfl::json::read<Person>(json_string);
std::cout << "OK" << std::endl << std::endl;
}
} // namespace test_unordered_set
+18 -16
View File
@@ -7,26 +7,28 @@
#include "write_and_read.hpp"
// Makes sure that we can set default values.
void test_variant() {
std::cout << "test_variant" << std::endl;
namespace test_variant {
struct Circle {
rfl::Field<"radius", double> radius;
};
struct Circle {
double radius;
};
struct Rectangle {
rfl::Field<"height", double> height;
rfl::Field<"width", double> width;
};
struct Rectangle {
double height;
double width;
};
struct Square {
rfl::Field<"width", double> width;
};
struct Square {
double width;
};
using Shapes = std::variant<Circle, Rectangle, std::unique_ptr<Square>>;
using Shapes = std::variant<Circle, Rectangle, std::unique_ptr<Square>>;
const Shapes r = Rectangle{.height = 10, .width = 5};
void test() {
std::cout << "test_variant" << std::endl;
write_and_read(r, R"({"height":10.0,"width":5.0})");
const Shapes r = Rectangle{.height = 10, .width = 5};
write_and_read(r, R"({"height":10.0,"width":5.0})");
}
} // namespace test_variant
+54 -54
View File
@@ -19,7 +19,6 @@
#include "test_field_variant.hpp"
#include "test_flatten.hpp"
#include "test_flatten_anonymous.hpp"
#include "test_flatten_tuple.hpp"
#include "test_forward_list.hpp"
#include "test_list.hpp"
#include "test_literal.hpp"
@@ -34,6 +33,7 @@
#include "test_optional_fields.hpp"
#include "test_readme_example.hpp"
#include "test_ref.hpp"
#include "test_rename.hpp"
#include "test_replace.hpp"
#include "test_replace_flatten.hpp"
#include "test_replace_with_other_struct.hpp"
@@ -54,65 +54,65 @@
#include "test_variant.hpp"
int main() {
test_readme_example();
test_default_values();
test_optional_fields();
test_unique_ptr();
test_unique_ptr2();
test_literal();
test_variant();
test_tagged_union();
test_field_variant();
test_ref();
test_box();
test_array();
test_timestamp();
test_flatten();
test_flatten_anonymous();
test_flatten_tuple();
test_deque();
test_forward_list();
test_list();
test_map();
test_map_with_key_validation();
test_unordered_map();
test_set();
test_unordered_set();
test_multimap();
test_unordered_multimap();
test_multiset();
test_unordered_multiset();
test_string_map();
test_string_unordered_map();
test_alphanumeric_map();
test_alphanumeric_unordered_map();
test_literal_map();
test_literal_unordered_map();
test_error_messages();
test_result();
test_anonymous_fields();
test_monster_example();
test_readme_example::test();
test_default_values::test();
test_optional_fields::test();
test_unique_ptr::test();
test_unique_ptr2::test();
test_literal::test();
test_variant::test();
test_tagged_union::test();
test_field_variant::test();
test_ref::test();
test_box::test();
test_array::test();
test_timestamp::test();
test_flatten::test();
test_flatten_anonymous::test();
test_rename::test();
test_deque::test();
test_forward_list::test();
test_list::test();
test_map::test();
test_map_with_key_validation::test();
test_unordered_map::test();
test_set::test();
test_unordered_set::test();
test_multimap::test();
test_unordered_multimap::test();
test_multiset::test();
test_unordered_multiset::test();
test_string_map::test();
test_string_unordered_map::test();
test_alphanumeric_map::test();
test_alphanumeric_unordered_map::test();
test_literal_map::test();
test_literal_unordered_map::test();
test_error_messages::test();
test_result::test();
test_anonymous_fields::test();
test_monster_example::test();
test_custom_class1();
test_custom_class2();
test_custom_class3();
test_custom_class4();
test_custom_class1::test();
test_custom_class2::test();
test_custom_class3::test();
test_custom_class4::test();
test_replace();
test_replace_flatten();
test_replace_with_other_struct();
test_as();
test_replace::test();
test_replace_flatten::test();
test_replace_with_other_struct::test();
test_as::test();
test_as_flatten();
test_custom_constructor();
test_custom_constructor::test();
test_all_of();
test_any_of();
test_one_of();
test_email();
test_size();
test_all_of::test();
test_any_of::test();
test_one_of::test();
test_email::test();
test_size::test();
test_save_load();
test_save_load::test();
return 0;
}