Make sure rfl::Generic is NOT optional; fixes #352 (#358)

This commit is contained in:
Dr. Patrick Urbanke (劉自成)
2025-02-20 21:17:27 +01:00
committed by GitHub
parent 33eac03e0d
commit c6d0626267
3 changed files with 30 additions and 2 deletions
+3 -1
View File
@@ -5,6 +5,7 @@
#include <optional>
#include <type_traits>
#include "../Generic.hpp"
#include "../Rename.hpp"
#include "../internal/StringLiteral.hpp"
#include "../internal/is_rename.hpp"
@@ -37,7 +38,8 @@ constexpr bool is_never_required_v = is_never_required<T>::value;
template <class T, bool _ignore_empty_containers>
consteval bool is_required() {
using Type = std::remove_cvref_t<std::remove_pointer_t<T>>;
if constexpr (internal::has_reflection_type_v<Type>) {
if constexpr (internal::has_reflection_type_v<Type> &&
!std::is_same_v<Type, rfl::Generic>) {
return is_required<typename Type::ReflectionType,
_ignore_empty_containers>();
} else if constexpr (internal::is_rename_v<Type>) {
+1 -1
View File
@@ -28,7 +28,7 @@ SOFTWARE.
namespace rfl {
Generic::Generic() : value_(false) {}
Generic::Generic() : value_(std::nullopt) {}
Generic::Generic(Generic&& _other) noexcept = default;
+26
View File
@@ -0,0 +1,26 @@
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <source_location>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace test_generic2 {
TEST(json, test_generic2) {
std::string response =
"{\"kind\":2,\"error\":\"Internal "
"error\",\"status\":101,\"stats\":{\"cpuTime\":803,\"totalTime\":1}}";
struct DataHolder {
int kind = 0;
rfl::Generic data;
};
auto result = rfl::json::read<DataHolder>(response);
EXPECT_TRUE(!(result && true));
}
} // namespace test_generic2