Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/rfl/Object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define RFL_OBJECT_HPP_

#include <algorithm>
#include <cstddef>
#include <stdexcept>
#include <string>
#include <type_traits>
Expand Down Expand Up @@ -128,6 +129,10 @@ class Object {
/// @return The maximum number of elements that can be stored
auto max_size() const { return data_.max_size(); }

/// Reserves storage for at least the specified number of elements.
/// @param _n The minimum number of elements to allocate storage for
void reserve(const std::size_t _n) { data_.reserve(_n); }

/// Inserts new elements at the end (variadic copy version).
/// @tparam Args Types of the values to insert
/// @param _values The key-value pairs to insert
Expand Down
14 changes: 12 additions & 2 deletions include/rfl/parsing/NamedTupleParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,19 @@ struct NamedTupleParser {
std::map<std::string, schema::Type>* _definitions,
View* _view = nullptr) noexcept {
SchemaType schema;
constexpr size_t num_schema_fields =
(static_cast<size_t>(
!internal::is_skip_v<
std::remove_cvref_t<typename FieldTypes::Type>> &&
!internal::is_extra_fields_v<
std::remove_cvref_t<typename FieldTypes::Type>>) +
... + size_t{0});
if constexpr (num_schema_fields > 0) {
schema.types_.reserve(num_schema_fields);
}
build_schema(_definitions, &schema, _view,
std::make_integer_sequence<int, size_>());
return schema::Type{schema};
return schema::Type{std::move(schema)};
}

private:
Expand Down Expand Up @@ -305,7 +315,7 @@ struct NamedTupleParser {
if constexpr (_no_field_names) {
_schema->types_.emplace_back(std::move(s));
} else {
_schema->types_[std::string(F::name())] = std::move(s);
_schema->types_.insert(std::string(F::name()), std::move(s));
}
}
};
Expand Down
7 changes: 7 additions & 0 deletions include/rfl/parsing/schema/Type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ struct RFL_API Type {
*/
Type(const VariantType& _variant);

/**
* @brief Move constructor from a variant.
*
* @param _variant The variant to move from.
*/
Type(VariantType&& _variant) noexcept;

/**
* @brief Copy constructor.
*/
Expand Down
4 changes: 3 additions & 1 deletion include/rfl/parsing/schema/make.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define RFL_PARSING_SCHEMA_MAKE_HPP_

#include <map>
#include <utility>

#include "../Parser.hpp"
#include "Definition.hpp"
Expand All @@ -22,7 +23,8 @@ template <class R, class W, class T, class ProcessorsType>
Definition make() {
std::map<std::string, Type> definitions;
auto root = Parser<R, W, T, ProcessorsType>::to_schema(&definitions);
return Definition{.root_ = root, .definitions_ = definitions};
return Definition{.root_ = std::move(root),
.definitions_ = std::move(definitions)};
}

} // namespace rfl::parsing::schema
Expand Down
99 changes: 59 additions & 40 deletions src/rfl/json/to_schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ SOFTWARE.

#include "rfl/json/to_schema.hpp"

#include <utility>

#include "rfl/json/schema/Type.hpp"
#include "rfl/json/write.hpp"

Expand Down Expand Up @@ -80,24 +82,30 @@ schema::Type handle_validation_type(
using ValidationType = parsing::schema::ValidationType;
if constexpr (std::is_same<T, ValidationType::AllOf>()) {
auto all_of = std::vector<schema::Type>();
all_of.reserve(_v.types_.size());
for (const auto& t : _v.types_) {
all_of.emplace_back(handle_validation_type(_type, t, _no_required));
}
return schema::Type{.value = schema::Type::AllOf{.allOf = all_of}};
return schema::Type{
.value = schema::Type::AllOf{.allOf = std::move(all_of)}};

} else if constexpr (std::is_same<T, ValidationType::AnyOf>()) {
auto any_of = std::vector<schema::Type>();
any_of.reserve(_v.types_.size());
for (const auto& t : _v.types_) {
any_of.emplace_back(handle_validation_type(_type, t, _no_required));
}
return schema::Type{.value = schema::Type::AnyOf{.anyOf = any_of}};
return schema::Type{
.value = schema::Type::AnyOf{.anyOf = std::move(any_of)}};

} else if constexpr (std::is_same<T, ValidationType::OneOf>()) {
auto one_of = std::vector<schema::Type>();
one_of.reserve(_v.types_.size());
for (const auto& t : _v.types_) {
one_of.emplace_back(handle_validation_type(_type, t, _no_required));
}
return schema::Type{.value = schema::Type::OneOf{.oneOf = one_of}};
return schema::Type{
.value = schema::Type::OneOf{.oneOf = std::move(one_of)}};

} else if constexpr (std::is_same<T, ValidationType::Regex>()) {
return schema::Type{.value = schema::Type::Regex{.pattern = _v.pattern_}};
Expand All @@ -119,31 +127,32 @@ schema::Type handle_validation_type(
std::is_same<U, schema::Type::String>()) {
if constexpr (std::is_same<V, ValidationType::Minimum>()) {
_t.minSize = _size_limit.value_.visit(to_size);
return t;
return std::move(t);

} else if constexpr (std::is_same<V, ValidationType::Maximum>()) {
_t.maxSize = _size_limit.value_.visit(to_size);
return t;
return std::move(t);

} else if constexpr (std::is_same<V, ValidationType::EqualTo>()) {
_t.minSize = _size_limit.value_.visit(to_size);
_t.maxSize = _size_limit.value_.visit(to_size);
return t;
return std::move(t);

} else if constexpr (std::is_same<V, ValidationType::AnyOf>() ||
std::is_same<V, ValidationType::AllOf>()) {
V v;
v.types_.reserve(_size_limit.types_.size());
for (const auto& limiter : _size_limit.types_) {
v.types_.push_back(ValidationType{ValidationType::Size{
.size_limit_ = rfl::Ref<ValidationType>::make(limiter)}});
}
return handle_validation_type(_type, ValidationType{.variant_ = v},
_no_required);
return handle_validation_type(
_type, ValidationType{.variant_ = std::move(v)}, _no_required);
} else {
return t;
return std::move(t);
}
} else {
return t;
return std::move(t);
}
};

Expand Down Expand Up @@ -228,39 +237,37 @@ schema::Type type_to_json_schema_type(const parsing::schema::Type& _type,

} else if constexpr (std::is_same<T, Type::AnyOf>()) {
auto any_of = std::vector<schema::Type>();
any_of.reserve(_t.types_.size());
for (const auto& t : _t.types_) {
any_of.emplace_back(type_to_json_schema_type(t, _no_required));
}
return schema::Type{.value = schema::Type::AnyOf{.anyOf = any_of}};
return schema::Type{
.value = schema::Type::AnyOf{.anyOf = std::move(any_of)}};

} else if constexpr (std::is_same<T, Type::DefaultVal>()) {
auto res = type_to_json_schema_type(*_t.type_, _no_required);
if (!_t.default_value_.has_value()) {
return res;
}
const auto update_prediction = [&](auto _v) -> schema::Type {
res.value.visit([&](auto& _v) {
_v.annotations.value_.defaultValue = *_t.default_value_;
return schema::Type{_v};
};
return rfl::visit(update_prediction, res.value);
});
return res;

} else if constexpr (std::is_same<T, Type::Deprecated>()) {
auto res = type_to_json_schema_type(*_t.type_, _no_required);
const auto update_prediction = [&](auto _v) -> schema::Type {
res.value.visit([&](auto& _v) {
_v.annotations.value_.description = _t.description_;
_v.annotations.value_.deprecated = true;
_v.annotations.value_.deprecationMessage = _t.deprecation_message_;
return schema::Type{_v};
};
return rfl::visit(update_prediction, res.value);
});
return res;

} else if constexpr (std::is_same<T, Type::Description>()) {
auto res = type_to_json_schema_type(*_t.type_, _no_required);
const auto update_prediction = [&](auto _v) -> schema::Type {
_v.annotations.value_.description = _t.description_;
return schema::Type{_v};
};
return rfl::visit(update_prediction, res.value);
res.value.visit(
[&](auto& _v) { _v.annotations.value_.description = _t.description_; });
return res;

} else if constexpr (std::is_same<T, Type::FixedSizeTypedArray>()) {
return schema::Type{
Expand All @@ -277,6 +284,7 @@ schema::Type type_to_json_schema_type(const parsing::schema::Type& _type,
} else if constexpr (std::is_same<T, Type::DescribedLiteral>()) {
// Convert to OneOf with StringConst for each described value
auto one_of = std::vector<schema::Type>();
one_of.reserve(_t.values_.size());
for (const auto& v : _t.values_) {
one_of.push_back(schema::Type{
.value = schema::Type::StringConst{
Expand All @@ -288,13 +296,18 @@ schema::Type type_to_json_schema_type(const parsing::schema::Type& _type,
: std::optional<std::string>(v.description_)},
.value = v.value_}});
}
return schema::Type{.value = schema::Type::OneOf{.oneOf = one_of}};
return schema::Type{
.value = schema::Type::OneOf{.oneOf = std::move(one_of)}};

} else if constexpr (std::is_same<T, Type::Object>()) {
auto properties = rfl::Object<schema::Type>();
properties.reserve(_t.types_.size());
auto required = std::vector<std::string>();
if (!_no_required) {
required.reserve(_t.types_.size());
}
for (const auto& [k, v] : _t.types_) {
properties[k] = type_to_json_schema_type(v, _no_required);
properties.insert(k, type_to_json_schema_type(v, _no_required));
if (!is_optional(v) && !_no_required) {
required.push_back(k);
}
Expand All @@ -315,16 +328,19 @@ schema::Type type_to_json_schema_type(const parsing::schema::Type& _type,
}
},
_t.additional_properties_);
return schema::Type{.value = schema::Type::Object{
.properties = properties,
.required = required,
.additionalProperties = additional_properties}};
return schema::Type{
.value = schema::Type::Object{
.properties = std::move(properties),
.required = std::move(required),
.additionalProperties = std::move(additional_properties)}};

} else if constexpr (std::is_same<T, Type::Optional>()) {
auto any_of = std::vector<schema::Type>();
any_of.reserve(2);
any_of.emplace_back(type_to_json_schema_type(*_t.type_, _no_required));
any_of.emplace_back(schema::Type{schema::Type::Null{}});
return schema::Type{
.value = schema::Type::AnyOf{
.anyOf = {type_to_json_schema_type(*_t.type_, _no_required),
schema::Type{schema::Type::Null{}}}}};
.value = schema::Type::AnyOf{.anyOf = std::move(any_of)}};

} else if constexpr (std::is_same<T, Type::Reference>()) {
return schema::Type{
Expand All @@ -338,10 +354,12 @@ schema::Type type_to_json_schema_type(const parsing::schema::Type& _type,

} else if constexpr (std::is_same<T, Type::Tuple>()) {
auto items = std::vector<schema::Type>();
items.reserve(_t.types_.size());
for (const auto& t : _t.types_) {
items.emplace_back(type_to_json_schema_type(t, _no_required));
}
return schema::Type{.value = schema::Type::Tuple{.prefixItems = items}};
return schema::Type{
.value = schema::Type::Tuple{.prefixItems = std::move(items)}};

} else if constexpr (std::is_same<T, Type::TypedArray>()) {
return schema::Type{
Expand All @@ -366,21 +384,22 @@ std::string to_schema_internal_schema(
const std::string& comment) {
auto definitions = std::map<std::string, schema::Type>();
for (const auto& [k, v] : internal_schema.definitions_) {
definitions[k] = type_to_json_schema_type(v, _no_required);
definitions.emplace_hint(definitions.end(), k,
type_to_json_schema_type(v, _no_required));
}
using JSONSchemaType =
typename TypeHelper<schema::Type::ReflectionType>::JSONSchemaType;
const auto to_schema = [&](auto&& _root) -> JSONSchemaType {
const auto to_schema = [&](auto& _root) -> JSONSchemaType {
using U = std::decay_t<decltype(_root)>;
return schema::JSONSchema<U>{
.comment =
!comment.empty() ? std::optional(std::move(comment)) : std::nullopt,
.root = std::forward<decltype(_root)>(_root),
.definitions = definitions,
!comment.empty() ? std::optional<std::string>(comment) : std::nullopt,
.root = std::move(_root),
.definitions = std::move(definitions),
};
};
auto root = type_to_json_schema_type(internal_schema.root_, _no_required);
const auto json_schema = rfl::visit(to_schema, std::move(root.value));
const auto json_schema = rfl::visit(to_schema, root.value);
return write(json_schema, _flag);
}

Expand Down
4 changes: 4 additions & 0 deletions src/rfl/parsing/schema/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ SOFTWARE.

#include "rfl/parsing/schema/Type.hpp"

#include <utility>

namespace rfl::parsing::schema {

/// Requires a lot of template instantiation, so we do not want this to be
Expand All @@ -35,6 +37,8 @@ Type::Type() : variant_() {}

Type::Type(const VariantType& _variant) : variant_(_variant) {}

Type::Type(VariantType&& _variant) noexcept : variant_(std::move(_variant)) {}

Type::Type(const Type& _other) = default;

Type::Type(Type&& _other) noexcept = default;
Expand Down
5 changes: 4 additions & 1 deletion src/rfl/parsing/schemaful/tuple_to_object.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "rfl/parsing/schemaful/tuple_to_object.hpp"

#include <utility>

namespace rfl::parsing::schemaful {

std::string to_field_name(const size_t _i) {
Expand All @@ -10,8 +12,9 @@ std::string to_field_name(const size_t _i) {

schema::Type::Object tuple_to_object(const schema::Type::Tuple& _tup) {
auto obj = schema::Type::Object{};
obj.types_.reserve(_tup.types_.size());
for (size_t i = 0; i < _tup.types_.size(); ++i) {
obj.types_[to_field_name(i)] = _tup.types_[i];
obj.types_.insert(std::make_pair(to_field_name(i), _tup.types_[i]));
}
return obj;
}
Expand Down
Loading