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
90 changes: 54 additions & 36 deletions include/rfl/parsing/FieldVariantParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
#define RFL_PARSING_FIELD_VARIANT_PARSER_HPP_

#include <map>
#include <optional>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

#include "../NamedTuple.hpp"
#include "../Result.hpp"
Expand All @@ -18,6 +21,32 @@

namespace rfl::parsing {

/// Namespace-scope visitor for writing one externally tagged field alternative,
/// parameterized only by `(R, W, ProcessorsType, P)` and the active `FieldType`
/// — never the enclosing `FieldTypes...` pack.
template <class R, class W, class ProcessorsType, class P>
struct FieldVariantWriter {
const W& w_;
const P& parent_;

template <class FieldType>
void operator()(const FieldType& _field) const {
const auto named_tuple = make_named_tuple(internal::to_ptr_field(_field));
using NamedTupleType = std::remove_cvref_t<decltype(named_tuple)>;
Parser<R, W, NamedTupleType, ProcessorsType>::write(w_, named_tuple,
parent_);
}
};

/// Namespace-scope schema builder for one externally tagged field alternative,
/// parameterized only by `(R, W, FieldType, ProcessorsType)`.
template <class R, class W, class FieldType, class ProcessorsType>
inline schema::Type field_variant_alternative_to_schema(
std::map<std::string, schema::Type>* _definitions) noexcept {
using NamedTupleType = NamedTuple<std::remove_cvref_t<FieldType>>;
return Parser<R, W, NamedTupleType, ProcessorsType>::to_schema(_definitions);
}

/// To be used when all options of the variants are rfl::Field. Essentially,
/// this is an externally tagged union.
template <class R, class W, class ProcessorsType, class... FieldTypes>
Expand All @@ -43,24 +72,23 @@ struct FieldVariantParser {
"Externally tagged variants cannot have duplicate field "
"names.");

const auto to_result = [&](const auto _obj) -> ResultType {
auto field_variant = std::optional<Result<FieldVariantType>>();
const auto reader =
FieldVariantReader<R, W, ProcessorsType, FieldTypes...>(
&_r, &field_variant);
auto err = _r.read_object(reader, _obj);
if (err) {
return error(std::move(*err));
}
if (!field_variant) {
return error(
"Could not parse: Expected the object to have "
"exactly one field, but found none.");
}
return std::move(*field_variant);
};

return _r.to_object(_var).and_then(to_result);
auto obj = _r.to_object(_var);
if (!obj) [[unlikely]] {
return error(std::move(obj).error());
}
auto field_variant = std::optional<Result<FieldVariantType>>();
const auto reader = FieldVariantReader<R, W, ProcessorsType, FieldTypes...>(
&_r, &field_variant);
auto err = _r.read_object(reader, *obj);
if (err) [[unlikely]] {
return error(std::move(*err));
}
if (!field_variant) [[unlikely]] {
return error(
"Could not parse: Expected the object to have "
"exactly one field, but found none.");
}
return std::move(*field_variant);
}

/**
Expand All @@ -79,12 +107,7 @@ struct FieldVariantParser {
"Externally tagged variants cannot have duplicate field "
"names.");

_v.visit([&](const auto& _field) {
const auto named_tuple = make_named_tuple(internal::to_ptr_field(_field));
using NamedTupleType = std::remove_cvref_t<decltype(named_tuple)>;
Parser<R, W, NamedTupleType, ProcessorsType>::write(_w, named_tuple,
_parent);
});
_v.visit(FieldVariantWriter<R, W, ProcessorsType, P>{_w, _parent});
}

/**
Expand All @@ -95,18 +118,13 @@ struct FieldVariantParser {
*/
static schema::Type to_schema(
std::map<std::string, schema::Type>* _definitions) {
return schema::Type{schema::Type::AnyOf{
.types_ = std::vector<schema::Type>(
{one_field_to_type<FieldTypes>(_definitions)...})}};
}

private:
template <class FieldType>
static schema::Type one_field_to_type(
std::map<std::string, schema::Type>* _definitions) noexcept {
using NamedTupleType = NamedTuple<std::remove_cvref_t<FieldType>>;
return Parser<R, W, NamedTupleType, ProcessorsType>::to_schema(
_definitions);
std::vector<schema::Type> types;
types.reserve(sizeof...(FieldTypes));
(types.emplace_back(
field_variant_alternative_to_schema<R, W, FieldTypes, ProcessorsType>(
_definitions)),
...);
return schema::Type{schema::Type::AnyOf{.types_ = std::move(types)}};
}
};
} // namespace rfl::parsing
Expand Down
121 changes: 82 additions & 39 deletions include/rfl/parsing/FieldVariantReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,80 @@
#define RFL_PARSING_FIELDVARIANTREADER_HPP_

#include <optional>
#include <sstream>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>

#include "../Literal.hpp"
#include "../Result.hpp"
#include "../Variant.hpp"
#include "../internal/is_array.hpp"
#include "../internal/nth_element_t.hpp"
#include "../internal/always_inline.hpp"
#include "../internal/cold.hpp"
#include "Parser_base.hpp"
#include "find_field_index.hpp"

namespace rfl::parsing {

RFL_COLD inline Error make_field_variant_unknown_field_error(
const std::string_view _disc_value) {
std::string msg;
msg.reserve(64 + _disc_value.size());
msg.append("Could not parse rfl::Variant, could not match field named '");
msg.append(_disc_value);
msg.append("'.");
return Error(std::move(msg));
}

RFL_COLD inline Error make_field_variant_field_error(
const std::string_view _disc_value, const Error& _e) {
const auto& what = _e.what();
std::string msg;
msg.reserve(46 + _disc_value.size() + what.size());
msg.append("Could not parse rfl::Variant with field '");
msg.append(_disc_value);
msg.append("': ");
msg.append(what);
return Error(std::move(msg));
}

/// Reads one field-variant alternative. Parameterized only by
/// `(R, W, FieldType, ProcessorsType)` — never the enclosing `FieldTypes...`
/// pack — so mangled symbols and debug info stay O(1) per alternative.
template <class R, class W, class FieldType, class ProcessorsType>
inline std::optional<std::remove_cvref_t<FieldType>>
read_field_variant_alternative(const R& _r,
const typename R::InputVarType& _var,
const std::string_view _disc_value,
std::optional<Error>* _err) noexcept {
using CleanFieldType = std::remove_cvref_t<FieldType>;
using ValueType = std::remove_cvref_t<typename CleanFieldType::Type>;
auto res = Parser<R, W, ValueType, ProcessorsType>::read(_r, _var);
if (!res) [[unlikely]] {
_err->emplace(
make_field_variant_field_error(_disc_value, std::move(res).error()));
return std::nullopt;
}
return std::optional<CleanFieldType>(std::in_place, std::move(*res));
}

template <class VariantType, class FieldType>
RFL_ALWAYS_INLINE void assign_field_variant_if_present(
std::optional<Result<VariantType>>* _out,
std::optional<FieldType>&& _field) noexcept {
if (_field) [[likely]] {
_out->emplace(VariantType(std::move(*_field)));
}
}

template <class R, class W, class ProcessorsType, class... FieldTypes>
class FieldVariantReader {
private:
using InputVarType = typename R::InputVarType;
using FieldVariantType = rfl::Variant<FieldTypes...>;
using ResultType = Result<FieldVariantType>;
using NameLookup =
FieldNameLookup<Literal<std::remove_cvref_t<FieldTypes>::name_...>>;

public:
/**
Expand All @@ -43,52 +98,40 @@ class FieldVariantReader {
*/
void read(const std::string_view& _disc_value,
const InputVarType& _var) const noexcept {
if (*field_variant_) {
if (*field_variant_) [[unlikely]] {
*field_variant_ = error(
"Could not parse rfl::Variant: Expected the object to have "
"exactly one field, but found more than one.");
return;
}
try_matching_fields(
_disc_value, _var,
std::make_integer_sequence<int, sizeof...(FieldTypes)>());
if (!*field_variant_) {
std::stringstream stream;
stream << "Could not parse rfl::Variant, could not match field named "
"'"
<< _disc_value << "'.";
*field_variant_ = error(stream.str());
const int idx = NameLookup::find_direct(_disc_value);
if (idx < 0) [[unlikely]] {
*field_variant_ =
error(make_field_variant_unknown_field_error(_disc_value));
return;
}
read_by_index(idx, _disc_value, _var,
std::make_integer_sequence<int, sizeof...(FieldTypes)>());
}

private:
template <int... _is>
void try_matching_fields(const std::string_view& _disc_value,
const InputVarType& _var,
std::integer_sequence<int, _is...>) const noexcept {
(try_one<_is>(_disc_value, _var), ...);
}

template <int _i>
void try_one(const std::string_view& _disc_value,
const InputVarType& _var) const noexcept {
using FieldType =
std::remove_cvref_t<internal::nth_element_t<_i, FieldTypes...>>;
using ValueType = std::remove_cvref_t<typename FieldType::Type>;
constexpr auto key = FieldType::name_.string_view();
if (key == _disc_value) {
const auto to_variant = [](ValueType&& _val) {
return rfl::Variant<FieldTypes...>(FieldType(std::move(_val)));
};
const auto embellish_error = [&](const Error& _e) {
std::stringstream stream;
stream << "Could not parse rfl::Variant with field '"
<< std::string(_disc_value) << "': " << _e.what();
return Error(stream.str());
};
*field_variant_ = Parser<R, W, ValueType, ProcessorsType>::read(*r_, _var)
.transform(to_variant)
.transform_error(embellish_error);
void read_by_index(const int _idx, const std::string_view _disc_value,
const InputVarType& _var,
std::integer_sequence<int, _is...>) const noexcept {
std::optional<Error> err;
static_cast<void>(
((_idx == _is
? (assign_field_variant_if_present<FieldVariantType>(
field_variant_,
read_field_variant_alternative<
R, W, std::remove_cvref_t<FieldTypes>, ProcessorsType>(
*r_, _var, _disc_value, &err)),
true)
: false) ||
...));
if (err) [[unlikely]] {
field_variant_->emplace(error(std::move(*err)));
}
}

Expand Down
Loading
Loading