diff --git a/docs/content/processors.md b/docs/content/processors.md index a431bb4e6..8e7baef31 100644 --- a/docs/content/processors.md +++ b/docs/content/processors.md @@ -41,6 +41,7 @@ reflect-cpp currently supports the following processors: - `rfl::DurationsAsCount` - `rfl::EnumNamesOnly` - `rfl::FailFast` +- `rfl::NoDuplicateKeys` - `rfl::NoExtraFields` - `rfl::NoFieldNames` - `rfl::NoOptionals` @@ -366,6 +367,23 @@ Failed to parse field 'color': Invalid enum value: '2'. Must be one of [red, gre This processor only affects reading. Enum values that cannot be matched to a declared name are still written as their integer representation. +### `rfl::NoDuplicateKeys` + +By default, `rfl::json::read` and `rfl::yaml::read` keep their existing behavior +when an input object or mapping contains repeated keys. Pass +`rfl::NoDuplicateKeys` to reject duplicate keys and report each repeated key as +an error. Works across structs, maps, `rfl::Object`, `rfl::Generic`, +`rfl::FieldVariant`, and `rfl::TaggedUnion`. Passing `rfl::NoDuplicateKeys` to a +reader that does not support duplicate-key detection fails at compile time +(`static_assert`): + +```cpp +const auto json_res = + rfl::json::read(json_str); +const auto yaml_res = + rfl::yaml::read(yaml_str); +``` + ### `rfl::NoExtraFields` When reading an object and the object contains a field that cannot be diff --git a/include/rfl.hpp b/include/rfl.hpp index 432fff2f6..9c5f43b52 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -32,6 +32,7 @@ #include "rfl/Hex.hpp" #include "rfl/Literal.hpp" #include "rfl/NamedTuple.hpp" +#include "rfl/NoDuplicateKeys.hpp" #include "rfl/NoExtraFields.hpp" #include "rfl/NoFieldNames.hpp" #include "rfl/NoOptionals.hpp" diff --git a/include/rfl/NoDuplicateKeys.hpp b/include/rfl/NoDuplicateKeys.hpp new file mode 100644 index 000000000..0663ee0d4 --- /dev/null +++ b/include/rfl/NoDuplicateKeys.hpp @@ -0,0 +1,25 @@ +#ifndef RFL_NODUPLICATEKEYS_HPP_ +#define RFL_NODUPLICATEKEYS_HPP_ + +#include + +namespace rfl { + +/// Reject duplicate keys in input objects/mappings for readers that support +/// duplicate-key detection (JSON and YAML). +/// +/// By default, duplicate keys keep existing reader behavior for backwards +/// compatibility. Pass rfl::NoDuplicateKeys to report each repeated key as an +/// error: +/// rfl::json::read(json) +/// rfl::yaml::read(yaml) +struct NoDuplicateKeys { + template + static auto process(auto&& _named_tuple) { + return std::forward(_named_tuple); + } +}; + +} // namespace rfl + +#endif diff --git a/include/rfl/internal/no_duplicate_keys_v.hpp b/include/rfl/internal/no_duplicate_keys_v.hpp new file mode 100644 index 000000000..2979be19d --- /dev/null +++ b/include/rfl/internal/no_duplicate_keys_v.hpp @@ -0,0 +1,36 @@ +#ifndef RFL_INTERNAL_NODUPLICATEKEYS_HPP_ +#define RFL_INTERNAL_NODUPLICATEKEYS_HPP_ + +#include + +#include "../NoDuplicateKeys.hpp" +#include "../Processors.hpp" + +namespace rfl::internal { + +template +struct no_duplicate_keys : std::false_type {}; + +template <> +struct no_duplicate_keys : std::true_type {}; + +template +struct no_duplicate_keys> + : std::bool_constant<(no_duplicate_keys::value || ...)> {}; + +template +inline constexpr bool no_duplicate_keys_v = + no_duplicate_keys>::value; + +template +inline constexpr bool reader_supports_no_duplicate_keys_v = []() constexpr { + if constexpr (requires { R::supports_no_duplicate_keys; }) { + return static_cast(R::supports_no_duplicate_keys); + } else { + return false; + } +}(); + +} // namespace rfl::internal + +#endif diff --git a/include/rfl/json/Reader.hpp b/include/rfl/json/Reader.hpp index fe4c576b3..864284706 100644 --- a/include/rfl/json/Reader.hpp +++ b/include/rfl/json/Reader.hpp @@ -14,11 +14,16 @@ #include #include #include +#include +#include +#include #include "../Bytestring.hpp" #include "../Result.hpp" #include "../always_false.hpp" #include "../internal/base64.hpp" +#include "../parsing/to_single_error_message.hpp" +#include "../parsing/wrap_error.hpp" namespace rfl { namespace json { @@ -60,6 +65,9 @@ struct Reader { static constexpr bool has_custom_constructor = (requires(InputVarType var) { T::from_json_obj(var); }); + /// Advertises support for the opt-in rfl::NoDuplicateKeys processor. + static constexpr bool supports_no_duplicate_keys = true; + /// Retrieves an element from a JSON array by index. /// @param _idx The zero-based index of the element to retrieve /// @param _arr The JSON array to read from @@ -132,12 +140,39 @@ struct Reader { yyjson_obj_iter iter; yyjson_obj_iter_init(_obj.val_, &iter); yyjson_val* key; - while ((key = yyjson_obj_iter_next(&iter))) { - const auto name = - std::string_view(yyjson_get_str(key), yyjson_get_len(key)); - _object_reader.read(name, InputVarType(yyjson_obj_iter_get_val(key))); + if constexpr (is_no_duplicate_keys()) { + std::unordered_set seen_keys; + seen_keys.reserve(yyjson_obj_size(_obj.val_)); + std::vector duplicate_errors; + while ((key = yyjson_obj_iter_next(&iter))) { + const auto name = + std::string_view(yyjson_get_str(key), yyjson_get_len(key)); + if (!seen_keys.insert(name).second) { + auto dup_err = parsing::make_nested_error( + std::string(name), "Duplicate key '" + std::string(name) + "'."); + if constexpr (requires { + _object_reader.add_error(std::move(dup_err)); + }) { + _object_reader.add_error(std::move(dup_err)); + } else { + duplicate_errors.emplace_back(std::move(dup_err)); + } + continue; + } + _object_reader.read(name, InputVarType(yyjson_obj_iter_get_val(key))); + } + if (!duplicate_errors.empty()) { + return parsing::to_single_error(std::move(duplicate_errors)); + } + return std::nullopt; + } else { + while ((key = yyjson_obj_iter_next(&iter))) { + const auto name = + std::string_view(yyjson_get_str(key), yyjson_get_len(key)); + _object_reader.read(name, InputVarType(yyjson_obj_iter_get_val(key))); + } + return std::nullopt; } - return std::nullopt; } /// Converts a JSON value to a basic C++ type. @@ -235,6 +270,16 @@ struct Reader { return error(e.what()); } } + + private: + template + static constexpr bool is_no_duplicate_keys() noexcept { + if constexpr (requires { ObjectReader::no_duplicate_keys; }) { + return ObjectReader::no_duplicate_keys; + } else { + return false; + } + } }; } // namespace json diff --git a/include/rfl/parsing/FieldVariantReader.hpp b/include/rfl/parsing/FieldVariantReader.hpp index b7454b1a7..068e5528d 100644 --- a/include/rfl/parsing/FieldVariantReader.hpp +++ b/include/rfl/parsing/FieldVariantReader.hpp @@ -10,6 +10,7 @@ #include "../Result.hpp" #include "../Variant.hpp" #include "../internal/is_array.hpp" +#include "../internal/no_duplicate_keys_v.hpp" #include "../internal/nth_element_t.hpp" #include "Parser_base.hpp" @@ -23,6 +24,12 @@ class FieldVariantReader { using ResultType = Result; public: + static constexpr bool no_duplicate_keys = + internal::no_duplicate_keys_v; + static_assert(!no_duplicate_keys || + internal::reader_supports_no_duplicate_keys_v, + "rfl::NoDuplicateKeys is not supported by this reader."); + /** * @brief Constructor. * diff --git a/include/rfl/parsing/MapReader.hpp b/include/rfl/parsing/MapReader.hpp index fe1e8ffdc..587db6eae 100644 --- a/include/rfl/parsing/MapReader.hpp +++ b/include/rfl/parsing/MapReader.hpp @@ -10,6 +10,7 @@ #include "../Result.hpp" #include "../always_false.hpp" #include "../internal/has_reflection_type_v.hpp" +#include "../internal/no_duplicate_keys_v.hpp" #include "Parser_base.hpp" #include "wrap_error.hpp" @@ -24,6 +25,12 @@ class MapReader { std::remove_cvref_t; public: + static constexpr bool no_duplicate_keys = + internal::no_duplicate_keys_v; + static_assert(!no_duplicate_keys || + internal::reader_supports_no_duplicate_keys_v, + "rfl::NoDuplicateKeys is not supported by this reader."); + /** * @brief Constructor. * @@ -52,6 +59,14 @@ class MapReader { } } + /** + * @brief Records a field-level error reported directly by the reader (for + * example, a duplicate key). + */ + void add_error(Error&& _err) const noexcept { + errors_->emplace_back(std::move(_err)); + } + private: template Result key_to_numeric(auto& _pair) const noexcept { diff --git a/include/rfl/parsing/Parser_default.hpp b/include/rfl/parsing/Parser_default.hpp index 80d98b31a..6626e270b 100644 --- a/include/rfl/parsing/Parser_default.hpp +++ b/include/rfl/parsing/Parser_default.hpp @@ -20,6 +20,7 @@ #include "../internal/is_description.hpp" #include "../internal/is_literal.hpp" #include "../internal/is_validator.hpp" +#include "../internal/no_duplicate_keys_v.hpp" #include "../internal/processed_t.hpp" #include "../internal/ptr_cast.hpp" #include "../internal/to_ptr_named_tuple.hpp" @@ -92,6 +93,9 @@ struct Parser { * @return A Result containing the parsed value or an error. */ static RFL_ALWAYS_INLINE auto read(const R& _r, const InputVarType& _var) { + static_assert(!internal::no_duplicate_keys_v || + internal::reader_supports_no_duplicate_keys_v, + "rfl::NoDuplicateKeys is not supported by this reader."); if constexpr (internal::is_basic_type_v) { return ParserBasicType::read(_r, _var); diff --git a/include/rfl/parsing/ViewReader.hpp b/include/rfl/parsing/ViewReader.hpp index 84ee4b826..4a1661b44 100644 --- a/include/rfl/parsing/ViewReader.hpp +++ b/include/rfl/parsing/ViewReader.hpp @@ -11,6 +11,7 @@ #include "../Tuple.hpp" #include "../internal/always_inline.hpp" #include "../internal/is_array.hpp" +#include "../internal/no_duplicate_keys_v.hpp" #include "../internal/no_extra_fields_v.hpp" #include "FieldErrors.hpp" #include "Parser_base.hpp" @@ -25,6 +26,12 @@ class ViewReader { using InputVarType = typename R::InputVarType; using ViewType = _ViewType; + static constexpr bool no_duplicate_keys = + internal::no_duplicate_keys_v; + static_assert(!no_duplicate_keys || + internal::reader_supports_no_duplicate_keys_v, + "rfl::NoDuplicateKeys is not supported by this reader."); + static constexpr size_t size_ = ViewType::size(); /** @@ -55,6 +62,16 @@ class ViewReader { std::make_integer_sequence()); } + /** + * @brief Records a field-level error reported directly by the reader (for + * example, a duplicate key). + */ + void add_error(Error&& _err) const { + if (should_stop_on_field_error(*errors_)) return; + errors_->reserve(size_); + errors_->emplace_back(std::move(_err)); + } + /** * @brief Reads a single field into the view. * diff --git a/include/rfl/parsing/ViewReaderWithDefault.hpp b/include/rfl/parsing/ViewReaderWithDefault.hpp index c2e034322..3c04ea350 100644 --- a/include/rfl/parsing/ViewReaderWithDefault.hpp +++ b/include/rfl/parsing/ViewReaderWithDefault.hpp @@ -11,6 +11,7 @@ #include "../Ref.hpp" #include "../Tuple.hpp" #include "../internal/is_array.hpp" +#include "../internal/no_duplicate_keys_v.hpp" #include "../internal/no_extra_fields_v.hpp" #include "FieldErrors.hpp" #include "Parser_base.hpp" @@ -24,6 +25,12 @@ class ViewReaderWithDefault { using InputVarType = typename R::InputVarType; using ViewType = _ViewType; + static constexpr bool no_duplicate_keys = + internal::no_duplicate_keys_v; + static_assert(!no_duplicate_keys || + internal::reader_supports_no_duplicate_keys_v, + "rfl::NoDuplicateKeys is not supported by this reader."); + static constexpr size_t size_ = ViewType::size(); /** @@ -60,6 +67,15 @@ class ViewReaderWithDefault { std::make_integer_sequence()); } + /** + * @brief Records a field-level error reported directly by the reader (for + * example, a duplicate key). + */ + void add_error(Error&& _err) const { + if (should_stop_on_field_error(*errors_)) return; + errors_->emplace_back(std::move(_err)); + } + /** * @brief Returns the size of the view. * diff --git a/include/rfl/yaml/Reader.hpp b/include/rfl/yaml/Reader.hpp index c8625c04a..2dc8389a6 100644 --- a/include/rfl/yaml/Reader.hpp +++ b/include/rfl/yaml/Reader.hpp @@ -14,10 +14,14 @@ #include #include #include +#include +#include #include #include "../Result.hpp" #include "../always_false.hpp" +#include "../parsing/to_single_error_message.hpp" +#include "../parsing/wrap_error.hpp" namespace rfl { namespace yaml { @@ -59,6 +63,9 @@ struct Reader { static constexpr bool has_custom_constructor = (requires(InputVarType var) { T::from_yaml_obj(var); }); + /// @brief Advertises support for the opt-in rfl::NoDuplicateKeys processor. + static constexpr bool supports_no_duplicate_keys = true; + /// @brief Constructs a Reader from a YAML string view. /// @param _yaml_str The YAML document as a string view. Reader(const std::string_view& _yaml_str) noexcept : yaml_str_(_yaml_str) {} @@ -182,15 +189,46 @@ struct Reader { template std::optional read_object(const ObjectReader& _object_reader, const InputObjectType& _obj) const noexcept { - for (const auto& p : _obj.node_) { - try { - const auto k = p.first.as(); + if constexpr (is_no_duplicate_keys()) { + std::unordered_set seen_keys; + seen_keys.reserve(_obj.node_.size()); + std::vector duplicate_errors; + for (const auto& p : _obj.node_) { + std::string k; + try { + k = p.first.as(); + } catch (const std::exception&) { + continue; + } + if (!seen_keys.insert(k).second) { + auto dup_err = + parsing::make_nested_error(k, "Duplicate key '" + k + "'."); + if constexpr (requires { + _object_reader.add_error(std::move(dup_err)); + }) { + _object_reader.add_error(std::move(dup_err)); + } else { + duplicate_errors.emplace_back(std::move(dup_err)); + } + continue; + } _object_reader.read(std::string_view(k), InputVarType(p.second)); - } catch (std::exception& e) { - continue; } + if (!duplicate_errors.empty()) { + return parsing::to_single_error(std::move(duplicate_errors)); + } + return std::nullopt; + } else { + for (const auto& p : _obj.node_) { + try { + const auto k = p.first.as(); + _object_reader.read(std::string_view(k), InputVarType(p.second)); + } catch (std::exception&) { + continue; + } + } + return std::nullopt; } - return std::nullopt; } /// @brief Converts a YAMLInputVar to a YAMLInputObject if it is a mapping. @@ -221,6 +259,15 @@ struct Reader { } private: + template + static constexpr bool is_no_duplicate_keys() noexcept { + if constexpr (requires { ObjectReader::no_duplicate_keys; }) { + return ObjectReader::no_duplicate_keys; + } else { + return false; + } + } + /// @brief The YAML document as a string view, used for context and error /// reporting. std::string_view yaml_str_; diff --git a/tests/json/test_no_duplicate_keys.cpp b/tests/json/test_no_duplicate_keys.cpp new file mode 100644 index 000000000..699f6a270 --- /dev/null +++ b/tests/json/test_no_duplicate_keys.cpp @@ -0,0 +1,160 @@ +#include + +#include +#include +#include +#include +#include +#include + +namespace test_json_no_duplicate_keys { + +static_assert(rfl::internal::reader_supports_no_duplicate_keys_v< + rfl::json::Reader>); +static_assert(!rfl::internal::reader_supports_no_duplicate_keys_v< + rfl::generic::Reader>); + +using Path = std::vector; + +struct Item { + std::string name; + int code; +}; + +struct Entry { + std::string name; + int code; + int port; +}; + +struct WithDefault { + int code = 0; + std::string name = "default"; +}; + +struct Circle { + using Tag = rfl::Literal<"circle">; + double radius; +}; + +struct Square { + using Tag = rfl::Literal<"square">; + double side; +}; + +using Shape = rfl::TaggedUnion<"kind", Circle, Square>; + +TEST(json, test_duplicate_keys_accepted_by_default_for_backwards_compat) { + const std::string json_str = R"([{"name":"a","code":1,"code":2}])"; + + const auto res = rfl::json::read>(json_str); + ASSERT_TRUE(res) << res.error().what(); + ASSERT_EQ(res->size(), 1u); + EXPECT_EQ((*res)[0].name, "a"); + EXPECT_EQ((*res)[0].code, 1); +} + +TEST(json, test_no_duplicate_keys_rejects_repeated_struct_fields) { + const std::string json_str = R"([{"name":"a","code":1,"code":2}])"; + + const auto res1 = + rfl::json::read, rfl::NoDuplicateKeys>(json_str); + ASSERT_FALSE(res1); + const auto details1 = res1.error().details(); + ASSERT_EQ(details1.size(), 1u) << res1.error().what(); + EXPECT_EQ(details1[0].path, (Path{size_t{0}, "code"})); + EXPECT_EQ(details1[0].message, "Duplicate key 'code'."); + + // With NoExtraFields + NoDuplicateKeys: reported as duplicate key, NOT + // "Value named 'code' not used." + const auto res2 = + rfl::json::read, rfl::NoExtraFields, + rfl::NoDuplicateKeys>(json_str); + ASSERT_FALSE(res2); + const auto details2 = res2.error().details(); + ASSERT_EQ(details2.size(), 1u) << res2.error().what(); + EXPECT_EQ(details2[0].path, (Path{size_t{0}, "code"})); + EXPECT_EQ(details2[0].message, "Duplicate key 'code'."); +} + +TEST(json, test_no_duplicate_keys_aggregated_with_other_errors_and_fail_fast) { + const std::string json_str = R"({"code":1,"code":2,"port":"bad"})"; + + const auto res = + rfl::json::read( + json_str); + ASSERT_FALSE(res); + + const auto details = res.error().details(); + ASSERT_EQ(details.size(), 3u) << res.error().what(); + EXPECT_EQ(details[0].path, (Path{"code"})); + EXPECT_EQ(details[0].message, "Duplicate key 'code'."); + EXPECT_EQ(details[1].path, (Path{"port"})); + EXPECT_EQ(details[2].path, (Path{"name"})); + EXPECT_EQ(details[2].message, "Field named 'name' not found."); + + const auto ff = + rfl::json::read(json_str); + ASSERT_FALSE(ff); + ASSERT_EQ(ff.error().details().size(), 1u) << ff.error().what(); + EXPECT_EQ(ff.error().details()[0].message, "Duplicate key 'code'."); +} + +TEST(json, test_no_duplicate_keys_maps_defaults_generic_and_tagged_union) { + const auto map_res = + rfl::json::read, rfl::NoDuplicateKeys>( + R"({"a":1,"b":2,"a":3})"); + ASSERT_FALSE(map_res); + ASSERT_EQ(map_res.error().details().size(), 1u) << map_res.error().what(); + EXPECT_EQ(map_res.error().details()[0].path, (Path{"a"})); + EXPECT_EQ(map_res.error().details()[0].message, "Duplicate key 'a'."); + + const auto obj_res = + rfl::json::read, rfl::NoDuplicateKeys>( + R"({"x":1,"x":2})"); + ASSERT_FALSE(obj_res); + EXPECT_EQ(obj_res.error().details()[0].message, "Duplicate key 'x'."); + + const auto def_res = + rfl::json::read( + R"({"code":1,"code":2,"extra":3})"); + ASSERT_FALSE(def_res); + ASSERT_EQ(def_res.error().details().size(), 2u) << def_res.error().what(); + EXPECT_EQ(def_res.error().details()[0].message, "Duplicate key 'code'."); + EXPECT_EQ(def_res.error().details()[1].path, (Path{"extra"})); + + // rfl::Generic (top-level and nested) + const auto gen_ok = rfl::json::read( + R"({"outer":{"a":1,"b":2},"list":[{"k":1},{"k":2}]})"); + ASSERT_TRUE(gen_ok) << gen_ok.error().what(); + + const auto gen_dup_top = rfl::json::read( + R"({"a":1,"a":2})"); + ASSERT_FALSE(gen_dup_top); + EXPECT_NE(gen_dup_top.error().what().find("Duplicate key 'a'"), + std::string::npos); + + const auto gen_dup_nested = + rfl::json::read( + R"({"outer":{"k":1,"k":2}})"); + ASSERT_FALSE(gen_dup_nested); + EXPECT_NE(gen_dup_nested.error().what().find("Duplicate key 'k'"), + std::string::npos); + + // rfl::TaggedUnion (duplicate payload key and duplicate discriminator key) + const auto tu_dup = rfl::json::read( + R"({"kind":"circle","radius":1.0,"radius":2.0})"); + ASSERT_FALSE(tu_dup); + ASSERT_EQ(tu_dup.error().details().size(), 1u) << tu_dup.error().what(); + EXPECT_EQ(tu_dup.error().details()[0].message, "Duplicate key 'radius'."); + + const auto tu_dup_disc = rfl::json::read( + R"({"kind":"circle","kind":"square","radius":1.0})"); + ASSERT_FALSE(tu_dup_disc); + ASSERT_EQ(tu_dup_disc.error().details().size(), 1u) + << tu_dup_disc.error().what(); + EXPECT_EQ(tu_dup_disc.error().details()[0].message, "Duplicate key 'kind'."); +} + +} // namespace test_json_no_duplicate_keys diff --git a/tests/yaml/test_no_duplicate_keys.cpp b/tests/yaml/test_no_duplicate_keys.cpp new file mode 100644 index 000000000..22eb626ee --- /dev/null +++ b/tests/yaml/test_no_duplicate_keys.cpp @@ -0,0 +1,127 @@ +#include + +#include +#include +#include +#include +#include +#include + +namespace test_yaml_no_duplicate_keys { + +static_assert(rfl::internal::reader_supports_no_duplicate_keys_v< + rfl::yaml::Reader>); +static_assert(!rfl::internal::reader_supports_no_duplicate_keys_v< + rfl::generic::Reader>); + +using Path = std::vector; + +struct Item { + std::string name; + int code; +}; + +struct Entry { + std::string name; + int code; + int port; +}; + +struct WithDefault { + int code = 0; + std::string name = "default"; +}; + +TEST(yaml, test_duplicate_keys_accepted_by_default_for_backwards_compat) { + const std::string yaml_str = + "- name: a\n" + " code: 1\n" + " code: 2\n"; + + const auto res = rfl::yaml::read>(yaml_str); + ASSERT_TRUE(res) << res.error().what(); + ASSERT_EQ(res->size(), 1u); + EXPECT_EQ((*res)[0].name, "a"); + EXPECT_EQ((*res)[0].code, 1); +} + +TEST(yaml, test_no_duplicate_keys_processor_rejects_repeated_keys) { + const std::string yaml_str = + "- name: a\n" + " code: 1\n" + " code: 2\n"; + + const auto res1 = + rfl::yaml::read, rfl::NoDuplicateKeys>(yaml_str); + ASSERT_FALSE(res1); + const auto details1 = res1.error().details(); + ASSERT_EQ(details1.size(), 1u) << res1.error().what(); + EXPECT_EQ(details1[0].path, (Path{size_t{0}, "code"})); + EXPECT_EQ(details1[0].message, "Duplicate key 'code'."); + + const auto res2 = + rfl::yaml::read, rfl::NoExtraFields, + rfl::NoDuplicateKeys>(yaml_str); + ASSERT_FALSE(res2); + const auto details2 = res2.error().details(); + ASSERT_EQ(details2.size(), 1u) << res2.error().what(); + EXPECT_EQ(details2[0].path, (Path{size_t{0}, "code"})); + EXPECT_EQ(details2[0].message, "Duplicate key 'code'."); +} + +TEST(yaml, test_no_duplicate_keys_aggregated_with_other_errors_and_fail_fast) { + const std::string yaml_str = + "code: 1\n" + "code: 2\n" + "port: bad\n"; + + const auto result = + rfl::yaml::read( + yaml_str); + ASSERT_FALSE(result); + + const auto details = result.error().details(); + ASSERT_EQ(details.size(), 3u) << result.error().what(); + EXPECT_EQ(details[0].path, (Path{"code"})); + EXPECT_EQ(details[0].message, "Duplicate key 'code'."); + EXPECT_EQ(details[1].path, (Path{"port"})); + EXPECT_EQ(details[2].path, (Path{"name"})); + EXPECT_EQ(details[2].message, "Field named 'name' not found."); + + const auto ff_res = + rfl::yaml::read(yaml_str); + ASSERT_FALSE(ff_res); + ASSERT_EQ(ff_res.error().details().size(), 1u) << ff_res.error().what(); + EXPECT_EQ(ff_res.error().details()[0].message, "Duplicate key 'code'."); +} + +TEST(yaml, test_no_duplicate_keys_in_map_and_default_if_missing) { + const std::string map_yaml = + "a: 1\n" + "b: 2\n" + "a: 3\n"; + const auto map_res = + rfl::yaml::read, rfl::NoDuplicateKeys>( + map_yaml); + ASSERT_FALSE(map_res); + const auto map_details = map_res.error().details(); + ASSERT_EQ(map_details.size(), 1u) << map_res.error().what(); + EXPECT_EQ(map_details[0].path, (Path{"a"})); + EXPECT_EQ(map_details[0].message, "Duplicate key 'a'."); + + const std::string def_yaml = + "code: 1\n" + "code: 2\n" + "extra: 3\n"; + const auto def_res = + rfl::yaml::read(def_yaml); + ASSERT_FALSE(def_res); + const auto def_details = def_res.error().details(); + ASSERT_EQ(def_details.size(), 2u) << def_res.error().what(); + EXPECT_EQ(def_details[0].path, (Path{"code"})); + EXPECT_EQ(def_details[0].message, "Duplicate key 'code'."); + EXPECT_EQ(def_details[1].path, (Path{"extra"})); +} + +} // namespace test_yaml_no_duplicate_keys