From a2f0e74d4d6f01b871fc72fd477a067ca883cd2a Mon Sep 17 00:00:00 2001 From: Denis Popov Date: Wed, 18 Nov 2026 09:17:30 +0100 Subject: [PATCH 1/3] Accept const argv and ranges of arguments in rfl::cli::read (#784) --- README.md | 7 + docs/content/supported_formats/cli.md | 43 +++- include/rfl/cli/parse_argv.hpp | 234 +++++++++++++---- include/rfl/cli/read.hpp | 112 ++++++--- tests/cli/test_const_argv_and_ranges.cpp | 305 +++++++++++++++++++++++ 5 files changed, 612 insertions(+), 89 deletions(-) create mode 100644 tests/cli/test_const_argv_and_ranges.cpp diff --git a/README.md b/README.md index 29a7399a..9cfdfa22 100644 --- a/README.md +++ b/README.md @@ -353,6 +353,13 @@ int main(int argc, char* argv[]) { Field names are automatically converted from `snake_case` to `kebab-case` (`host_name` matches `--host-name`). +`rfl::cli::read` also takes a range of arguments, such as a `std::vector`, and parses every element ([details](https://munich-data-engineering.github.io/reflect-cpp/cli#passing-a-sequence-of-arguments)): + +```cpp +const std::vector args = {"--host-name=localhost", "--port=8080"}; +const auto config = rfl::cli::read(args); +``` + You can mark fields as positional arguments with `rfl::Positional` and add single-character aliases with `rfl::Short<"x", T>`: ```cpp diff --git a/docs/content/supported_formats/cli.md b/docs/content/supported_formats/cli.md index 4f9abb15..f7672ebe 100644 --- a/docs/content/supported_formats/cli.md +++ b/docs/content/supported_formats/cli.md @@ -4,7 +4,8 @@ breadcrumbs: true # rfl::cli — Command-Line Argument Parser -Parse `argc`/`argv` into any reflectable struct via `rfl::cli::read(argc, argv)`. +Parse `argc`/`argv` into any reflectable struct via `rfl::cli::read(argc, argv)`, +or a sequence of arguments via `rfl::cli::read(args)`. ## Usage @@ -28,6 +29,46 @@ int main(int argc, char* argv[]) { Field names undergo automatic `snake_case` -> `kebab-case` conversion: `host_name` matches `--host-name`. +`argv` may be `char*[]` or `const char*[]`. `argv[0]` is the program name +and is skipped. + +## Passing a sequence of arguments + +When the arguments do not come straight from `main`, pass them as a range, +such as `std::vector`, `std::vector`, +`std::span` or `const char* const[]`, or as a braced list of strings. +Ranges of wide strings (`std::vector`, `const wchar_t*[]`) are +converted to UTF-8. A null pointer in a range or array of pointers is an +error. A braced list holds `std::string_view`s, so a null pointer in it is +undefined behaviour; pass a `std::vector` or an array of pointers instead. + +The parser keeps a `std::string_view` to each narrow argument, so a range of +narrow strings must be a `std::forward_range` of lvalue strings, pointers or +`std::string_view`s. `std::views::istream`, or a view that +yields temporary `std::string`s, does not compile; copy the arguments into a +`std::vector` first. + +**Every element is parsed: unlike `read(argc, argv)`, the first one is not +skipped as the program name.** So `std::span(argv, argc)` would read the +program name as an argument (for a struct with a `rfl::Positional` field, as +its first value). + +```cpp +// ./app serve --host-name=localhost --port=8080 --verbose --tags=a,b +int main(int argc, char* argv[]) { + // argv[0] is the program name, argv[1] a subcommand the program handles. + const auto rest = std::span(argv, argc).subspan(std::min(argc, 2)); + const auto result = rfl::cli::read(rest); +} + +// Arguments from somewhere other than main: +const std::vector args = {"--host-name=localhost", "--port=8080", + "--verbose", "--tags=a,b"}; +const auto result = rfl::cli::read(args); +``` + +`rfl::cli::parse_argv` has the same overloads. + ## Positional arguments Wrap a field with `rfl::Positional` to accept it as a bare (non-flag) argument: diff --git a/include/rfl/cli/parse_argv.hpp b/include/rfl/cli/parse_argv.hpp index abf16cbf..6991abe5 100644 --- a/include/rfl/cli/parse_argv.hpp +++ b/include/rfl/cli/parse_argv.hpp @@ -2,10 +2,15 @@ #define RFL_CLI_PARSE_ARGV_HPP_ #include +#include +#include #include +#include #include #include #include +#include +#include #include #include "../Result.hpp" @@ -28,20 +33,27 @@ inline bool looks_like_option(std::string_view _token) noexcept { namespace internal { -/// Parses command-line arguments as parse_argv does. A token that looks like -/// a negative number (-5, -.5) is a positional argument unless -/// `_negative_numbers_are_options` is true, as it is when the target struct -/// has a short option whose name looks like a number (rfl::Short<"1", ...>). -/// Then such tokens are short options, as in argparse. -inline rfl::Result parse_args(int argc, char* argv[], - bool _negative_numbers_are_options) { - if (argc < 0 || (argc > 0 && !argv)) { - return error("Invalid argc/argv."); - } +/// Parses command-line arguments into categorized buckets. +/// Handles three types of arguments: +/// - Long options: --key=value or --flag (stored in `named`) +/// - Short options: -x value, -x=value, or -x (stored in `short_args`) +/// - Positional arguments: bare arguments not prefixed with dashes (stored in `positional`) +/// The special argument "--" forces all subsequent arguments to be treated as positional. +/// Every element of `_args` is parsed; nothing is skipped as the program name. +/// A token that looks like a negative number (-5, -.5) is a positional +/// argument unless `_negative_numbers_are_options` is true, as it is when the +/// target struct has a short option whose name looks like a number +/// (rfl::Short<"1", ...>). Then such tokens are short options, as in argparse. +/// @param _args The command-line arguments to parse +/// @return A Result containing ParsedArgs with categorized arguments or an error +/// --key=value / --flag → named +/// -x value / -x=value / -x → short_args +/// bare arguments → positional +/// -- → everything after goes to positional +inline rfl::Result parse_string_views( + std::span _args, + bool _negative_numbers_are_options) { ParsedArgs result; - if (argc <= 1) { - return result; - } const auto add_short = [&](std::string _key, std::string _val, std::string_view _raw) -> rfl::Result { @@ -52,10 +64,9 @@ inline rfl::Result parse_args(int argc, char* argv[], return true; }; - const auto args = std::span(argv + 1, argc - 1); bool force_positional = false; - for (size_t i = 0; i < args.size(); ++i) { - const std::string_view arg_raw(args[i]); + for (size_t i = 0; i < _args.size(); ++i) { + const std::string_view arg_raw(_args[i]); if (force_positional) { result.positional.emplace_back(arg_raw); @@ -106,8 +117,8 @@ inline rfl::Result parse_args(int argc, char* argv[], // -x value or -x (bool flag). // Peek at next token: consume as value if it doesn't look like an option. auto val = std::string(); - if (i + 1 < args.size() && !looks_like_option(args[i + 1])) { - val = std::string(args[++i]); + if (i + 1 < _args.size() && !looks_like_option(_args[i + 1])) { + val = std::string(_args[++i]); } const auto r = add_short(std::string(arg), std::move(val), arg_raw); if (!r) { return error(r.error().what()); } @@ -120,58 +131,171 @@ inline rfl::Result parse_args(int argc, char* argv[], return result; } +/// An element type that a `std::string_view` can point into after the +/// iterator has moved on: an lvalue reference to a string, a pointer, or a +/// `std::string_view`. A temporary `std::string` is not one, because the +/// view would dangle. +template +concept StableStringReference = + std::convertible_to && + (std::is_lvalue_reference_v || std::is_pointer_v || + std::same_as, std::string_view>); + +/// A range of narrow strings, such as `std::vector`, +/// `std::span` or `const char*[]`. The parser keeps a +/// `std::string_view` to every element, so the range must be multi-pass +/// (`std::views::istream` is rejected) and must not yield temporary +/// `std::string`s. +template +concept StringRange = + std::ranges::forward_range && + StableStringReference>; + +/// A range of wide strings, such as `std::vector` or +/// `const wchar_t*[]`. Every element is converted to UTF-8 before the next +/// one is read, so single-pass ranges and temporaries are fine here. +template +concept WideStringRange = + std::ranges::input_range && + std::convertible_to, std::wstring_view>; + +template +bool is_null(const S& _str) noexcept { + if constexpr (std::is_pointer_v) { + return _str == nullptr; + } else { + return false; + } +} + +/// Parses every element of `_args`, rejecting null pointers. +/// `_first_index` is the index of the first element in error messages. +template +rfl::Result parse_strings(R&& _args, size_t _first_index, + bool _negative_numbers_are_options) { + std::vector views; + auto i = _first_index; + for (auto&& arg : _args) { + if (is_null(arg)) { + return error("Argument " + std::to_string(i) + " is a null pointer."); + } + const std::string_view view = arg; + views.push_back(view); + ++i; + } + return parse_string_views(views, _negative_numbers_are_options); +} + +/// Converts every element of `_args` to UTF-8 and parses it. +/// `_first_index` is the index of the first element in error messages. +template +rfl::Result parse_wide_strings( + R&& _args, size_t _first_index, bool _negative_numbers_are_options) { + std::vector narrow; + auto i = _first_index; + for (auto&& arg : _args) { + if (is_null(arg)) { + return error("Argument " + std::to_string(i) + " is a null pointer."); + } + const std::wstring_view view = arg; + auto str = rfl::internal::strings::wstring_to_utf8(std::wstring(view)); + if (!str) { + return error("Could not convert argument " + std::to_string(i) + + " from wide to UTF-8."); + } + narrow.emplace_back(std::move(*str)); + ++i; + } + return parse_strings(narrow, _first_index, _negative_numbers_are_options); +} + +/// Validates argc/argv and returns the arguments after argv[0], the program +/// name. +template +rfl::Result> skip_program_name( + int argc, const CharT* const argv[]) { + if (argc < 0 || (argc > 0 && !argv)) { + return error("Invalid argc/argv."); + } + if (argc <= 1) { + return std::span(); + } + return std::span(argv + 1, static_cast(argc - 1)); +} + +/// Parses every element of `_args`, as parse_argv(_args) does. +/// `_negative_numbers_are_options` is as for parse_string_views. +template + requires StringRange || WideStringRange +rfl::Result parse_args(R&& _args, + bool _negative_numbers_are_options) { + if constexpr (StringRange) { + return parse_strings(std::forward(_args), 0, + _negative_numbers_are_options); + } else { + return parse_wide_strings(std::forward(_args), 0, + _negative_numbers_are_options); + } +} + +/// Parses argc/argv, skipping argv[0], as parse_argv(argc, argv) does. +/// `_negative_numbers_are_options` is as for parse_string_views. +template +rfl::Result parse_args(int argc, const CharT* const argv[], + bool _negative_numbers_are_options) { + return skip_program_name(argc, argv).and_then([&](auto _args) { + if constexpr (std::is_same_v) { + return parse_strings(_args, 1, _negative_numbers_are_options); + } else { + return parse_wide_strings(_args, 1, _negative_numbers_are_options); + } + }); +} + } // namespace internal -/// Parses command-line arguments into categorized buckets. -/// Handles three types of arguments: -/// - Long options: --key=value or --flag (stored in `named`) -/// - Short options: -x value, -x=value, or -x (stored in `short_args`) -/// - Positional arguments: bare arguments not prefixed with dashes (stored in `positional`) -/// The special argument "--" forces all subsequent arguments to be treated as positional. +/// Parses a sequence of command-line arguments into categorized buckets. +/// Accepts a range of narrow or wide strings (wide strings are converted +/// to UTF-8) or a braced list of narrow strings. A range of narrow strings +/// must be multi-pass and must not yield temporary `std::string`s, as for +/// rfl::cli::read. Every element is parsed: unlike the argc/argv overloads, +/// the first one is NOT skipped as the program name. /// Without the target struct, a token such as -5 or -.5 is a short option. /// rfl::cli::read, which knows the struct, reads it as a negative number /// unless the struct has a short option like rfl::Short<"1", ...>. +/// @param _args The command-line arguments to parse +/// @return A Result containing ParsedArgs with categorized arguments or an error +template > + requires internal::StringRange || internal::WideStringRange +rfl::Result parse_argv(R&& _args) { + return internal::parse_args(std::forward(_args), true); +} + +/// Parses command-line arguments into categorized buckets, skipping argv[0] +/// (the program name). /// @param argc Number of command-line arguments /// @param argv Array of command-line argument strings /// @return A Result containing ParsedArgs with categorized arguments or an error -/// --key=value / --flag → named -/// -x value / -x=value / -x → short_args -/// bare arguments → positional -/// -- → everything after goes to positional -inline rfl::Result parse_argv(int argc, char* argv[]) { +inline rfl::Result parse_argv(int argc, const char* const argv[]) { return internal::parse_args(argc, argv, true); } -/// Parses wide-character command-line arguments into categorized buckets. -/// Converts each wide string to UTF-8 and delegates to the narrow parse_argv. +inline rfl::Result parse_argv(int argc, char* argv[]) { + return parse_argv(argc, static_cast(argv)); +} + +/// Parses wide-character command-line arguments into categorized buckets, +/// skipping argv[0] (the program name). Converts each wide string to UTF-8. /// @param argc Number of wide-character command-line arguments /// @param argv Array of wide-character command-line argument strings /// @return A Result containing ParsedArgs with UTF-8 encoded arguments -inline rfl::Result parse_argv(int argc, wchar_t* argv[]) { - if (argc < 0 || (argc > 0 && !argv)) { - return error("Invalid argc/argv."); - } - if (argc <= 1) { - return ParsedArgs{}; - } - - std::vector narrow_argv; - narrow_argv.reserve(argc); - std::vector narrow_argv_ptrs; - narrow_argv_ptrs.reserve(argc); - - for (int i = 0; i < argc; ++i) { - const auto str = rfl::internal::strings::wstring_to_utf8(argv[i]); - if (!str) { - return error( - "Could not convert argument " + std::to_string(i) - + " from wide to UTF-8."); - } - narrow_argv.emplace_back(std::move(*str)); - narrow_argv_ptrs.push_back(narrow_argv.back().data()); - } +inline rfl::Result parse_argv(int argc, + const wchar_t* const argv[]) { + return internal::parse_args(argc, argv, true); +} - return parse_argv(argc, narrow_argv_ptrs.data()); +inline rfl::Result parse_argv(int argc, wchar_t* argv[]) { + return parse_argv(argc, static_cast(argv)); } } // namespace rfl::cli diff --git a/include/rfl/cli/read.hpp b/include/rfl/cli/read.hpp index 1d70dda7..d9883f07 100644 --- a/include/rfl/cli/read.hpp +++ b/include/rfl/cli/read.hpp @@ -1,9 +1,12 @@ #ifndef RFL_CLI_READ_HPP_ #define RFL_CLI_READ_HPP_ +#include +#include +#include + #include "../Processors.hpp" #include "../SnakeCaseToKebabCase.hpp" -#include "../internal/strings/utf8_conversions.hpp" #include "Parser.hpp" #include "Reader.hpp" #include "parse_argv.hpp" @@ -11,59 +14,102 @@ namespace rfl::cli { -/// Parses command-line arguments into a struct using reflection. +namespace internal { + +/// True if T has a short option whose name looks like a number, such as +/// rfl::Short<"1", ...>. Then, as in argparse, tokens like -5 and -.5 are +/// short options rather than negative numbers. +template +constexpr bool negative_numbers_are_options() { + using ProcessorsType = Processors; + using NT = rfl::internal::processed_t; + return has_numeric_short_name(std::make_index_sequence{}); +} + +/// Turns categorized arguments into a struct. +template +rfl::Result read_parsed(ParsedArgs _parsed) { + using ProcessorsType = Processors; + return resolve_args(std::move(_parsed)) + .and_then([](auto _args) -> rfl::Result { + const auto r = Reader(); + const auto var = CliVarType{&_args, "", std::nullopt}; + return Parser::read(r, var); + }); +} + +} // namespace internal + +/// Parses a sequence of command-line arguments into a struct using reflection. /// Field names are automatically converted from snake_case to kebab-case for CLI arguments. /// For example, a struct field named `host_name` will match the CLI argument `--host-name`. /// Supports nested objects (e.g., --database.host), arrays (e.g., --ports=8080,8081), /// positional arguments, and short flags. +/// +/// Accepts a range of narrow or wide strings, such as +/// `std::vector`, `std::span` or +/// `const wchar_t*[]`, or a braced list of narrow strings such as +/// `{"--port=8080"}`. Wide strings are converted to UTF-8. The number of +/// arguments is taken from the range. The parser keeps a std::string_view to +/// each narrow argument, so a range of narrow strings must be multi-pass and +/// must yield lvalue strings, pointers or string_views, not temporary +/// `std::string`s. +/// +/// Every element is parsed: unlike `read(argc, argv)`, the first one is NOT +/// skipped as the program name. To pass main()'s arguments, drop argv[0] +/// yourself: `std::span(argv, argc)` would parse the program name as an +/// argument. +/// @tparam T The struct type to parse into +/// @tparam Ps Optional processors to apply during parsing +/// @param _args The command-line arguments to parse +/// @return A Result containing the parsed struct or an error +template > + requires internal::StringRange || internal::WideStringRange +rfl::Result read(R&& _args) { + return internal::parse_args(std::forward(_args), + internal::negative_numbers_are_options()) + .and_then(internal::read_parsed); +} + +/// Parses command-line arguments into a struct using reflection. +/// argv[0] is skipped as the program name. See the sequence overload above +/// for the supported syntax. /// @tparam T The struct type to parse into /// @tparam Ps Optional processors to apply during parsing /// @param argc Number of command-line arguments /// @param argv Array of command-line argument strings /// @return A Result containing the parsed struct or an error +template +rfl::Result read(int argc, const char* const argv[]) { + return internal::parse_args(argc, argv, + internal::negative_numbers_are_options()) + .and_then(internal::read_parsed); +} + template rfl::Result read(int argc, char* argv[]) { - using ProcessorsType = Processors; - using NT = rfl::internal::processed_t; - constexpr bool negative_numbers_are_options = - internal::has_numeric_short_name( - std::make_index_sequence{}); - return internal::parse_args(argc, argv, negative_numbers_are_options) - .and_then(resolve_args) - .and_then([](auto _args) -> rfl::Result { - const auto r = Reader(); - const auto var = CliVarType{&_args, "", std::nullopt}; - return Parser::read(r, var); - }); + return read(argc, static_cast(argv)); } /// Parses wide-character command-line arguments into a struct using reflection. -/// Field names are automatically converted from snake_case to kebab-case for CLI arguments. -/// Wide strings are converted to UTF-8 before parsing. +/// argv[0] is skipped as the program name. Wide strings are converted to +/// UTF-8 before parsing. /// @tparam T The struct type to parse into /// @tparam Ps Optional processors to apply during parsing /// @param argc Number of wide-character command-line arguments /// @param argv Array of wide-character argument strings /// @return A Result containing the parsed struct or an error +template +rfl::Result read(int argc, const wchar_t* const argv[]) { + return internal::parse_args(argc, argv, + internal::negative_numbers_are_options()) + .and_then(internal::read_parsed); +} + template rfl::Result read(int argc, wchar_t* argv[]) { - if (argc < 0 || (argc > 0 && !argv)) { - return error("Invalid argc/argv."); - } - std::vector narrow_argv; - narrow_argv.reserve(argc); - std::vector narrow_argv_ptrs; - narrow_argv_ptrs.reserve(argc); - for (int i = 0; i < argc; ++i) { - const auto str = rfl::internal::strings::wstring_to_utf8(argv[i]); - if (!str) { - return error("Could not convert argument " + std::to_string(i) - + " from wide to UTF-8."); - } - narrow_argv.emplace_back(std::move(*str)); - narrow_argv_ptrs.push_back(narrow_argv.back().data()); - } - return read(argc, narrow_argv_ptrs.data()); + return read(argc, static_cast(argv)); } } // namespace rfl::cli diff --git a/tests/cli/test_const_argv_and_ranges.cpp b/tests/cli/test_const_argv_and_ranges.cpp new file mode 100644 index 00000000..e62cf023 --- /dev/null +++ b/tests/cli/test_const_argv_and_ranges.cpp @@ -0,0 +1,305 @@ +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace test_const_argv_and_ranges { + +struct Config { + std::string host; + int port; +}; + +struct PositionalConfig { + rfl::Positional input_file; + bool verbose; +}; + +struct OptionalConfig { + std::optional host; +}; + +struct CopyConfig { + rfl::Positional from; + rfl::Positional to; +}; + +struct NumberConfig { + rfl::Positional count; +}; + +template +concept Readable = requires(R&& _r) { + rfl::cli::read(std::forward(_r)); + rfl::cli::parse_argv(std::forward(_r)); +}; + +struct CopyString { + std::string operator()(const std::string& _s) const { return _s; } +}; + +struct ViewString { + std::string_view operator()(const std::string& _s) const { return _s; } +}; + +using Strings = std::vector; + +// The parser keeps a std::string_view to every narrow element, so a +// single-pass range (the next read overwrites the element) is rejected... +using IstreamView = + std::ranges::basic_istream_view>; +static_assert(!Readable); + +// ...and so is a range of temporary std::strings (the view would dangle). +using TemporaryStrings = + decltype(std::declval() | std::views::transform(CopyString())); +static_assert(!Readable); + +// Multi-pass ranges of lvalue strings, pointers or string_views are fine. +static_assert(Readable); +static_assert(Readable); +static_assert(Readable&>); +static_assert(Readable>); +static_assert(Readable() | + std::views::transform(ViewString()))>); + +// Wide elements are converted to UTF-8 one at a time, so a single-pass range +// of wide strings is fine. +static_assert(Readable>>); + +// There are (int, char**) overloads, so `&rfl::cli::read` converts to a +// function pointer taking (int, char**). +using ReadFn = rfl::Result (*)(int, char**); +using ReadWideFn = rfl::Result (*)(int, wchar_t**); +using ParseFn = rfl::Result (*)(int, char**); +using ParseWideFn = rfl::Result (*)(int, wchar_t**); + +template +void expect_config(R&& _args) { + const auto result = rfl::cli::read(std::forward(_args)); + ASSERT_TRUE(result) << result.error().what(); + EXPECT_EQ(result.value().host, "localhost"); + EXPECT_EQ(result.value().port, 8080); +} + +TEST(cli, test_address_of_mutable_argv_overloads) { + const ReadFn read = &rfl::cli::read; + const ReadWideFn read_wide = &rfl::cli::read; + const ParseFn parse = &rfl::cli::parse_argv; + const ParseWideFn parse_wide = &rfl::cli::parse_argv; + + char program[] = "program"; + char host[] = "--host=localhost"; + char port[] = "--port=8080"; + char* argv[] = {program, host, port}; + const auto result = read(3, argv); + ASSERT_TRUE(result) << result.error().what(); + EXPECT_EQ(result.value().port, 8080); + ASSERT_TRUE(parse(3, argv)); + EXPECT_NE(read_wide, nullptr); + EXPECT_NE(parse_wide, nullptr); +} + +TEST(cli, test_const_argv) { + const auto read = [](auto _argv) { + const auto result = rfl::cli::read(3, _argv); + ASSERT_TRUE(result) << result.error().what(); + EXPECT_EQ(result.value().host, "localhost"); + EXPECT_EQ(result.value().port, 8080); + ASSERT_TRUE(rfl::cli::parse_argv(3, _argv)); + }; + + const char* argv[] = {"program", "--host=localhost", "--port=8080"}; + read(argv); + const char** pointer = argv; + read(pointer); + const char* const const_argv[] = {"program", "--host=localhost", + "--port=8080"}; + read(const_argv); + const wchar_t* wide[] = {L"program", L"--host=localhost", L"--port=8080"}; + read(wide); +} + +TEST(cli, test_const_argv_still_skips_program_name) { + // argv[0] is the program name, even if it looks like a positional. + const char* argv[] = {"in.txt", "out.txt", "--verbose"}; + const auto result = rfl::cli::read(3, argv); + ASSERT_TRUE(result) << result.error().what(); + EXPECT_EQ(result.value().input_file(), "out.txt"); +} + +TEST(cli, test_const_argv_invalid_argc) { + const char* argv[] = {"program"}; + const auto result = rfl::cli::read(-1, argv); + ASSERT_FALSE(result); + EXPECT_EQ(result.error().what(), "Invalid argc/argv."); +} + +TEST(cli, test_null_element) { + const char* argv[] = {"program", "--host=localhost", nullptr}; + const auto a = rfl::cli::read(3, argv); + ASSERT_FALSE(a); + EXPECT_EQ(a.error().what(), "Argument 2 is a null pointer."); + + const std::vector args = {"--host=localhost", nullptr}; + const auto b = rfl::cli::read(args); + ASSERT_FALSE(b); + EXPECT_EQ(b.error().what(), "Argument 1 is a null pointer."); + + const wchar_t* wide[] = {L"--host=localhost", nullptr}; + const auto c = rfl::cli::read(wide); + ASSERT_FALSE(c); + EXPECT_EQ(c.error().what(), "Argument 1 is a null pointer."); +} + +TEST(cli, test_wide_argv_program_name_is_not_converted) { + // An unpaired surrogate is not valid UTF-16 or UTF-32. + const wchar_t bad[] = {static_cast(0xD800), L'\0'}; + const wchar_t* argv[] = {bad, L"--host=localhost", L"--port=8080"}; + const auto result = rfl::cli::read(3, argv); + ASSERT_TRUE(result) << result.error().what(); + EXPECT_EQ(result.value().port, 8080); + + const wchar_t* argv2[] = {L"program", L"--host=localhost", bad}; + const auto result2 = rfl::cli::read(3, argv2); + ASSERT_FALSE(result2); + EXPECT_EQ(result2.error().what(), + "Could not convert argument 2 from wide to UTF-8."); +} + +TEST(cli, test_sequence_containers) { + expect_config(std::vector{"--host=localhost", "--port=8080"}); + expect_config(std::vector{"--host=localhost", "--port=8080"}); + expect_config( + std::vector{"--host=localhost", "--port=8080"}); + expect_config(std::list{"--host=localhost", "--port=8080"}); + expect_config( + std::array{"--host=localhost", "--port=8080"}); + const char* const array[] = {"--host=localhost", "--port=8080"}; + expect_config(array); + const std::vector storage = {"--host=localhost", + "--port=8080"}; + expect_config(storage | std::views::transform(ViewString())); + + const auto braced = + rfl::cli::read({"--host=localhost", "--port=8080"}); + ASSERT_TRUE(braced) << braced.error().what(); + EXPECT_EQ(braced.value().port, 8080); + + const auto empty = rfl::cli::read({}); + ASSERT_TRUE(empty) << empty.error().what(); + EXPECT_FALSE(empty.value().host); +} + +TEST(cli, test_sequence_does_not_skip_first_element) { + const char* args[] = {"in.txt", "--verbose"}; + const auto result = rfl::cli::read(args); + ASSERT_TRUE(result) << result.error().what(); + EXPECT_EQ(result.value().input_file(), "in.txt"); + EXPECT_TRUE(result.value().verbose); +} + +TEST(cli, test_sequence_first_element_is_an_option) { + // With argc/argv, "--host=localhost" would be dropped as the program name. + const char* args[] = {"--host=localhost", "--port=8080"}; + const auto result = rfl::cli::read(std::span(args)); + ASSERT_TRUE(result) << result.error().what(); + EXPECT_EQ(result.value().host, "localhost"); +} + +TEST(cli, test_sequence_over_whole_argv_parses_program_name) { + // The pitfall the docs warn about: std::span(argv, argc) keeps argv[0]. + const char* argv[] = {"program", "--verbose"}; + const auto result = rfl::cli::read(std::span(argv, 2)); + ASSERT_TRUE(result) << result.error().what(); + EXPECT_EQ(result.value().input_file(), "program"); +} + +TEST(cli, test_sequence_over_rest_of_mutable_argv) { + // The program handles "program serve" itself and passes on the rest. + char program[] = "program"; + char subcommand[] = "serve"; + char host[] = "--host=localhost"; + char port[] = "--port=8080"; + char* argv[] = {program, subcommand, host, port}; + const int argc = 4; + const auto rest = std::span(argv, argc).subspan(2); + const auto result = rfl::cli::read(rest); + ASSERT_TRUE(result) << result.error().what(); + EXPECT_EQ(result.value().host, "localhost"); + EXPECT_EQ(result.value().port, 8080); +} + +TEST(cli, test_sequence_empty) { + const auto result = + rfl::cli::read(std::span()); + ASSERT_TRUE(result) << result.error().what(); + EXPECT_FALSE(result.value().host); +} + +TEST(cli, test_sequence_missing_field) { + const std::vector args = {"--host=localhost"}; + EXPECT_FALSE(rfl::cli::read(args)); +} + +TEST(cli, test_sequence_negative_positional) { + const std::vector args = {"-5"}; + const auto result = rfl::cli::read(args); + ASSERT_TRUE(result) << result.error().what(); + EXPECT_EQ(result.value().count(), -5); +} + +TEST(cli, test_wide_sequence_from_istream) { + std::wistringstream in(L"from.txt to__.txt"); + const auto result = rfl::cli::read( + std::ranges::basic_istream_view>(in)); + ASSERT_TRUE(result) << result.error().what(); + EXPECT_EQ(result.value().from(), "from.txt"); + EXPECT_EQ(result.value().to(), "to__.txt"); +} + +TEST(cli, test_wide_sequence) { + expect_config(std::vector{L"--host=localhost", L"--port=8080"}); + + // The first element is parsed, as with narrow strings. + const wchar_t* args[] = {L"in.txt", L"--verbose"}; + const auto result = rfl::cli::read(args); + ASSERT_TRUE(result) << result.error().what(); + EXPECT_EQ(result.value().input_file(), "in.txt"); +} + +TEST(cli, test_parse_argv_sequence) { + const char* args[] = {"first", "--host=localhost", "-p", "8080"}; + const auto result = rfl::cli::parse_argv(args); + ASSERT_TRUE(result) << result.error().what(); + ASSERT_EQ(result.value().positional.size(), 1u); + EXPECT_EQ(result.value().positional[0], "first"); + EXPECT_EQ(result.value().named.at("host"), "localhost"); + EXPECT_EQ(result.value().short_args.at("p"), "8080"); + + const auto braced = rfl::cli::parse_argv({"first", "--host=localhost"}); + ASSERT_TRUE(braced) << braced.error().what(); + ASSERT_EQ(braced.value().positional.size(), 1u); + EXPECT_EQ(braced.value().positional[0], "first"); +} + +TEST(cli, test_parse_argv_const_argv_skips_program_name) { + const char* argv[] = {"first", "second"}; + const auto result = rfl::cli::parse_argv(2, argv); + ASSERT_TRUE(result) << result.error().what(); + ASSERT_EQ(result.value().positional.size(), 1u); + EXPECT_EQ(result.value().positional[0], "second"); +} + +} // namespace test_const_argv_and_ranges From 62ed7382168025c90c0a58f5204b548baac1719f Mon Sep 17 00:00:00 2001 From: Denis Popov Date: Thu, 19 Nov 2026 15:44:28 +0100 Subject: [PATCH 2/3] Fix rfl::DefaultVal in NamedTuples and TaggedUnion schemas (#786) --- docs/content/default_val.md | 2 + include/rfl/internal/is_default_val_v.hpp | 18 ++++ include/rfl/parsing/NamedTupleParser.hpp | 91 ++++++++++++++++--- include/rfl/parsing/Parser_default.hpp | 19 +++- ...fault_val_without_default_construction.cpp | 76 ++++++++++++++++ 5 files changed, 191 insertions(+), 15 deletions(-) create mode 100644 tests/json/test_default_val_without_default_construction.cpp diff --git a/docs/content/default_val.md b/docs/content/default_val.md index 6b29b52b..817860bb 100644 --- a/docs/content/default_val.md +++ b/docs/content/default_val.md @@ -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` 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). diff --git a/include/rfl/internal/is_default_val_v.hpp b/include/rfl/internal/is_default_val_v.hpp index d0811eb5..01189f63 100644 --- a/include/rfl/internal/is_default_val_v.hpp +++ b/include/rfl/internal/is_default_val_v.hpp @@ -2,6 +2,7 @@ #define RFL_INTERNAL_ISDEFAULTVAL_HPP_ #include +#include #include "../DefaultVal.hpp" #include "../Rename.hpp" @@ -26,6 +27,23 @@ template constexpr bool is_default_val_v = is_default_val>>::value; +/// Returns the DefaultVal inside a field for which is_default_val_v is true. +template +const DefaultVal& get_default_val(const DefaultVal& _field) noexcept { + return _field; +} + +template + requires is_default_val_v +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 +using default_val_t = std::remove_cvref_t&>()))>; + } // namespace rfl::internal #endif diff --git a/include/rfl/parsing/NamedTupleParser.hpp b/include/rfl/parsing/NamedTupleParser.hpp index 835e7abc..90b062ed 100644 --- a/include/rfl/parsing/NamedTupleParser.hpp +++ b/include/rfl/parsing/NamedTupleParser.hpp @@ -130,8 +130,11 @@ struct NamedTupleParser { auto ptr = internal::ptr_cast*>(&buf); auto view = rfl::to_view(*ptr); using ViewType = std::remove_cvref_t; - auto [set, err] = - Parser::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 + // means without an initializer. + auto [set, err] = Parser::read_view( + _r, _var, &view, /*_value_initialize_default_vals=*/true); if (err) [[unlikely]] { call_destructors_where_necessary(set, &view); return error(std::move(*err)); @@ -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::optional> read_view(const R& _r, const InputVarType& _var, - NamedTuple* _view) noexcept { + NamedTuple* _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{}; 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{}; 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); } } @@ -331,20 +341,54 @@ struct NamedTupleParser { template static RFL_ALWAYS_INLINE void handle_one_missing_field( const std::array& _found, const NamedTupleType& _view, - std::array* _set, - FieldErrors* _errors) noexcept { + std::array* _set, FieldErrors* _errors, + const bool _value_initialize_default_vals) noexcept { if (should_stop_on_field_error(*_errors)) return; using FieldType = internal::nth_element_t<_i, FieldTypes...>; using ValueType = std::remove_reference_t< std::remove_pointer_t>; if (!std::get<_i>(_found)) { + constexpr bool is_default_val_field = + internal::is_default_val_v; constexpr bool is_required_field = - !internal::is_default_val_v && - !internal::is_extra_fields_v && + !is_default_val_field && !internal::is_extra_fields_v && (_all_required || is_required()); - 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()) { + 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; @@ -352,8 +396,13 @@ struct NamedTupleParser { << "' not found."; _errors->emplace_back( make_nested_error(std::string(current_name), stream.str())); + return; + } - } else if constexpr (!internal::has_default_val_v) { + if constexpr (!is_required_field && !is_default_val_field) { + if (defaults_constructed) { + return; + } using NonConstT = std::remove_const_t; const auto ptr = const_cast(rfl::get<_i>(_view)); if constexpr (std::is_nothrow_default_constructible_v) { @@ -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 + static constexpr bool can_value_initialize_default_val() { + using T = typename internal::default_val_t::Type; + return !std::is_const_v && std::is_default_constructible_v && + std::is_default_constructible_v; + } + /// Generates error messages for when fields are missing. template static RFL_ALWAYS_INLINE void handle_missing_fields( const std::array& _found, const NamedTupleType& _view, std::array* _set, FieldErrors* _errors, + const bool _value_initialize_default_vals, std::integer_sequence) 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, @@ -402,7 +464,8 @@ struct NamedTupleParser { std::pair, std::optional> 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(); found.fill(false); auto set = std::array(); @@ -422,6 +485,7 @@ struct NamedTupleParser { } if (!should_stop_on_field_error(errors)) { handle_missing_fields(found, *_view, &set, &errors, + _value_initialize_default_vals, std::make_integer_sequence()); } if constexpr (internal::fail_fast_v) { @@ -454,6 +518,7 @@ struct NamedTupleParser { !internal::default_if_missing_v) { if (!should_stop_on_field_error(errors)) { handle_missing_fields(reader.found(), *_view, nullptr, &errors, + /*_value_initialize_default_vals=*/false, std::make_integer_sequence()); } } diff --git a/include/rfl/parsing/Parser_default.hpp b/include/rfl/parsing/Parser_default.hpp index 91cd540d..80d98b31 100644 --- a/include/rfl/parsing/Parser_default.hpp +++ b/include/rfl/parsing/Parser_default.hpp @@ -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 { @@ -664,9 +665,19 @@ struct Parser { } else { using NamedTupleType = internal::processed_t; + // 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; if constexpr (internal::has_default_val_v) { - auto t = U{}; - auto view = ProcessorsType::template process(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( + to_view(defaults)); (*_definitions)[name] = Parser::to_schema( _definitions, &view); @@ -721,6 +732,10 @@ struct Parser { /// so we only use it when the DefaultIfMissing preprocessor is added. static Result read_struct_with_default(const R& _r, const InputVarType& _var) { + static_assert(!internal::has_default_val_v || 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. diff --git a/tests/json/test_default_val_without_default_construction.cpp b/tests/json/test_default_val_without_default_construction.cpp new file mode 100644 index 00000000..d56b6903 --- /dev/null +++ b/tests/json/test_default_val_without_default_construction.cpp @@ -0,0 +1,76 @@ +#include + +#include +#include +#include +#include + +// 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<"s", rfl::DefaultVal>, + rfl::Field<"opt", std::optional>>; + const auto str = rfl::json::read(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<"z", int>>; + const auto no_zero = rfl::json::read(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::Field<"z", int>>; + const auto invalid = rfl::json::read(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(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 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 From 2d334035778d3736d12cbd4f550fc1f78b6e1891 Mon Sep 17 00:00:00 2001 From: clockblocker Date: Tue, 17 Nov 2026 19:57:28 +0100 Subject: [PATCH 3/3] Read structs with defaults that hold a Validator without a valid default Co-Authored-By: Claude Opus 5.5 --- docs/content/default_val.md | 34 +- docs/content/processors.md | 9 +- include/rfl/Validator.hpp | 44 +- include/rfl/internal/DeferredDefaults.hpp | 87 +++ include/rfl/parsing/NamedTupleParser.hpp | 57 +- include/rfl/parsing/Parser_default.hpp | 188 +++++- include/rfl/parsing/ViewReaderWithDefault.hpp | 2 + ...ReaderWithDefaultAndStrippedFieldNames.hpp | 2 + include/rfl/parsing/check_default_value.hpp | 266 ++++++++ src/reflectcpp.cpp | 1 + src/rfl/internal/DeferredDefaults.cpp | 10 + tests/json/test_default_construct_error.cpp | 7 +- .../json/test_default_val_with_validator.cpp | 240 +++++++ ...test_default_val_with_validator_limits.cpp | 608 ++++++++++++++++++ 14 files changed, 1518 insertions(+), 37 deletions(-) create mode 100644 include/rfl/internal/DeferredDefaults.hpp create mode 100644 include/rfl/parsing/check_default_value.hpp create mode 100644 src/rfl/internal/DeferredDefaults.cpp create mode 100644 tests/json/test_default_val_with_validator.cpp create mode 100644 tests/json/test_default_val_with_validator_limits.cpp diff --git a/docs/content/default_val.md b/docs/content/default_val.md index 817860bb..81ea3bb8 100644 --- a/docs/content/default_val.md +++ b/docs/content/default_val.md @@ -52,41 +52,51 @@ const auto homer = rfl::json::read(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, rfl::Size>>; + struct Person { rfl::DefaultVal name = "Homer Simpson"; - rfl::Validator, rfl::Size>> favorite_foods; + Foods favorite_foods; }; -// Fails, even though favorite_foods is in the input. -rfl::json::read(R"({"favorite_foods":["donuts","pizza"]})"); +const auto homer = rfl::json::read(R"({"favorite_foods":["donuts","pizza"]})"); ``` -An empty vector does not satisfy `rfl::Size>`, 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 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();`): ``` 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 name = "Homer Simpson"; - rfl::Validator, rfl::Size>> favorite_foods = - std::vector({"none"}); + Foods favorite_foods = std::vector({"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()` 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` 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. diff --git a/docs/content/processors.md b/docs/content/processors.md index a431bb4e..014c44da 100644 --- a/docs/content/processors.md +++ b/docs/content/processors.md @@ -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` diff --git a/include/rfl/Validator.hpp b/include/rfl/Validator.hpp index 9be58bf6..103aa514 100644 --- a/include/rfl/Validator.hpp +++ b/include/rfl/Validator.hpp @@ -9,7 +9,9 @@ #include "AllOf.hpp" #include "Result.hpp" +#include "internal/DeferredDefaults.hpp" #include "internal/HasValidation.hpp" +#include "internal/cold.hpp" namespace rfl { @@ -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 @@ -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) { + 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(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(error(_err)).value(); + } + return T(); + } + /// Wraps the result of a validation without throwing if it failed. static Result wrap_validated(Result&& _res) { return std::move(_res).transform( diff --git a/include/rfl/internal/DeferredDefaults.hpp b/include/rfl/internal/DeferredDefaults.hpp new file mode 100644 index 00000000..2fd201d5 --- /dev/null +++ b/include/rfl/internal/DeferredDefaults.hpp @@ -0,0 +1,87 @@ +#ifndef RFL_INTERNAL_DEFERREDDEFAULTS_HPP_ +#define RFL_INTERNAL_DEFERREDDEFAULTS_HPP_ + +#include +#include + +#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& first_error() const noexcept { + return first_error_; + } + + private: + RFL_API static DeferredDefaults*& active() noexcept; + + private: + size_t num_ = 0; + std::optional first_error_; +}; + +} // namespace rfl::internal + +#endif diff --git a/include/rfl/parsing/NamedTupleParser.hpp b/include/rfl/parsing/NamedTupleParser.hpp index 90b062ed..1eac6c11 100644 --- a/include/rfl/parsing/NamedTupleParser.hpp +++ b/include/rfl/parsing/NamedTupleParser.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -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 read_view_with_default( - const R& _r, const InputVarType& _var, - NamedTuple* _view) noexcept { + const R& _r, const InputVarType& _var, NamedTuple* _view, + std::array* _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); } } @@ -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 static schema::Type to_schema( std::map* _definitions, - View* _view = nullptr) noexcept { + View* _view = nullptr, + const std::array* _no_default = nullptr) noexcept { SchemaType schema; - build_schema(_definitions, &schema, _view, + build_schema(_definitions, &schema, _view, _no_default, std::make_integer_sequence()); return schema::Type{schema}; } @@ -287,13 +294,18 @@ struct NamedTupleParser { static void add_field_to_schema( std::map* _definitions, SchemaType* _schema, - View* _view) noexcept { + View* _view, + const std::array* _no_default) noexcept { using F = internal::nth_element_t<_i, FieldTypes...>; using U = std::remove_cvref_t; if constexpr (!internal::is_skip_v && !internal::is_extra_fields_v) { // Add default value here auto s = Parser::to_schema(_definitions); - if constexpr (!std::is_same_v && internal::CanEmitDefault) { + 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 && + internal::CanEmitDefault) { s.variant_.visit([&](auto& value) { if constexpr (std::is_same_v, schema::Type::DefaultVal>) { @@ -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 inner; + _schema->variant_.visit([&](auto& _s) { + using S = std::remove_cvref_t; + if constexpr (std::is_same_v) { + inner = *_s.type_; + } else if constexpr (std::is_same_v || + std::is_same_v) { + remove_default_val(&*_s.type_); + } + }); + if (inner) { + *_schema = std::move(*inner); + } + } + template static void build_schema(std::map* _definitions, SchemaType* _schema, View* _view, + const std::array* _no_default, std::integer_sequence) noexcept { - (add_field_to_schema(_definitions, _schema, _view), ...); + (add_field_to_schema(_definitions, _schema, _view, + _no_default), + ...); if constexpr (NamedTupleType::pos_extra_fields() != -1) { using F = internal::nth_element_t read_object_or_array_with_default( const R& _r, const InputObjectOrArrayType& _obj_or_arr, - NamedTupleType* _view) noexcept { + NamedTupleType* _view, std::array* _found) noexcept { FieldErrors errors; const auto reader = ViewReaderWithDefaultType(&_r, _view, &errors); if constexpr (_no_field_names) { @@ -514,6 +548,9 @@ struct NamedTupleParser { return err; } } + if (_found) [[unlikely]] { + *_found = reader.found(); + } if constexpr (internal::has_default_val_v && !internal::default_if_missing_v) { if (!should_stop_on_field_error(errors)) { diff --git a/include/rfl/parsing/Parser_default.hpp b/include/rfl/parsing/Parser_default.hpp index 80d98b31..e261b816 100644 --- a/include/rfl/parsing/Parser_default.hpp +++ b/include/rfl/parsing/Parser_default.hpp @@ -1,12 +1,19 @@ #ifndef RFL_PARSING_PARSER_DEFAULT_HPP_ #define RFL_PARSING_PARSER_DEFAULT_HPP_ +#include +#include +#include #include +#include #include +#include #include +#include #include "../Result.hpp" #include "../always_false.hpp" +#include "../internal/DeferredDefaults.hpp" #include "../internal/HasFromReflectionMethod.hpp" #include "../internal/always_inline.hpp" #include "../internal/cold.hpp" @@ -25,6 +32,8 @@ #include "../internal/to_ptr_named_tuple.hpp" #include "../to_view.hpp" #include "AreReaderAndWriter.hpp" +#include "check_default_value.hpp" +#include "FieldErrors.hpp" #include "MapParser.hpp" #include "Parent.hpp" #include "ParserArray.hpp" @@ -72,6 +81,8 @@ #include "schemaful/IsSchemafulReader.hpp" #include "schemaful/IsSchemafulWriter.hpp" #include "tagged_union_wrapper_no_ptr.hpp" +#include "to_single_error_message.hpp" +#include "wrap_error.hpp" namespace rfl::parsing { @@ -675,12 +686,8 @@ struct Parser { "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( - to_view(defaults)); (*_definitions)[name] = - Parser::to_schema( - _definitions, &view); + schema_with_defaults(_definitions); } else { (*_definitions)[name] = Parser::to_schema( @@ -691,6 +698,42 @@ struct Parser { return Type{Type::Reference{name}}; } + /// The schema of U with the default values of its rfl::DefaultVal fields, + /// which are taken from a default-constructed instance. If constructing it + /// throws because the default value of a Validator is invalid, it is + /// constructed again with such Validators deferred, as read_struct_with_default + /// does (see internal::DeferredDefaults). The fields whose default value is + /// invalid, or cannot be checked, get no default value and are required, as + /// reading requires them. If an invalid default value has been used up while + /// constructing, the schema has no default values at all. Anything else that + /// throws is passed on. + template + static schema::Type schema_with_defaults( + std::map* _definitions) { + internal::DeferredDefaults deferred; + auto defaults = default_construct(&deferred); + auto view = ProcessorsType::template process(to_view(defaults)); + using ViewType = decltype(view); + if (deferred.num() == 0) [[likely]] { + return Parser::to_schema( + _definitions, &view); + } + if (!holds_all_deferred(deferred, view)) { + auto all_no_default = std::array{}; + all_no_default.fill(true); + return Parser::to_schema( + _definitions, &view, &all_no_default); + } + auto no_default = std::array{}; + for_each_field(view, [&](size_t _i, const auto& _field) { + auto check = DefaultValueCheck{}; + check_default_value(_field, &check); + no_default[_i] = check.invalid || check.unchecked; + }); + return Parser::to_schema( + _definitions, &view, &no_default); + } + template static schema::Type make_validated( std::map* _definitions) { @@ -736,16 +779,37 @@ struct Parser { "A struct with rfl::DefaultVal fields must be " "default-constructible, because the default values come " "from a default-constructed instance."); + internal::DeferredDefaults deferred; 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. - auto t = T{}; + // satisfy its validator and validation cannot be deferred. The outer + // handler is for this line only. + auto t = default_construct(&deferred); try { auto view = ProcessorsType::template process(to_view(t)); using ViewType = decltype(view); + const bool any_deferred = deferred.num() != 0; + if (any_deferred && !holds_all_deferred(deferred, view)) [[unlikely]] { + return error(default_construction_error( + deferred.first_error()->what().c_str())); + } + auto found = std::array{}; + bool extra_was_empty = false; + if constexpr (ViewType::pos_extra_fields() != -1) { + extra_was_empty = + rfl::get(view)->empty(); + } auto err = Parser::read_view_with_default( - _r, _var, &view); + _r, _var, &view, any_deferred ? &found : nullptr); + if (!err && any_deferred) [[unlikely]] { + if constexpr (ViewType::pos_extra_fields() != -1) { + if (extra_was_empty) { + found[ViewType::pos_extra_fields()] = true; + } + } + err = check_deferred_defaults(view, found); + } if (err) [[unlikely]] { return error(std::move(*err)); } @@ -758,6 +822,114 @@ struct Parser { } } + /// Returns U{}. If that throws, for instance because the default value of a + /// Validator is invalid, we try again with such Validators deferred to + /// _deferred (see internal::DeferredDefaults). + template + static U default_construct(internal::DeferredDefaults* _deferred) { + // Whether that has been necessary before, so that the first try would + // throw again. This is only a shortcut, so relaxed is enough. + static std::atomic defer_right_away = false; + if (!defer_right_away.load(std::memory_order_relaxed)) { + try { + // A read inside a default member initializer of a struct that we are + // constructing with deferred Validators must not defer its own. + const auto not_deferred = internal::DeferredDefaults::Scope(nullptr); + return U{}; + } catch (std::exception&) { + } + } + struct SetDeferRightAway { + const internal::DeferredDefaults* deferred; + std::atomic* flag; + int uncaught; + ~SetDeferRightAway() { + if (std::uncaught_exceptions() == uncaught && deferred->num() != 0) { + flag->store(true, std::memory_order_relaxed); + } + } + }; + const auto scope = internal::DeferredDefaults::Scope(_deferred); + const auto on_exit = SetDeferRightAway{_deferred, &defer_right_away, + std::uncaught_exceptions()}; + return U{}; + } + + /// Whether the struct still holds as many invalid Validators as have been + /// deferred. If one of them has been used up while constructing it, for + /// instance a temporary in a default member initializer, or is inside a + /// type that check_default_value cannot look into, we cannot tell where its + /// value went. Copies can make up for a used-up one, though. + template + static bool holds_all_deferred(const internal::DeferredDefaults& _deferred, + const ViewType& _view) { + auto check = DefaultValueCheck{.stop_early = false}; + for_each_field(_view, [&](size_t, const auto& _field) { + check_default_value(_field, &check); + }); + return check.num_invalid >= _deferred.num(); + } + + /// Returns an error if the default value of a field that is missing from + /// the input holds a Validator whose value is invalid, or a type that + /// check_default_value cannot look into. The fields that the input contains + /// have been replaced by values that were read as a whole (see + /// ViewReaderWithDefault and ViewReaderWithDefaultAndStrippedFieldNames). + template + static std::optional check_deferred_defaults( + const ViewType& _view, const std::array& _found) { + constexpr auto names = field_names( + std::make_index_sequence()); + FieldErrors errors; + for_each_field(_view, [&](size_t _i, const auto& _field) { + if (_found[_i] || should_stop_on_field_error(errors)) { + return; + } + auto check = DefaultValueCheck{}; + check_default_value(_field, &check); + if (!check.invalid && !check.unchecked) [[likely]] { + return; + } + const auto name = std::string(names[_i]); + const auto why = check.invalid + ? "is invalid: " + check.invalid->what() + : "could not be checked: " + *check.unchecked; + errors.emplace_back(make_nested_error( + name, "Field named '" + name + "' not found, and its default value " + + why)); + }); + if constexpr (internal::fail_fast_v) { + return std::move(errors).error(); + } else { + if (errors.empty()) { + return std::nullopt; + } + return to_single_error(std::move(errors)); + } + } + + template + static constexpr auto field_names(std::index_sequence<_is...>) { + return std::array{ + tuple_element_t<_is, typename ViewType::Fields>::name()...}; + } + + /// Calls _f(i, field) for every field i of _view that points into the + /// struct. + template + static void for_each_field(const ViewType& _view, const F& _f) { + [&](std::index_sequence<_is...>) { + const auto call = [&](std::integral_constant) { + using Type = + typename tuple_element_t<_i, typename ViewType::Fields>::Type; + if constexpr (std::is_pointer_v) { + _f(_i, *rfl::get<_i>(_view)); + } + }; + (call(std::integral_constant{}), ...); + }(std::make_index_sequence()); + } + /// The error returned by read_struct_with_default when T{} throws. On its /// own, the exception reads as if the input was wrong, so we name the struct /// and explain why it was default-constructed. diff --git a/include/rfl/parsing/ViewReaderWithDefault.hpp b/include/rfl/parsing/ViewReaderWithDefault.hpp index c2e03432..a024021d 100644 --- a/include/rfl/parsing/ViewReaderWithDefault.hpp +++ b/include/rfl/parsing/ViewReaderWithDefault.hpp @@ -88,6 +88,8 @@ class ViewReaderWithDefault { _errors->emplace_back(wrap_field_error(name, std::move(res).error())); return; } + // Parser::check_deferred_defaults relies on the field being replaced as + // a whole. if constexpr (std::is_pointer_v) { move_to(rfl::get(*_view), &(*res)); } else { diff --git a/include/rfl/parsing/ViewReaderWithDefaultAndStrippedFieldNames.hpp b/include/rfl/parsing/ViewReaderWithDefaultAndStrippedFieldNames.hpp index 72512ff6..5547de0b 100644 --- a/include/rfl/parsing/ViewReaderWithDefaultAndStrippedFieldNames.hpp +++ b/include/rfl/parsing/ViewReaderWithDefaultAndStrippedFieldNames.hpp @@ -91,6 +91,8 @@ class ViewReaderWithDefaultAndStrippedFieldNames { _errors->emplace_back(wrap_field_error(name, std::move(res).error())); return; } + // Parser::check_deferred_defaults relies on the field being replaced as + // a whole. if constexpr (std::is_pointer_v) { move_to(rfl::get(*_view), &(*res)); } else { diff --git a/include/rfl/parsing/check_default_value.hpp b/include/rfl/parsing/check_default_value.hpp new file mode 100644 index 00000000..447425e7 --- /dev/null +++ b/include/rfl/parsing/check_default_value.hpp @@ -0,0 +1,266 @@ +#ifndef RFL_PARSING_CHECK_DEFAULT_VALUE_HPP_ +#define RFL_PARSING_CHECK_DEFAULT_VALUE_HPP_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../Generic.hpp" +#include "../Result.hpp" +#include "../apply.hpp" +#include "../internal/has_reflection_method_v.hpp" +#include "../internal/has_reflection_type_v.hpp" +#include "../internal/has_reflector.hpp" +#include "../internal/is_bitset.hpp" +#include "../internal/is_default_val_v.hpp" +#include "../internal/is_deprecated.hpp" +#include "../internal/is_description.hpp" +#include "../internal/is_field.hpp" +#include "../internal/is_flatten_field.hpp" +#include "../internal/is_literal.hpp" +#include "../internal/is_named_tuple.hpp" +#include "../internal/is_validator.hpp" +#include "../num_fields.hpp" +#include "../to_view.hpp" +#include "../type_name_t.hpp" +#include "ParserAtomic.hpp" +#include "ParserBox.hpp" +#include "ParserCommented.hpp" +#include "ParserDuration.hpp" +#include "ParserExpected.hpp" +#include "ParserFilepath.hpp" +#include "ParserOptional.hpp" +#include "ParserPair.hpp" +#include "ParserPositional.hpp" +#include "ParserRef.hpp" +#include "ParserReferenceWrapper.hpp" +#include "ParserRename.hpp" +#include "ParserResult.hpp" +#include "ParserRflArray.hpp" +#include "ParserRflTuple.hpp" +#include "ParserRflVariant.hpp" +#include "ParserSharedPtr.hpp" +#include "ParserShort.hpp" +#include "ParserSkip.hpp" +#include "ParserTaggedUnion.hpp" +#include "ParserTimePoint.hpp" +#include "ParserTuple.hpp" +#include "ParserUniquePtr.hpp" +#include "ParserVariant.hpp" + +namespace rfl::parsing { + +/// What check_default_value has found in a default value. +struct DefaultValueCheck { + /// Whether to stop at the first invalid Validator or unchecked type. + bool stop_early = true; + + /// The number of Validators whose value is invalid. + size_t num_invalid = 0; + + /// Why the first of them is invalid. + std::optional invalid; + + /// The first type that we could not look into, so that we cannot tell + /// whether it holds an invalid Validator. + std::optional unchecked; + + /// Visited pointees of raw/shared pointers to avoid cycles and double-counts. + std::unordered_set visited; + + bool done() const noexcept { return stop_early && (invalid || unchecked); } +}; + +/// Types that cannot hold an rfl::Validator, so there is no need to look +/// into them. +template +constexpr bool holds_no_validator_v = + !std::is_pointer_v && + (std::is_arithmetic_v || std::is_enum_v || + std::is_null_pointer_v || std::is_member_pointer_v || + std::is_same_v || std::is_same_v || + std::is_same_v || std::is_same_v || + std::is_same_v || std::is_same_v || + is_duration_v || is_time_point_v || is_filepath_v || + internal::is_literal_v || internal::is_bitset_v || + (std::is_empty_v && !internal::is_validator_v)); + +/// Whether U is a reflect-cpp wrapper whose inner value can be inspected via +/// .value() by const reference without copying. +template +constexpr bool is_value_wrapper_v = + !std::is_pointer_v && + (is_skip_v || internal::is_default_val_v || is_rename_v || + internal::is_flatten_field_v || is_short_v || is_positional_v || + internal::is_field_v || internal::is_description_v || + internal::is_deprecated_v || is_commented_v); + +/// Looks for rfl::Validators whose value is invalid inside _value and records +/// them in _check. The parser uses it when a Validator has kept an invalid +/// default value (see internal::DeferredDefaults). It walks into structs, +/// containers, optionals, pointers, variants, tuples, the wrappers of +/// reflect-cpp, and classes with a reflection() method or an rfl::Reflector. +/// Any other type is recorded as unchecked, unless it cannot hold a Validator +/// at all (see holds_no_validator_v). +template +void check_default_value(const T& _value, DefaultValueCheck* _check) { + using U = std::remove_cvref_t; + + if (_check->done()) { + return; + } + + const auto in_each = [_check](const auto&... _vs) { + (check_default_value(_vs, _check), ...); + }; + + const auto in_alternative = [_check](const auto& _v) { + check_default_value(_v, _check); + }; + + if constexpr (std::is_pointer_v) { + using Pointee = std::remove_pointer_t; + if (!_value || std::is_function_v) { + return; + } + if constexpr (std::is_void_v) { + _check->unchecked = type_name_t().str(); + } else if (_check->visited.insert(static_cast(_value)).second) { + check_default_value(*_value, _check); + } + + } else if constexpr (internal::is_validator_v) { + const auto res = U::ValidationType::validate(_value.value()); + if (!res) { + ++_check->num_invalid; + if (!_check->invalid) { + _check->invalid = res.error(); + } + } + check_default_value(_value.value(), _check); + + } else if constexpr (holds_no_validator_v) { + return; + + } else if constexpr (is_optional_v) { + if (_value) { + check_default_value(*_value, _check); + } + + } else if constexpr (is_unique_ptr_v) { + if (_value) { + if constexpr (requires { *_value; } && + !std::is_void_v>) { + check_default_value(*_value, _check); + } else { + _check->unchecked = type_name_t().str(); + } + } + + } else if constexpr (is_shared_ptr_v) { + if (_value) { + if constexpr (requires { *_value; } && + !std::is_void_v>) { + if (_check->visited.insert(static_cast(_value.get())) + .second) { + check_default_value(*_value, _check); + } + } else { + _check->unchecked = type_name_t().str(); + } + } + + } else if constexpr (is_box_v) { + if (_value.get()) { + check_default_value(*_value, _check); + } + + } else if constexpr (is_ref_v) { + if (_value.get() && + _check->visited.insert(static_cast(_value.get())).second) { + check_default_value(*_value, _check); + } + + } else if constexpr (is_result_v || is_expected_v) { + if (_value) { + if constexpr (requires { *_value; } && + !std::is_void_v>) { + check_default_value(*_value, _check); + } + } else if constexpr (requires { _value.error(); }) { + check_default_value(_value.error(), _check); + } + + } else if constexpr (is_reference_wrapper_v) { + check_default_value(_value.get(), _check); + + } else if constexpr (is_atomic_v) { + check_default_value(_value.load(), _check); + + } else if constexpr (is_variant_v) { + if (!_value.valueless_by_exception()) { + std::visit(in_alternative, _value); + } + + } else if constexpr (is_rfl_variant_v) { + _value.visit(in_alternative); + + } else if constexpr (is_tagged_union_v) { + _value.variant().visit(in_alternative); + + } else if constexpr (is_tuple_v || is_pair_v) { + std::apply(in_each, _value); + + } else if constexpr (is_rfl_tuple_v) { + rfl::apply(in_each, _value); + + } else if constexpr (internal::is_named_tuple_v) { + rfl::apply(in_each, _value.values()); + + } else if constexpr (is_rfl_array_v) { + check_default_value(_value.arr_, _check); + + } else if constexpr (is_value_wrapper_v) { + check_default_value(_value.value(), _check); + + } else if constexpr (std::ranges::range) { + using Element = std::remove_cvref_t>; + if constexpr (!holds_no_validator_v) { + for (const auto& element : _value) { + check_default_value(element, _check); + } + } + + } else if constexpr (internal::has_write_reflector && + requires(const U& _u) { Reflector::from(_u); }) { + check_default_value(Reflector::from(_value), _check); + + } else if constexpr (internal::has_reflection_type_v && + internal::has_reflection_method_v && + requires(const U& _u) { _u.reflection(); }) { + check_default_value(_value.reflection(), _check); + + } else if constexpr (!internal::has_read_reflector && + std::is_class_v && std::is_aggregate_v) { + if constexpr (rfl::num_fields <= 128) { + rfl::apply([&](const auto&... _fields) { in_each(*_fields.value()...); }, + to_view(_value).fields()); + } else { + _check->unchecked = type_name_t().str(); + } + + } else { + _check->unchecked = type_name_t().str(); + } +} + +} // namespace rfl::parsing + +#endif diff --git a/src/reflectcpp.cpp b/src/reflectcpp.cpp index ea76c27c..454de81c 100644 --- a/src/reflectcpp.cpp +++ b/src/reflectcpp.cpp @@ -32,6 +32,7 @@ SOFTWARE. #include "rfl/Generic.cpp" #include "rfl/env/Writer.cpp" #include "rfl/generic/Writer.cpp" +#include "rfl/internal/DeferredDefaults.cpp" #include "rfl/internal/strings/strings.cpp" #include "rfl/parsing/schema/Type.cpp" #include "rfl/parsing/schemaful/tuple_to_object.cpp" diff --git a/src/rfl/internal/DeferredDefaults.cpp b/src/rfl/internal/DeferredDefaults.cpp new file mode 100644 index 00000000..29aa63f2 --- /dev/null +++ b/src/rfl/internal/DeferredDefaults.cpp @@ -0,0 +1,10 @@ +#include "rfl/internal/DeferredDefaults.hpp" + +namespace rfl::internal { + +DeferredDefaults*& DeferredDefaults::active() noexcept { + static thread_local DeferredDefaults* deferred = nullptr; + return deferred; +} + +} // namespace rfl::internal diff --git a/tests/json/test_default_construct_error.cpp b/tests/json/test_default_construct_error.cpp index 4157eadd..6ed4667b 100644 --- a/tests/json/test_default_construct_error.cpp +++ b/tests/json/test_default_construct_error.cpp @@ -11,9 +11,12 @@ namespace test_default_construct_error { using Foods = rfl::Validator, rfl::Size>>; +// Reading defers only the check of a default-constructed Validator (see +// test_default_val_with_validator.cpp), never that of an invalid value that +// is given explicitly. struct Person { rfl::DefaultVal name = "Homer Simpson"; - Foods favorite_foods; + Foods favorite_foods = std::vector(); }; struct PersonWithValidDefault { @@ -23,7 +26,7 @@ struct PersonWithValidDefault { struct Pet { std::string name; - Foods favorite_foods; + Foods favorite_foods = std::vector(); }; struct Family { diff --git a/tests/json/test_default_val_with_validator.cpp b/tests/json/test_default_val_with_validator.cpp new file mode 100644 index 00000000..abd2d13f --- /dev/null +++ b/tests/json/test_default_val_with_validator.cpp @@ -0,0 +1,240 @@ +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace test_default_val_with_validator { + +using Path = std::vector; + +using Foods = + rfl::Validator, rfl::Size>>; +using Positive = rfl::Validator>; + +const std::string foods_error = + "Size validation failed: Value expected to be greater than or equal to 1, " + "but got 0."; +const std::string positive_error = + "Value expected to be greater than or equal to 1, but got 0."; + +struct Person { + rfl::DefaultVal name = "Homer Simpson"; + Foods favorite_foods; +}; + +TEST(json, test_default_val_with_validator) { + const auto homer = + rfl::json::read(R"({"favorite_foods": ["donuts", "pizza"]})"); + ASSERT_TRUE(homer) << homer.error().what(); + EXPECT_EQ(homer->name.value(), "Homer Simpson"); + EXPECT_EQ(homer->favorite_foods.value(), + std::vector({"donuts", "pizza"})); + + const auto empty = rfl::json::read(R"({})"); + ASSERT_FALSE(empty); + EXPECT_EQ(empty.error().what(), "Field named 'favorite_foods' not found."); + + const auto invalid = rfl::json::read(R"({"favorite_foods": []})"); + ASSERT_FALSE(invalid); + EXPECT_EQ(invalid.error().what(), + "Failed to parse field 'favorite_foods': " + foods_error); + + // Reading does not make the default value valid elsewhere. + EXPECT_THROW((void)Person{}, std::exception); + EXPECT_THROW((void)Foods{}, std::exception); +} + +struct Reversed { + Foods favorite_foods; + rfl::DefaultVal name = "Homer Simpson"; +}; + +TEST(json, test_default_val_with_validator_reversed) { + const auto res = rfl::json::read( + R"({"name": "Bart", "favorite_foods": ["pizza"]})"); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_EQ(rfl::json::write(*res), + R"({"favorite_foods":["pizza"],"name":"Bart"})"); +} + +// A missing field keeps its default value, so with DefaultIfMissing or +// DefaultVal that value must be valid. +struct WithDefaultVal { + rfl::DefaultVal id; + rfl::DefaultVal retries; +}; + +TEST(json, test_default_val_with_validator_missing) { + const auto with_dim = + rfl::json::read(R"({})"); + ASSERT_FALSE(with_dim); + EXPECT_EQ(with_dim.error().what(), + "Field named 'favorite_foods' not found, and its default value is " + "invalid: " + + foods_error); + ASSERT_EQ(with_dim.error().details().size(), 1u); + EXPECT_EQ(with_dim.error().details()[0].path, Path({"favorite_foods"})); + + const auto present = rfl::json::read( + R"({"favorite_foods": ["donuts"]})"); + ASSERT_TRUE(present) << present.error().what(); + EXPECT_EQ(present->name.value(), "Homer Simpson"); + + const auto missing = rfl::json::read(R"({"id": 1})"); + ASSERT_FALSE(missing); + EXPECT_EQ(missing.error().what(), + "Field named 'retries' not found, and its default value is " + "invalid: " + + positive_error); + + const auto res = rfl::json::read(R"({"retries": 3})"); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_EQ(res->retries.value().value(), 3); +} + +struct Family { + rfl::DefaultVal town = "Springfield"; + Person head; + std::vector children; +}; + +TEST(json, test_default_val_with_validator_nested) { + const auto res = rfl::json::read( + R"({"head": {"favorite_foods": ["donuts"]}, + "children": [{"name": "Bart", "favorite_foods": ["pizza"]}]})"); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_EQ(res->head.name.value(), "Homer Simpson"); + EXPECT_EQ(res->children.at(0).favorite_foods->at(0), "pizza"); + + const auto missing = + rfl::json::read(R"({"children": []})"); + ASSERT_FALSE(missing); + EXPECT_EQ(missing.error().what(), + "Field named 'head' not found, and its default value is invalid: " + + foods_error); + + // The nested struct is read as a whole, so its own missing field is + // reported, with the full path. + const auto inner_missing = + rfl::json::read(R"({"head": {}})"); + ASSERT_FALSE(inner_missing); + ASSERT_EQ(inner_missing.error().details().size(), 1u); + EXPECT_EQ(inner_missing.error().details()[0].path, + Path({"head", "favorite_foods"})); +} + +struct Flat { + Positive f2; +}; + +struct Containers { + rfl::DefaultVal id; + std::array array; + std::optional optional; + std::variant variant; + rfl::TaggedUnion<"type", Person, Flat> tagged; + rfl::Flatten flat; + rfl::Rename<"renamed", Positive> rename; + rfl::ExtraFields extra; +}; + +TEST(json, test_default_val_with_validator_containers) { + const auto res = rfl::json::read( + R"({"array": [1, 2], "variant": 3, + "tagged": {"type": "Person", "favorite_foods": ["donuts"]}, + "f2": 4, "renamed": 5, "other": 6})"); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_EQ(rfl::json::write(*res), + R"({"id":0,"array":[1,2],"variant":3,)" + R"("tagged":{"type":"Person","name":"Homer Simpson",)" + R"("favorite_foods":["donuts"]},"f2":4,"renamed":5,"other":6})"); + + const auto missing = rfl::json::read( + R"({"array": [1, 2], "variant": 3, "f2": 4, "renamed": 5})"); + ASSERT_FALSE(missing); + EXPECT_EQ(missing.error().what(), + "Field named 'tagged' not found, and its default value is " + "invalid: " + + foods_error); + + const auto invalid_extra = rfl::json::read( + R"({"array": [1, 2], "variant": 3, + "tagged": {"type": "Flat", "f2": 1}, + "f2": 4, "renamed": 5, "other": 0})"); + ASSERT_FALSE(invalid_extra); + EXPECT_EQ(invalid_extra.error().what(), + "Failed to parse field 'other': " + positive_error); +} + +// rfl::Variant() moves its first alternative into place from a temporary. +struct WithRflVariant { + rfl::DefaultVal id; + rfl::Variant v; +}; + +TEST(json, test_default_val_with_validator_rfl_variant) { + const auto res = rfl::json::read(R"({"v": 3})"); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_EQ(rfl::json::write(*res), R"({"id":0,"v":3})"); + + const auto missing = + rfl::json::read(R"({})"); + ASSERT_FALSE(missing); + EXPECT_EQ(missing.error().what(), + "Field named 'v' not found, and its default value is invalid: " + + positive_error); + + EXPECT_THROW((rfl::Variant()), std::exception); + const auto str = rfl::Variant(); + EXPECT_EQ(str.index(), 0); + EXPECT_EQ(rfl::get(str), ""); +} + +TEST(json, test_default_val_with_validator_no_field_names) { + const auto res = rfl::json::read( + R"(["Bart", ["pizza"]])"); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_EQ(res->name.value(), "Bart"); + + const auto missing = + rfl::json::read( + R"(["Bart"])"); + ASSERT_FALSE(missing); + EXPECT_EQ(missing.error().what(), + "Field named 'favorite_foods' not found, and its default value is " + "invalid: " + + foods_error); +} + +struct Snake { + rfl::DefaultVal user_id; + Positive retry_count; +}; + +TEST(json, test_default_val_with_validator_processors) { + using Processors = + rfl::Processors>; + const auto res = rfl::json::read( + R"({"type": "Snake", "retryCount": 2})"); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_EQ(res->retry_count.value(), 2); + + const auto missing = + rfl::json::read( + R"({"type": "Snake"})"); + ASSERT_FALSE(missing); + EXPECT_EQ(missing.error().details().at(0).path, Path({"retryCount"})); + + const auto fail_fast = + rfl::json::read(R"({})"); + ASSERT_FALSE(fail_fast); + EXPECT_EQ(fail_fast.error().details().size(), 1u); +} + +} // namespace test_default_val_with_validator diff --git a/tests/json/test_default_val_with_validator_limits.cpp b/tests/json/test_default_val_with_validator_limits.cpp new file mode 100644 index 00000000..ce2d1a43 --- /dev/null +++ b/tests/json/test_default_val_with_validator_limits.cpp @@ -0,0 +1,608 @@ +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// What does not change, and the limits of reading structs that contain a +// Validator without a valid default value. +namespace test_default_val_with_validator_limits { + +using Path = std::vector; + +using Foods = + rfl::Validator, rfl::Size>>; +using Positive = rfl::Validator>; + +const std::string positive_error = + "Value expected to be greater than or equal to 1, but got 0."; + +std::string construction_error(const std::string& _type) { + return "Could not default-construct " + "test_default_val_with_validator_limits::" + + _type + + ", which is required because it contains rfl::DefaultVal. Give every " + "field a valid default value. Original error: " + + positive_error; +} + +struct Person { + rfl::DefaultVal name = "Homer Simpson"; + Foods favorite_foods; +}; + +// A default member initializer that catches the exception of a Validator +// still sees it, as long as the struct can be default-constructed. If it +// cannot, every Validator that is default-constructed while the struct is +// constructed again keeps its invalid value, wherever it is, so the fallback +// is not used. Whether the Validator is returned through a temporary does not +// matter. +inline Positive port_or_fallback() { + try { + return Positive(); + } catch (std::exception&) { + return Positive(8080); + } +} + +// Deferring does not touch the copy and move operations of Validator. +static_assert(std::is_trivially_copyable_v); + +struct Fallback { + rfl::DefaultVal host = "localhost"; + Positive port = port_or_fallback(); +}; + +struct FallbackAndInvalid { + Positive port = port_or_fallback(); + Positive retries; +}; + +TEST(json, test_default_val_with_validator_limits_fallback) { + const auto res = rfl::json::read(R"({})"); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_EQ(rfl::json::write(*res), R"({"host":"localhost","port":8080})"); + + const auto res2 = rfl::json::read( + R"({"retries": 2})"); + ASSERT_FALSE(res2); + EXPECT_EQ(std::string(res2.error().what()), + "Field named 'port' not found, and its default value is " + "invalid: " + + positive_error); + + // If the input contains the field, it does not matter. + const auto res3 = rfl::json::read( + R"({"port": 443, "retries": 2})"); + ASSERT_TRUE(res3) << res3.error().what(); + EXPECT_EQ(rfl::json::write(*res3), R"({"port":443,"retries":2})"); +} + +// A read inside a default member initializer has its own invalid default +// values deferred, not those of the struct around it. +struct WithNestedRead { + Person person = + rfl::json::read(R"({"favorite_foods": ["beer"]})").value(); + rfl::DefaultVal retries; +}; + +TEST(json, test_default_val_with_validator_limits_nested_read) { + const auto res = rfl::json::read( + R"({"retries": 2})"); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_EQ(res->person.favorite_foods->at(0), "beer"); + EXPECT_EQ(res->retries.value().value(), 2); +} + +// Validators that are default-constructed on other threads still throw. +TEST(json, test_default_val_with_validator_limits_threads) { + std::atomic done = false; + std::atomic constructed = 0; + auto other = std::thread([&] { + while (!done) { + try { + Foods foods; + ++constructed; + } catch (std::exception&) { + } + } + }); + auto readers = std::vector(); + std::atomic failed_reads = 0; + for (int i = 0; i < 2; ++i) { + readers.emplace_back([&] { + for (int j = 0; j < 2000; ++j) { + if (!rfl::json::read(R"({"favorite_foods": ["donuts"]})")) { + ++failed_reads; + } + } + }); + } + for (auto& reader : readers) { + reader.join(); + } + done = true; + other.join(); + EXPECT_EQ(failed_reads, 0); + EXPECT_EQ(constructed, 0); +} + +// The regressions found in the review of an earlier attempt to fix upstream +// issue #700. +struct WithMap { + rfl::DefaultVal x; + std::map m; +}; + +struct NoDefaultConstructor { + NoDefaultConstructor(int _v) : v_(_v) {} + using ReflectionType = int; + int reflection() const { return v_; } + int v_; +}; + +struct NotDefaultConstructible { + NoDefaultConstructor n; + Positive p; +}; + +static_assert(!std::is_default_constructible_v); + +struct ConstValidator { + const rfl::Validator> age; + rfl::DefaultVal name = "Maggie"; +}; + +TEST(json, test_default_val_with_validator_limits_earlier_regressions) { + EXPECT_FALSE(rfl::json::read(R"({"m": {"0": "zero"}})")); + const auto map = rfl::json::read(R"({"m": {"1": "one"}})"); + ASSERT_TRUE(map) << map.error().what(); + EXPECT_EQ(map->m.begin()->first.value(), 1); + + const auto ndc = + rfl::json::read(R"({"n": 3, "p": 4})"); + ASSERT_TRUE(ndc) << ndc.error().what(); + EXPECT_EQ(ndc->p.value(), 4); + + const auto con = rfl::json::read(R"({"age": 8})"); + ASSERT_TRUE(con) << con.error().what(); + EXPECT_EQ(rfl::json::write(*con), R"({"age":8,"name":"Maggie"})"); +} + +// A class that is not reflectable, kept out of reading by rfl::Skip. Reading +// cannot look into it, so with a deferred Validator in the struct, the +// skipped field cannot be checked and reading fails, as it did before. +class Cache { + public: + int hits() const { return hits_; } + + private: + int hits_ = 0; + std::string last_; +}; + +struct WithCache { + rfl::DefaultVal port = 8080; + rfl::Skip cache; + Positive retries; +}; + +struct Schema { + rfl::DefaultVal nullable = false; + std::optional> all_of; + std::optional> properties; + std::unique_ptr items; +}; + +TEST(json, test_default_val_with_validator_limits_other_types) { + const auto cache = rfl::json::read(R"({"retries": 1})"); + ASSERT_FALSE(cache); + EXPECT_EQ(cache.error().what(), + "Field named 'cache' not found, and its default value could not be " + "checked: test_default_val_with_validator_limits::Cache"); + + const auto schema = rfl::json::read( + R"({"properties": {"a": {"nullable": true}}, "items": {}})"); + ASSERT_TRUE(schema) << schema.error().what(); + EXPECT_TRUE(schema->properties->at("a").nullable.value()); + EXPECT_FALSE(schema->items->nullable.value()); +} + +// The JSON schema keeps the valid default values of a struct whose +// Validator has no valid default value. That Validator gets no default value. +struct WithPerson { + int id; + Person person; +}; + +const std::string person_schema = + R"("test_default_val_with_validator_limits__Person":{"type":"object","properties":{"name":{"type":"string","default":"Homer Simpson"},"favorite_foods":{"type":"array","items":{"type":"string"},"minItems":1}},"required":["favorite_foods"]})"; + +TEST(json, test_default_val_with_validator_limits_to_schema) { + EXPECT_EQ( + rfl::json::to_schema(), + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/test_default_val_with_validator_limits__Person","$defs":{)" + + person_schema + "}}"); +} + +TEST(json, test_default_val_with_validator_limits_to_schema_nested) { + EXPECT_EQ( + rfl::json::to_schema(), + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/test_default_val_with_validator_limits__WithPerson","$defs":{)" + + person_schema + + R"(,"test_default_val_with_validator_limits__WithPerson":{"type":"object","properties":{"id":{"type":"integer"},"person":{"$ref":"#/$defs/test_default_val_with_validator_limits__Person"}},"required":["id","person"]}}})"); +} + +// Two Validators without a valid default value, one rfl::DefaultVal with a +// valid one, and one rfl::DefaultVal whose default value is invalid. That one +// gets no default value and is required, as reading requires it. +struct Server { + rfl::DefaultVal retries = 3; + Positive port; + rfl::Validator>> host; + rfl::DefaultVal threads; +}; + +TEST(json, test_default_val_with_validator_limits_to_schema_several) { + EXPECT_EQ( + rfl::json::to_schema(), + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/test_default_val_with_validator_limits__Server","$defs":{"test_default_val_with_validator_limits__Server":{"type":"object","properties":{"retries":{"type":"integer","default":3},"port":{"minimum":1,"type":"integer"},"host":{"type":"string","minLength":1},"threads":{"minimum":1,"type":"integer"}},"required":["port","host","threads"]}}})"); + + const auto server = + rfl::json::read(R"({"port":80,"host":"h","threads":2})"); + ASSERT_TRUE(server) << server.error().what(); + EXPECT_EQ(server.value().retries.value(), 3); + EXPECT_FALSE(rfl::json::read(R"({"port":80,"host":"h"})")); +} + +// [[no_unique_address]] can place a field in the tail padding of another. +struct InTailPadding { + rfl::Validator> a; + int8_t c = 0; +}; + +struct SharesPadding { + [[no_unique_address]] InTailPadding inner; + rfl::DefaultVal p; +}; + +TEST(json, test_default_val_with_validator_limits_no_unique_address) { + const auto res = + rfl::json::read(R"({"inner": {"a": 1, "c": 2}})"); + ASSERT_FALSE(res); + EXPECT_EQ(res.error().details().at(0).path, Path({"p"})); + + const auto res2 = + rfl::json::read(R"({"inner": {"a": 1, "c": 2}, "p": 3})"); + ASSERT_TRUE(res2) << res2.error().what(); + EXPECT_EQ(res2->p.value().value(), 3); +} + +// A field in the input replaces the default value as a whole. Should a field +// ever be read into its default value instead, the invalid default of p would +// survive here. +struct Inner { + Positive p; + rfl::DefaultVal q; +}; + +struct Outer { + rfl::DefaultVal x; + Inner inner; +}; + +TEST(json, test_default_val_with_validator_limits_found_fields_are_replaced) { + const auto res = + rfl::json::read(R"({"inner": {"q": 1}})"); + ASSERT_FALSE(res); + EXPECT_EQ(res.error().details().at(0).path, Path({"inner", "p"})); +} + +// A Validator that is default-constructed elsewhere while the struct is +// constructed, such as a temporary or on the heap, keeps its invalid value as +// well. The fields that hold it must then be in the input. +struct OnTheHeap { + rfl::DefaultVal x; + std::vector v = std::vector(1); +}; + +struct FromTemporary { + rfl::DefaultVal x; + rfl::DefaultVal p = Positive(); +}; + +struct InRflTuple { + rfl::DefaultVal x; + rfl::Tuple t; +}; + +TEST(json, test_default_val_with_validator_limits_not_in_place) { + const auto heap = rfl::json::read(R"({"v": [2]})"); + ASSERT_TRUE(heap) << heap.error().what(); + EXPECT_EQ(heap->v.at(0).value(), 2); + const auto heap_missing = + rfl::json::read(R"({"x": 1})"); + ASSERT_FALSE(heap_missing); + EXPECT_EQ(heap_missing.error().what(), + "Field named 'v' not found, and its default value is invalid: " + + positive_error); + + const auto temp = rfl::json::read(R"({"p": 2})"); + ASSERT_TRUE(temp) << temp.error().what(); + EXPECT_EQ(temp->p.value().value(), 2); + EXPECT_FALSE(rfl::json::read(R"({"x": 1})")); + + const auto tuple = rfl::json::read(R"({"t": [2, 3]})"); + ASSERT_TRUE(tuple) << tuple.error().what(); + EXPECT_EQ(rfl::json::write(*tuple), R"({"x":0,"t":[2,3]})"); + EXPECT_FALSE(rfl::json::read(R"({"x": 1})")); +} + +// A field that is never read keeps its default value. +struct SkipsValidator { + rfl::DefaultVal x; + rfl::Skip s; +}; + +TEST(json, test_default_val_with_validator_limits_skip) { + const auto res = rfl::json::read(R"({"x": 1})"); + ASSERT_FALSE(res); + EXPECT_EQ(res.error().what(), + "Field named 's' not found, and its default value is invalid: " + + positive_error); + + const auto supplied = rfl::json::read(R"({"x": 1, "s": 5})"); + ASSERT_FALSE(supplied); +} + +// A copy of a Validator whose default value is invalid is invalid as well, +// so it must be in the input like the original, wherever it is. +struct Copies { + Positive a; + rfl::DefaultVal b = a; +}; + +struct CopiesToHeap { + rfl::DefaultVal x; + Positive a; + std::vector v = {a}; +}; + +TEST(json, test_default_val_with_validator_limits_copies) { + EXPECT_THROW((void)Copies{}, std::exception); + const auto res = rfl::json::read(R"({"a": 5})"); + ASSERT_FALSE(res); + EXPECT_EQ(res.error().what(), + "Field named 'b' not found, and its default value is invalid: " + + positive_error); + + const auto res2 = rfl::json::read(R"({"a": 5, "b": 6})"); + ASSERT_TRUE(res2) << res2.error().what(); + EXPECT_EQ(res2->b.value().value(), 6); + + const auto heap = rfl::json::read(R"({"a": 5, "v": [6]})"); + ASSERT_TRUE(heap) << heap.error().what(); + EXPECT_EQ(heap->v.at(0).value(), 6); + const auto heap_missing = + rfl::json::read(R"({"a": 5})"); + ASSERT_FALSE(heap_missing); + EXPECT_EQ(heap_missing.error().what(), + "Field named 'v' not found, and its default value is invalid: " + + positive_error); +} + +// An invalid default value that a default member initializer has used up, +// such as a temporary, is not in the struct any more, so reading cannot check +// it and fails as before. +struct FromTemporaryValue { + rfl::DefaultVal x; + int port = Positive().value(); +}; + +// The same holds for an invalid default value inside a class that reading +// cannot look into. +class Opaque { + public: + using ReflectionType = int; + Opaque() = default; + explicit Opaque(int _v) : p_(_v) {} + int get() const { return p_.value(); } + + private: + Positive p_; +}; + +struct WithOpaque { + rfl::DefaultVal x; + rfl::DefaultVal o; +}; + +// A missing field of such a class cannot be checked either, even if it holds +// no Validator. +class Port { + public: + using ReflectionType = int; + Port() = default; + explicit Port(int _v) : v_(_v) {} + int get() const { return v_; } + + private: + int v_ = 80; +}; + +struct WithPort { + Positive retries; + rfl::DefaultVal port; +}; + +TEST(json, test_default_val_with_validator_limits_unchecked) { + EXPECT_THROW((void)FromTemporaryValue{}, std::exception); + const auto temp = rfl::json::read( + R"({"x": 1, "port": 2})"); + ASSERT_FALSE(temp); + EXPECT_EQ(temp.error().what(), construction_error("FromTemporaryValue")); + + EXPECT_THROW((void)WithOpaque{}, std::exception); + const auto opaque = rfl::json::read(R"({"x": 1, "o": 3})"); + ASSERT_FALSE(opaque); + EXPECT_EQ(opaque.error().what(), construction_error("WithOpaque")); + + const auto port = rfl::json::read(R"({"retries": 1})"); + ASSERT_FALSE(port); + EXPECT_EQ(port.error().what(), + "Field named 'port' not found, and its default value could not be " + "checked: test_default_val_with_validator_limits::Port"); + const auto port_present = + rfl::json::read(R"({"retries": 1, "port": 8080})"); + ASSERT_TRUE(port_present) << port_present.error().what(); + EXPECT_EQ(port_present->port.value().get(), 8080); +} + +// The gaps: a default member initializer that reads a Validator gets its +// invalid value, and a copy of an invalid Validator makes up for a used-up +// temporary, which then goes unnoticed. +struct ReadsValidator { + Positive a; + int b = a.value() * 2; +}; + +struct CopyAndTemporary { + Positive a; + rfl::DefaultVal b = a; + int port = Positive().value(); +}; + +TEST(json, test_default_val_with_validator_limits_gaps) { + const auto reads = + rfl::json::read(R"({"a": 5})"); + ASSERT_TRUE(reads) << reads.error().what(); + EXPECT_EQ(reads->b, 0); + + const auto copy = rfl::json::read( + R"({"a": 5, "b": 6})"); + ASSERT_TRUE(copy) << copy.error().what(); + EXPECT_EQ(copy->port, 0); +} + +struct NonCopyableSkip { + rfl::DefaultVal x = 1; + rfl::Skip> handle; + rfl::Skip ptr = nullptr; + rfl::Skip ctx = nullptr; +}; + +struct WithRawPositivePtr { + rfl::DefaultVal x = 1; + Positive* ptr = nullptr; +}; + +struct NonMovableSchema { + rfl::DefaultVal x = 1; + std::atomic counter{0}; +}; + +struct NestedInner { + Positive x; +}; + +struct NonZeroNestedInner { + static rfl::Result validate(NestedInner _i) { + return _i.x.value() > 0 ? rfl::Result(_i) + : rfl::error("bad inner"); + } +}; + +struct NestedValidatorStruct { + rfl::DefaultVal id = 0; + rfl::Validator v; +}; + +struct WithExtraPort { + rfl::DefaultVal x = 1; + Positive p; + rfl::ExtraFields extra; +}; + +struct CycleNode { + std::shared_ptr next; +}; + +inline std::shared_ptr make_cycle() { + auto a = std::make_shared(); + auto b = std::make_shared(); + a->next = b; + b->next = a; + return a; +} + +struct SharedCycleNode { + rfl::DefaultVal id = 0; + Positive p; + rfl::Skip> cycle = make_cycle(); +}; + +struct SharedAlias { + rfl::DefaultVal x = 0; + std::shared_ptr a = std::make_shared(); + std::shared_ptr b = a; + int port = Positive().value(); +}; + +TEST(json, test_default_val_with_validator_limits_regressions) { + // Non-copyable rfl::Skip, raw Positive*, and nullptr void* must compile. + const auto ncs = rfl::json::read(R"({})"); + ASSERT_TRUE(ncs) << ncs.error().what(); + EXPECT_EQ(ncs->x.value(), 1); + + const auto raw_ptr = + rfl::json::read(R"({"ptr": null})"); + ASSERT_TRUE(raw_ptr) << raw_ptr.error().what(); + EXPECT_EQ(raw_ptr->ptr, nullptr); + + // Non-movable struct with DefaultVal must still generate a schema. + const auto nms = rfl::json::to_schema(); + EXPECT_NE(nms.find(R"("default":1)"), std::string::npos) << nms; + + // Nested Validator must not double-count inner deferred Validators. + const auto nested = + rfl::json::read(R"({"v": {"x": 5}})"); + ASSERT_TRUE(nested) << nested.error().what(); + EXPECT_EQ(nested->v.value().x.value(), 5); + EXPECT_FALSE( + (rfl::json::read(R"({})"))); + + // Input-parsed ExtraFields elements must not be rejected as unchecked defaults. + const auto extra = rfl::json::read(R"({"p": 1, "k": 80})"); + ASSERT_TRUE(extra) << extra.error().what(); + EXPECT_EQ(extra->extra.at("k").get(), 80); + + // Cyclic shared_ptr graph must not stack-overflow. + const auto cyc = rfl::json::read(R"({"p": 7})"); + ASSERT_TRUE(cyc) << cyc.error().what(); + EXPECT_EQ(cyc->p.value(), 7); + // Break cycle before destruction to avoid leaking under ASan/LSan. + cyc->cycle.value()->next.reset(); + + // Two shared_ptrs to one invalid Validator must not double-count to mask a + // discarded temporary. + const auto alias = + rfl::json::read(R"({"a": 1, "b": 2, "port": 3})"); + ASSERT_FALSE(alias); + + // When !holds_all_deferred, to_schema must still mark DefaultVal fields + // required (no DefaultVal wrapper left behind). + const auto temp_schema = rfl::json::to_schema(); + EXPECT_NE(temp_schema.find(R"("required":["x","port"])"), std::string::npos) + << temp_schema; +} + +} // namespace test_default_val_with_validator_limits