diff --git a/docs/processors.md b/docs/processors.md index 7df6cee1..973d79fa 100644 --- a/docs/processors.md +++ b/docs/processors.md @@ -35,6 +35,7 @@ reflect-cpp currently supports the following processors: - `rfl::AddStructName` - `rfl::DefaultIfMissing` +- `rfl::NoExtraFields` - `rfl::NoFieldNames` - `rfl::NoOptionals` - `rfl::UnderlyingEnums` @@ -99,6 +100,48 @@ 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. +### `rfl::NoExtraFields` + +When reading an object and the object contains a field that cannot be +matched to any of the fields in the struct, that field is simply ignored. + +However, when `rfl::NoExtraFields` is added to `read`, then such extra fields +will lead to an error. + +This can be overriden by adding `rfl::ExtraFields` to the struct. + +Example: + +```cpp +struct Person { + std::string first_name; + std::string last_name = "Simpson"; +}; +``` + +```json +{"first_name":"Homer","last_name":"Simpson","extra_field":0} +``` + +If you call `rfl::json::read(json_string)`, then `extra_field` will +simply be ignored. + +But if you call `rfl::json::read(json_string)`, +you will get an error. + +However, suppose the struct looked like this: + +```cpp +struct Person { + std::string first_name; + std::string last_name = "Simpson"; + rfl::ExtraFields extras; +}; +``` + +In this case, `rfl::json::read(json_string)` +will not fail, because `extra_field` would be included in `extras`. + ### `rfl::NoFieldNames` We can also remove the field names altogether: diff --git a/include/rfl.hpp b/include/rfl.hpp index 06b4d571..456b3159 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -23,6 +23,7 @@ #include "rfl/Hex.hpp" #include "rfl/Literal.hpp" #include "rfl/NamedTuple.hpp" +#include "rfl/NoExtraFields.hpp" #include "rfl/NoFieldNames.hpp" #include "rfl/NoOptionals.hpp" #include "rfl/Object.hpp" diff --git a/include/rfl/NoExtraFields.hpp b/include/rfl/NoExtraFields.hpp new file mode 100644 index 00000000..85cf82ac --- /dev/null +++ b/include/rfl/NoExtraFields.hpp @@ -0,0 +1,19 @@ +#ifndef RFL_NOEXTRAFIELDS_HPP_ +#define RFL_NOEXTRAFIELDS_HPP_ + +namespace rfl { + +/// This is a "fake" processor - it doesn't do much in itself, but its +/// inclusion instructs the parsers to return an error when there are extra +/// fields instead of ignoring them. +struct NoExtraFields { + public: + template + static auto process(auto&& _named_tuple) { + return _named_tuple; + } +}; + +} // namespace rfl + +#endif diff --git a/include/rfl/Processors.hpp b/include/rfl/Processors.hpp index 4e7babba..abe05700 100644 --- a/include/rfl/Processors.hpp +++ b/include/rfl/Processors.hpp @@ -4,6 +4,7 @@ #include #include "internal/is_default_if_missing_v.hpp" +#include "internal/is_no_extra_fields_v.hpp" #include "internal/is_no_field_names_v.hpp" #include "internal/is_no_optionals_v.hpp" #include "internal/is_underlying_enums_v.hpp" @@ -17,6 +18,7 @@ template <> struct Processors<> { static constexpr bool all_required_ = false; static constexpr bool default_if_missing_ = false; + static constexpr bool no_extra_fields_ = false; static constexpr bool no_field_names_ = false; static constexpr bool underlying_enums_ = false; @@ -36,6 +38,10 @@ struct Processors { std::disjunction_v, internal::is_default_if_missing...>; + static constexpr bool no_extra_fields_ = + std::disjunction_v, + internal::is_no_extra_fields...>; + static constexpr bool no_field_names_ = std::disjunction_v, internal::is_no_field_names...>; diff --git a/include/rfl/internal/is_no_extra_fields_v.hpp b/include/rfl/internal/is_no_extra_fields_v.hpp new file mode 100644 index 00000000..58916ee6 --- /dev/null +++ b/include/rfl/internal/is_no_extra_fields_v.hpp @@ -0,0 +1,29 @@ +#ifndef RFL_INTERNAL_ISNOEXTRAFIELDS_HPP_ +#define RFL_INTERNAL_ISNOEXTRAFIELDS_HPP_ + +#include +#include +#include + +#include "../NoExtraFields.hpp" + +namespace rfl { +namespace internal { + +template +class is_no_extra_fields; + +template +class is_no_extra_fields : public std::false_type {}; + +template <> +class is_no_extra_fields : public std::true_type {}; + +template +constexpr bool is_no_extra_fields_v = + is_no_extra_fields>>::value; + +} // namespace internal +} // namespace rfl + +#endif diff --git a/include/rfl/parsing/ViewReader.hpp b/include/rfl/parsing/ViewReader.hpp index e7a2f9d9..65dcc0a1 100644 --- a/include/rfl/parsing/ViewReader.hpp +++ b/include/rfl/parsing/ViewReader.hpp @@ -117,6 +117,13 @@ class ViewReader { assign_to_extra_fields(_r, _current_name, _var, _view, _errors, _found, _set); } + } else if constexpr (ProcessorsType::no_extra_fields_) { + if (!already_assigned) { + _errors->emplace_back( + Error("Value named '" + std::string(_current_name) + + "' not used. Remove the rfl::NoExtraFields processor or add " + "rfl::ExtraFields to avoid this error message.")); + } } } diff --git a/include/rfl/parsing/ViewReaderWithDefault.hpp b/include/rfl/parsing/ViewReaderWithDefault.hpp index 4fc00e0b..85c80333 100644 --- a/include/rfl/parsing/ViewReaderWithDefault.hpp +++ b/include/rfl/parsing/ViewReaderWithDefault.hpp @@ -98,6 +98,13 @@ class ViewReaderWithDefault { if (!already_assigned) { assign_to_extra_fields(_r, _current_name, _var, _view, _errors); } + } else if constexpr (ProcessorsType::no_extra_fields_) { + if (!already_assigned) { + _errors->emplace_back( + Error("Value named '" + std::string(_current_name) + + "' not used. Remove the rfl::NoExtraFields processor or add " + "rfl::ExtraFields to avoid this error message.")); + } } } diff --git a/tests/json/test_extra_fields_with_no_extra_fields.cpp b/tests/json/test_extra_fields_with_no_extra_fields.cpp new file mode 100644 index 00000000..86e747e9 --- /dev/null +++ b/tests/json/test_extra_fields_with_no_extra_fields.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_extra_fields_with_no_extra_fields { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + rfl::ExtraFields extra_fields; +}; + +TEST(json, test_extra_fields_with_no_extra_fields) { + auto homer = Person{.first_name = "Homer"}; + + homer.extra_fields["age"] = 45; + homer.extra_fields["email"] = "homer@simpson.com"; + homer.extra_fields["town"] = "Springfield"; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","age":45,"email":"homer@simpson.com","town":"Springfield"})"); +} +} // namespace test_extra_fields_with_no_extra_fields diff --git a/tests/json/test_no_extra_fields.cpp b/tests/json/test_no_extra_fields.cpp new file mode 100644 index 00000000..511a8da0 --- /dev/null +++ b/tests/json/test_no_extra_fields.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_no_extra_fields { + +struct Person { + std::string first_name; + std::string last_name = "Simpson"; +}; + +TEST(json, test_no_extra_fields) { + auto homer = rfl::json::read( + R"({"first_name":"Homer","last_name":"Simpson","extra_field":0})"); + + EXPECT_EQ(homer && true, false); +} +} // namespace test_no_extra_fields diff --git a/tests/json/test_no_extra_fields_with_default.cpp b/tests/json/test_no_extra_fields_with_default.cpp new file mode 100644 index 00000000..7b5b3937 --- /dev/null +++ b/tests/json/test_no_extra_fields_with_default.cpp @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_no_extra_fields_with_default { + +struct Person { + std::string first_name; + std::string last_name = "Simpson"; +}; + +TEST(json, test_no_extra_fields_with_default) { + auto homer = + rfl::json::read( + R"({"first_name":"Homer","last_name":"Simpson","extra_field":0})"); + + EXPECT_EQ(homer && true, false); +} +} // namespace test_no_extra_fields_with_default