Move some code into source files to reduce compile time; #86 (#136)

---------

Co-authored-by: Patrick Urbanke <patrick@getml.com>
This commit is contained in:
Dr. Patrick Urbanke (劉自成)
2024-07-03 23:14:10 +02:00
committed by GitHub
co-authored by Patrick Urbanke
parent 1fa2391876
commit 7f0350f576
13 changed files with 427 additions and 380 deletions
+1 -1
View File
@@ -47,7 +47,7 @@ if(REFLECTCPP_USE_BUNDLED_DEPENDENCIES)
add_library(reflectcpp STATIC)
endif()
target_sources(reflectcpp PRIVATE src/yyjson.c)
target_sources(reflectcpp PRIVATE src/reflectcpp.cpp src/yyjson.c)
set_source_files_properties(src/yyjson.c PROPERTIES LANGUAGE CXX)
target_compile_features(reflectcpp PUBLIC cxx_std_20)
+1 -1
View File
@@ -1,6 +1,6 @@
MIT License
Copyright (c) 2023 getML
Copyright (c) 2023-2024 Code17 GmbH
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
+7 -10
View File
@@ -18,7 +18,7 @@ Design principles for reflect-cpp include:
- Close integration with containers from the C++ standard library
- Close adherence to C++ idioms
- Out-of-the-box support for JSON
- Simple installation: If no JSON support is required, reflect-cpp is header-only. For JSON support, only a single source file needs to be compiled.
- Simple installation
- Simple extendability to other serialization formats
- Simple extendability to custom classes
@@ -520,17 +520,14 @@ The following compilers are supported:
- Clang 14.0 or higher
- MSVC 17.8 (19.38) or higher
### Option 1: Header-only
### Option 1: Include source files into your own build
If you **do not** need JSON support or you want to link YYJSON yourself, then reflect-cpp is header-only. Simply copy the contents of the folder `include` into your source repository or add it to your include path.
### Option 2: Include source files into your own build
Simply copy the contents of the folder `include` into your source repository or add it to your include path and also add `src/yyjson.c` to your source files for compilation.
Simply copy the contents of the folder `include` into your source repository or add it to your include path and also add `src/reflectcpp.cpp` and `src/yyjson.c` to your source files for compilation.
If you want to link to your own version of yyjson, then only copy `src/reflectcpp.cpp`.
If you need support for other serialization formats like flexbuffers or XML, you should also include and link the respective libraries, as listed in the section on serialization formats.
### Option 3: Compilation using cmake
### Option 2: Compilation using cmake
This will simply compile YYJSON, which is the JSON library underlying reflect-cpp. You can then include reflect-cpp in your project and link to the binary
to get reflect-cpp with JSON support.
@@ -541,7 +538,7 @@ cmake --build build -j 4 # gcc, clang
cmake --build build --config Release -j 4 # MSVC
```
### Option 4: Compilation using cmake and vcpkg
### Option 3: Compilation using cmake and vcpkg
If you want serialization formats other than JSON, you can either install them manually or use vcpkg.
@@ -636,7 +633,7 @@ To run the tests, do the following:
### Make sure includes are relative
In order for the library to be able to function header-only, we need internal includes to be relative and not depend on any externally set include directory.
We need internal includes to be relative and not depend on any externally set include directory.
That is, for example, if you are within any file in `rfl/internal`, prefer
```cpp
+16 -66
View File
@@ -17,15 +17,15 @@ class Generic {
using ReflectionType =
std::variant<bool, int, double, std::string, Object, Array>;
Generic() : value_(false) {}
Generic();
Generic(Generic&& _other) noexcept = default;
Generic(Generic&& _other) noexcept;
Generic(const Generic& _other) = default;
Generic(const Generic& _other);
Generic(const ReflectionType& _value) : value_(_value) {}
Generic(const ReflectionType& _value);
Generic(ReflectionType&& _value) noexcept : value_(std::move(_value)) {}
Generic(ReflectionType&& _value) noexcept;
template <class T,
typename std::enable_if<std::is_convertible_v<T, ReflectionType>,
@@ -37,22 +37,16 @@ class Generic {
bool>::type = true>
Generic(T&& _value) noexcept : value_(std::forward<T>(_value)) {}
~Generic() = default;
~Generic();
/// Returns the underlying object.
const ReflectionType& get() const { return value_; }
/// Assigns the underlying object.
auto& operator=(const ReflectionType& _value) {
value_ = _value;
return *this;
}
Generic& operator=(const ReflectionType& _value);
/// Assigns the underlying object.
auto& operator=(ReflectionType&& _value) noexcept {
value_ = std::move(_value);
return *this;
}
Generic& operator=(ReflectionType&& _value) noexcept;
/// Assigns the underlying object.
template <class T,
@@ -64,81 +58,37 @@ class Generic {
}
/// Assigns the underlying object.
Generic& operator=(const Generic& _other) = default;
Generic& operator=(const Generic& _other);
/// Assigns the underlying object.
Generic& operator=(Generic&& _other) = default;
Generic& operator=(Generic&& _other);
/// Returns the underlying object, necessary for the serialization to work.
const ReflectionType& reflection() const { return value_; };
/// Casts the underlying value to an rfl::Generic::Array or returns an
/// rfl::Error, if the underlying value is not an rfl::Generic::Array.
Result<Array> to_array() const noexcept {
if (const auto* ptr = std::get_if<Array>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to an "
"rfl::Generic::Array.");
}
}
Result<Array> to_array() const noexcept;
/// Casts the underlying value to a boolean or returns an rfl::Error, if the
/// underlying value is not a boolean.
Result<bool> to_bool() const noexcept {
if (const auto* ptr = std::get_if<bool>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to a boolean.");
}
}
Result<bool> to_bool() const noexcept;
/// Casts the underlying value to a double or returns an rfl::Error, if the
/// underlying value is not a double.
Result<double> to_double() const noexcept {
if (const auto* ptr = std::get_if<double>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to a double.");
}
}
Result<double> to_double() const noexcept;
/// Casts the underlying value to an integer or returns an rfl::Error, if the
/// underlying value is not an integer.
Result<int> to_int() const noexcept {
if (const auto* ptr = std::get_if<int>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to an integer.");
}
}
Result<int> to_int() const noexcept;
/// Casts the underlying value to an rfl::Generic::Object or returns an
/// rfl::Error, if the underlying value is not an rfl::Generic::Object.
Result<Object> to_object() const noexcept {
if (const auto* ptr = std::get_if<Object>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to an "
"rfl::Generic::Object.");
}
}
Result<Object> to_object() const noexcept;
/// Casts the underlying value to a string or returns an rfl::Error, if the
/// underlying value is not a string.
Result<std::string> to_string() const noexcept {
if (const auto* ptr = std::get_if<std::string>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to a string.");
}
}
Result<std::string> to_string() const noexcept;
private:
ReflectionType value_;
-5
View File
@@ -4,14 +4,9 @@
#include <array>
#include <iostream>
#include <optional>
#include <ranges>
#include <span>
#include <stdexcept>
#include <string>
#include <tuple>
#include <type_traits>
#include <variant>
#include <vector>
#include "internal/is_array.hpp"
#include "internal/to_std_array.hpp"
-1
View File
@@ -5,7 +5,6 @@
#include "Ref.hpp"
#include "Result.hpp"
#include "parsing/Parser.hpp"
#include "parsing/schema/ValidationType.hpp"
namespace rfl {
-53
View File
@@ -1,53 +0,0 @@
#ifndef RFL_INTERNAL_MEMOIZATION_HPP_
#define RFL_INTERNAL_MEMOIZATION_HPP_
#include <atomic>
#include <mutex>
namespace rfl {
namespace internal {
/// For a thread-safe memoization pattern.
template <class T>
class Memoization {
public:
Memoization() { flag_.clear(); }
~Memoization() = default;
public:
/// Returns the underlying value.
template <class F>
const T& value(const F& _f) {
if (flag_.test()) {
return value_;
}
std::lock_guard<std::mutex> guard(mtx_);
if (flag_.test()) {
return value_;
}
_f(&value_);
flag_.test_and_set();
return value_;
}
private:
/// Signifies whether t_ has been set.
std::atomic_flag flag_;
/// A mutex, only needed for writing.
std::mutex mtx_;
/// The type to be initialized.
T value_;
};
} // namespace internal
} // namespace rfl
#endif
+1 -214
View File
@@ -25,220 +25,7 @@
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);
}
}
auto additional_properties =
_t.additional_properties_
? std::make_shared<schema::Type>(
type_to_json_schema_type(*_t.additional_properties_))
: std::shared_ptr<schema::Type>();
return schema::Type{.value = schema::Type::Object{
.properties = properties,
.required = required,
.additionalProperties = additional_properties}};
} 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_);
}
schema::Type type_to_json_schema_type(const parsing::schema::Type& _type);
template <class T>
struct TypeHelper {};
-1
View File
@@ -11,7 +11,6 @@
#include "../NamedTuple.hpp"
#include "../Result.hpp"
#include "../always_false.hpp"
#include "../internal/Memoization.hpp"
#include "../internal/is_array.hpp"
#include "../internal/is_attribute.hpp"
#include "../internal/is_basic_type.hpp"
+20 -14
View File
@@ -61,29 +61,35 @@ class Parser<R, W, std::variant<FieldTypes...>, ProcessorsType> {
}
}
template <size_t _i = 0>
static schema::Type to_schema(
std::map<std::string, schema::Type>* _definitions,
std::vector<schema::Type> _types = {}) {
std::map<std::string, schema::Type>* _definitions) {
if constexpr (internal::all_fields<std::tuple<FieldTypes...>>()) {
return FieldVariantParser<R, W, ProcessorsType, 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, ProcessorsType>::to_schema(_definitions));
return to_schema<_i + 1>(_definitions, std::move(_types));
}
std::vector<schema::Type> types;
build_schema(_definitions, &types,
std::make_integer_sequence<int, sizeof...(FieldTypes)>());
return schema::Type{schema::Type::AnyOf{.types_ = std::move(types)}};
}
}
private:
template <size_t _i>
static void add_to_schema(std::map<std::string, schema::Type>* _definitions,
std::vector<schema::Type>* _types) noexcept {
using U = std::remove_cvref_t<
std::variant_alternative_t<_i, std::variant<FieldTypes...>>>;
_types->push_back(Parser<R, W, U, ProcessorsType>::to_schema(_definitions));
}
template <int... _is>
static void build_schema(std::map<std::string, schema::Type>* _definitions,
std::vector<schema::Type>* _types,
std::integer_sequence<int, _is...>) noexcept {
(add_to_schema<_is>(_definitions, _types), ...);
}
template <int _i = 0>
static void read_variant(const R& _r, const InputVarType& _var,
std::optional<std::variant<FieldTypes...>>* _result,
+19 -14
View File
@@ -60,24 +60,29 @@ 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, ProcessorsType>::to_schema(_definitions));
return to_schema<_i + 1>(_definitions, std::move(_types));
}
std::map<std::string, schema::Type>* _definitions) {
std::vector<schema::Type> types;
build_schema(_definitions, &types,
std::make_integer_sequence<int, sizeof...(Ts)>());
return schema::Type{schema::Type::Tuple{.types_ = std::move(types)}};
}
private:
template <size_t _i>
static void add_to_schema(std::map<std::string, schema::Type>* _definitions,
std::vector<schema::Type>* _types) noexcept {
using U = std::remove_cvref_t<std::tuple_element_t<_i, std::tuple<Ts...>>>;
_types->push_back(Parser<R, W, U, ProcessorsType>::to_schema(_definitions));
}
template <int... _is>
static void build_schema(std::map<std::string, schema::Type>* _definitions,
std::vector<schema::Type>* _types,
std::integer_sequence<int, _is...>) noexcept {
(add_to_schema<_is>(_definitions, _types), ...);
}
template <int _i, class P>
static void to_array(const W& _w, const std::tuple<Ts...>& _tup,
const P& _parent) noexcept {
+6
View File
@@ -91,6 +91,12 @@ struct Type {
Literal, Object, Optional, Reference, StringMap, Tuple,
TypedArray, Validated>;
Type();
Type(const VariantType& _variant);
~Type();
/// A type can be determined to be any of the above.
VariantType variant_;
};
+356
View File
@@ -0,0 +1,356 @@
/*
MIT License
Copyright (c) 2023-2024 Code17 GmbH
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// ----------------------------------------------------------------------------
#include "rfl/Generic.hpp"
namespace rfl {
Generic::Generic() : value_(false) {}
Generic::Generic(Generic&& _other) noexcept = default;
Generic::Generic(const Generic& _other) = default;
Generic::Generic(const ReflectionType& _value) : value_(_value) {}
Generic::Generic(ReflectionType&& _value) noexcept
: value_(std::move(_value)) {}
Generic::~Generic() = default;
Generic& Generic::operator=(const ReflectionType& _value) {
value_ = _value;
return *this;
}
Generic& Generic::operator=(ReflectionType&& _value) noexcept {
value_ = std::move(_value);
return *this;
}
Generic& Generic::operator=(const Generic& _other) = default;
Generic& Generic::operator=(Generic&& _other) = default;
Result<Generic::Array> Generic::to_array() const noexcept {
if (const auto* ptr = std::get_if<Array>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to an "
"rfl::Generic::Array.");
}
}
Result<bool> Generic::to_bool() const noexcept {
if (const auto* ptr = std::get_if<bool>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to a boolean.");
}
}
Result<double> Generic::to_double() const noexcept {
if (const auto* ptr = std::get_if<double>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to a double.");
}
}
Result<int> Generic::to_int() const noexcept {
if (const auto* ptr = std::get_if<int>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to an integer.");
}
}
Result<Generic::Object> Generic::to_object() const noexcept {
if (const auto* ptr = std::get_if<Object>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to an "
"rfl::Generic::Object.");
}
}
Result<std::string> Generic::to_string() const noexcept {
if (const auto* ptr = std::get_if<std::string>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to a string.");
}
}
} // namespace rfl
// ----------------------------------------------------------------------------
#include "rfl/json/to_schema.hpp"
namespace rfl::json {
schema::Type type_to_json_schema_type(const parsing::schema::Type& _type);
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_);
}
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_);
}
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_);
}
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);
}
}
auto additional_properties =
_t.additional_properties_
? std::make_shared<schema::Type>(
type_to_json_schema_type(*_t.additional_properties_))
: std::shared_ptr<schema::Type>();
return schema::Type{.value = schema::Type::Object{
.properties = properties,
.required = required,
.additionalProperties = additional_properties}};
} 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_);
}
} // namespace rfl::json
// ----------------------------------------------------------------------------
#include "rfl/parsing/schema/Type.hpp"
namespace rfl::parsing::schema {
/// Requires a lot of template instantiation, so we do not want this to be
/// inlined.
Type::Type() : variant_() {}
Type::Type(const VariantType& _variant) : variant_(_variant) {}
Type::~Type() = default;
} // namespace rfl::parsing::schema
// ----------------------------------------------------------------------------