mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 18:56:19 +08:00
@@ -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<Person>(json_string)`, then `extra_field` will
|
||||
simply be ignored.
|
||||
|
||||
But if you call `rfl::json::read<Person, rfl::NoExtraFields>(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<int> extras;
|
||||
};
|
||||
```
|
||||
|
||||
In this case, `rfl::json::read<Person, rfl::NoExtraFields>(json_string)`
|
||||
will not fail, because `extra_field` would be included in `extras`.
|
||||
|
||||
### `rfl::NoFieldNames`
|
||||
|
||||
We can also remove the field names altogether:
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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 <class StructType>
|
||||
static auto process(auto&& _named_tuple) {
|
||||
return _named_tuple;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <type_traits>
|
||||
|
||||
#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<Head, Tail...> {
|
||||
std::disjunction_v<internal::is_default_if_missing<Head>,
|
||||
internal::is_default_if_missing<Tail>...>;
|
||||
|
||||
static constexpr bool no_extra_fields_ =
|
||||
std::disjunction_v<internal::is_no_extra_fields<Head>,
|
||||
internal::is_no_extra_fields<Tail>...>;
|
||||
|
||||
static constexpr bool no_field_names_ =
|
||||
std::disjunction_v<internal::is_no_field_names<Head>,
|
||||
internal::is_no_field_names<Tail>...>;
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef RFL_INTERNAL_ISNOEXTRAFIELDS_HPP_
|
||||
#define RFL_INTERNAL_ISNOEXTRAFIELDS_HPP_
|
||||
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "../NoExtraFields.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class T>
|
||||
class is_no_extra_fields;
|
||||
|
||||
template <class T>
|
||||
class is_no_extra_fields : public std::false_type {};
|
||||
|
||||
template <>
|
||||
class is_no_extra_fields<NoExtraFields> : public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
constexpr bool is_no_extra_fields_v =
|
||||
is_no_extra_fields<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -117,6 +117,13 @@ class ViewReader {
|
||||
assign_to_extra_fields<pos>(_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."));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -98,6 +98,13 @@ class ViewReaderWithDefault {
|
||||
if (!already_assigned) {
|
||||
assign_to_extra_fields<pos>(_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."));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<rfl::Generic> 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<rfl::NoExtraFields>(
|
||||
homer,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","age":45,"email":"homer@simpson.com","town":"Springfield"})");
|
||||
}
|
||||
} // namespace test_extra_fields_with_no_extra_fields
|
||||
@@ -0,0 +1,22 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<Person, rfl::NoExtraFields>(
|
||||
R"({"first_name":"Homer","last_name":"Simpson","extra_field":0})");
|
||||
|
||||
EXPECT_EQ(homer && true, false);
|
||||
}
|
||||
} // namespace test_no_extra_fields
|
||||
@@ -0,0 +1,23 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<Person, rfl::NoExtraFields, rfl::DefaultIfMissing>(
|
||||
R"({"first_name":"Homer","last_name":"Simpson","extra_field":0})");
|
||||
|
||||
EXPECT_EQ(homer && true, false);
|
||||
}
|
||||
} // namespace test_no_extra_fields_with_default
|
||||
Reference in New Issue
Block a user