-
Notifications
You must be signed in to change notification settings - Fork 180
cbor supports std::optional; fixes #135 #462
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| #include <rfl.hpp> | ||
| #include <rfl/cbor.hpp> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "write_and_read.hpp" | ||
|
|
||
| namespace test_no_optionals { | ||
|
|
||
| struct Person { | ||
| rfl::Rename<"firstName", std::string> first_name; | ||
| rfl::Rename<"lastName", std::string> last_name = "Simpson"; | ||
| rfl::Rename<"children", std::optional<std::vector<Person>>> children; | ||
| }; | ||
|
|
||
| struct OptionalPerson { | ||
| std::optional<Person> opt_person; | ||
| }; | ||
|
|
||
| struct Empty{}; | ||
|
|
||
| TEST(cbor, test_no_optionals) { | ||
| auto empty_struct_cbor = rfl::cbor::write<rfl::NoOptionals>(Empty{}); | ||
| auto no_person_cbor = rfl::cbor::write<rfl::NoOptionals>(OptionalPerson{}); | ||
|
|
||
| EXPECT_NE(empty_struct_cbor.size(), no_person_cbor.size()); | ||
| } | ||
| } // namespace test_no_optionals | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,62 @@ | ||||||||||||||
| #include <iostream> | ||||||||||||||
| #include <rfl.hpp> | ||||||||||||||
| #include <rfl/cbor.hpp> | ||||||||||||||
| #include <string> | ||||||||||||||
| #include <vector> | ||||||||||||||
|
|
||||||||||||||
| #include "write_and_read.hpp" | ||||||||||||||
|
|
||||||||||||||
| namespace test_optional_fields { | ||||||||||||||
|
|
||||||||||||||
| struct Person { | ||||||||||||||
| rfl::Rename<"firstName", std::string> first_name; | ||||||||||||||
| rfl::Rename<"lastName", std::string> last_name = "Simpson"; | ||||||||||||||
| rfl::Rename<"children", std::optional<std::vector<Person>>> children; | ||||||||||||||
|
|
||||||||||||||
| bool operator==(const Person& other) const { | ||||||||||||||
| return first_name.get() == other.first_name.get() && | ||||||||||||||
| last_name.get() == other.last_name.get() && | ||||||||||||||
| children.get() == other.children.get(); | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+16
to
+20
Contributor
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: Use the C++20 default comparison operator:
Suggested change
Although now that I'm thinking about it, perhaps this was done because the default comparison operator is not able to use the If that's the case, it would be helpful to briefly document that fact here.
Contributor
Author
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.
Yep, exactly
Contributor
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. @liuzicheng1987 @dcorbeil I wonder if there is some comparison operator that |
||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| struct OptionalPerson { | ||||||||||||||
| std::optional<Person> opt_person; | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| struct Empty{}; | ||||||||||||||
|
|
||||||||||||||
| TEST(cbor, test_optional_no_fields) { | ||||||||||||||
| auto empty_struct_cbor = rfl::cbor::write(Empty{}); | ||||||||||||||
| auto no_person_cbor = rfl::cbor::write(OptionalPerson{}); | ||||||||||||||
|
|
||||||||||||||
| EXPECT_EQ(empty_struct_cbor.size(), no_person_cbor.size()); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| TEST(cbor, test_optional_fields) { | ||||||||||||||
| const auto bart = Person{.first_name = "Bart"}; | ||||||||||||||
|
|
||||||||||||||
| const auto lisa = Person{.first_name = "Lisa"}; | ||||||||||||||
|
|
||||||||||||||
| const auto maggie = Person{.first_name = "Maggie"}; | ||||||||||||||
|
|
||||||||||||||
| const auto homer = | ||||||||||||||
| Person{.first_name = "Homer", | ||||||||||||||
| .children = std::vector<Person>({bart, lisa, maggie})}; | ||||||||||||||
|
|
||||||||||||||
| const OptionalPerson homer_optional = { | ||||||||||||||
| .opt_person = homer | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| const auto homer_optional_cbor = rfl::cbor::write(homer_optional); | ||||||||||||||
| const auto res = rfl::cbor::read<OptionalPerson>(homer_optional_cbor); | ||||||||||||||
|
|
||||||||||||||
| EXPECT_TRUE(res && true) << "Test failed on read. Error: " | ||||||||||||||
| << res.error().what(); | ||||||||||||||
|
|
||||||||||||||
| const auto actual_homer = res.value(); | ||||||||||||||
|
|
||||||||||||||
| EXPECT_TRUE(actual_homer.opt_person.has_value()); | ||||||||||||||
| EXPECT_EQ(*actual_homer.opt_person, homer); | ||||||||||||||
| } | ||||||||||||||
| } // namespace test_optional_fields | ||||||||||||||
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.
The
Personstruct is also defined intests/cbor/test_optional_fields.cpp. Consider moving this common struct definition to a shared header file to avoid code duplication and improve maintainability.