Skip to content
Draft
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
34 changes: 22 additions & 12 deletions docs/content/default_val.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,41 +52,51 @@ const auto homer = rfl::json::read<Person, rfl::NoFieldNames>(R"(["Homer"])").va
// homer.town == ""
```

## All fields need a valid default value
## Fields without a valid default value

A struct that contains an `rfl::DefaultVal` is default-constructed before it is read. The fields found in the input are then written into it. This means that every field of the struct needs a valid default value, even a required field that is in the input.
A struct that contains an `rfl::DefaultVal` is default-constructed before it is read, so that the fields missing from the input keep their default values. The fields found in the input are then replaced by the values read from it.

This matters when the default value of a field's type is not valid, for instance when the field has a validator:
Some fields have no valid default value, for instance a validator that an empty vector does not satisfy:

```cpp
using Foods = rfl::Validator<std::vector<std::string>, rfl::Size<rfl::Minimum<1>>>;

struct Person {
rfl::DefaultVal<std::string> name = "Homer Simpson";
rfl::Validator<std::vector<std::string>, rfl::Size<rfl::Minimum<1>>> favorite_foods;
Foods favorite_foods;
};

// Fails, even though favorite_foods is in the input.
rfl::json::read<Person>(R"({"favorite_foods":["donuts","pizza"]})");
const auto homer = rfl::json::read<Person>(R"({"favorite_foods":["donuts","pizza"]})");
```

An empty vector does not satisfy `rfl::Size<rfl::Minimum<1>>`, so `Person{}` throws an exception and you get the following error:
`Person{}` throws, but `read` then constructs the struct again with the check of such a validator deferred until the input has been read. If `favorite_foods` is in the input, its invalid default value has been replaced. If it is missing, you get the error `Field named 'favorite_foods' not found.` Outside of `read`, `Person{}` still throws.

A field whose default value is invalid must not be missing, even where a missing field would otherwise get its default value, that is, for an `rfl::DefaultVal` or with the `rfl::DefaultIfMissing` processor:

```
Field named 'favorite_foods' not found, and its default value is invalid: Size validation failed: Value expected to be greater than or equal to 1, but got 0.
```

While the struct is constructed again, every validator that is default-constructed on the same thread keeps its invalid value instead of throwing, wherever it is: in a field, in a nested struct or container, in a temporary, or in a copy such as `rfl::DefaultVal<Foods> other_foods = favorite_foods;`. After reading, `read` checks the validators inside every field that is missing from the input, and fails with the error above if one of them is invalid. `read` looks into structs, containers, `std::optional`, smart pointers, variants, tuples, the wrappers of reflect-cpp and classes with a `reflection()` method. If a missing field holds a class it cannot look into, you get the error `Field named '...' not found, and its default value could not be checked: ...`.

If the struct holds fewer invalid validators than were default-constructed, an invalid value has been used up on the way, for instance a temporary in `int port = Port().value();`, or it is inside a class that `read` cannot look into. A copy can make up for a used-up value, though, and a default member initializer that reads a validator, as in `int count = favorite_foods->size();`, gets its invalid value without an error. A default member initializer that catches the exception of such a validator in order to fall back to another value gets no exception while validation is deferred, so the field must then be in the input. The struct cannot be read, and you get the error below, if an invalid value has been used up, or if `Person{}` throws for another reason, including a validator that is given an invalid value explicitly (as in `Foods favorite_foods = std::vector<std::string>();`):

```
Could not default-construct Person, which is required because it contains rfl::DefaultVal. Give every field a valid default value. Original error: Size validation failed: Value expected to be greater than or equal to 1, but got 0.
```

To avoid this, give the field a valid default value:
In all of these cases, you can give the field a valid default value:

```cpp
struct Person {
rfl::DefaultVal<std::string> name = "Homer Simpson";
rfl::Validator<std::vector<std::string>, rfl::Size<rfl::Minimum<1>>> favorite_foods =
std::vector<std::string>({"none"});
Foods favorite_foods = std::vector<std::string>({"none"});
};
```

The field is still required. If `favorite_foods` is missing in the input, you get the error `Field named 'favorite_foods' not found.` The default value is only used for a missing field if you pass the `rfl::DefaultIfMissing` processor.
The field is still required. The default value is only used for a missing field if you pass the `rfl::DefaultIfMissing` processor.

The same restriction applies to every struct that is read with the `rfl::DefaultIfMissing` processor, whether it contains an `rfl::DefaultVal` or not.
All of this applies to every struct that is read with the `rfl::DefaultIfMissing` processor, whether it contains an `rfl::DefaultVal` or not. In JSON schemas, structs with `rfl::DefaultVal` keep their valid default values; any field whose default value holds an invalid validator or a class that cannot be checked gets no default value and is listed as required (unless `rfl::DefaultIfMissing` omits `required` for the whole schema). If an invalid value has been used up, the schema keeps no default values and marks those fields required. If `Person{}` throws for another reason, `rfl::json::to_schema<Person>()` throws that exception. If such a struct is nested inside another struct, the program ends with `std::terminate`, as it does without this feature.

An `rfl::NamedTuple` has no in-class initializers. A missing `rfl::DefaultVal<T>` field in it is set to `T{}`. If `T` has no default constructor, or `T{}` throws (for instance a validator that rejects its zero value), the field must be present in the input.

Expand Down
9 changes: 6 additions & 3 deletions docs/content/processors.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,12 @@ have gotten had you read the following JSON string:
Because you have not passed a default value to town, the default value
of the type is used instead.

Note that the struct is default-constructed before it is read. Every field
therefore needs a valid default value, even if the field is in the input.
This is explained in the documentation on `rfl::DefaultVal`.
Note that the struct is default-constructed before it is read. A field
whose default value is invalid, such as a validator that the default value
of its type does not satisfy, can be read, but it must not be missing: you
get the error `Field named '...' not found, and its default value is
invalid: ...`. The documentation on `rfl::DefaultVal` explains this and the
cases in which such a struct cannot be read at all.

### `rfl::DurationsAsCount`

Expand Down
44 changes: 42 additions & 2 deletions include/rfl/Validator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

#include "AllOf.hpp"
#include "Result.hpp"
#include "internal/DeferredDefaults.hpp"
#include "internal/HasValidation.hpp"
#include "internal/cold.hpp"

namespace rfl {

Expand Down Expand Up @@ -52,8 +54,10 @@ struct Validator {
}

/// Default constructor - validates a default-constructed T.
/// @throws std::exception if validation of the default value fails
Validator() : value_(ValidationType::validate(T()).value()) {}
/// @throws std::exception if validation of the default value fails, unless
/// the parser is default-constructing a struct to read into, see
/// internal::DeferredDefaults.
Validator() : value_(valid_or_deferred()) {}

/// Move constructor.
/// @param _other The validator to move from
Expand Down Expand Up @@ -197,6 +201,42 @@ struct Validator {

Validator(AlreadyValidated, T&& _value) : value_(std::move(_value)) {}

/// Returns the validated default value. If it is invalid, this throws,
/// unless the parser defers its validation, see internal::DeferredDefaults.
static T valid_or_deferred() {
if constexpr (std::is_copy_constructible_v<T>) {
auto val = T();
auto res = ValidationType::validate(val);
if (!res) [[unlikely]] {
return deferred_or_throw(res.error(), std::move(val));
}
return *std::move(res);
} else {
const auto saved_num = internal::DeferredDefaults::current_num();
auto res = ValidationType::validate(T());
if (!res) [[unlikely]] {
return deferred_or_throw_reconstruct(res.error(), saved_num);
}
return *std::move(res);
}
}

RFL_COLD static T deferred_or_throw(const Error& _err, T&& _val) {
if (!internal::DeferredDefaults::defer(_err)) {
Result<T>(error(_err)).value();
}
return std::move(_val);
}

RFL_COLD static T deferred_or_throw_reconstruct(const Error& _err,
size_t _saved_num) {
internal::DeferredDefaults::reset_num(_saved_num);
if (!internal::DeferredDefaults::defer(_err)) {
Result<T>(error(_err)).value();
}
return T();
}

/// Wraps the result of a validation without throwing if it failed.
static Result<Validator> wrap_validated(Result<T>&& _res) {
return std::move(_res).transform(
Expand Down
87 changes: 87 additions & 0 deletions include/rfl/internal/DeferredDefaults.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#ifndef RFL_INTERNAL_DEFERREDDEFAULTS_HPP_
#define RFL_INTERNAL_DEFERREDDEFAULTS_HPP_

#include <cstddef>
#include <optional>

#include "../Result.hpp"
#include "../common.hpp"

namespace rfl::internal {

/// Parser::read_struct_with_default default-constructs the struct it reads
/// into, to obtain the default values of the fields that are missing from the
/// input. If an rfl::Validator in it has an invalid default value, that
/// throws, even if the input contains the field. The parser then constructs
/// the struct again while a DeferredDefaults is active on this thread. Every
/// Validator that is default-constructed on this thread in the meantime keeps
/// its invalid default value instead of throwing, and is counted here. The
/// parser then checks with parsing::check_default_value that the struct holds
/// at least that many invalid Validators, so that none has been used up on
/// the way, and that none of them is in a field that is missing from the
/// input.
class DeferredDefaults {
public:
/// Makes _deferred the active DeferredDefaults of this thread for as long
/// as the Scope lives. A null _deferred turns deferring off.
class Scope {
public:
explicit Scope(DeferredDefaults* _deferred) noexcept : prev_(active()) {
active() = _deferred;
}

~Scope() { active() = prev_; }

Scope(const Scope&) = delete;
Scope& operator=(const Scope&) = delete;

private:
DeferredDefaults* prev_;
};

/// Called by rfl::Validator() if its default value is invalid. Returns
/// whether the Validator keeps the value instead of throwing.
static bool defer(const Error& _error) {
auto* deferred = active();
if (!deferred) {
return false;
}
if (!deferred->first_error_) {
deferred->first_error_ = _error;
}
++deferred->num_;
return true;
}

/// Returns the current deferred count on this thread, or 0 when inactive.
static size_t current_num() noexcept {
auto* deferred = active();
return deferred ? deferred->num_ : 0;
}

/// Resets the deferred count on this thread to _num when active.
static void reset_num(size_t _num) noexcept {
if (auto* deferred = active()) {
deferred->num_ = _num;
}
}

/// The number of Validators that have kept an invalid default value.
size_t num() const noexcept { return num_; }

/// Why the first of them is invalid.
const std::optional<Error>& first_error() const noexcept {
return first_error_;
}

private:
RFL_API static DeferredDefaults*& active() noexcept;

private:
size_t num_ = 0;
std::optional<Error> first_error_;
};

} // namespace rfl::internal

#endif
57 changes: 47 additions & 10 deletions include/rfl/parsing/NamedTupleParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <array>
#include <exception>
#include <map>
#include <optional>
#include <sstream>
#include <type_traits>
#include <utility>
Expand Down Expand Up @@ -187,23 +188,24 @@ struct NamedTupleParser {
* @param _r The reader to use.
* @param _var The input variable to read from.
* @param _view The view to read into.
* @param _found If not null, receives which fields the input contains.
* @return An optional error.
*/
static std::optional<Error> read_view_with_default(
const R& _r, const InputVarType& _var,
NamedTuple<FieldTypes...>* _view) noexcept {
const R& _r, const InputVarType& _var, NamedTuple<FieldTypes...>* _view,
std::array<bool, size_>* _found = nullptr) noexcept {
if constexpr (_no_field_names) {
auto arr = _r.to_array(_var);
if (!arr) [[unlikely]] {
return arr.error();
}
return read_object_or_array_with_default(_r, *arr, _view);
return read_object_or_array_with_default(_r, *arr, _view, _found);
} else {
auto obj = _r.to_object(_var);
if (!obj) [[unlikely]] {
return obj.error();
}
return read_object_or_array_with_default(_r, *obj, _view);
return read_object_or_array_with_default(_r, *obj, _view, _found);
}
}

Expand Down Expand Up @@ -233,14 +235,19 @@ struct NamedTupleParser {
* @brief Generates the schema for the named tuple.
*
* @param _definitions The map of definitions to add to.
* @param _view A default-constructed instance to take the default values of
* rfl::DefaultVal fields from.
* @param _no_default The fields of _view whose default value is invalid.
* They get no default value and are required.
* @return The schema type.
*/
template <typename View = void>
static schema::Type to_schema(
std::map<std::string, schema::Type>* _definitions,
View* _view = nullptr) noexcept {
View* _view = nullptr,
const std::array<bool, size_>* _no_default = nullptr) noexcept {
SchemaType schema;
build_schema(_definitions, &schema, _view,
build_schema(_definitions, &schema, _view, _no_default,
std::make_integer_sequence<int, size_>());
return schema::Type{schema};
}
Expand Down Expand Up @@ -287,13 +294,18 @@ struct NamedTupleParser {
static void add_field_to_schema(
std::map<std::string, schema::Type>* _definitions,
SchemaType* _schema,
View* _view) noexcept {
View* _view,
const std::array<bool, size_>* _no_default) noexcept {
using F = internal::nth_element_t<_i, FieldTypes...>;
using U = std::remove_cvref_t<typename F::Type>;
if constexpr (!internal::is_skip_v<U> && !internal::is_extra_fields_v<U>) {
// Add default value here
auto s = Parser<R, W, U, ProcessorsType>::to_schema(_definitions);
if constexpr (!std::is_same_v<View, void> && internal::CanEmitDefault<U>) {
if (_no_default && std::get<_i>(*_no_default)) [[unlikely]] {
// Its default value is invalid, so reading requires the field.
remove_default_val(&s);
} else if constexpr (!std::is_same_v<View, void> &&
internal::CanEmitDefault<U>) {
s.variant_.visit([&](auto& value) {
if constexpr (std::is_same_v<std::remove_cvref_t<decltype(value)>,
schema::Type::DefaultVal>) {
Expand All @@ -317,12 +329,34 @@ struct NamedTupleParser {
(add_field_to_object<_is>(_w, _tup, _ptr), ...);
}

/// Replaces the DefaultVal node in _schema by the type it holds, which
/// makes the field required. Description and Deprecated wrap the DefaultVal
/// node in one of their own.
static void remove_default_val(schema::Type* _schema) {
std::optional<schema::Type> inner;
_schema->variant_.visit([&](auto& _s) {
using S = std::remove_cvref_t<decltype(_s)>;
if constexpr (std::is_same_v<S, schema::Type::DefaultVal>) {
inner = *_s.type_;
} else if constexpr (std::is_same_v<S, schema::Type::Description> ||
std::is_same_v<S, schema::Type::Deprecated>) {
remove_default_val(&*_s.type_);
}
});
if (inner) {
*_schema = std::move(*inner);
}
}

template <typename View, int... _is>
static void build_schema(std::map<std::string, schema::Type>* _definitions,
SchemaType* _schema,
View* _view,
const std::array<bool, size_>* _no_default,
std::integer_sequence<int, _is...>) noexcept {
(add_field_to_schema<View, _is>(_definitions, _schema, _view), ...);
(add_field_to_schema<View, _is>(_definitions, _schema, _view,
_no_default),
...);

if constexpr (NamedTupleType::pos_extra_fields() != -1) {
using F = internal::nth_element_t<NamedTupleType::pos_extra_fields(),
Expand Down Expand Up @@ -500,7 +534,7 @@ struct NamedTupleParser {

static std::optional<Error> read_object_or_array_with_default(
const R& _r, const InputObjectOrArrayType& _obj_or_arr,
NamedTupleType* _view) noexcept {
NamedTupleType* _view, std::array<bool, size_>* _found) noexcept {
FieldErrors<ProcessorsType> errors;
const auto reader = ViewReaderWithDefaultType(&_r, _view, &errors);
if constexpr (_no_field_names) {
Expand All @@ -514,6 +548,9 @@ struct NamedTupleParser {
return err;
}
}
if (_found) [[unlikely]] {
*_found = reader.found();
}
if constexpr (internal::has_default_val_v<NamedTupleType> &&
!internal::default_if_missing_v<ProcessorsType>) {
if (!should_stop_on_field_error<ProcessorsType>(errors)) {
Expand Down
Loading
Loading