Skip to content

Commit 674ae5d

Browse files
committed
Allow using rfl::AddTagsToVariants with rfl::Generic and same-name structs
The first one was the main reason for this change; the second one was a result of applying the change. Added tests for both. The issue was that both `std::vector<Generic>` and `rfl::Object<Generic>` were getting the type name 'Generic', which caused a compiler error due to duplicated field names. This will slightly increase the size of serialized formats when using the AddTagsToVariants, but it seems like a worthwhile change.
1 parent 908ee22 commit 674ae5d

3 files changed

Lines changed: 47 additions & 6 deletions

File tree

include/rfl/parsing/VariantAlternativeWrapper.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ consteval auto make_tag() {
2828
return typename T::Tag();
2929
} else {
3030
return Literal<
31-
internal::remove_namespaces<internal::get_type_name<T>()>()>();
31+
internal::get_type_name<T>()>();
3232
}
3333
}
3434

tests/json/test_add_tag_to_rfl_variant.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
#include <cassert>
2-
#include <iostream>
32
#include <rfl.hpp>
43
#include <rfl/json.hpp>
54
#include <string>
6-
#include <variant>
75
#include <vector>
86

97
#include "write_and_read.hpp"
@@ -34,6 +32,6 @@ TEST(json, test_add_tag_to_rfl_variant) {
3432

3533
write_and_read<rfl::AddTagsToVariants>(
3634
vec,
37-
R"([{"button_pressed_t":{}},{"button_released_t":{"button":4}},{"key_pressed":{"key":99}},{"int":3}])");
35+
R"([{"test_add_tag_to_rfl_variant::button_pressed_t":{}},{"test_add_tag_to_rfl_variant::button_released_t":{"button":4}},{"key_pressed":{"key":99}},{"int":3}])");
3836
}
3937
} // namespace test_add_tag_to_rfl_variant

tests/json/test_add_tag_to_variant.cpp

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include <cassert>
2-
#include <iostream>
32
#include <rfl.hpp>
43
#include <rfl/json.hpp>
54
#include <string>
@@ -10,6 +9,8 @@
109

1110
namespace test_add_tag_to_variant {
1211

12+
// test 1 -> normal behaviour
13+
1314
struct button_pressed_t {};
1415

1516
struct button_released_t {};
@@ -22,12 +23,54 @@ struct key_pressed_t {
2223
using my_event_type_t =
2324
std::variant<button_pressed_t, button_released_t, key_pressed_t, int>;
2425

26+
// test 2 -> 'Generic' within a struct like this cannot be read/written
27+
// due to the underlying `std::variant` holding two fields with name 'Generic'
28+
// once 'remove_namespaces' is applied to the type name extraction (which is removed)
29+
// in the MR this test is added
30+
struct APIResult {
31+
rfl::Generic result;
32+
};
33+
struct APIError {
34+
rfl::Generic error;
35+
};
36+
37+
using APICallOutput = rfl::Variant<APIResult, APIError>;
38+
39+
// test 3 -> two structs with the same name in different namespaces should still
40+
// be serializable
41+
42+
namespace Result {
43+
struct Message {
44+
std::string result;
45+
};
46+
} // namespace Result
47+
48+
namespace Error {
49+
struct Message {
50+
std::string error;
51+
int error_id;
52+
};
53+
}; // namespace Error
54+
55+
using Messages = std::variant<Result::Message, Error::Message>;
56+
2557
TEST(json, test_add_tag_to_variant) {
2658
const auto vec = std::vector<my_event_type_t>(
2759
{button_pressed_t{}, button_released_t{}, key_pressed_t{'c'}, 3});
2860

2961
write_and_read<rfl::AddTagsToVariants>(
3062
vec,
31-
R"([{"button_pressed_t":{}},{"button_released_t":{}},{"key_pressed":{"key":99}},{"int":3}])");
63+
R"([{"test_add_tag_to_variant::button_pressed_t":{}},{"test_add_tag_to_variant::button_released_t":{}},{"key_pressed":{"key":99}},{"int":3}])");
64+
}
65+
66+
TEST(generic, test_add_tag_to_variant_with_generic) {
67+
APICallOutput output = APIResult{rfl::to_generic<int>(200)};
68+
write_and_read<rfl::AddTagsToVariants>(output,
69+
R"({"test_add_tag_to_variant::APIResult":{"result":{"long long":200}}})");
70+
}
71+
TEST(generic, test_add_tag_to_variant_different_namespaces) {
72+
Messages m = Error::Message{.error="an error", .error_id=2};
73+
write_and_read<rfl::AddTagsToVariants>(m,
74+
R"({"test_add_tag_to_variant::Error::Message":{"error":"an error","error_id":2}})");
3275
}
3376
} // namespace test_add_tag_to_variant

0 commit comments

Comments
 (0)