Skip to content
Closed
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
4 changes: 3 additions & 1 deletion docs/content/processors.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ reflect-cpp currently supports the following processors:
By default, reflect-cpp collects errors from multiple object fields.
`rfl::FailFast` reports the first field error and skips decoding the remaining
fields of that object. It also applies to nested structs and named tuples,
including structs stored in containers, across serialization formats.
including structs stored in containers, across serialization formats. For YAML,
`rfl::FailFast` also stops sequence reading at the first failing element
instead of collecting all element errors.

```cpp
const auto result = rfl::yas::read<MyStruct, rfl::FailFast>(bytes);
Expand Down
2 changes: 1 addition & 1 deletion docs/content/supported_formats/yaml.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,6 @@ source file is compiled.
- Unknown fields under `rfl::NoExtraFields` point at the offending key (`exact = true`).
- Missing required fields point at the enclosing mapping or sequence (`exact = false`).

Recursive YAML aliases are rejected and reported through `rfl::Result`.
Sequence reading collects errors from all failing elements (unless `rfl::FailFast` is passed), matching object field error aggregation. Recursive YAML aliases are rejected and reported through `rfl::Result`.


39 changes: 28 additions & 11 deletions include/rfl/parsing/ArrayReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <type_traits>

#include "../Result.hpp"
#include "../internal/fail_fast_v.hpp"
#include "../internal/is_array.hpp"
#include "Parser_base.hpp"
#include "wrap_error.hpp"
Expand All @@ -20,13 +21,15 @@ class ArrayReader {
static constexpr size_t size_ = _size;

public:
static constexpr bool fail_fast = internal::fail_fast_v<ProcessorsType>;

/**
* @brief Constructor.
* @param _r The reader to use.
* @param _array The array to read into.
*/
ArrayReader(const R* _r, std::array<T, _size>* _array)
: array_(_array), num_set_(0), r_(_r) {}
: array_(_array), num_set_(0), num_failed_(0), r_(_r) {}

~ArrayReader() = default;

Expand Down Expand Up @@ -54,22 +57,33 @@ class ArrayReader {
* @return An error if reading fails or if the array is already full, std::nullopt otherwise.
*/
std::optional<Error> read(const InputVarType& _var) const {
if (num_set_ == size_) {
return attach_source_location(
*r_, _var,
Error("Expected " + std::to_string(size_) +
" elements, got at least " + std::to_string(size_ + 1) + "."));
const auto idx = num_set_ + num_failed_;
if (idx >= size_) [[unlikely]] {
++num_failed_;
if (idx == size_) {
return attach_source_location(
*r_, _var,
Error("Expected " + std::to_string(size_) +
" elements, got at least " + std::to_string(size_ + 1) +
"."));
}
return std::nullopt;
}
auto res =
Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::read(*r_, _var);
if (res) {
move_to(&((*array_)[num_set_]), &(*res));
if (num_failed_ == 0) [[likely]] {
move_to(&((*array_)[num_set_]), &(*res));
++num_set_;
} else {
++num_failed_;
}
} else {
++num_failed_;
return wrap_error(
"Failed to parse element " + std::to_string(num_set_) + ": ",
num_set_, std::move(res).error());
"Failed to parse element " + std::to_string(idx) + ": ", idx,
std::move(res).error());
}
++num_set_;
return std::optional<Error>();
}

Expand Down Expand Up @@ -103,9 +117,12 @@ class ArrayReader {
/// The underlying array.
std::array<T, size_>* array_;

/// Indicates the current field
/// Number of contiguous prefix elements constructed in *array_.
mutable size_t num_set_;

/// Number of elements seen after the first failure (not constructed in *array_).
mutable size_t num_failed_;

/// The underlying reader.
const R* r_;
};
Expand Down
35 changes: 25 additions & 10 deletions include/rfl/parsing/TupleReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "../Result.hpp"
#include "../Tuple.hpp"
#include "../internal/always_inline.hpp"
#include "../internal/fail_fast_v.hpp"
#include "../internal/is_array.hpp"
#include "AreReaderAndWriter.hpp"
#include "Parser_base.hpp"
Expand All @@ -25,14 +26,16 @@ class TupleReader {
static constexpr size_t size_ = rfl::tuple_size_v<TupleType>;

public:
static constexpr bool fail_fast = internal::fail_fast_v<ProcessorsType>;

/**
* @brief Constructor.
*
* @param _r The reader to use.
* @param _tuple The tuple to read into.
*/
TupleReader(const R* _r, TupleType* _tuple)
: num_set_(0), r_(_r), tuple_(_tuple) {}
: num_set_(0), num_failed_(0), r_(_r), tuple_(_tuple) {}

/**
* @brief Handles any fields that were missing from the input.
Expand Down Expand Up @@ -63,17 +66,20 @@ class TupleReader {
* @return An optional error.
*/
std::optional<Error> read(const InputVarType& _var) const {
if (num_set_ == size_) {
const auto idx = num_set_ + num_failed_;
if (idx > size_) [[unlikely]] {
++num_failed_;
return std::nullopt;
}
if (idx == size_) [[unlikely]] {
++num_failed_;
std::stringstream stream;
stream << "Expected " << size_ << " fields, but got at least one more.";
return attach_source_location(*r_, _var, Error(stream.str()));
}
std::optional<Error> err;
[&]<size_t... _is>(std::integer_sequence<size_t, _is...>) {
(read_impl<_is>(*this, _var, &err), ...);
if (!err) {
++num_set_;
}
(read_impl<_is>(*this, idx, _var, &err), ...);
}(std::make_integer_sequence<size_t, size_>());
return err;
}
Expand Down Expand Up @@ -122,9 +128,9 @@ class TupleReader {
}

template <size_t _i>
static void read_impl(const TupleReader& _tuple_reader,
static void read_impl(const TupleReader& _tuple_reader, const size_t _idx,
const InputVarType& _var, std::optional<Error>* _err) {
if (_tuple_reader.num_set_ != _i) {
if (_idx != _i) {
return;
}
using CurrentType =
Expand All @@ -134,8 +140,14 @@ class TupleReader {
*_tuple_reader.r_, _var);

if (res) {
move_to(&(rfl::get<_i>(*_tuple_reader.tuple_)), &(*res));
if (_tuple_reader.num_failed_ == 0) [[likely]] {
move_to(&(rfl::get<_i>(*_tuple_reader.tuple_)), &(*res));
++_tuple_reader.num_set_;
} else {
++_tuple_reader.num_failed_;
}
} else {
++_tuple_reader.num_failed_;
*_err = wrap_error("Failed to parse field " + std::to_string(_i) + ": ",
_i, std::move(res).error());
}
Expand All @@ -161,9 +173,12 @@ class TupleReader {
}

private:
/// Indicates the last field that was set.
/// Number of contiguous prefix fields constructed in *tuple_.
mutable size_t num_set_;

/// Number of fields seen after the first failure (not constructed in *tuple_).
mutable size_t num_failed_;

/// The underlying reader.
const R* r_;

Expand Down
35 changes: 29 additions & 6 deletions include/rfl/parsing/VectorReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "../Result.hpp"
#include "../internal/always_inline.hpp"
#include "../internal/fail_fast_v.hpp"
#include "Parser_base.hpp"
#include "is_inplace_vector.hpp"
#include "is_map_like.hpp"
Expand Down Expand Up @@ -36,14 +37,16 @@ class VectorReader {
using ParsedType = typename decltype(parsed_type())::type;

public:
static constexpr bool fail_fast = internal::fail_fast_v<ProcessorsType>;

/**
* @brief Constructor.
*
* @param _r The reader to use.
* @param _vec The vector to read into.
*/
VectorReader(const R* _r, VecType* _vec)
: num_inserted_(0), r_(_r), vec_(_vec) {}
: num_inserted_(0), num_failed_(0), r_(_r), vec_(_vec) {}

~VectorReader() = default;

Expand All @@ -67,17 +70,33 @@ class VectorReader {
* @return An optional error.
*/
RFL_ALWAYS_INLINE std::optional<Error> read(const InputVarType& _var) const {
if constexpr (is_inplace_vector_v<VecType>) {
const auto idx = index();
if (idx >= vec_->capacity()) [[unlikely]] {
++num_failed_;
if (idx == vec_->capacity()) {
return attach_source_location(
*r_, _var,
Error("Too many elements, capacity is " +
std::to_string(vec_->capacity()) + "."));
}
return std::nullopt;
}
}
auto res = Parser<R, W, ParsedType, ProcessorsType>::read(*r_, _var);
if (!res) {
if (!res) [[unlikely]] {
// The message stays as it is, but the index of the element becomes
// part of the path.
return wrap_error("", index(), std::move(res).error());
const auto idx = index();
++num_failed_;
return wrap_error("", idx, std::move(res).error());
}
if constexpr (is_map_like_v<VecType> || is_set_like_v<VecType>) {
vec_->insert(std::move(*res));
++num_inserted_;
} else if constexpr (is_inplace_vector_v<VecType>) {
if (!vec_->try_emplace_back(std::move(*res))) {
++num_failed_;
return attach_source_location(
*r_, _var,
Error("Too many elements, capacity is " +
Expand All @@ -91,19 +110,23 @@ class VectorReader {

private:
/// Returns the index of the element that is currently being read.
size_t index() const {
size_t index() const noexcept {
if constexpr (is_map_like_v<VecType> || is_set_like_v<VecType>) {
// Inserting a duplicate does not change the size of a map or set.
return num_inserted_;
return num_inserted_ + num_failed_;
} else {
return vec_->size();
return vec_->size() + num_failed_;
}
}

private:
/// The number of elements inserted into a map or set so far.
mutable size_t num_inserted_;

/// The number of elements that failed to parse so far (only touched on the
/// error path to avoid adding overhead to successful std::vector reads).
mutable size_t num_failed_;

/// The underlying reader.
const R* r_;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ViewReaderWithDefaultAndStrippedFieldNames {
using InputVarType = typename R::InputVarType;
using ViewType = _ViewType;

static constexpr bool fail_fast = internal::fail_fast_v<ProcessorsType>;
static constexpr size_t size_ = ViewType::size();

/**
Expand Down
1 change: 1 addition & 0 deletions include/rfl/parsing/ViewReaderWithStrippedFieldNames.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ViewReaderWithStrippedFieldNames {
using InputVarType = typename R::InputVarType;
using ViewType = _ViewType;

static constexpr bool fail_fast = internal::fail_fast_v<ProcessorsType>;
static constexpr size_t size_ = ViewType::size();

/**
Expand Down
20 changes: 19 additions & 1 deletion include/rfl/yaml/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "../Result.hpp"
#include "../always_false.hpp"
#include "../common.hpp"
#include "../parsing/to_single_error_message.hpp"
#include "../parsing/wrap_error.hpp"

namespace rfl {
Expand Down Expand Up @@ -249,12 +250,20 @@ struct Reader {
return err;
}
}
std::vector<Error> errors;
for (size_t i = 0; i < _arr.node_.size(); ++i) {
auto err = _array_reader.read(InputVarType(_arr.node_[i]));
if (err) {
return err;
if constexpr (is_fail_fast<ArrayReader>()) {
return err;
} else {
errors.emplace_back(std::move(*err));
}
}
}
if (!errors.empty()) {
return parsing::to_single_error(std::move(errors));
}
return std::nullopt;
}

Expand Down Expand Up @@ -325,6 +334,15 @@ struct Reader {
}

private:
template <class ArrayReader>
static constexpr bool is_fail_fast() noexcept {
if constexpr (requires { ArrayReader::fail_fast; }) {
return ArrayReader::fail_fast;
} else {
return false;
}
}

/// @brief The YAML document as a string view, used for context and error
/// reporting.
std::string_view yaml_str_;
Expand Down
Loading