rfl::UnderlyingEnums (#186)

* First part

* Plural

* Update rfl.hpp

* Documentation

* Update rfl.hpp

* Update processors.md

* underlying_enums test

* Update Parser_default.hpp

* Fix typo in test
This commit is contained in:
AnsiV01
2024-09-12 09:37:31 +02:00
committed by GitHub
parent 7274a2eb47
commit dfc24ad932
7 changed files with 124 additions and 7 deletions
+25
View File
@@ -34,7 +34,9 @@ The resulting JSON string looks like this:
reflect-cpp currently supports the following processors:
- `rfl::AddStructName`
- `rfl::NoFieldNames`
- `rfl::NoOptionals`
- `rfl::UnderlyingEnums`
- `rfl::SnakeCaseToCamelCase`
- `rfl::SnakeCaseToPascalCase`
@@ -117,6 +119,29 @@ leaving out the field `town`. However, if you want to require the field
rfl::json::read<Person, rfl::NoOptionals>(json_string);
```
### `rfl::UnderlyingEnums`
By passing the processor `rfl::UnderlyingEnums`, fields of the enum type will be written and read as integers
```cpp
enum class Color { red, green, blue, yellow };
struct Circle {
float radius;
Color color;
};
const auto circle = Circle{.radius = 2.0, .color = Color::green};
rfl::json::write<rfl::UnderlyingEnums>(circle);
```
The resulting JSON string looks like this:
```json
{"radius":2.0,"color":1}
```
### `rfl::SnakeCaseToCamelCase`
Please refer to the example above.
+1
View File
@@ -38,6 +38,7 @@
#include "rfl/SnakeCaseToPascalCase.hpp"
#include "rfl/TaggedUnion.hpp"
#include "rfl/Timestamp.hpp"
#include "rfl/UnderlyingEnums.hpp"
#include "rfl/Validator.hpp"
#include "rfl/Variant.hpp"
#include "rfl/always_false.hpp"
+6
View File
@@ -5,6 +5,7 @@
#include "internal/is_no_field_names_v.hpp"
#include "internal/is_no_optionals_v.hpp"
#include "internal/is_underlying_enums_v.hpp"
namespace rfl {
@@ -15,6 +16,7 @@ template <>
struct Processors<> {
static constexpr bool all_required_ = false;
static constexpr bool no_field_names_ = false;
static constexpr bool underlying_enums_ = false;
template <class T, class NamedTupleType>
static auto process(NamedTupleType&& _named_tuple) {
@@ -32,6 +34,10 @@ struct Processors<Head, Tail...> {
std::disjunction_v<internal::is_no_field_names<Head>,
internal::is_no_field_names<Tail>...>;
static constexpr bool underlying_enums_ =
std::disjunction_v<internal::is_underlying_enums<Head>,
internal::is_underlying_enums<Tail>...>;
template <class T, class NamedTupleType>
static auto process(NamedTupleType&& _named_tuple) {
return Processors<Tail...>::template process<T>(
+18
View File
@@ -0,0 +1,18 @@
#ifndef RFL_UNDERLYINGENUMS_HPP_
#define RFL_UNDERLYINGENUMS_HPP_
namespace rfl {
/// This is a ‘fake’ processor - it doesn't do much by itself, but its
/// its inclusion instructs parsers not to convert enum types to strings, but to integers
struct UnderlyingEnums {
public:
template <class StructType>
static auto process(auto&& _named_tuple) {
return _named_tuple;
}
};
} // namespace rfl
#endif
@@ -0,0 +1,29 @@
#ifndef RFL_INTERNAL_ISUNDERLYINGENUMS_HPP_
#define RFL_INTERNAL_ISUNDERLYINGENUMS_HPP_
#include <tuple>
#include <type_traits>
#include <utility>
#include "../UnderlyingEnums.hpp"
namespace rfl {
namespace internal {
template <class T>
class is_underlying_enums;
template <class T>
class is_underlying_enums : public std::false_type {};
template <>
class is_underlying_enums<UnderlyingEnums> : public std::true_type {};
template <class T>
constexpr bool is_underlying_enums_v =
is_underlying_enums<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
} // namespace internal
} // namespace rfl
#endif
+20 -7
View File
@@ -15,6 +15,7 @@
#include "../internal/is_basic_type.hpp"
#include "../internal/is_description.hpp"
#include "../internal/is_literal.hpp"
#include "../internal/is_underlying_enums_v.hpp"
#include "../internal/is_validator.hpp"
#include "../internal/processed_t.hpp"
#include "../internal/to_ptr_named_tuple.hpp"
@@ -69,9 +70,13 @@ struct Parser {
} else if constexpr (std::is_class_v<T> && std::is_aggregate_v<T>) {
return read_struct(_r, _var);
} else if constexpr (std::is_enum_v<T>) {
using StringConverter = internal::enums::StringConverter<T>;
return _r.template to_basic_type<std::string>(_var).and_then(
StringConverter::string_to_enum);
if constexpr (ProcessorsType::underlying_enums_) {
return static_cast<T>(*_r.template to_basic_type<std::underlying_type_t<T>>(_var));
} else {
using StringConverter = internal::enums::StringConverter<T>;
return _r.template to_basic_type<std::string>(_var).and_then(
StringConverter::string_to_enum);
}
} else {
return _r.template to_basic_type<std::remove_cvref_t<T>>(_var);
}
@@ -99,9 +104,14 @@ struct Parser {
Parser<R, W, PtrNamedTupleType, ProcessorsType>::write(
_w, ptr_named_tuple, _parent);
} else if constexpr (std::is_enum_v<T>) {
using StringConverter = internal::enums::StringConverter<T>;
const auto str = StringConverter::enum_to_string(_var);
ParentType::add_value(_w, str, _parent);
if constexpr (ProcessorsType::underlying_enums_) {
const auto val = static_cast<std::underlying_type_t<T>>(_var);
ParentType::add_value(_w, val, _parent);
} else {
using StringConverter = internal::enums::StringConverter<T>;
const auto str = StringConverter::enum_to_string(_var);
ParentType::add_value(_w, str, _parent);
}
} else {
ParentType::add_value(_w, _var, _parent);
}
@@ -179,7 +189,10 @@ struct Parser {
std::map<std::string, schema::Type>* _definitions) {
using Type = schema::Type;
using S = internal::enums::StringConverter<U>;
if constexpr (S::is_flag_enum_) {
if constexpr (ProcessorsType::underlying_enums_) {
return Type{Type::Integer{}};
}
else if constexpr (S::is_flag_enum_) {
return Type{Type::String{}};
} else {
return Parser<R, W, typename S::NamesLiteral, ProcessorsType>::to_schema(
+25
View File
@@ -0,0 +1,25 @@
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace test_underlying_enums {
enum class Color { red, green, blue, yellow };
struct Circle {
float radius;
Color color;
};
TEST(json, test_underlying_enums) {
const auto circle = Circle{.radius = 2.0, .color = Color::green};
write_and_read<rfl::UnderlyingEnums>(circle, R"({"radius":2.0,"color":1})");
}
} // namespace test_enum1