mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 16:56:17 +08:00
Added the JSON schema; #25
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
#include "rfl/AnyOf.hpp"
|
||||
#include "rfl/Attribute.hpp"
|
||||
#include "rfl/Box.hpp"
|
||||
#include "rfl/Description.hpp"
|
||||
#include "rfl/Field.hpp"
|
||||
#include "rfl/Flatten.hpp"
|
||||
#include "rfl/Literal.hpp"
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
#ifndef RFL_ALLOF_HPP_
|
||||
#define RFL_ALLOF_HPP_
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "Result.hpp"
|
||||
#include "parsing/schema/ValidationType.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
@@ -13,6 +16,14 @@ struct AllOf {
|
||||
return validate_impl<T, C, Cs...>(_value);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
static parsing::schema::ValidationType to_schema() {
|
||||
using ValidationType = parsing::schema::ValidationType;
|
||||
const auto types = std::vector<ValidationType>(
|
||||
{C::template to_schema<T>(), Cs::template to_schema<T>()...});
|
||||
return ValidationType{ValidationType::AllOf{.types_ = types}};
|
||||
}
|
||||
|
||||
private:
|
||||
template <class T, class Head, class... Tail>
|
||||
static rfl::Result<T> validate_impl(T _value) noexcept {
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "Result.hpp"
|
||||
#include "parsing/schema/ValidationType.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
@@ -17,6 +18,14 @@ struct AnyOf {
|
||||
return validate_impl<T, C, Cs...>(_value, {});
|
||||
}
|
||||
|
||||
template <class T>
|
||||
static parsing::schema::ValidationType to_schema() {
|
||||
using ValidationType = parsing::schema::ValidationType;
|
||||
const auto types = std::vector<ValidationType>(
|
||||
{C::template to_schema<T>(), Cs::template to_schema<T>()...});
|
||||
return ValidationType{ValidationType::AnyOf{.types_ = types}};
|
||||
}
|
||||
|
||||
private:
|
||||
static Error make_error_message(const std::vector<Error>& _errors) {
|
||||
std::string msg =
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
#ifndef RFL_DESCRIPTION_HPP_
|
||||
#define RFL_DESCRIPTION_HPP_
|
||||
|
||||
#include <algorithm>
|
||||
#include <string_view>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "Literal.hpp"
|
||||
#include "default.hpp"
|
||||
#include "internal/StringLiteral.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// Used to add a description to the field - this is only relevant for the JSON
|
||||
/// schema and will be ignored by the normal serialization routines.
|
||||
template <internal::StringLiteral _description, class T>
|
||||
struct Description {
|
||||
/// The underlying type.
|
||||
using Type = T;
|
||||
|
||||
/// The description of the field.
|
||||
using Content = rfl::Literal<_description>;
|
||||
|
||||
using ReflectionType = Type;
|
||||
|
||||
Description() : value_(Type()) {}
|
||||
|
||||
Description(const Type& _value) : value_(_value) {}
|
||||
|
||||
Description(Type&& _value) noexcept : value_(std::move(_value)) {}
|
||||
|
||||
Description(Description<_description, T>&& _field) noexcept = default;
|
||||
|
||||
Description(const Description<_description, Type>& _field) = default;
|
||||
|
||||
template <class U>
|
||||
Description(const Description<_description, U>& _field)
|
||||
: value_(_field.get()) {}
|
||||
|
||||
template <class U>
|
||||
Description(Description<_description, U>&& _field) : value_(_field.get()) {}
|
||||
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>,
|
||||
bool>::type = true>
|
||||
Description(const U& _value) : value_(_value) {}
|
||||
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>,
|
||||
bool>::type = true>
|
||||
Description(U&& _value) noexcept : value_(std::forward<U>(_value)) {}
|
||||
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>,
|
||||
bool>::type = true>
|
||||
Description(const Description<_description, 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>
|
||||
Description(const Default& _default) : value_(Type()) {}
|
||||
|
||||
~Description() = default;
|
||||
|
||||
/// The description of the field, for internal use.
|
||||
constexpr static const internal::StringLiteral description_ = _description;
|
||||
|
||||
/// 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.
|
||||
Description<_description, T>& operator=(
|
||||
const Description<_description, T>& _field) = default;
|
||||
|
||||
/// Assigns the underlying object.
|
||||
Description<_description, T>& operator=(
|
||||
Description<_description, T>&& _field) = default;
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class U>
|
||||
auto& operator=(const Description<_description, U>& _field) {
|
||||
value_ = _field.get();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class U>
|
||||
auto& operator=(Description<_description, U>&& _field) {
|
||||
value_ = std::forward<T>(_field.value_);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Returns the underlying object - necessary for the reflection to work.
|
||||
const Type& reflection() const { return value_; }
|
||||
|
||||
/// 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
|
||||
@@ -62,12 +62,12 @@ struct Field {
|
||||
/// The name of the field, for internal use.
|
||||
constexpr static const internal::StringLiteral name_ = _name;
|
||||
|
||||
/// The name of the field.
|
||||
constexpr static std::string_view name() { return name_.string_view(); }
|
||||
|
||||
/// Returns the underlying object.
|
||||
const Type& get() const { return value_; }
|
||||
|
||||
/// The name of the field.
|
||||
constexpr static std::string_view name() { return name_.string_view(); }
|
||||
|
||||
/// Returns the underlying object.
|
||||
Type& operator()() { return value_; }
|
||||
|
||||
|
||||
+23
-5
@@ -134,6 +134,9 @@ class Literal {
|
||||
/// The name defined by the Literal.
|
||||
std::string name() const { return find_name(); }
|
||||
|
||||
/// Returns all possible values of the literal as a std::vector<std::string>.
|
||||
static std::vector<std::string> names() { return allowed_strings_vec(); }
|
||||
|
||||
/// Helper function to retrieve a name at compile time.
|
||||
template <int _value>
|
||||
constexpr static auto name_of() {
|
||||
@@ -168,6 +171,9 @@ class Literal {
|
||||
/// Alias for .name().
|
||||
std::string str() const { return name(); }
|
||||
|
||||
/// Alias for .names().
|
||||
static std::vector<std::string> strings() { return allowed_strings_vec(); }
|
||||
|
||||
/// Returns the value actually contained in the Literal.
|
||||
ValueType value() const { return value_; }
|
||||
|
||||
@@ -183,16 +189,28 @@ class Literal {
|
||||
/// Only the static methods are allowed to access this.
|
||||
Literal(const ValueType _value) : value_(_value) {}
|
||||
|
||||
/// Returns all of the allowed fields.
|
||||
static std::string allowed_strings() {
|
||||
const auto vec = allowed_strings_vec();
|
||||
std::string str;
|
||||
for (size_t i = 0; i < vec.size(); ++i) {
|
||||
const auto head = "'" + vec[i] + "'";
|
||||
str += i == 0 ? head : (", " + head);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
/// Returns all of the allowed fields.
|
||||
template <int _i = 0>
|
||||
static std::string allowed_strings(const std::string& _values = "") {
|
||||
static std::vector<std::string> allowed_strings_vec(
|
||||
std::vector<std::string> _values = {}) {
|
||||
using FieldType = typename std::tuple_element<_i, FieldsType>::type;
|
||||
const auto head = "'" + FieldType::field_.str() + "'";
|
||||
const auto values = _values.size() == 0 ? head : _values + ", " + head;
|
||||
const auto head = FieldType::field_.str();
|
||||
_values.push_back(head);
|
||||
if constexpr (_i + 1 < num_fields_) {
|
||||
return allowed_strings<_i + 1>(values);
|
||||
return allowed_strings_vec<_i + 1>(std::move(_values));
|
||||
} else {
|
||||
return values;
|
||||
return _values;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+20
-20
@@ -162,6 +162,26 @@ class NamedTuple {
|
||||
std::forward<Tail>(_tail)...);
|
||||
}
|
||||
|
||||
/// Invokes a callable object once for each field in order.
|
||||
template <typename F>
|
||||
void apply(F&& f) {
|
||||
std::apply(
|
||||
[&f]<typename... AFields>(AFields&&... fields) {
|
||||
((f(std::forward<AFields>(fields))), ...);
|
||||
},
|
||||
fields());
|
||||
}
|
||||
|
||||
/// Invokes a callable object once for each field in order.
|
||||
template <typename F>
|
||||
void apply(F&& f) const {
|
||||
std::apply(
|
||||
[&f]<typename... AFields>(AFields&&... fields) {
|
||||
((f(std::forward<AFields>(fields))), ...);
|
||||
},
|
||||
fields());
|
||||
}
|
||||
|
||||
/// Returns a tuple containing the fields.
|
||||
Fields fields() { return make_fields(); }
|
||||
|
||||
@@ -210,26 +230,6 @@ class NamedTuple {
|
||||
return rfl::make_field<_field_name>(rfl::get<_field_name>(*this));
|
||||
}
|
||||
|
||||
/// Invokes a callable object once for each field in order.
|
||||
template <typename F>
|
||||
void apply(F&& f) {
|
||||
std::apply(
|
||||
[&f]<typename... AFields>(AFields&&... fields) {
|
||||
((f(std::forward<AFields>(fields))), ...);
|
||||
},
|
||||
fields());
|
||||
}
|
||||
|
||||
/// Invokes a callable object once for each field in order.
|
||||
template <typename F>
|
||||
void apply(F&& f) const {
|
||||
std::apply(
|
||||
[&f]<typename... AFields>(AFields&&... fields) {
|
||||
((f(std::forward<AFields>(fields))), ...);
|
||||
},
|
||||
fields());
|
||||
}
|
||||
|
||||
/// Copy assignment operator.
|
||||
NamedTuple<FieldTypes...>& operator=(
|
||||
const NamedTuple<FieldTypes...>& _other) = default;
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "Result.hpp"
|
||||
#include "parsing/schema/ValidationType.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
@@ -17,6 +18,14 @@ struct OneOf {
|
||||
return validate_impl<T, C, Cs...>(_value, {});
|
||||
}
|
||||
|
||||
template <class T>
|
||||
static parsing::schema::ValidationType to_schema() {
|
||||
using ValidationType = parsing::schema::ValidationType;
|
||||
const auto types = std::vector<ValidationType>(
|
||||
{C::template to_schema<T>(), Cs::template to_schema<T>()...});
|
||||
return ValidationType{ValidationType::OneOf{.types_ = types}};
|
||||
}
|
||||
|
||||
private:
|
||||
static Error make_error_message(const std::vector<Error>& _errors) {
|
||||
std::string msg = "Expected exactly 1 out of " +
|
||||
|
||||
@@ -4,19 +4,31 @@
|
||||
#include <string>
|
||||
|
||||
#include "../ctre.hpp"
|
||||
#include "Literal.hpp"
|
||||
#include "Result.hpp"
|
||||
#include "internal/StringLiteral.hpp"
|
||||
#include "parsing/schema/ValidationType.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
template <internal::StringLiteral _regex, internal::StringLiteral _name>
|
||||
struct PatternValidator {
|
||||
using Name = Literal<_name>;
|
||||
using Regex = Literal<_regex>;
|
||||
|
||||
static Result<std::string> validate(const std::string& _str) noexcept {
|
||||
if (ctre::match<_regex.arr_>(_str))
|
||||
if (ctre::match<_regex.arr_>(_str)) {
|
||||
return _str;
|
||||
else
|
||||
} else {
|
||||
return rfl::Error("String '" + _str + "' did not match format '" +
|
||||
_name.str() + "': '" + _regex.str() + "'.");
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
static parsing::schema::ValidationType to_schema() {
|
||||
using ValidationType = parsing::schema::ValidationType;
|
||||
return ValidationType{ValidationType::Regex{.pattern_ = Regex().str()}};
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -13,7 +13,8 @@
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// Used to define a field in the NamedTuple.
|
||||
/// Used to assign a new name to a field, which is different from the name
|
||||
/// inside the struct.
|
||||
template <internal::StringLiteral _name, class T>
|
||||
struct Rename {
|
||||
/// The underlying type.
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
#ifndef RFL_SIZE_HPP_
|
||||
#define RFL_SIZE_HPP_
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "Ref.hpp"
|
||||
#include "Result.hpp"
|
||||
#include "parsing/Parser.hpp"
|
||||
#include "parsing/schema/ValidationType.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
@@ -15,6 +20,11 @@ struct Size {
|
||||
};
|
||||
return V::validate(_t.size()).transform(to_t).or_else(embellish_error);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
static parsing::schema::ValidationType to_schema() {
|
||||
return V::template to_schema<size_t>();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
+13
-12
@@ -19,11 +19,10 @@ namespace rfl {
|
||||
template <class T, class V, class... Vs>
|
||||
requires internal::HasValidation<AllOf<V, Vs...>, T>
|
||||
struct Validator {
|
||||
private:
|
||||
using AllOfVs = std::conditional_t<sizeof...(Vs) == 0, V, AllOf<V, Vs...>>;
|
||||
|
||||
public:
|
||||
using ReflectionType = T;
|
||||
using ValidationType =
|
||||
std::conditional_t<sizeof...(Vs) == 0, V, AllOf<V, Vs...>>;
|
||||
|
||||
/// Exception-free validation.
|
||||
static Result<Validator<T, V, Vs...>> from_value(const T& _value) noexcept {
|
||||
@@ -34,36 +33,38 @@ struct Validator {
|
||||
}
|
||||
}
|
||||
|
||||
Validator() : value_(AllOfVs::validate(T()).value()) {}
|
||||
Validator() : value_(ValidationType::validate(T()).value()) {}
|
||||
|
||||
Validator(Validator<T, V, Vs...>&& _other) noexcept = default;
|
||||
|
||||
Validator(const Validator<T, V, Vs...>& _other) = default;
|
||||
|
||||
Validator(T&& _value) : value_(AllOfVs::validate(_value).value()) {}
|
||||
Validator(T&& _value) : value_(ValidationType::validate(_value).value()) {}
|
||||
|
||||
Validator(const T& _value) : value_(AllOfVs::validate(_value).value()) {}
|
||||
Validator(const T& _value)
|
||||
: value_(ValidationType::validate(_value).value()) {}
|
||||
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
Validator(U&& _value)
|
||||
: value_(AllOfVs::validate(T(std::forward<U>(_value))).value()) {}
|
||||
: value_(ValidationType::validate(T(std::forward<U>(_value))).value()) {}
|
||||
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
Validator(const U& _value) : value_(AllOfVs::validate(T(_value)).value()) {}
|
||||
Validator(const U& _value)
|
||||
: value_(ValidationType::validate(T(_value)).value()) {}
|
||||
|
||||
~Validator() = default;
|
||||
|
||||
/// Assigns the underlying object.
|
||||
auto& operator=(const T& _value) {
|
||||
value_ = AllOfVs::validate(_value).value();
|
||||
value_ = ValidationType::validate(_value).value();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
auto& operator=(T&& _value) {
|
||||
value_ = AllOfVs::validate(std::forward<T>(_value)).value();
|
||||
value_ = ValidationType::validate(std::forward<T>(_value)).value();
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -79,7 +80,7 @@ struct Validator {
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
auto& operator=(U&& _value) noexcept {
|
||||
value_ = AllOfVs::validate(T(std::forward<U>(_value))).value();
|
||||
value_ = ValidationType::validate(T(std::forward<U>(_value))).value();
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -87,7 +88,7 @@ struct Validator {
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
auto& operator=(const U& _value) {
|
||||
value_ = AllOfVs::validate(T(_value)).value();
|
||||
value_ = ValidationType::validate(T(_value)).value();
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
#ifndef RFL_COMPARISONS_HPP_
|
||||
#define RFL_COMPARISONS_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "Result.hpp"
|
||||
#include "parsing/schema/ValidationType.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
@@ -17,6 +20,16 @@ struct EqualTo {
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
static parsing::schema::ValidationType to_schema() {
|
||||
using ValidationType = parsing::schema::ValidationType;
|
||||
const auto value =
|
||||
std::is_floating_point_v<T>
|
||||
? std::variant<double, int>(static_cast<double>(_threshold))
|
||||
: std::variant<double, int>(static_cast<int>(_threshold));
|
||||
return ValidationType{ValidationType::EqualTo{.value_ = value}};
|
||||
}
|
||||
};
|
||||
|
||||
template <auto _threshold>
|
||||
@@ -31,6 +44,16 @@ struct Minimum {
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
static parsing::schema::ValidationType to_schema() {
|
||||
using ValidationType = parsing::schema::ValidationType;
|
||||
const auto value =
|
||||
std::is_floating_point_v<T>
|
||||
? std::variant<double, int>(static_cast<double>(_threshold))
|
||||
: std::variant<double, int>(static_cast<int>(_threshold));
|
||||
return ValidationType{ValidationType::Minimum{.value_ = value}};
|
||||
}
|
||||
};
|
||||
|
||||
template <auto _threshold>
|
||||
@@ -45,6 +68,16 @@ struct ExclusiveMinimum {
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
static parsing::schema::ValidationType to_schema() {
|
||||
using ValidationType = parsing::schema::ValidationType;
|
||||
const auto value =
|
||||
std::is_floating_point_v<T>
|
||||
? std::variant<double, int>(static_cast<double>(_threshold))
|
||||
: std::variant<double, int>(static_cast<int>(_threshold));
|
||||
return ValidationType{ValidationType::ExclusiveMinimum{.value_ = value}};
|
||||
}
|
||||
};
|
||||
|
||||
template <auto _threshold>
|
||||
@@ -59,6 +92,16 @@ struct Maximum {
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
static parsing::schema::ValidationType to_schema() {
|
||||
using ValidationType = parsing::schema::ValidationType;
|
||||
const auto value =
|
||||
std::is_floating_point_v<T>
|
||||
? std::variant<double, int>(static_cast<double>(_threshold))
|
||||
: std::variant<double, int>(static_cast<int>(_threshold));
|
||||
return ValidationType{ValidationType::Maximum{.value_ = value}};
|
||||
}
|
||||
};
|
||||
|
||||
template <auto _threshold>
|
||||
@@ -73,6 +116,16 @@ struct ExclusiveMaximum {
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
static parsing::schema::ValidationType to_schema() {
|
||||
using ValidationType = parsing::schema::ValidationType;
|
||||
const auto value =
|
||||
std::is_floating_point_v<T>
|
||||
? std::variant<double, int>(static_cast<double>(_threshold))
|
||||
: std::variant<double, int>(static_cast<int>(_threshold));
|
||||
return ValidationType{ValidationType::ExclusiveMaximum{.value_ = value}};
|
||||
}
|
||||
};
|
||||
|
||||
template <auto _threshold>
|
||||
@@ -87,6 +140,16 @@ struct NotEqualTo {
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
static parsing::schema::ValidationType to_schema() {
|
||||
using ValidationType = parsing::schema::ValidationType;
|
||||
const auto value =
|
||||
std::is_floating_point_v<T>
|
||||
? std::variant<double, int>(static_cast<double>(_threshold))
|
||||
: std::variant<double, int>(static_cast<int>(_threshold));
|
||||
return ValidationType{ValidationType::NotEqualTo{.value_ = value}};
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
@@ -8,11 +8,11 @@
|
||||
#include <type_traits>
|
||||
|
||||
#include "../../Result.hpp"
|
||||
#include "get_enum_names.hpp"
|
||||
#include "is_flag_enum.hpp"
|
||||
#include "../../internal/strings/join.hpp"
|
||||
#include "../../internal/strings/split.hpp"
|
||||
#include "../../type_name_t.hpp"
|
||||
#include "get_enum_names.hpp"
|
||||
#include "is_flag_enum.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
@@ -20,7 +20,7 @@ namespace enums {
|
||||
|
||||
template <class EnumType>
|
||||
class StringConverter {
|
||||
private:
|
||||
public:
|
||||
static constexpr bool is_flag_enum_ = is_flag_enum<EnumType>;
|
||||
|
||||
static constexpr auto names_ = get_enum_names<EnumType, is_flag_enum_>();
|
||||
|
||||
@@ -113,6 +113,7 @@ auto concat_literals(const Head& _head, const Tail&... _tail) {
|
||||
#ifdef __clang__
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wundefined-var-template"
|
||||
#pragma clang diagnostic ignored "-Wundefined-inline"
|
||||
#endif
|
||||
|
||||
template <class T>
|
||||
|
||||
@@ -21,14 +21,9 @@ class is_attribute : public std::false_type {};
|
||||
template <class Type>
|
||||
class is_attribute<Attribute<Type>> : public std::true_type {};
|
||||
|
||||
template <class Type>
|
||||
class is_attribute<Attribute<Type>*> : public std::true_type {};
|
||||
|
||||
template <class Type>
|
||||
class is_attribute<const Attribute<Type>*> : public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
constexpr bool is_attribute_v = is_attribute<std::remove_cvref_t<T>>::value;
|
||||
constexpr bool is_attribute_v =
|
||||
is_attribute<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
#ifndef RFL_INTERNAL_ISDESCRIPTION_HPP_
|
||||
#define RFL_INTERNAL_ISDESCRIPTION_HPP_
|
||||
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "../Description.hpp"
|
||||
#include "StringLiteral.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class T>
|
||||
class is_description;
|
||||
|
||||
template <class T>
|
||||
class is_description : public std::false_type {};
|
||||
|
||||
template <StringLiteral _name, class Type>
|
||||
class is_description<Description<_name, Type>> : public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
constexpr bool is_description_v =
|
||||
is_description<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -20,14 +20,9 @@ class is_field : public std::false_type {};
|
||||
template <StringLiteral _name, class Type>
|
||||
class is_field<Field<_name, Type>> : public std::true_type {};
|
||||
|
||||
template <StringLiteral _name, class Type>
|
||||
class is_field<Field<_name, Type>*> : public std::true_type {};
|
||||
|
||||
template <StringLiteral _name, class Type>
|
||||
class is_field<const Field<_name, Type>*> : public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
constexpr bool is_field_v = is_field<T>::value;
|
||||
constexpr bool is_field_v =
|
||||
is_field<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
@@ -20,13 +20,8 @@ template <class T>
|
||||
class is_flatten_field<Flatten<T>> : public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
class is_flatten_field<Flatten<T>*> : public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
class is_flatten_field<const Flatten<T>*> : public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
constexpr bool is_flatten_field_v = is_flatten_field<T>::value;
|
||||
constexpr bool is_flatten_field_v =
|
||||
is_flatten_field<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#ifndef RFL_INTERNAL_ISLITERAL_HPP_
|
||||
#define RFL_INTERNAL_ISLITERAL_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Literal.hpp"
|
||||
#include "StringLiteral.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class T>
|
||||
class is_literal;
|
||||
|
||||
template <class T>
|
||||
class is_literal : public std::false_type {};
|
||||
|
||||
template <StringLiteral... _s>
|
||||
class is_literal<Literal<_s...>> : public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
constexpr bool is_literal_v =
|
||||
is_literal<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -18,7 +18,8 @@ template <class... Fields>
|
||||
class is_named_tuple<NamedTuple<Fields...>> : public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
constexpr bool is_named_tuple_v = is_named_tuple<T>::value;
|
||||
constexpr bool is_named_tuple_v =
|
||||
is_named_tuple<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#ifndef RFL_INTERNAL_ISPATTERN_HPP_
|
||||
#define RFL_INTERNAL_ISPATTERN_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Pattern.hpp"
|
||||
#include "StringLiteral.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class T>
|
||||
class is_pattern;
|
||||
|
||||
template <class T>
|
||||
class is_pattern : public std::false_type {};
|
||||
|
||||
template <StringLiteral _regex, internal::StringLiteral _name>
|
||||
class is_pattern<Pattern<_regex, _name>> : public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
constexpr bool is_pattern_v =
|
||||
is_pattern<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -20,14 +20,9 @@ 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;
|
||||
constexpr bool is_rename_v =
|
||||
is_rename<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#ifndef RFL_INTERNAL_ISVALIDATOR_HPP_
|
||||
#define RFL_INTERNAL_ISVALIDATOR_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Validator.hpp"
|
||||
#include "StringLiteral.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class T>
|
||||
class is_validator;
|
||||
|
||||
template <class T>
|
||||
class is_validator : public std::false_type {};
|
||||
|
||||
template <class T, class V, class... Vs>
|
||||
class is_validator<Validator<T, V, Vs...>> : public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
constexpr bool is_validator_v =
|
||||
is_validator<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef RFL_INTERNAL_ISVARIANT_HPP_
|
||||
#define RFL_INTERNAL_ISVARIANT_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
#include <variant>
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class T>
|
||||
class is_variant;
|
||||
|
||||
template <class T>
|
||||
class is_variant : public std::false_type {};
|
||||
|
||||
template <class... T>
|
||||
class is_variant<std::variant<T...>> : public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
constexpr bool is_variant_v =
|
||||
is_variant<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -39,6 +39,12 @@ This is the purpose of get_nested_array_size().
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef __clang__
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wundefined-var-template"
|
||||
#pragma clang diagnostic ignored "-Wundefined-inline"
|
||||
#endif
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
@@ -114,4 +120,8 @@ constexpr std::size_t num_fields = CountFieldsHelper<T>::count_fields();
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#ifdef __clang__
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "json/load.hpp"
|
||||
#include "json/read.hpp"
|
||||
#include "json/save.hpp"
|
||||
#include "json/to_schema.hpp"
|
||||
#include "json/write.hpp"
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef RFL_JSON_SAVE_HPP_
|
||||
#define RFL_JSON_SAVE_HPP_
|
||||
|
||||
#include <yyjson.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef RFL_JSON_SCHEMA_JSONSCHEMA_HPP_
|
||||
#define RFL_JSON_SCHEMA_JSONSCHEMA_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
#include "../../Flatten.hpp"
|
||||
#include "../../Literal.hpp"
|
||||
#include "../../Rename.hpp"
|
||||
#include "Type.hpp"
|
||||
|
||||
namespace rfl::json::schema {
|
||||
|
||||
template <class T>
|
||||
struct JSONSchema {
|
||||
Rename<"$schema", Literal<"https://json-schema.org/draft/2020-12/schema">>
|
||||
schema;
|
||||
Flatten<T> root;
|
||||
std::map<std::string, Type> definitions;
|
||||
};
|
||||
|
||||
} // namespace rfl::json::schema
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,147 @@
|
||||
#ifndef RFL_JSON_SCHEMA_TYPE_HPP_
|
||||
#define RFL_JSON_SCHEMA_TYPE_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
#include "../../Literal.hpp"
|
||||
#include "../../Rename.hpp"
|
||||
|
||||
namespace rfl::json::schema {
|
||||
|
||||
/// The JSON representation of internal::schema::Type.
|
||||
struct Type {
|
||||
struct Boolean {
|
||||
std::optional<std::string> description;
|
||||
Literal<"boolean"> type;
|
||||
};
|
||||
|
||||
struct Integer {
|
||||
Literal<"integer"> type;
|
||||
std::optional<std::string> description;
|
||||
};
|
||||
|
||||
struct Number {
|
||||
Literal<"number"> type;
|
||||
std::optional<std::string> description;
|
||||
};
|
||||
|
||||
struct String {
|
||||
Literal<"string"> type;
|
||||
std::optional<std::string> description;
|
||||
};
|
||||
|
||||
using NumericType = std::variant<Integer, Number>;
|
||||
|
||||
struct AllOf {
|
||||
std::optional<std::string> description;
|
||||
std::vector<Type> allOf;
|
||||
};
|
||||
|
||||
struct AnyOf {
|
||||
std::optional<std::string> description;
|
||||
std::vector<Type> anyOf;
|
||||
};
|
||||
|
||||
struct ExclusiveMaximum {
|
||||
std::optional<std::string> description;
|
||||
std::variant<double, int> exclusiveMaximum;
|
||||
std::string type;
|
||||
};
|
||||
|
||||
struct ExclusiveMinimum {
|
||||
std::optional<std::string> description;
|
||||
std::variant<double, int> exclusiveMinimum;
|
||||
std::string type;
|
||||
};
|
||||
|
||||
struct FixedSizeTypedArray {
|
||||
Literal<"array"> type;
|
||||
std::optional<std::string> description;
|
||||
rfl::Ref<Type> items;
|
||||
size_t minContains;
|
||||
size_t maxContains;
|
||||
};
|
||||
|
||||
struct Maximum {
|
||||
std::optional<std::string> description;
|
||||
std::variant<double, int> maximum;
|
||||
std::string type;
|
||||
};
|
||||
|
||||
struct Minimum {
|
||||
std::optional<std::string> description;
|
||||
std::variant<double, int> minimum;
|
||||
std::string type;
|
||||
};
|
||||
|
||||
struct Null {
|
||||
Literal<"null"> type;
|
||||
std::optional<std::string> description;
|
||||
};
|
||||
|
||||
struct Object {
|
||||
Literal<"object"> type;
|
||||
std::optional<std::string> description;
|
||||
std::map<std::string, Type> properties;
|
||||
std::vector<std::string> required;
|
||||
};
|
||||
|
||||
struct OneOf {
|
||||
std::optional<std::string> description;
|
||||
std::vector<Type> oneOf;
|
||||
};
|
||||
|
||||
struct Reference {
|
||||
Rename<"$ref", std::optional<std::string>> ref;
|
||||
std::optional<std::string> description;
|
||||
};
|
||||
|
||||
struct Regex {
|
||||
Literal<"string"> type;
|
||||
std::optional<std::string> description;
|
||||
std::string pattern;
|
||||
};
|
||||
|
||||
struct StringEnum {
|
||||
Literal<"string"> type;
|
||||
std::optional<std::string> description;
|
||||
rfl::Rename<"enum", std::vector<std::string>> values;
|
||||
};
|
||||
|
||||
struct StringMap {
|
||||
Literal<"object"> type;
|
||||
std::optional<std::string> description;
|
||||
rfl::Ref<Type> additionalProperties;
|
||||
};
|
||||
|
||||
struct Tuple {
|
||||
Literal<"array"> type;
|
||||
std::optional<std::string> description;
|
||||
std::vector<Type> prefixItems;
|
||||
bool items = false;
|
||||
};
|
||||
|
||||
struct TypedArray {
|
||||
Literal<"array"> type;
|
||||
std::optional<std::string> description;
|
||||
rfl::Ref<Type> items;
|
||||
};
|
||||
|
||||
using ReflectionType =
|
||||
std::variant<AllOf, AnyOf, Boolean, ExclusiveMaximum, ExclusiveMinimum,
|
||||
FixedSizeTypedArray, Integer, Maximum, Minimum, Number, Null,
|
||||
Object, OneOf, Reference, Regex, String, StringEnum,
|
||||
StringMap, Tuple, TypedArray>;
|
||||
|
||||
const auto& reflection() const { return value; }
|
||||
|
||||
ReflectionType value;
|
||||
};
|
||||
|
||||
} // namespace rfl::json::schema
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,260 @@
|
||||
#ifndef RFL_JSON_TOSCHEMA_HPP_
|
||||
#define RFL_JSON_TOSCHEMA_HPP_
|
||||
|
||||
#include <yyjson.h>
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <variant>
|
||||
|
||||
#include "../Literal.hpp"
|
||||
#include "../parsing/schema/Type.hpp"
|
||||
#include "../parsing/schema/ValidationType.hpp"
|
||||
#include "../parsing/schema/make.hpp"
|
||||
#include "Reader.hpp"
|
||||
#include "Writer.hpp"
|
||||
#include "schema/JSONSchema.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
#include "write.hpp"
|
||||
|
||||
namespace rfl::json {
|
||||
|
||||
inline schema::Type type_to_json_schema_type(
|
||||
const parsing::schema::Type& _type);
|
||||
|
||||
inline bool is_optional(const parsing::schema::Type& _t) {
|
||||
const auto handle = [](const auto& _v) -> bool {
|
||||
using T = std::remove_cvref_t<decltype(_v)>;
|
||||
return std::is_same<T, parsing::schema::Type::Optional>();
|
||||
};
|
||||
return std::visit(handle, _t.variant_);
|
||||
}
|
||||
|
||||
inline std::string numeric_type_to_string(const parsing::schema::Type& _type) {
|
||||
const auto handle_variant = [](const auto& _t) -> std::string {
|
||||
using T = std::remove_cvref_t<decltype(_t)>;
|
||||
using Type = parsing::schema::Type;
|
||||
if constexpr (std::is_same<T, Type::Int32>() ||
|
||||
std::is_same<T, Type::Int64>() ||
|
||||
std::is_same<T, Type::UInt32>() ||
|
||||
std::is_same<T, Type::UInt64>() ||
|
||||
std::is_same<T, Type::Integer>()) {
|
||||
return schema::Type::Integer{}.type.str();
|
||||
} else {
|
||||
return schema::Type::Number{}.type.str();
|
||||
}
|
||||
};
|
||||
return std::visit(handle_variant, _type.variant_);
|
||||
}
|
||||
|
||||
inline schema::Type handle_validation_type(
|
||||
const parsing::schema::Type& _type,
|
||||
const parsing::schema::ValidationType& _validation_type) {
|
||||
const auto handle_variant = [&](const auto& _v) -> schema::Type {
|
||||
using T = std::remove_cvref_t<decltype(_v)>;
|
||||
using ValidationType = parsing::schema::ValidationType;
|
||||
if constexpr (std::is_same<T, ValidationType::AllOf>()) {
|
||||
auto all_of = std::vector<schema::Type>();
|
||||
for (const auto& t : _v.types_) {
|
||||
all_of.emplace_back(handle_validation_type(_type, t));
|
||||
}
|
||||
return schema::Type{.value = schema::Type::AllOf{.allOf = all_of}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::AnyOf>()) {
|
||||
auto any_of = std::vector<schema::Type>();
|
||||
for (const auto& t : _v.types_) {
|
||||
any_of.emplace_back(handle_validation_type(_type, t));
|
||||
}
|
||||
return schema::Type{.value = schema::Type::AnyOf{.anyOf = any_of}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::OneOf>()) {
|
||||
auto one_of = std::vector<schema::Type>();
|
||||
for (const auto& t : _v.types_) {
|
||||
one_of.emplace_back(handle_validation_type(_type, t));
|
||||
}
|
||||
return schema::Type{.value = schema::Type::OneOf{.oneOf = one_of}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::Regex>()) {
|
||||
return schema::Type{.value = schema::Type::Regex{.pattern = _v.pattern_}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::Size>()) {
|
||||
return type_to_json_schema_type(_type);
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::ExclusiveMaximum>()) {
|
||||
return schema::Type{.value = schema::Type::ExclusiveMaximum{
|
||||
.exclusiveMaximum = _v.value_,
|
||||
.type = numeric_type_to_string(_type)}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::ExclusiveMinimum>()) {
|
||||
return schema::Type{.value = schema::Type::ExclusiveMinimum{
|
||||
.exclusiveMinimum = _v.value_,
|
||||
.type = numeric_type_to_string(_type)}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::Maximum>()) {
|
||||
return schema::Type{
|
||||
.value = schema::Type::Maximum{
|
||||
.maximum = _v.value_, .type = numeric_type_to_string(_type)}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::Minimum>()) {
|
||||
return schema::Type{
|
||||
.value = schema::Type::Minimum{
|
||||
.minimum = _v.value_, .type = numeric_type_to_string(_type)}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::EqualTo>()) {
|
||||
const auto maximum = schema::Type{
|
||||
.value = schema::Type::Maximum{
|
||||
.maximum = _v.value_, .type = numeric_type_to_string(_type)}};
|
||||
const auto minimum = schema::Type{
|
||||
.value = schema::Type::Minimum{
|
||||
.minimum = _v.value_, .type = numeric_type_to_string(_type)}};
|
||||
return schema::Type{.value =
|
||||
schema::Type::AllOf{.allOf = {maximum, minimum}}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::NotEqualTo>()) {
|
||||
const auto excl_maximum =
|
||||
schema::Type{.value = schema::Type::ExclusiveMaximum{
|
||||
.exclusiveMaximum = _v.value_,
|
||||
.type = numeric_type_to_string(_type)}};
|
||||
const auto excl_minimum =
|
||||
schema::Type{.value = schema::Type::ExclusiveMinimum{
|
||||
.exclusiveMinimum = _v.value_,
|
||||
.type = numeric_type_to_string(_type)}};
|
||||
return schema::Type{
|
||||
.value = schema::Type::AnyOf{.anyOf = {excl_maximum, excl_minimum}}};
|
||||
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Not all cases were covered.");
|
||||
}
|
||||
};
|
||||
|
||||
return std::visit(handle_variant, _validation_type.variant_);
|
||||
}
|
||||
|
||||
inline schema::Type type_to_json_schema_type(
|
||||
const parsing::schema::Type& _type) {
|
||||
const auto handle_variant = [](const auto& _t) -> schema::Type {
|
||||
using T = std::remove_cvref_t<decltype(_t)>;
|
||||
using Type = parsing::schema::Type;
|
||||
if constexpr (std::is_same<T, Type::Boolean>()) {
|
||||
return schema::Type{.value = schema::Type::Boolean{}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Int32>() ||
|
||||
std::is_same<T, Type::Int64>() ||
|
||||
std::is_same<T, Type::UInt32>() ||
|
||||
std::is_same<T, Type::UInt64>() ||
|
||||
std::is_same<T, Type::Integer>()) {
|
||||
return schema::Type{.value = schema::Type::Integer{}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Float>() ||
|
||||
std::is_same<T, Type::Double>()) {
|
||||
return schema::Type{.value = schema::Type::Number{}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::String>()) {
|
||||
return schema::Type{.value = schema::Type::String{}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::AnyOf>()) {
|
||||
auto any_of = std::vector<schema::Type>();
|
||||
for (const auto& t : _t.types_) {
|
||||
any_of.emplace_back(type_to_json_schema_type(t));
|
||||
}
|
||||
return schema::Type{.value = schema::Type::AnyOf{.anyOf = any_of}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Description>()) {
|
||||
auto res = type_to_json_schema_type(*_t.type_);
|
||||
const auto update_prediction = [&](auto _v) -> schema::Type {
|
||||
_v.description = _t.description_;
|
||||
return schema::Type{_v};
|
||||
};
|
||||
return std::visit(update_prediction, res.value);
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::FixedSizeTypedArray>()) {
|
||||
return schema::Type{.value = schema::Type::FixedSizeTypedArray{
|
||||
.items = Ref<schema::Type>::make(
|
||||
type_to_json_schema_type(*_t.type_)),
|
||||
.minContains = _t.size_,
|
||||
.maxContains = _t.size_}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Literal>()) {
|
||||
return schema::Type{.value =
|
||||
schema::Type::StringEnum{.values = _t.values_}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Object>()) {
|
||||
auto properties = std::map<std::string, schema::Type>();
|
||||
auto required = std::vector<std::string>();
|
||||
for (const auto& [k, v] : _t.types_) {
|
||||
properties[k] = type_to_json_schema_type(v);
|
||||
if (!is_optional(v)) {
|
||||
required.push_back(k);
|
||||
}
|
||||
}
|
||||
return schema::Type{.value = schema::Type::Object{
|
||||
.properties = properties, .required = required}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Optional>()) {
|
||||
return schema::Type{.value = schema::Type::AnyOf{
|
||||
.anyOf = {type_to_json_schema_type(*_t.type_),
|
||||
schema::Type{schema::Type::Null{}}}}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Reference>()) {
|
||||
return schema::Type{
|
||||
.value = schema::Type::Reference{.ref = "#/definitions/" + _t.name_}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::StringMap>()) {
|
||||
return schema::Type{.value = schema::Type::StringMap{
|
||||
.additionalProperties = Ref<schema::Type>::make(
|
||||
type_to_json_schema_type(*_t.value_type_))}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Tuple>()) {
|
||||
auto items = std::vector<schema::Type>();
|
||||
for (const auto& t : _t.types_) {
|
||||
items.emplace_back(type_to_json_schema_type(t));
|
||||
}
|
||||
return schema::Type{.value = schema::Type::Tuple{.prefixItems = items}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::TypedArray>()) {
|
||||
return schema::Type{.value = schema::Type::TypedArray{
|
||||
.items = Ref<schema::Type>::make(
|
||||
type_to_json_schema_type(*_t.type_))}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Validated>()) {
|
||||
return handle_validation_type(*_t.type_, _t.validation_);
|
||||
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Not all cases were covered.");
|
||||
}
|
||||
};
|
||||
|
||||
return std::visit(handle_variant, _type.variant_);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
struct TypeHelper {};
|
||||
|
||||
template <class... Ts>
|
||||
struct TypeHelper<std::variant<Ts...> > {
|
||||
using JSONSchemaType = std::variant<schema::JSONSchema<Ts>...>;
|
||||
};
|
||||
|
||||
/// Returns the JSON schema for a class.
|
||||
template <class T>
|
||||
std::string to_schema(const yyjson_write_flag _flag = 0) {
|
||||
const auto internal_schema = parsing::schema::make<Reader, Writer, T>();
|
||||
auto definitions = std::map<std::string, schema::Type>();
|
||||
for (const auto& [k, v] : internal_schema.definitions_) {
|
||||
definitions[k] = type_to_json_schema_type(v);
|
||||
}
|
||||
using JSONSchemaType =
|
||||
typename TypeHelper<schema::Type::ReflectionType>::JSONSchemaType;
|
||||
const auto to_schema = [&](auto&& _root) -> JSONSchemaType {
|
||||
using U = std::decay_t<decltype(_root)>;
|
||||
return schema::JSONSchema<U>{.root = std::move(_root),
|
||||
.definitions = definitions};
|
||||
};
|
||||
auto root = type_to_json_schema_type(internal_schema.root_);
|
||||
const auto json_schema = std::visit(to_schema, std::move(root.value));
|
||||
return write(json_schema, _flag);
|
||||
}
|
||||
} // namespace rfl::json
|
||||
|
||||
#endif
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef RFL_PARSING_FIELD_VARIANT_PARSER_HPP_
|
||||
#define RFL_PARSING_FIELD_VARIANT_PARSER_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
@@ -9,6 +10,7 @@
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
@@ -68,6 +70,13 @@ struct FieldVariantParser {
|
||||
std::visit(handle, _v);
|
||||
}
|
||||
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions,
|
||||
std::vector<schema::Type> _types = {}) {
|
||||
using VariantType = std::variant<NamedTuple<FieldTypes>...>;
|
||||
return Parser<R, W, VariantType>::to_schema(_definitions);
|
||||
}
|
||||
|
||||
private:
|
||||
template <int _i = 0>
|
||||
static ResultType find_matching_alternative(
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
#ifndef RFL_PARSING_MAPPARSER_HPP_
|
||||
#define RFL_PARSING_MAPPARSER_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
@@ -67,6 +70,12 @@ struct MapParser {
|
||||
_w.end_object(&obj);
|
||||
}
|
||||
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
return schema::Type{schema::Type::StringMap{Ref<schema::Type>::make(
|
||||
Parser<R, W, ValueType>::to_schema(_definitions))}};
|
||||
}
|
||||
|
||||
private:
|
||||
template <class T>
|
||||
static Result<T> key_to_numeric(auto& _pair) noexcept {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef RFL_PARSING_NAMEDTUPLEPARSER_HPP_
|
||||
#define RFL_PARSING_NAMEDTUPLEPARSER_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
@@ -17,6 +18,7 @@
|
||||
#include "Parser_base.hpp"
|
||||
#include "is_empty.hpp"
|
||||
#include "is_required.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
@@ -58,6 +60,23 @@ struct NamedTupleParser {
|
||||
_w.end_object(&obj);
|
||||
}
|
||||
|
||||
template <size_t _i = 0>
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions,
|
||||
std::map<std::string, schema::Type> _values = {}) {
|
||||
using Type = schema::Type;
|
||||
using T = NamedTuple<FieldTypes...>;
|
||||
constexpr size_t size = T::size();
|
||||
if constexpr (_i == size) {
|
||||
return Type{Type::Object{_values}};
|
||||
} else {
|
||||
using F = std::tuple_element_t<_i, typename T::Fields>;
|
||||
_values[std::string(F::name())] =
|
||||
Parser<R, W, typename F::Type>::to_schema(_definitions);
|
||||
return to_schema<_i + 1>(_definitions, _values);
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
/// Builds the named tuple field by field.
|
||||
template <class... Args>
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include "Parser_rfl_array.hpp"
|
||||
#include "Parser_rfl_variant.hpp"
|
||||
#include "Parser_shared_ptr.hpp"
|
||||
#include "Parser_string_view.hpp"
|
||||
#include "Parser_tagged_union.hpp"
|
||||
#include "Parser_tuple.hpp"
|
||||
#include "Parser_unique_ptr.hpp"
|
||||
|
||||
@@ -2,13 +2,16 @@
|
||||
#define RFL_PARSING_PARSER_ARRAY_HPP_
|
||||
|
||||
#include <array>
|
||||
#include <map>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
@@ -57,6 +60,15 @@ struct Parser<R, W, std::array<T, _size>> {
|
||||
_w.end_array(&arr);
|
||||
}
|
||||
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
using U = std::remove_cvref_t<T>;
|
||||
return schema::Type{schema::Type::FixedSizeTypedArray{
|
||||
.size_ = _size,
|
||||
.type_ =
|
||||
Ref<schema::Type>::make(Parser<R, W, U>::to_schema(_definitions))}};
|
||||
}
|
||||
|
||||
private:
|
||||
/// Extracts values from the array, field by field.
|
||||
template <class... AlreadyExtracted>
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
#ifndef RFL_PARSING_PARSER_BOX_HPP_
|
||||
#define RFL_PARSING_PARSER_BOX_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Box.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
@@ -27,6 +29,11 @@ struct Parser<R, W, Box<T>> {
|
||||
const P& _parent) noexcept {
|
||||
Parser<R, W, std::remove_cvref_t<T>>::write(_w, *_box, _parent);
|
||||
}
|
||||
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
return Parser<R, W, std::remove_cvref_t<T>>::to_schema(_definitions);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef RFL_PARSING_PARSER_C_ARRAY_HPP_
|
||||
#define RFL_PARSING_PARSER_C_ARRAY_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Result.hpp"
|
||||
@@ -10,6 +11,7 @@
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_array.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
@@ -43,6 +45,12 @@ struct Parser<R, W, T[_size]> {
|
||||
}
|
||||
_w.end_array(&arr);
|
||||
}
|
||||
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
using StdArray = internal::to_std_array_t<CArray>;
|
||||
return Parser<R, W, std::remove_cvref_t<StdArray>>::to_schema(_definitions);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef RFL_PARSING_PARSER_DEFAULT_HPP_
|
||||
#define RFL_PARSING_PARSER_DEFAULT_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
|
||||
@@ -11,10 +12,16 @@
|
||||
#include "../internal/has_reflection_method_v.hpp"
|
||||
#include "../internal/has_reflection_type_v.hpp"
|
||||
#include "../internal/is_basic_type.hpp"
|
||||
#include "../internal/is_description.hpp"
|
||||
#include "../internal/is_literal.hpp"
|
||||
#include "../internal/is_validator.hpp"
|
||||
#include "../internal/to_ptr_named_tuple.hpp"
|
||||
#include "../type_name_t.hpp"
|
||||
#include "AreReaderAndWriter.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "is_tagged_union_wrapper.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
@@ -96,6 +103,127 @@ struct Parser {
|
||||
"support for your own classes.");
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates a schema for the underlying type.
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
using U = std::remove_cvref_t<T>;
|
||||
using Type = schema::Type;
|
||||
if constexpr (std::is_same<U, bool>()) {
|
||||
return Type{Type::Boolean{}};
|
||||
|
||||
} else if constexpr (std::is_same<U, std::int32_t>()) {
|
||||
return Type{Type::Int32{}};
|
||||
|
||||
} else if constexpr (std::is_same<U, std::int64_t>()) {
|
||||
return Type{Type::Int64{}};
|
||||
|
||||
} else if constexpr (std::is_same<U, std::uint32_t>()) {
|
||||
return Type{Type::UInt32{}};
|
||||
|
||||
} else if constexpr (std::is_same<U, std::uint64_t>()) {
|
||||
return Type{Type::UInt64{}};
|
||||
|
||||
} else if constexpr (std::is_integral<U>()) {
|
||||
return Type{Type::Integer{}};
|
||||
|
||||
} else if constexpr (std::is_same<U, float>()) {
|
||||
return Type{Type::Float{}};
|
||||
|
||||
} else if constexpr (std::is_floating_point_v<U>) {
|
||||
return Type{Type::Double{}};
|
||||
|
||||
} else if constexpr (std::is_same<U, std::string>()) {
|
||||
return Type{Type::String{}};
|
||||
|
||||
} else if constexpr (rfl::internal::is_description_v<U>) {
|
||||
return make_description<U>(_definitions);
|
||||
|
||||
} else if constexpr (std::is_enum_v<U>) {
|
||||
return make_enum<U>(_definitions);
|
||||
|
||||
} else if constexpr (std::is_class_v<U> && std::is_aggregate_v<U>) {
|
||||
return make_reference<U>(_definitions);
|
||||
|
||||
} else if constexpr (internal::is_literal_v<U>) {
|
||||
return Type{Type::Literal{.values_ = U::strings()}};
|
||||
|
||||
} else if constexpr (internal::is_validator_v<U>) {
|
||||
return make_validated<U>(_definitions);
|
||||
|
||||
} else if constexpr (internal::has_reflection_type_v<U>) {
|
||||
return Parser<R, W, typename U::ReflectionType>::to_schema(_definitions);
|
||||
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<U>, "Unsupported type.");
|
||||
}
|
||||
}
|
||||
|
||||
template <class U>
|
||||
static schema::Type make_description(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
using Type = schema::Type;
|
||||
return Type{Type::Description{
|
||||
.description_ = typename U::Content().str(),
|
||||
.type_ = Ref<Type>::make(
|
||||
Parser<R, W, std::remove_cvref_t<typename U::Type>>::to_schema(
|
||||
_definitions))}};
|
||||
}
|
||||
|
||||
template <class U>
|
||||
static schema::Type make_enum(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
using Type = schema::Type;
|
||||
using S = internal::enums::StringConverter<U>;
|
||||
if constexpr (S::is_flag_enum_) {
|
||||
return Type{Type::String{}};
|
||||
} else {
|
||||
return Parser<R, W, typename S::NamesLiteral>::to_schema(_definitions);
|
||||
}
|
||||
}
|
||||
|
||||
template <class U>
|
||||
static schema::Type make_reference(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
using Type = schema::Type;
|
||||
const auto name = make_type_name<U>();
|
||||
if (_definitions->find(name) == _definitions->end()) {
|
||||
(*_definitions)[name] =
|
||||
Type{Type::Integer{}}; // Placeholder to avoid infinite loop.
|
||||
(*_definitions)[name] =
|
||||
Parser<R, W, named_tuple_t<U>>::to_schema(_definitions);
|
||||
}
|
||||
return Type{Type::Reference{name}};
|
||||
}
|
||||
|
||||
template <class U>
|
||||
static schema::Type make_validated(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
using Type = schema::Type;
|
||||
using ReflectionType = std::remove_cvref_t<typename U::ReflectionType>;
|
||||
using ValidationType = std::remove_cvref_t<typename U::ValidationType>;
|
||||
return Type{Type::Validated{
|
||||
.type_ = Ref<Type>::make(
|
||||
Parser<R, W, ReflectionType>::to_schema(_definitions)),
|
||||
.validation_ = ValidationType::template to_schema<ReflectionType>()}};
|
||||
}
|
||||
|
||||
template <class U>
|
||||
static std::string make_type_name() {
|
||||
if constexpr (is_tagged_union_wrapper_v<U>) {
|
||||
return replace_non_alphanumeric(type_name_t<typename U::Type>().str() +
|
||||
"__tagged");
|
||||
} else {
|
||||
return replace_non_alphanumeric(type_name_t<U>().str());
|
||||
}
|
||||
}
|
||||
|
||||
static std::string replace_non_alphanumeric(std::string _str) {
|
||||
for (auto& ch : _str) {
|
||||
ch = std::isalnum(ch) ? ch : '_';
|
||||
}
|
||||
return _str;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
#ifndef RFL_PARSING_PARSER_OPTIONAL_HPP_
|
||||
#define RFL_PARSING_PARSER_OPTIONAL_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
@@ -39,6 +42,13 @@ struct Parser<R, W, std::optional<T>> {
|
||||
}
|
||||
Parser<R, W, std::remove_cvref_t<T>>::write(_w, *_o, _parent);
|
||||
}
|
||||
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
using U = std::remove_cvref_t<T>;
|
||||
return schema::Type{schema::Type::Optional{
|
||||
Ref<schema::Type>::make(Parser<R, W, U>::to_schema(_definitions))}};
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef RFL_PARSING_PARSER_PAIR_HPP_
|
||||
#define RFL_PARSING_PARSER_PAIR_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
@@ -8,6 +9,7 @@
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
@@ -36,6 +38,12 @@ struct Parser<R, W, std::pair<FirstType, SecondType>> {
|
||||
Parser<R, W, std::tuple<const FirstType*, const SecondType*>>::write(
|
||||
_w, tup, _parent);
|
||||
}
|
||||
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
return Parser<R, W, std::tuple<FirstType, SecondType>>::to_schema(
|
||||
_definitions);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
#ifndef RFL_PARSING_PARSER_PTR_HPP_
|
||||
#define RFL_PARSING_PARSER_PTR_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
@@ -38,6 +41,11 @@ struct Parser<R, W, T*> {
|
||||
}
|
||||
Parser<R, W, std::remove_cvref_t<T>>::write(_w, *_ptr, _parent);
|
||||
}
|
||||
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
return Parser<R, W, std::remove_cvref_t<T>>::to_schema(_definitions);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
#ifndef RFL_PARSING_PARSER_REF_HPP_
|
||||
#define RFL_PARSING_PARSER_REF_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
@@ -28,6 +30,11 @@ struct Parser<R, W, Ref<T>> {
|
||||
const P& _parent) noexcept {
|
||||
Parser<R, W, std::remove_cvref_t<T>>::write(_w, *_ref, _parent);
|
||||
}
|
||||
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
return Parser<R, W, std::remove_cvref_t<T>>::to_schema(_definitions);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -2,11 +2,13 @@
|
||||
#define RFL_PARSING_PARSER_REFERENCE_WRAPPER_HPP_
|
||||
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
@@ -33,6 +35,11 @@ struct Parser<R, W, std::reference_wrapper<T>> {
|
||||
const P& _p) noexcept {
|
||||
Parser<R, W, std::remove_cvref_t<T>>::write(_w, _ref.get(), _p);
|
||||
}
|
||||
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
return Parser<R, W, std::remove_cvref_t<T>>::to_schema(_definitions);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef RFL_PARSING_PARSER_RENAME_HPP_
|
||||
#define RFL_PARSING_PARSER_RENAME_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Rename.hpp"
|
||||
@@ -8,6 +9,7 @@
|
||||
#include "../always_false.hpp"
|
||||
#include "../internal/StringLiteral.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
@@ -32,6 +34,11 @@ struct Parser<R, W, Rename<_name, T>> {
|
||||
const P& _parent) noexcept {
|
||||
Parser<R, W, std::remove_cvref_t<T>>::write(_w, _rename.value(), _parent);
|
||||
}
|
||||
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
return Parser<R, W, std::remove_cvref_t<T>>::to_schema(_definitions);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
#ifndef RFL_PARSING_PARSER_RESULT_HPP_
|
||||
#define RFL_PARSING_PARSER_RESULT_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "../internal/StringLiteral.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
@@ -55,6 +57,11 @@ struct Parser<R, W, Result<T>> {
|
||||
|
||||
_r.transform(write_t).or_else(write_err);
|
||||
}
|
||||
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
return Parser<R, W, std::remove_cvref_t<T>>::to_schema(_definitions);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef RFL_PARSING_PARSER_RFL_ARRAY_HPP_
|
||||
#define RFL_PARSING_PARSER_RFL_ARRAY_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Result.hpp"
|
||||
@@ -9,6 +10,7 @@
|
||||
#include "../internal/to_std_array.hpp"
|
||||
#include "Parser_array.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
@@ -35,6 +37,11 @@ struct Parser<R, W, internal::Array<T>> {
|
||||
const P& _parent) noexcept {
|
||||
Parser<R, W, StdArray>::write(_w, _arr.arr_, _parent);
|
||||
}
|
||||
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
return Parser<R, W, StdArray>::to_schema(_definitions);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef RFL_PARSING_PARSER_RFL_VARIANT_HPP_
|
||||
#define RFL_PARSING_PARSER_RFL_VARIANT_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <type_traits>
|
||||
#include <variant>
|
||||
|
||||
@@ -9,6 +10,7 @@
|
||||
#include "../always_false.hpp"
|
||||
#include "FieldVariantParser.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
@@ -36,6 +38,11 @@ struct Parser<R, W, rfl::Variant<FieldTypes...>> {
|
||||
Parser<R, W, std::variant<FieldTypes...>>::write(_w, _variant.variant(),
|
||||
_parent);
|
||||
}
|
||||
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
return Parser<R, W, std::variant<FieldTypes...>>::to_schema(_definitions);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
#ifndef RFL_PARSING_PARSER_SHARED_PTR_HPP_
|
||||
#define RFL_PARSING_PARSER_SHARED_PTR_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
@@ -33,14 +36,21 @@ struct Parser<R, W, std::shared_ptr<T>> {
|
||||
}
|
||||
|
||||
template <class P>
|
||||
static OutputVarType write(const W& _w, const std::shared_ptr<T>& _s,
|
||||
const P& _parent) noexcept {
|
||||
static void write(const W& _w, const std::shared_ptr<T>& _s,
|
||||
const P& _parent) noexcept {
|
||||
if (!_s) {
|
||||
ParentType::add_null(_w, _parent);
|
||||
return;
|
||||
}
|
||||
Parser<R, W, std::remove_cvref_t<T>>::write(_w, *_s, _parent);
|
||||
}
|
||||
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
using U = std::remove_cvref_t<T>;
|
||||
return schema::Type{schema::Type::Optional{
|
||||
Ref<schema::Type>::make(Parser<R, W, U>::to_schema(_definitions))}};
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
#ifndef RFL_PARSING_PARSER_STRING_VIEW_HPP_
|
||||
#define RFL_PARSING_PARSER_STRING_VIEW_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class R, class W>
|
||||
requires AreReaderAndWriter<R, W, std::string_view>
|
||||
struct Parser<R, W, std::string_view> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
static Result<std::string_view> read(const R& _r,
|
||||
const InputVarType& _var) noexcept {
|
||||
static_assert(always_false_v<R>,
|
||||
"Reading into std::string_view is dangerous and "
|
||||
"therefore unsupported. "
|
||||
"Please consider using std::string instead.");
|
||||
return Error("Unsupported.");
|
||||
}
|
||||
|
||||
template <class P>
|
||||
static void write(const W& _w, const std::string_view& _str,
|
||||
const P& _p) noexcept {
|
||||
Parser<R, W, std::string_view>::write(_w, std::string(_str), _p);
|
||||
}
|
||||
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
return Parser<R, W, std::string>::to_schema(_definitions);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef RFL_PARSING_PARSER_TAGGED_UNION_HPP_
|
||||
#define RFL_PARSING_PARSER_TAGGED_UNION_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Result.hpp"
|
||||
@@ -8,14 +9,15 @@
|
||||
#include "../always_false.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "TaggedUnionWrapper.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class R, class W, internal::StringLiteral _discriminator,
|
||||
class... AlternativeTypes>
|
||||
requires AreReaderAndWriter<R, W,
|
||||
TaggedUnion<_discriminator, AlternativeTypes...>>
|
||||
requires AreReaderAndWriter<R, W,
|
||||
TaggedUnion<_discriminator, AlternativeTypes...>>
|
||||
struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>> {
|
||||
using ResultType = Result<TaggedUnion<_discriminator, AlternativeTypes...>>;
|
||||
|
||||
@@ -49,6 +51,14 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>> {
|
||||
std::visit(handle, _tagged_union.variant_);
|
||||
}
|
||||
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
using VariantType =
|
||||
std::variant<std::invoke_result_t<decltype(wrap<AlternativeTypes>),
|
||||
AlternativeTypes>...>;
|
||||
return Parser<R, W, VariantType>::to_schema(_definitions);
|
||||
}
|
||||
|
||||
private:
|
||||
template <int _i = 0>
|
||||
static ResultType find_matching_alternative(
|
||||
@@ -114,18 +124,25 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>> {
|
||||
template <class T, class P>
|
||||
static void write_wrapped(const W& _w, const T& _val,
|
||||
const P& _parent) noexcept {
|
||||
const auto wrapped = wrap(_val);
|
||||
Parser<R, W, std::remove_cvref_t<decltype(wrapped)>>::write(_w, wrapped,
|
||||
_parent);
|
||||
}
|
||||
|
||||
/// Generates a wrapped version of the original object, which contains the
|
||||
/// tag.
|
||||
template <class T>
|
||||
static auto wrap(const T& _val) noexcept {
|
||||
const auto tag = internal::make_tag<T>();
|
||||
using TagType = std::remove_cvref_t<decltype(tag)>;
|
||||
if constexpr (internal::has_fields<std::remove_cvref_t<T>>()) {
|
||||
using WrapperType =
|
||||
TaggedUnionWrapperWithFields<T, TagType, _discriminator>;
|
||||
const auto wrapper = WrapperType{.tag = tag, .fields = &_val};
|
||||
Parser<R, W, WrapperType>::write(_w, wrapper, _parent);
|
||||
return WrapperType{.tag = tag, .fields = &_val};
|
||||
} else {
|
||||
using WrapperType =
|
||||
TaggedUnionWrapperNoFields<T, TagType, _discriminator>;
|
||||
const auto wrapper = WrapperType{.tag = tag, .fields = &_val};
|
||||
Parser<R, W, WrapperType>::write(_w, wrapper, _parent);
|
||||
return WrapperType{.tag = tag, .fields = &_val};
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
#ifndef RFL_PARSING_PARSER_UNIQUE_PTR_HPP_
|
||||
#define RFL_PARSING_PARSER_UNIQUE_PTR_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
@@ -41,6 +44,13 @@ struct Parser<R, W, std::unique_ptr<T>> {
|
||||
}
|
||||
Parser<R, W, std::remove_cvref_t<T>>::write(_w, *_s, _parent);
|
||||
}
|
||||
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
using U = std::remove_cvref_t<T>;
|
||||
return schema::Type{schema::Type::Optional{
|
||||
Ref<schema::Type>::make(Parser<R, W, U>::to_schema(_definitions))}};
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef RFL_PARSING_PARSER_VARIANT_HPP_
|
||||
#define RFL_PARSING_PARSER_VARIANT_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <type_traits>
|
||||
#include <variant>
|
||||
|
||||
@@ -9,6 +10,7 @@
|
||||
#include "../always_false.hpp"
|
||||
#include "FieldVariantParser.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
@@ -62,6 +64,26 @@ struct Parser<R, W, std::variant<FieldTypes...>> {
|
||||
return std::visit(handle, _variant);
|
||||
}
|
||||
}
|
||||
|
||||
template <size_t _i = 0>
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions,
|
||||
std::vector<schema::Type> _types = {}) {
|
||||
if constexpr (internal::all_fields<std::tuple<FieldTypes...>>()) {
|
||||
return FieldVariantParser<R, W, FieldTypes...>::to_schema(_definitions);
|
||||
} else {
|
||||
using Type = schema::Type;
|
||||
constexpr size_t size = sizeof...(FieldTypes);
|
||||
if constexpr (_i == size) {
|
||||
return Type{Type::AnyOf{.types_ = _types}};
|
||||
} else {
|
||||
using U = std::remove_cvref_t<
|
||||
std::variant_alternative_t<_i, std::variant<FieldTypes...>>>;
|
||||
_types.push_back(Parser<R, W, U>::to_schema(_definitions));
|
||||
return to_schema<_i + 1>(_definitions, std::move(_types));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
#ifndef RFL_PARSING_PARSER_WSTRING_HPP_
|
||||
#define RFL_PARSING_PARSER_WSTRING_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class R, class W>
|
||||
requires AreReaderAndWriter<R, W, std::wstring>
|
||||
requires AreReaderAndWriter<R, W, std::wstring>
|
||||
struct Parser<R, W, std::wstring> {
|
||||
public:
|
||||
using InputVarType = typename R::InputVarType;
|
||||
@@ -66,6 +68,11 @@ struct Parser<R, W, std::wstring> {
|
||||
|
||||
ParentType::add_value(_w, outStr, _parent);
|
||||
}
|
||||
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
return Parser<R, W, std::string>::to_schema(_definitions);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -12,12 +12,14 @@ namespace parsing {
|
||||
|
||||
template <class T, class TagType, internal::StringLiteral _discriminator>
|
||||
struct TaggedUnionWrapperNoFields {
|
||||
using Type = T;
|
||||
rfl::Rename<_discriminator, TagType> tag;
|
||||
rfl::Flatten<const T*> fields;
|
||||
};
|
||||
|
||||
template <class T, class TagType, internal::StringLiteral _discriminator>
|
||||
struct TaggedUnionWrapperWithFields {
|
||||
using Type = T;
|
||||
rfl::Field<_discriminator, TagType> tag;
|
||||
rfl::Flatten<const T*> fields;
|
||||
};
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
#ifndef RFL_PARSING_TUPLEPARSER_HPP_
|
||||
#define RFL_PARSING_TUPLEPARSER_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
@@ -55,6 +57,22 @@ struct TupleParser {
|
||||
_w.end_array(&arr);
|
||||
}
|
||||
|
||||
template <size_t _i = 0>
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions,
|
||||
std::vector<schema::Type> _types = {}) {
|
||||
using Type = schema::Type;
|
||||
constexpr size_t size = sizeof...(Ts);
|
||||
if constexpr (_i == size) {
|
||||
return Type{Type::Tuple{.types_ = _types}};
|
||||
} else {
|
||||
using U =
|
||||
std::remove_cvref_t<std::tuple_element_t<_i, std::tuple<Ts...>>>;
|
||||
_types.push_back(Parser<R, W, U>::to_schema(_definitions));
|
||||
return to_schema<_i + 1>(_definitions, std::move(_types));
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
template <class... AlreadyExtracted>
|
||||
static Result<std::tuple<Ts...>> extract_field_by_field(
|
||||
@@ -77,8 +95,8 @@ struct TupleParser {
|
||||
template <int _i>
|
||||
static auto extract_single_field(
|
||||
const R& _r, const std::vector<InputVarType>& _vec) noexcept {
|
||||
using NewFieldType = std::remove_cvref_t<
|
||||
typename std::tuple_element<_i, std::tuple<Ts...>>::type>;
|
||||
using NewFieldType =
|
||||
std::remove_cvref_t<std::tuple_element_t<_i, std::tuple<Ts...>>>;
|
||||
|
||||
using ResultType = Result<NewFieldType>;
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define RFL_PARSING_VECTORPARSER_HPP_
|
||||
|
||||
#include <iterator>
|
||||
#include <map>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
@@ -15,6 +16,7 @@
|
||||
#include "is_map_like.hpp"
|
||||
#include "is_map_like_not_multimap.hpp"
|
||||
#include "is_set_like.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
@@ -64,6 +66,14 @@ struct VectorParser {
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates a schema for the underlying type.
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
using Type = schema::Type;
|
||||
return Type{Type::TypedArray{
|
||||
.type_ = Ref<Type>::make(Parser<R, W, T>::to_schema(_definitions))}};
|
||||
}
|
||||
|
||||
private:
|
||||
static auto get_elem(const R& _r, auto& _v) {
|
||||
return Parser<R, W, std::remove_cvref_t<T>>::read(_r, _v).value();
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
#ifndef RFL_PARSING_ISTAGGEDUNIONWRAPPER_HPP_
|
||||
#define RFL_PARSING_ISTAGGEDUNIONWRAPPER_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "../internal/StringLiteral.hpp"
|
||||
#include "TaggedUnionWrapper.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class T>
|
||||
class is_tagged_union_wrapper;
|
||||
|
||||
template <class T>
|
||||
class is_tagged_union_wrapper : public std::false_type {};
|
||||
|
||||
template <class T, class TagType, internal::StringLiteral _discriminator>
|
||||
class is_tagged_union_wrapper<
|
||||
TaggedUnionWrapperNoFields<T, TagType, _discriminator>>
|
||||
: public std::true_type {};
|
||||
|
||||
template <class T, class TagType, internal::StringLiteral _discriminator>
|
||||
class is_tagged_union_wrapper<
|
||||
TaggedUnionWrapperWithFields<T, TagType, _discriminator>>
|
||||
: public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
constexpr bool is_tagged_union_wrapper_v = is_tagged_union_wrapper<T>::value;
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,21 @@
|
||||
#ifndef RFL_PARSING_SCHEMA_DEFINITION_HPP_
|
||||
#define RFL_PARSING_SCHEMA_DEFINITION_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include "Type.hpp"
|
||||
|
||||
namespace rfl::parsing::schema {
|
||||
|
||||
struct Definition {
|
||||
/// Contains the root element of the schema definition.
|
||||
Type root_;
|
||||
|
||||
/// Contains the definitions to be referenced by Type::Reference.
|
||||
std::map<std::string, Type> definitions_;
|
||||
};
|
||||
|
||||
} // namespace rfl::parsing::schema
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,98 @@
|
||||
#ifndef RFL_PARSING_SCHEMA_TYPE_HPP_
|
||||
#define RFL_PARSING_SCHEMA_TYPE_HPP_
|
||||
|
||||
#include <cstddef>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include "../../Ref.hpp"
|
||||
#include "ValidationType.hpp"
|
||||
|
||||
namespace rfl::parsing::schema {
|
||||
|
||||
struct Type {
|
||||
struct Boolean {};
|
||||
|
||||
struct Int32 {};
|
||||
|
||||
struct Int64 {};
|
||||
|
||||
struct UInt32 {};
|
||||
|
||||
struct UInt64 {};
|
||||
|
||||
struct Integer {};
|
||||
|
||||
struct Float {};
|
||||
|
||||
struct Double {};
|
||||
|
||||
struct String {};
|
||||
|
||||
struct AnyOf {
|
||||
std::vector<Type> types_;
|
||||
};
|
||||
|
||||
struct Description {
|
||||
std::string description_;
|
||||
Ref<Type> type_;
|
||||
};
|
||||
|
||||
struct FixedSizeTypedArray {
|
||||
size_t size_;
|
||||
Ref<Type> type_;
|
||||
};
|
||||
|
||||
struct Literal {
|
||||
std::vector<std::string> values_;
|
||||
};
|
||||
|
||||
struct Object {
|
||||
std::map<std::string, Type> types_;
|
||||
};
|
||||
|
||||
/// All values are assumed to be required unless explicitly stated otherwise
|
||||
/// using this wrapper.
|
||||
struct Optional {
|
||||
Ref<Type> type_;
|
||||
};
|
||||
|
||||
/// The is necessary to resolve circular definitions. Refers to something in
|
||||
/// Definitions.
|
||||
struct Reference {
|
||||
std::string name_;
|
||||
};
|
||||
|
||||
// A map with key type string.
|
||||
struct StringMap {
|
||||
Ref<Type> value_type_;
|
||||
};
|
||||
|
||||
struct Tuple {
|
||||
std::vector<Type> types_;
|
||||
};
|
||||
|
||||
struct TypedArray {
|
||||
Ref<Type> type_;
|
||||
};
|
||||
|
||||
struct Validated {
|
||||
Ref<Type> type_;
|
||||
ValidationType validation_;
|
||||
};
|
||||
|
||||
using VariantType =
|
||||
std::variant<Boolean, Int32, Int64, UInt32, UInt64, Integer, Float,
|
||||
Double, String, AnyOf, Description, FixedSizeTypedArray,
|
||||
Literal, Object, Optional, Reference, StringMap, Tuple,
|
||||
TypedArray, Validated>;
|
||||
|
||||
/// A type can be determined to be any of the above.
|
||||
VariantType variant_;
|
||||
};
|
||||
|
||||
} // namespace rfl::parsing::schema
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,69 @@
|
||||
#ifndef RFL_PARSING_SCHEMA_VALIDATIONTYPE_HPP_
|
||||
#define RFL_PARSING_SCHEMA_VALIDATIONTYPE_HPP_
|
||||
|
||||
#include <cstddef>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include "../../Ref.hpp"
|
||||
|
||||
namespace rfl::parsing::schema {
|
||||
|
||||
struct ValidationType {
|
||||
struct AllOf {
|
||||
std::vector<ValidationType> types_;
|
||||
};
|
||||
|
||||
struct AnyOf {
|
||||
std::vector<ValidationType> types_;
|
||||
};
|
||||
|
||||
struct EqualTo {
|
||||
std::variant<double, int> value_;
|
||||
};
|
||||
|
||||
struct ExclusiveMaximum {
|
||||
std::variant<double, int> value_;
|
||||
};
|
||||
|
||||
struct ExclusiveMinimum {
|
||||
std::variant<double, int> value_;
|
||||
};
|
||||
|
||||
struct Maximum {
|
||||
std::variant<double, int> value_;
|
||||
};
|
||||
|
||||
struct Minimum {
|
||||
std::variant<double, int> value_;
|
||||
};
|
||||
|
||||
struct NotEqualTo {
|
||||
std::variant<double, int> value_;
|
||||
};
|
||||
|
||||
struct OneOf {
|
||||
std::vector<ValidationType> types_;
|
||||
};
|
||||
|
||||
struct Regex {
|
||||
std::string pattern_;
|
||||
};
|
||||
|
||||
struct Size {
|
||||
Ref<ValidationType> size_limit_;
|
||||
};
|
||||
|
||||
using VariantType =
|
||||
std::variant<AllOf, AnyOf, EqualTo, ExclusiveMaximum, ExclusiveMinimum,
|
||||
Maximum, Minimum, OneOf, NotEqualTo, Regex, Size>;
|
||||
|
||||
/// A type can be determined to be any of the above.
|
||||
VariantType variant_;
|
||||
};
|
||||
|
||||
} // namespace rfl::parsing::schema
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,21 @@
|
||||
#ifndef RFL_PARSING_SCHEMA_MAKE_HPP_
|
||||
#define RFL_PARSING_SCHEMA_MAKE_HPP_
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "../Parser.hpp"
|
||||
#include "Definition.hpp"
|
||||
#include "Type.hpp"
|
||||
|
||||
namespace rfl::parsing::schema {
|
||||
|
||||
template <class R, class W, class T>
|
||||
Definition make() {
|
||||
std::map<std::string, Type> definitions;
|
||||
auto root = Parser<R, W, T>::to_schema(&definitions);
|
||||
return Definition{.root_ = root, .definitions_ = definitions};
|
||||
}
|
||||
|
||||
} // namespace rfl::parsing::schema
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,43 @@
|
||||
#ifndef RFL_PARSING_SCHEMA_TONUMERICTYPE_HPP_
|
||||
#define RFL_PARSING_SCHEMA_TONUMERICTYPE_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "../../always_false.hpp"
|
||||
#include "Type.hpp"
|
||||
|
||||
namespace rfl::parsing::schema {
|
||||
|
||||
template <class T>
|
||||
inline schema::Type::NumericType to_numeric_type() {
|
||||
using U = std::remove_cvref_t<T>;
|
||||
|
||||
if constexpr (std::is_same<U, std::int32_t>()) {
|
||||
return Type::NumericType(Type::Int32{});
|
||||
|
||||
} else if constexpr (std::is_same<U, std::int64_t>()) {
|
||||
return Type::NumericType(Type::Int64{});
|
||||
|
||||
} else if constexpr (std::is_same<U, std::uint32_t>()) {
|
||||
return Type::NumericType(Type::UInt32{});
|
||||
|
||||
} else if constexpr (std::is_same<U, std::uint64_t>()) {
|
||||
return Type::NumericType(Type::UInt64{});
|
||||
|
||||
} else if constexpr (std::is_integral<U>()) {
|
||||
return Type::NumericType(Type::Integer{});
|
||||
|
||||
} else if constexpr (std::is_same<U, float>()) {
|
||||
return Type::NumericType(Type::Float{});
|
||||
|
||||
} else if constexpr (std::is_floating_point_v<U>) {
|
||||
return Type::NumericType(Type::Double{});
|
||||
|
||||
} else {
|
||||
static_assert(always_false_v<U>, "Not a numeric value.");
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace rfl::parsing::schema
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,81 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include "test_readme_example.hpp"
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_json_schema {
|
||||
|
||||
enum class Color { red, green, blue };
|
||||
|
||||
struct Rectangle {
|
||||
double width;
|
||||
double height;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
double width;
|
||||
};
|
||||
|
||||
struct Circle {
|
||||
double radius;
|
||||
};
|
||||
|
||||
using Age = rfl::Validator<unsigned int, rfl::Minimum<0>, rfl::Maximum<130>>;
|
||||
|
||||
using FieldVariant =
|
||||
rfl::Variant<rfl::Field<"rectangle", Rectangle>,
|
||||
rfl::Field<"square", Square>, rfl::Field<"circle", Circle>>;
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Description<"Must be a proper email in the form xxx@xxx.xxx.",
|
||||
rfl::Email>
|
||||
email;
|
||||
std::string town = "Springfield";
|
||||
Color color;
|
||||
Age age;
|
||||
float salary;
|
||||
rfl::Description<
|
||||
"The person's children. Pass an empty array for no children.",
|
||||
std::vector<Person>>
|
||||
children;
|
||||
std::variant<Color, std::vector<Person>, int> variant;
|
||||
std::tuple<Color, std::vector<Person>, int> tuple;
|
||||
rfl::Rename<"taggedUnion",
|
||||
rfl::TaggedUnion<"shape", Rectangle, Square, Circle>>
|
||||
tagged_union;
|
||||
rfl::Rename<"fieldVariant", FieldVariant> field_variant;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto json_schema = rfl::json::to_schema<
|
||||
rfl::Description<"JSON schema that describes the required "
|
||||
"attributes for the person class.",
|
||||
Person>>();
|
||||
|
||||
const std::string expected =
|
||||
R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/definitions/test_json_schema__Person","description":"JSON schema that describes the required attributes for the person class.","definitions":{"test_json_schema__Circle":{"type":"object","properties":{"radius":{"type":"number"}},"required":["radius"]},"test_json_schema__Circle__tagged":{"type":"object","properties":{"radius":{"type":"number"},"shape":{"type":"string","enum":["Circle"]}},"required":["radius","shape"]},"test_json_schema__Person":{"type":"object","properties":{"age":{"allOf":[{"minimum":0,"type":"integer"},{"maximum":130,"type":"integer"}]},"children":{"type":"array","description":"The person's children. Pass an empty array for no children.","items":{"$ref":"#/definitions/test_json_schema__Person"}},"color":{"type":"string","enum":["red","green","blue"]},"email":{"type":"string","description":"Must be a proper email in the form xxx@xxx.xxx.","pattern":"^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$"},"fieldVariant":{"anyOf":[{"type":"object","properties":{"rectangle":{"$ref":"#/definitions/test_json_schema__Rectangle"}},"required":["rectangle"]},{"type":"object","properties":{"square":{"$ref":"#/definitions/test_json_schema__Square"}},"required":["square"]},{"type":"object","properties":{"circle":{"$ref":"#/definitions/test_json_schema__Circle"}},"required":["circle"]}]},"firstName":{"type":"string"},"lastName":{"type":"string"},"salary":{"type":"number"},"taggedUnion":{"anyOf":[{"$ref":"#/definitions/test_json_schema__Rectangle__tagged"},{"$ref":"#/definitions/test_json_schema__Square__tagged"},{"$ref":"#/definitions/test_json_schema__Circle__tagged"}]},"town":{"type":"string"},"tuple":{"type":"array","prefixItems":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/definitions/test_json_schema__Person"}},{"type":"integer"}],"items":false},"variant":{"anyOf":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/definitions/test_json_schema__Person"}},{"type":"integer"}]}},"required":["age","children","color","email","fieldVariant","firstName","lastName","salary","taggedUnion","town","tuple","variant"]},"test_json_schema__Rectangle":{"type":"object","properties":{"height":{"type":"number"},"width":{"type":"number"}},"required":["height","width"]},"test_json_schema__Rectangle__tagged":{"type":"object","properties":{"height":{"type":"number"},"shape":{"type":"string","enum":["Rectangle"]},"width":{"type":"number"}},"required":["height","shape","width"]},"test_json_schema__Square":{"type":"object","properties":{"width":{"type":"number"}},"required":["width"]},"test_json_schema__Square__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Square"]},"width":{"type":"number"}},"required":["shape","width"]}}})";
|
||||
|
||||
if (json_schema != expected) {
|
||||
std::cout << "Test failed. Expected:" << std::endl
|
||||
<< expected << std::endl
|
||||
<< "Got: " << std::endl
|
||||
<< json_schema << std::endl
|
||||
<< std::endl;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
} // namespace test_json_schema
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_json_schema {
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
#include "test_flatten_anonymous.hpp"
|
||||
#include "test_forward_list.hpp"
|
||||
#include "test_inside_function.hpp"
|
||||
#include "test_json_schema.hpp"
|
||||
#include "test_list.hpp"
|
||||
#include "test_literal.hpp"
|
||||
#include "test_literal_map.hpp"
|
||||
@@ -152,6 +153,7 @@ int main() {
|
||||
test_meta_fields::test();
|
||||
|
||||
test_wstring::test();
|
||||
test_json_schema::test();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user