From 7f0350f576a8a0705331264a316c92bd3673452e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dr=2E=20Patrick=20Urbanke=20=28=E5=8A=89=E8=87=AA=E6=88=90?= =?UTF-8?q?=29?= Date: Wed, 3 Jul 2024 23:14:10 +0200 Subject: [PATCH] Move some code into source files to reduce compile time; #86 (#136) --------- Co-authored-by: Patrick Urbanke --- CMakeLists.txt | 2 +- LICENSE | 2 +- README.md | 17 +- include/rfl/Generic.hpp | 82 +----- include/rfl/Result.hpp | 5 - include/rfl/Size.hpp | 1 - include/rfl/internal/Memoization.hpp | 53 ---- include/rfl/json/to_schema.hpp | 215 +------------- include/rfl/parsing/NamedTupleParser.hpp | 1 - include/rfl/parsing/Parser_variant.hpp | 34 ++- include/rfl/parsing/TupleParser.hpp | 33 ++- include/rfl/parsing/schema/Type.hpp | 6 + src/reflectcpp.cpp | 356 +++++++++++++++++++++++ 13 files changed, 427 insertions(+), 380 deletions(-) delete mode 100644 include/rfl/internal/Memoization.hpp create mode 100644 src/reflectcpp.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a12d96c2..25774b0a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/LICENSE b/LICENSE index 1efcaec2..b1533c79 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/README.md b/README.md index a738ea9e..48d0b088 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/include/rfl/Generic.hpp b/include/rfl/Generic.hpp index fa4217df..e751e942 100644 --- a/include/rfl/Generic.hpp +++ b/include/rfl/Generic.hpp @@ -17,15 +17,15 @@ class Generic { using ReflectionType = std::variant; - 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 , @@ -37,22 +37,16 @@ class Generic { bool>::type = true> Generic(T&& _value) noexcept : value_(std::forward(_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 to_array() const noexcept { - if (const auto* ptr = std::get_if(&value_)) { - return *ptr; - } else { - return Error( - "rfl::Generic: Could not cast the underlying value to an " - "rfl::Generic::Array."); - } - } + Result 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 to_bool() const noexcept { - if (const auto* ptr = std::get_if(&value_)) { - return *ptr; - } else { - return Error( - "rfl::Generic: Could not cast the underlying value to a boolean."); - } - } + Result 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 to_double() const noexcept { - if (const auto* ptr = std::get_if(&value_)) { - return *ptr; - } else { - return Error( - "rfl::Generic: Could not cast the underlying value to a double."); - } - } + Result 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 to_int() const noexcept { - if (const auto* ptr = std::get_if(&value_)) { - return *ptr; - } else { - return Error( - "rfl::Generic: Could not cast the underlying value to an integer."); - } - } + Result 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 to_object() const noexcept { - if (const auto* ptr = std::get_if(&value_)) { - return *ptr; - } else { - return Error( - "rfl::Generic: Could not cast the underlying value to an " - "rfl::Generic::Object."); - } - } + Result 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 to_string() const noexcept { - if (const auto* ptr = std::get_if(&value_)) { - return *ptr; - } else { - return Error( - "rfl::Generic: Could not cast the underlying value to a string."); - } - } + Result to_string() const noexcept; private: ReflectionType value_; diff --git a/include/rfl/Result.hpp b/include/rfl/Result.hpp index fa0dc6ba..56289fca 100644 --- a/include/rfl/Result.hpp +++ b/include/rfl/Result.hpp @@ -4,14 +4,9 @@ #include #include #include -#include -#include #include #include -#include #include -#include -#include #include "internal/is_array.hpp" #include "internal/to_std_array.hpp" diff --git a/include/rfl/Size.hpp b/include/rfl/Size.hpp index 4070e70c..82eb99d6 100644 --- a/include/rfl/Size.hpp +++ b/include/rfl/Size.hpp @@ -5,7 +5,6 @@ #include "Ref.hpp" #include "Result.hpp" -#include "parsing/Parser.hpp" #include "parsing/schema/ValidationType.hpp" namespace rfl { diff --git a/include/rfl/internal/Memoization.hpp b/include/rfl/internal/Memoization.hpp deleted file mode 100644 index 7dd0024c..00000000 --- a/include/rfl/internal/Memoization.hpp +++ /dev/null @@ -1,53 +0,0 @@ -#ifndef RFL_INTERNAL_MEMOIZATION_HPP_ -#define RFL_INTERNAL_MEMOIZATION_HPP_ - -#include -#include - -namespace rfl { -namespace internal { - -/// For a thread-safe memoization pattern. -template -class Memoization { - public: - Memoization() { flag_.clear(); } - - ~Memoization() = default; - - public: - /// Returns the underlying value. - template - const T& value(const F& _f) { - if (flag_.test()) { - return value_; - } - - std::lock_guard 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 diff --git a/include/rfl/json/to_schema.hpp b/include/rfl/json/to_schema.hpp index d3badef6..e33f56c2 100644 --- a/include/rfl/json/to_schema.hpp +++ b/include/rfl/json/to_schema.hpp @@ -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; - return std::is_same(); - }; - 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; - using Type = parsing::schema::Type; - if constexpr (std::is_same() || - std::is_same() || - std::is_same() || - std::is_same() || - std::is_same()) { - 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; - using ValidationType = parsing::schema::ValidationType; - if constexpr (std::is_same()) { - auto all_of = std::vector(); - 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()) { - auto any_of = std::vector(); - 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()) { - auto one_of = std::vector(); - 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()) { - return schema::Type{.value = schema::Type::Regex{.pattern = _v.pattern_}}; - - } else if constexpr (std::is_same()) { - return type_to_json_schema_type(_type); - - } else if constexpr (std::is_same()) { - return schema::Type{.value = schema::Type::ExclusiveMaximum{ - .exclusiveMaximum = _v.value_, - .type = numeric_type_to_string(_type)}}; - - } else if constexpr (std::is_same()) { - return schema::Type{.value = schema::Type::ExclusiveMinimum{ - .exclusiveMinimum = _v.value_, - .type = numeric_type_to_string(_type)}}; - - } else if constexpr (std::is_same()) { - return schema::Type{ - .value = schema::Type::Maximum{ - .maximum = _v.value_, .type = numeric_type_to_string(_type)}}; - - } else if constexpr (std::is_same()) { - return schema::Type{ - .value = schema::Type::Minimum{ - .minimum = _v.value_, .type = numeric_type_to_string(_type)}}; - - } else if constexpr (std::is_same()) { - 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()) { - 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, "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; - using Type = parsing::schema::Type; - if constexpr (std::is_same()) { - return schema::Type{.value = schema::Type::Boolean{}}; - - } else if constexpr (std::is_same() || - std::is_same() || - std::is_same() || - std::is_same() || - std::is_same()) { - return schema::Type{.value = schema::Type::Integer{}}; - - } else if constexpr (std::is_same() || - std::is_same()) { - return schema::Type{.value = schema::Type::Number{}}; - - } else if constexpr (std::is_same()) { - return schema::Type{.value = schema::Type::String{}}; - - } else if constexpr (std::is_same()) { - auto any_of = std::vector(); - 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()) { - 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()) { - return schema::Type{.value = schema::Type::FixedSizeTypedArray{ - .items = Ref::make( - type_to_json_schema_type(*_t.type_)), - .minContains = _t.size_, - .maxContains = _t.size_}}; - - } else if constexpr (std::is_same()) { - return schema::Type{.value = - schema::Type::StringEnum{.values = _t.values_}}; - - } else if constexpr (std::is_same()) { - auto properties = std::map(); - auto required = std::vector(); - 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( - type_to_json_schema_type(*_t.additional_properties_)) - : std::shared_ptr(); - return schema::Type{.value = schema::Type::Object{ - .properties = properties, - .required = required, - .additionalProperties = additional_properties}}; - - } else if constexpr (std::is_same()) { - 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()) { - return schema::Type{ - .value = schema::Type::Reference{.ref = "#/definitions/" + _t.name_}}; - - } else if constexpr (std::is_same()) { - return schema::Type{.value = schema::Type::StringMap{ - .additionalProperties = Ref::make( - type_to_json_schema_type(*_t.value_type_))}}; - - } else if constexpr (std::is_same()) { - auto items = std::vector(); - 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()) { - return schema::Type{.value = schema::Type::TypedArray{ - .items = Ref::make( - type_to_json_schema_type(*_t.type_))}}; - - } else if constexpr (std::is_same()) { - return handle_validation_type(*_t.type_, _t.validation_); - - } else { - static_assert(rfl::always_false_v, "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 struct TypeHelper {}; diff --git a/include/rfl/parsing/NamedTupleParser.hpp b/include/rfl/parsing/NamedTupleParser.hpp index 1a2b8bcf..a2dc35af 100644 --- a/include/rfl/parsing/NamedTupleParser.hpp +++ b/include/rfl/parsing/NamedTupleParser.hpp @@ -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" diff --git a/include/rfl/parsing/Parser_variant.hpp b/include/rfl/parsing/Parser_variant.hpp index fd638919..e6749f70 100644 --- a/include/rfl/parsing/Parser_variant.hpp +++ b/include/rfl/parsing/Parser_variant.hpp @@ -61,29 +61,35 @@ class Parser, ProcessorsType> { } } - template static schema::Type to_schema( - std::map* _definitions, - std::vector _types = {}) { + std::map* _definitions) { if constexpr (internal::all_fields>()) { return FieldVariantParser::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>>; - _types.push_back( - Parser::to_schema(_definitions)); - return to_schema<_i + 1>(_definitions, std::move(_types)); - } + std::vector types; + build_schema(_definitions, &types, + std::make_integer_sequence()); + return schema::Type{schema::Type::AnyOf{.types_ = std::move(types)}}; } } private: + template + static void add_to_schema(std::map* _definitions, + std::vector* _types) noexcept { + using U = std::remove_cvref_t< + std::variant_alternative_t<_i, std::variant>>; + _types->push_back(Parser::to_schema(_definitions)); + } + + template + static void build_schema(std::map* _definitions, + std::vector* _types, + std::integer_sequence) noexcept { + (add_to_schema<_is>(_definitions, _types), ...); + } + template static void read_variant(const R& _r, const InputVarType& _var, std::optional>* _result, diff --git a/include/rfl/parsing/TupleParser.hpp b/include/rfl/parsing/TupleParser.hpp index c838f4c5..3b4004e2 100644 --- a/include/rfl/parsing/TupleParser.hpp +++ b/include/rfl/parsing/TupleParser.hpp @@ -60,24 +60,29 @@ struct TupleParser { _w.end_array(&arr); } - template static schema::Type to_schema( - std::map* _definitions, - std::vector _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>>; - _types.push_back( - Parser::to_schema(_definitions)); - return to_schema<_i + 1>(_definitions, std::move(_types)); - } + std::map* _definitions) { + std::vector types; + build_schema(_definitions, &types, + std::make_integer_sequence()); + return schema::Type{schema::Type::Tuple{.types_ = std::move(types)}}; } private: + template + static void add_to_schema(std::map* _definitions, + std::vector* _types) noexcept { + using U = std::remove_cvref_t>>; + _types->push_back(Parser::to_schema(_definitions)); + } + + template + static void build_schema(std::map* _definitions, + std::vector* _types, + std::integer_sequence) noexcept { + (add_to_schema<_is>(_definitions, _types), ...); + } + template static void to_array(const W& _w, const std::tuple& _tup, const P& _parent) noexcept { diff --git a/include/rfl/parsing/schema/Type.hpp b/include/rfl/parsing/schema/Type.hpp index 26ce4dd8..f8dfea0a 100644 --- a/include/rfl/parsing/schema/Type.hpp +++ b/include/rfl/parsing/schema/Type.hpp @@ -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_; }; diff --git a/src/reflectcpp.cpp b/src/reflectcpp.cpp new file mode 100644 index 00000000..cd6e9819 --- /dev/null +++ b/src/reflectcpp.cpp @@ -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::to_array() const noexcept { + if (const auto* ptr = std::get_if(&value_)) { + return *ptr; + } else { + return Error( + "rfl::Generic: Could not cast the underlying value to an " + "rfl::Generic::Array."); + } +} + +Result Generic::to_bool() const noexcept { + if (const auto* ptr = std::get_if(&value_)) { + return *ptr; + } else { + return Error( + "rfl::Generic: Could not cast the underlying value to a boolean."); + } +} + +Result Generic::to_double() const noexcept { + if (const auto* ptr = std::get_if(&value_)) { + return *ptr; + } else { + return Error( + "rfl::Generic: Could not cast the underlying value to a double."); + } +} + +Result Generic::to_int() const noexcept { + if (const auto* ptr = std::get_if(&value_)) { + return *ptr; + } else { + return Error( + "rfl::Generic: Could not cast the underlying value to an integer."); + } +} + +Result Generic::to_object() const noexcept { + if (const auto* ptr = std::get_if(&value_)) { + return *ptr; + } else { + return Error( + "rfl::Generic: Could not cast the underlying value to an " + "rfl::Generic::Object."); + } +} + +Result Generic::to_string() const noexcept { + if (const auto* ptr = std::get_if(&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; + return std::is_same(); + }; + 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; + using Type = parsing::schema::Type; + if constexpr (std::is_same() || + std::is_same() || + std::is_same() || + std::is_same() || + std::is_same()) { + 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; + using ValidationType = parsing::schema::ValidationType; + if constexpr (std::is_same()) { + auto all_of = std::vector(); + 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()) { + auto any_of = std::vector(); + 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()) { + auto one_of = std::vector(); + 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()) { + return schema::Type{.value = schema::Type::Regex{.pattern = _v.pattern_}}; + + } else if constexpr (std::is_same()) { + return type_to_json_schema_type(_type); + + } else if constexpr (std::is_same()) { + return schema::Type{.value = schema::Type::ExclusiveMaximum{ + .exclusiveMaximum = _v.value_, + .type = numeric_type_to_string(_type)}}; + + } else if constexpr (std::is_same()) { + return schema::Type{.value = schema::Type::ExclusiveMinimum{ + .exclusiveMinimum = _v.value_, + .type = numeric_type_to_string(_type)}}; + + } else if constexpr (std::is_same()) { + return schema::Type{ + .value = schema::Type::Maximum{ + .maximum = _v.value_, .type = numeric_type_to_string(_type)}}; + + } else if constexpr (std::is_same()) { + return schema::Type{ + .value = schema::Type::Minimum{ + .minimum = _v.value_, .type = numeric_type_to_string(_type)}}; + + } else if constexpr (std::is_same()) { + 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()) { + 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, "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; + using Type = parsing::schema::Type; + if constexpr (std::is_same()) { + return schema::Type{.value = schema::Type::Boolean{}}; + + } else if constexpr (std::is_same() || + std::is_same() || + std::is_same() || + std::is_same() || + std::is_same()) { + return schema::Type{.value = schema::Type::Integer{}}; + + } else if constexpr (std::is_same() || + std::is_same()) { + return schema::Type{.value = schema::Type::Number{}}; + + } else if constexpr (std::is_same()) { + return schema::Type{.value = schema::Type::String{}}; + + } else if constexpr (std::is_same()) { + auto any_of = std::vector(); + 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()) { + 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()) { + return schema::Type{.value = schema::Type::FixedSizeTypedArray{ + .items = Ref::make( + type_to_json_schema_type(*_t.type_)), + .minContains = _t.size_, + .maxContains = _t.size_}}; + + } else if constexpr (std::is_same()) { + return schema::Type{.value = + schema::Type::StringEnum{.values = _t.values_}}; + + } else if constexpr (std::is_same()) { + auto properties = std::map(); + auto required = std::vector(); + 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( + type_to_json_schema_type(*_t.additional_properties_)) + : std::shared_ptr(); + return schema::Type{.value = schema::Type::Object{ + .properties = properties, + .required = required, + .additionalProperties = additional_properties}}; + + } else if constexpr (std::is_same()) { + 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()) { + return schema::Type{ + .value = schema::Type::Reference{.ref = "#/definitions/" + _t.name_}}; + + } else if constexpr (std::is_same()) { + return schema::Type{.value = schema::Type::StringMap{ + .additionalProperties = Ref::make( + type_to_json_schema_type(*_t.value_type_))}}; + + } else if constexpr (std::is_same()) { + auto items = std::vector(); + 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()) { + return schema::Type{.value = schema::Type::TypedArray{ + .items = Ref::make( + type_to_json_schema_type(*_t.type_))}}; + + } else if constexpr (std::is_same()) { + return handle_validation_type(*_t.type_, _t.validation_); + + } else { + static_assert(rfl::always_false_v, "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 + +// ----------------------------------------------------------------------------