-
Notifications
You must be signed in to change notification settings - Fork 143
Allow using rfl::AddTagsToVariants with rfl::Generic and same-name-different-namespace structs
#467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Overall, I think it would be helpful to maintain the test structure of |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,4 @@ | ||||||||||
| #include <cassert> | ||||||||||
| #include <iostream> | ||||||||||
| #include <rfl.hpp> | ||||||||||
| #include <rfl/json.hpp> | ||||||||||
| #include <string> | ||||||||||
|
|
@@ -10,6 +9,8 @@ | |||||||||
|
|
||||||||||
| namespace test_add_tag_to_variant { | ||||||||||
|
|
||||||||||
| // test 1 -> normal behaviour | ||||||||||
|
|
||||||||||
| struct button_pressed_t {}; | ||||||||||
|
|
||||||||||
| struct button_released_t {}; | ||||||||||
|
|
@@ -22,12 +23,54 @@ struct key_pressed_t { | |||||||||
| using my_event_type_t = | ||||||||||
| std::variant<button_pressed_t, button_released_t, key_pressed_t, int>; | ||||||||||
|
|
||||||||||
| // test 2 -> 'Generic' within a struct like this cannot be read/written | ||||||||||
| // due to the underlying `std::variant` holding two fields with name 'Generic' | ||||||||||
| // once 'remove_namespaces' is applied to the type name extraction (which is removed) | ||||||||||
| // in the MR this test is added | ||||||||||
jmcken8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
| struct APIResult { | ||||||||||
| rfl::Generic result; | ||||||||||
| }; | ||||||||||
| struct APIError { | ||||||||||
| rfl::Generic error; | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| using APICallOutput = rfl::Variant<APIResult, APIError>; | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please fix: I think this was meant to be
Suggested change
|
||||||||||
|
|
||||||||||
| // test 3 -> two structs with the same name in different namespaces should still | ||||||||||
| // be serializable | ||||||||||
|
|
||||||||||
| namespace Result { | ||||||||||
| struct Message { | ||||||||||
| std::string result; | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Make a note of the alternative approach using
Suggested change
|
||||||||||
| }; | ||||||||||
| } // namespace Result | ||||||||||
|
|
||||||||||
| namespace Error { | ||||||||||
| struct Message { | ||||||||||
| std::string error; | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Make a note of the alternative approach using
Suggested change
|
||||||||||
| int error_id; | ||||||||||
| }; | ||||||||||
| }; // namespace Error | ||||||||||
|
|
||||||||||
| using Messages = std::variant<Result::Message, Error::Message>; | ||||||||||
|
|
||||||||||
| TEST(json, test_add_tag_to_variant) { | ||||||||||
| const auto vec = std::vector<my_event_type_t>( | ||||||||||
| {button_pressed_t{}, button_released_t{}, key_pressed_t{'c'}, 3}); | ||||||||||
|
|
||||||||||
| write_and_read<rfl::AddTagsToVariants>( | ||||||||||
| vec, | ||||||||||
| R"([{"button_pressed_t":{}},{"button_released_t":{}},{"key_pressed":{"key":99}},{"int":3}])"); | ||||||||||
| R"([{"test_add_tag_to_variant::button_pressed_t":{}},{"test_add_tag_to_variant::button_released_t":{}},{"key_pressed":{"key":99}},{"int":3}])"); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| TEST(json, test_add_tag_to_variant_with_generic) { | ||||||||||
| APICallOutput output = APIResult{"200"}; | ||||||||||
| write_and_read<rfl::AddTagsToVariants>(output, | ||||||||||
| R"({"test_add_tag_to_variant::APIResult":{"result":{"std::string":"200"}}})"); | ||||||||||
| } | ||||||||||
| TEST(json, test_add_tag_to_variant_different_namespaces) { | ||||||||||
| Messages m = Error::Message{.error="an error", .error_id=2}; | ||||||||||
| write_and_read<rfl::AddTagsToVariants>(m, | ||||||||||
| R"({"test_add_tag_to_variant::Error::Message":{"error":"an error","error_id":2}})"); | ||||||||||
jmcken8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
| } | ||||||||||
| } // namespace test_add_tag_to_variant | ||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: Add another test to explicitly demonstrate how to avoid any namespace decoration in the serialization.