Skip to content
Merged
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
2 changes: 2 additions & 0 deletions docs/content/default_val.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ The field is still required. If `favorite_foods` is missing in the input, you ge

The same restriction applies to every struct that is read with the `rfl::DefaultIfMissing` processor, whether it contains an `rfl::DefaultVal` or not.

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.

## When to use

Use `rfl::DefaultVal` when you want a field to be optional at the input side but still available as a value on the resulting object (no std::optional or pointer indirection). It is particularly useful for fields with sensible defaults (for example, a common last name, a default configuration value, or empty containers/strings).
Expand Down
18 changes: 18 additions & 0 deletions include/rfl/internal/is_default_val_v.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define RFL_INTERNAL_ISDEFAULTVAL_HPP_

#include <type_traits>
#include <utility>

#include "../DefaultVal.hpp"
#include "../Rename.hpp"
Expand All @@ -26,6 +27,23 @@ template <class T>
constexpr bool is_default_val_v =
is_default_val<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;

/// Returns the DefaultVal inside a field for which is_default_val_v is true.
template <class T>
const DefaultVal<T>& get_default_val(const DefaultVal<T>& _field) noexcept {
return _field;
}

template <class T>
requires is_default_val_v<T>
const auto& get_default_val(const T& _field) noexcept {
return get_default_val(_field.value());
}

/// The DefaultVal type inside a field for which is_default_val_v is true.
template <class T>
using default_val_t = std::remove_cvref_t<decltype(get_default_val(
std::declval<const std::remove_cvref_t<T>&>()))>;

} // namespace rfl::internal

#endif
91 changes: 78 additions & 13 deletions include/rfl/parsing/NamedTupleParser.hpp

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am pretty sure this is UB...

"Modifying a const object through a non-const access path and referring to a volatile object through a non-volatile glvalue results in undefined behavior."

https://en.cppreference.com/cpp/language/const_cast

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. It was not a UB, but the cast itself was unnecessary.

As far as I understand, this cast was ~the same as this piece in the doc:

image

AI-authored:

The const is only on the pointer: value_initialize_default_val takes const ValueType*, while the field itself isn't const (a NamedTuple with a const field doesn't compile). The storage is the raw buf in NamedTupleParser::read, with no object in it yet, so placement-new creates an object and doesn't modify one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separately:

I've asked Jarvis to scan the codebase searching for the problematic "const modification through a non-const access" and this spot seems to have it.

Filed a PR to fix it.

Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,11 @@ struct NamedTupleParser {
auto ptr = internal::ptr_cast<NamedTuple<FieldTypes...>*>(&buf);
auto view = rfl::to_view(*ptr);
using ViewType = std::remove_cvref_t<decltype(view)>;
auto [set, err] =
Parser<R, W, ViewType, ProcessorsType>::read_view(_r, _var, &view);
// A NamedTuple has no in-class initializers, so a missing bare
// rfl::DefaultVal field is value-initialized, which is what DefaultVal<T>
// means without an initializer.
auto [set, err] = Parser<R, W, ViewType, ProcessorsType>::read_view(
_r, _var, &view, /*_value_initialize_default_vals=*/true);
if (err) [[unlikely]] {
call_destructors_where_necessary(set, &view);
return error(std::move(*err));
Expand All @@ -147,27 +150,34 @@ struct NamedTupleParser {
* @param _r The reader to use.
* @param _var The input variable to read from.
* @param _view The view to read into.
* @param _value_initialize_default_vals Whether a missing rfl::DefaultVal
* field is value-initialized. Otherwise it is required, because
* the default value in the struct's in-class initializer is not
* available.
* @return A pair containing a boolean array indicating which fields were
* found and an optional error.
*/
static RFL_ALWAYS_INLINE
std::pair<std::array<bool, NamedTupleType::size()>, std::optional<Error>>
read_view(const R& _r, const InputVarType& _var,
NamedTuple<FieldTypes...>* _view) noexcept {
NamedTuple<FieldTypes...>* _view,
const bool _value_initialize_default_vals = false) noexcept {
if constexpr (_no_field_names) {
auto arr = _r.to_array(_var);
if (!arr) [[unlikely]] {
auto set = std::array<bool, NamedTupleType::size()>{};
return std::make_pair(set, arr.error());
}
return read_object_or_array(_r, *arr, _view);
return read_object_or_array(_r, *arr, _view,
_value_initialize_default_vals);
} else {
auto obj = _r.to_object(_var);
if (!obj) [[unlikely]] {
auto set = std::array<bool, NamedTupleType::size()>{};
return std::make_pair(set, obj.error());
}
return read_object_or_array(_r, *obj, _view);
return read_object_or_array(_r, *obj, _view,
_value_initialize_default_vals);
}
}

Expand Down Expand Up @@ -331,29 +341,68 @@ struct NamedTupleParser {
template <int _i>
static RFL_ALWAYS_INLINE void handle_one_missing_field(
const std::array<bool, size_>& _found, const NamedTupleType& _view,
std::array<bool, size_>* _set,
FieldErrors<ProcessorsType>* _errors) noexcept {
std::array<bool, size_>* _set, FieldErrors<ProcessorsType>* _errors,
const bool _value_initialize_default_vals) noexcept {
if (should_stop_on_field_error<ProcessorsType>(*_errors)) return;
using FieldType = internal::nth_element_t<_i, FieldTypes...>;
using ValueType = std::remove_reference_t<
std::remove_pointer_t<typename FieldType::Type>>;

if (!std::get<_i>(_found)) {
constexpr bool is_default_val_field =
internal::is_default_val_v<ValueType>;
constexpr bool is_required_field =
!internal::is_default_val_v<ValueType> &&
!internal::is_extra_fields_v<ValueType> &&
!is_default_val_field && !internal::is_extra_fields_v<ValueType> &&
(_all_required || is_required<ValueType, _ignore_empty_containers>());

if constexpr (is_required_field) {
// Without _set, the struct was default-constructed before it was read,
// so the field already holds its default value. With _set, the fields
// are constructed in raw memory. An rfl::NamedTuple has no in-class
// initializers, so a missing bare rfl::DefaultVal field is
// value-initialized there. Otherwise the field's default value is not
// available, so the field is required.
const bool defaults_constructed = _set == nullptr;

if constexpr (is_default_val_field) {
if constexpr (can_value_initialize_default_val<ValueType>()) {
if (_value_initialize_default_vals && !defaults_constructed) {
// The value's constructor can throw, for instance a Validator
// whose zero value is invalid.
try {
::new (rfl::get<_i>(_view)) ValueType();
} catch (std::exception& e) {
constexpr auto current_name =
internal::nth_element_t<_i, FieldTypes...>::name();
_errors->emplace_back(make_nested_error(
std::string(current_name),
"Field named '" + std::string(current_name) +
"' not found, and its default value could not be "
"constructed: " +
e.what()));
return;
}
std::get<_i>(*_set) = true;
return;
}
}
}

if (is_required_field ||
(is_default_val_field && !defaults_constructed)) {
constexpr auto current_name =
internal::nth_element_t<_i, FieldTypes...>::name();
std::stringstream stream;
stream << "Field named '" << std::string(current_name)
<< "' not found.";
_errors->emplace_back(
make_nested_error(std::string(current_name), stream.str()));
return;
}

} else if constexpr (!internal::has_default_val_v<NamedTupleType>) {
if constexpr (!is_required_field && !is_default_val_field) {
if (defaults_constructed) {
return;
}
using NonConstT = std::remove_const_t<ValueType>;
const auto ptr = const_cast<NonConstT*>(rfl::get<_i>(_view));
if constexpr (std::is_nothrow_default_constructible_v<NonConstT>) {
Expand All @@ -380,13 +429,26 @@ struct NamedTupleParser {
}
}

/// Whether a missing rfl::DefaultVal field can be value-initialized. Only
/// an rfl::NamedTuple does this at runtime, and it cannot have const fields,
/// so const fields of structs need no instantiation.
template <class ValueType>
static constexpr bool can_value_initialize_default_val() {
using T = typename internal::default_val_t<ValueType>::Type;
return !std::is_const_v<ValueType> && std::is_default_constructible_v<T> &&
std::is_default_constructible_v<ValueType>;
}

/// Generates error messages for when fields are missing.
template <int... _is>
static RFL_ALWAYS_INLINE void handle_missing_fields(
const std::array<bool, size_>& _found, const NamedTupleType& _view,
std::array<bool, size_>* _set, FieldErrors<ProcessorsType>* _errors,
const bool _value_initialize_default_vals,
std::integer_sequence<int, _is...>) noexcept {
(handle_one_missing_field<_is>(_found, _view, _set, _errors), ...);
(handle_one_missing_field<_is>(_found, _view, _set, _errors,
_value_initialize_default_vals),
...);
}

static auto make_parent(const std::string_view& _name,
Expand All @@ -402,7 +464,8 @@ struct NamedTupleParser {
std::pair<std::array<bool, NamedTupleType::size()>, std::optional<Error>>
read_object_or_array(const R& _r,
const InputObjectOrArrayType& _obj_or_arr,
NamedTupleType* _view) noexcept {
NamedTupleType* _view,
const bool _value_initialize_default_vals) noexcept {
auto found = std::array<bool, NamedTupleType::size()>();
found.fill(false);
auto set = std::array<bool, NamedTupleType::size()>();
Expand All @@ -422,6 +485,7 @@ struct NamedTupleParser {
}
if (!should_stop_on_field_error<ProcessorsType>(errors)) {
handle_missing_fields(found, *_view, &set, &errors,
_value_initialize_default_vals,
std::make_integer_sequence<int, size_>());
}
if constexpr (internal::fail_fast_v<ProcessorsType>) {
Expand Down Expand Up @@ -454,6 +518,7 @@ struct NamedTupleParser {
!internal::default_if_missing_v<ProcessorsType>) {
if (!should_stop_on_field_error<ProcessorsType>(errors)) {
handle_missing_fields(reader.found(), *_view, nullptr, &errors,
/*_value_initialize_default_vals=*/false,
std::make_integer_sequence<int, size_>());
}
}
Expand Down
19 changes: 17 additions & 2 deletions include/rfl/parsing/Parser_default.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
#include "schema/Type.hpp"
#include "schemaful/IsSchemafulReader.hpp"
#include "schemaful/IsSchemafulWriter.hpp"
#include "tagged_union_wrapper_no_ptr.hpp"

namespace rfl::parsing {

Expand Down Expand Up @@ -664,9 +665,19 @@ struct Parser {

} else {
using NamedTupleType = internal::processed_t<U, ProcessorsType>;
// The schema takes the default values of rfl::DefaultVal fields from
// a default-constructed instance. A tagged union wrapper holds its
// alternative through a pointer that U{} leaves null, so we construct
// the wrapper that holds the alternative by value instead.
using DefaultsType = tagged_union_wrapper_no_ptr_t<U>;
if constexpr (internal::has_default_val_v<U>) {
auto t = U{};
auto view = ProcessorsType::template process<U>(to_view(t));
static_assert(requires { DefaultsType{}; },
"A struct with rfl::DefaultVal fields must be "
"default-constructible, because the default values "
"come from a default-constructed instance.");
auto defaults = DefaultsType{};
auto view = ProcessorsType::template process<DefaultsType>(
to_view(defaults));
(*_definitions)[name] =
Parser<R, W, NamedTupleType, ProcessorsType>::to_schema(
_definitions, &view);
Expand Down Expand Up @@ -721,6 +732,10 @@ struct Parser {
/// so we only use it when the DefaultIfMissing preprocessor is added.
static Result<T> read_struct_with_default(const R& _r,
const InputVarType& _var) {
static_assert(!internal::has_default_val_v<T> || requires { T{}; },
"A struct with rfl::DefaultVal fields must be "
"default-constructible, because the default values come "
"from a default-constructed instance.");
try {
// This might fail, for instance if the default value of a field does not
// satisfy its validator. The outer handler is for this line only.
Expand Down
76 changes: 76 additions & 0 deletions tests/json/test_default_val_without_default_construction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <gtest/gtest.h>

#include <optional>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>

// rfl::DefaultVal in places where the struct around it is not
// default-constructed before it is read: rfl::NamedTuple and the schema of an
// rfl::TaggedUnion alternative.
namespace test_default_val_without_default_construction {

struct NoDefaultConstructor {
using ReflectionType = int;
NoDefaultConstructor(int _v) : v_(_v) {}
int reflection() const { return v_; }
int v_;
};

// A NamedTuple has no in-class initializers, so a missing rfl::DefaultVal
// field is value-initialized. It is constructed before it is used.
TEST(json, test_default_val_named_tuple_missing_field) {
using WithString =
rfl::NamedTuple<rfl::Field<"n", int>,
rfl::Field<"s", rfl::DefaultVal<std::string>>,
rfl::Field<"opt", std::optional<int>>>;
const auto str = rfl::json::read<WithString>(R"({"n":3})");
ASSERT_TRUE(str) << str.error().what();
EXPECT_EQ(rfl::get<"s">(str.value()).value(), "");
EXPECT_FALSE(rfl::get<"opt">(str.value()));
}

// If the value cannot be value-initialized, or its constructor throws, the
// missing field is an error.
TEST(json, test_default_val_named_tuple_missing_field_without_zero_value) {
using NoZero =
rfl::NamedTuple<rfl::Field<"o", rfl::DefaultVal<NoDefaultConstructor>>,
rfl::Field<"z", int>>;
const auto no_zero = rfl::json::read<NoZero>(R"({"z":1})");
ASSERT_FALSE(no_zero);
EXPECT_EQ(no_zero.error().what(), std::string("Field named 'o' not found."));

using Invalid = rfl::NamedTuple<
rfl::Field<"v", rfl::DefaultVal<rfl::Validator<int, rfl::Minimum<1>>>>,
rfl::Field<"z", int>>;
const auto invalid = rfl::json::read<Invalid>(R"({"z":1})");
ASSERT_FALSE(invalid);
EXPECT_EQ(invalid.error().what(),
std::string("Field named 'v' not found, and its default value "
"could not be constructed: Value expected to be "
"greater than or equal to 1, but got 0."));

const auto present = rfl::json::read<Invalid>(R"({"v":3,"z":1})");
ASSERT_TRUE(present) << present.error().what();
EXPECT_EQ(rfl::get<"v">(present.value()).value().value(), 3);
}

// The schema reads the defaults of an rfl::TaggedUnion alternative from a
// default-constructed instance of that alternative.
struct DefaultAlternative {
rfl::DefaultVal<std::string> output = std::string("out.csv");
int k = 0;
};

struct OtherAlternative {
std::string q;
};

TEST(json, test_default_val_tagged_union_schema) {
EXPECT_EQ(
(rfl::json::to_schema<
rfl::TaggedUnion<"type", DefaultAlternative, OtherAlternative>>()),
R"({"$schema":"https://json-schema.org/draft/2020-12/schema","anyOf":[{"$ref":"#/$defs/test_default_val_without_default_construction__DefaultAlternative__tagged"},{"$ref":"#/$defs/test_default_val_without_default_construction__OtherAlternative__tagged"}],"$defs":{"test_default_val_without_default_construction__DefaultAlternative__tagged":{"type":"object","properties":{"type":{"type":"string","enum":["DefaultAlternative"]},"output":{"type":"string","default":"out.csv"},"k":{"type":"integer"}},"required":["type","k"]},"test_default_val_without_default_construction__OtherAlternative__tagged":{"type":"object","properties":{"type":{"type":"string","enum":["OtherAlternative"]},"q":{"type":"string"}},"required":["type","q"]}}})");
}

} // namespace test_default_val_without_default_construction
Loading