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
24 changes: 20 additions & 4 deletions include/rfl/parsing/NamedTupleParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define RFL_PARSING_NAMEDTUPLEPARSER_HPP_

#include <array>
#include <exception>
#include <map>
#include <sstream>
#include <type_traits>
Expand Down Expand Up @@ -353,11 +354,26 @@ struct NamedTupleParser {
make_nested_error(std::string(current_name), stream.str()));

} else if constexpr (!internal::has_default_val_v<NamedTupleType>) {
if constexpr (!std::is_const_v<ValueType>) {
::new (rfl::get<_i>(_view)) ValueType();
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>) {
::new (ptr) NonConstT();
} else {
using NonConstT = std::remove_const_t<ValueType>;
::new (const_cast<NonConstT*>(rfl::get<_i>(_view))) NonConstT();
// The default value can throw, for instance a Validator whose
// default value is invalid.
try {
::new (ptr) NonConstT();
} 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;
}
Expand Down
11 changes: 9 additions & 2 deletions include/rfl/parsing/ParserSkip.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef RFL_PARSING_PARSER_SKIP_HPP_
#define RFL_PARSING_PARSER_SKIP_HPP_

#include <exception>
#include <map>
#include <type_traits>

Expand Down Expand Up @@ -39,8 +40,14 @@ struct ParserSkip {
static Result<internal::Skip<T, _skip_serialization, _skip_deserialization>>
read(const R& _r, const InputVarType& _var) noexcept {
if constexpr (_skip_deserialization) {
return internal::Skip<T, _skip_serialization, _skip_deserialization>(
std::remove_cvref_t<T>());
// The default value can throw, for instance a Validator whose default
// value is invalid.
try {
return internal::Skip<T, _skip_serialization, _skip_deserialization>(
std::remove_cvref_t<T>());
} catch (std::exception& e) {
return error(e.what());
}
} else {
const auto to_skip = [&](auto&& _t) {
return internal::Skip<T, _skip_serialization, _skip_deserialization>(
Expand Down
19 changes: 18 additions & 1 deletion include/rfl/parsing/TupleReader.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef RFL_PARSING_TUPLEREADER_HPP_
#define RFL_PARSING_TUPLEREADER_HPP_

#include <exception>
#include <sstream>
#include <string>
#include <type_traits>
Expand Down Expand Up @@ -80,9 +81,25 @@ class TupleReader {
static_cast<size_t>(_i),
"Field " + std::to_string(_i) + " was required, but missing.");
return;
} else {
} else if constexpr (std::is_nothrow_default_constructible_v<
CurrentType>) {
::new (&(rfl::get<_i>(*tuple_))) CurrentType();
++num_set_;
} else {
// The default value can throw, for instance a Validator whose
// default value is invalid.
try {
::new (&(rfl::get<_i>(*tuple_))) CurrentType();
++num_set_;
} catch (std::exception& e) {
*_err = make_nested_error(
static_cast<size_t>(_i),
"Field " + std::to_string(_i) +
" was missing, and its default value could not be "
"constructed: " +
e.what());
return;
}
}
}
handle_missing_fields_impl<_i + 1>(_err);
Expand Down
107 changes: 107 additions & 0 deletions tests/json/test_skip_throwing_default.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include <gtest/gtest.h>

#include <optional>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <stdexcept>
#include <tuple>
#include <vector>

namespace test_skip_throwing_default {

using Positive = rfl::Validator<int, rfl::Minimum<1>>;

using Path = std::vector<rfl::ErrorDetail::PathSegment>;

struct WithSkip {
rfl::Skip<Positive> s;
int y;
};

struct SkipLast {
int y;
rfl::Skip<Positive> s;
};

/// Not required, because its ReflectionType is a std::optional, and counts
/// the instances that are alive.
struct ThrowingDefault {
using ReflectionType = std::optional<int>;

static inline int alive = 0;

ThrowingDefault() { throw std::runtime_error("no default"); }
ThrowingDefault(const ReflectionType&) { ++alive; }
ThrowingDefault(const ThrowingDefault&) { ++alive; }
~ThrowingDefault() { --alive; }

ReflectionType reflection() const { return std::nullopt; }
};

struct WithThrowingDefault {
int y;
ThrowingDefault t;
};

TEST(json, test_skip_throwing_default) {
const auto res = rfl::json::read<WithSkip>(R"({"y": 1, "s": 5})");
ASSERT_FALSE(res);
EXPECT_EQ(res.error().what(),
"Failed to parse field 's': Value expected to be greater than or "
"equal to 1, but got 0.");
}

TEST(json, test_skip_throwing_default_field_absent) {
const auto res = rfl::json::read<WithSkip>(R"({"y": 1})");
ASSERT_FALSE(res);
EXPECT_EQ(res.error().what(),
"Field named 's' 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 details = res.error().details();
ASSERT_EQ(details.size(), 1);
EXPECT_EQ(details[0].path, (Path{"s"}));
EXPECT_EQ(details[0].message, res.error().what());
}

TEST(json, test_skip_throwing_default_field_absent_no_field_names) {
const auto res = rfl::json::read<SkipLast, rfl::NoFieldNames>(R"([1])");
ASSERT_FALSE(res);
EXPECT_EQ(res.error().what(),
"Field named 's' not found, and its default value could not be "
"constructed: Value expected to be greater than or equal to 1, "
"but got 0.");
}

TEST(json, test_skip_throwing_default_tuple_element_absent) {
const auto res =
rfl::json::read<std::tuple<int, rfl::Skip<Positive>>>(R"([1])");
ASSERT_FALSE(res);
EXPECT_EQ(res.error().what(),
"Field 1 was missing, and its default value could not be "
"constructed: Value expected to be greater than or equal to 1, "
"but got 0.");
const auto details = res.error().details();
ASSERT_EQ(details.size(), 1);
EXPECT_EQ(details[0].path, (Path{size_t{1}}));
}

// No destructor may run for the field that could not be constructed.
TEST(json, test_skip_throwing_default_does_not_destroy_the_field) {
const auto res = rfl::json::read<WithThrowingDefault>(R"({"y": 1})");
ASSERT_FALSE(res);
EXPECT_EQ(res.error().what(),
"Field named 't' not found, and its default value could not be "
"constructed: no default");
EXPECT_EQ(ThrowingDefault::alive, 0);

const auto tup =
rfl::json::read<std::tuple<int, ThrowingDefault>>(R"([1])");
ASSERT_FALSE(tup);
EXPECT_EQ(ThrowingDefault::alive, 0);

ASSERT_TRUE(rfl::json::read<WithThrowingDefault>(R"({"y": 1, "t": 2})"));
EXPECT_EQ(ThrowingDefault::alive, 0);
}

} // namespace test_skip_throwing_default