mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-04 01:06:19 +08:00
This commit is contained in:
@@ -20,6 +20,7 @@
|
|||||||
#include "../internal/processed_t.hpp"
|
#include "../internal/processed_t.hpp"
|
||||||
#include "../internal/ptr_cast.hpp"
|
#include "../internal/ptr_cast.hpp"
|
||||||
#include "../internal/to_ptr_named_tuple.hpp"
|
#include "../internal/to_ptr_named_tuple.hpp"
|
||||||
|
#include "../thirdparty/enchantum/enchantum.hpp"
|
||||||
#include "../to_view.hpp"
|
#include "../to_view.hpp"
|
||||||
#include "AreReaderAndWriter.hpp"
|
#include "AreReaderAndWriter.hpp"
|
||||||
#include "Parent.hpp"
|
#include "Parent.hpp"
|
||||||
@@ -86,8 +87,11 @@ struct Parser {
|
|||||||
} else if constexpr (std::is_enum_v<T>) {
|
} else if constexpr (std::is_enum_v<T>) {
|
||||||
if constexpr (ProcessorsType::underlying_enums_ ||
|
if constexpr (ProcessorsType::underlying_enums_ ||
|
||||||
schemaful::IsSchemafulReader<R>) {
|
schemaful::IsSchemafulReader<R>) {
|
||||||
return static_cast<T>(
|
static_assert(enchantum::ScopedEnum<T>,
|
||||||
*_r.template to_basic_type<std::underlying_type_t<T>>(_var));
|
"The enum must be a scoped enum in order to retrieve "
|
||||||
|
"the underlying value.");
|
||||||
|
return _r.template to_basic_type<std::underlying_type_t<T>>(_var)
|
||||||
|
.transform([](const auto _val) { return static_cast<T>(_val); });
|
||||||
} else {
|
} else {
|
||||||
return _r.template to_basic_type<std::string>(_var).and_then(
|
return _r.template to_basic_type<std::string>(_var).and_then(
|
||||||
rfl::string_to_enum<T>);
|
rfl::string_to_enum<T>);
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ struct Person {
|
|||||||
std::vector<Person> children;
|
std::vector<Person> children;
|
||||||
};
|
};
|
||||||
|
|
||||||
TEST(bson, test_snake_case_to_pascal_case) {
|
TEST(generic, test_snake_case_to_pascal_case) {
|
||||||
const auto bart = Person{
|
const auto bart = Person{
|
||||||
.first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19"};
|
.first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19"};
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include <rfl/json.hpp>
|
||||||
|
|
||||||
|
namespace test_enum_underlying {
|
||||||
|
|
||||||
|
enum class CameraSource : uint32_t { kWide, kInfrared, kZoom };
|
||||||
|
|
||||||
|
struct ChangeCameraSourcePayload {
|
||||||
|
CameraSource source;
|
||||||
|
};
|
||||||
|
|
||||||
|
constexpr const char* payloads[2] = {R"({"source":20})",
|
||||||
|
R"({"source":"hello"})"};
|
||||||
|
|
||||||
|
TEST(json, test_enum_underlying) {
|
||||||
|
const auto res1 = static_cast<int>(
|
||||||
|
rfl::json::read<ChangeCameraSourcePayload, rfl::UnderlyingEnums>(
|
||||||
|
payloads[0])
|
||||||
|
.value()
|
||||||
|
.source);
|
||||||
|
const auto res2 =
|
||||||
|
rfl::json::read<CameraSource, rfl::UnderlyingEnums>(payloads[1]);
|
||||||
|
|
||||||
|
EXPECT_EQ(res1, 20);
|
||||||
|
EXPECT_TRUE(!res2); // should not succeed
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace test_enum_underlying
|
||||||
Reference in New Issue
Block a user