diff --git a/include/rfl/flexbuf/read.hpp b/include/rfl/flexbuf/read.hpp index da8c99b6..d84500d3 100644 --- a/include/rfl/flexbuf/read.hpp +++ b/include/rfl/flexbuf/read.hpp @@ -13,13 +13,19 @@ namespace flexbuf { using InputVarType = typename Reader::InputVarType; +/// Parses an object from flexbuf var. +template +Result read(const InputVarType& _obj) { + const auto r = Reader(); + return Parser::read(r, _obj); +} + /// Parses an object from flexbuf using reflection. template Result read(const unsigned char* _bytes, const size_t _size) { const InputVarType root = flexbuffers::GetRoot(reinterpret_cast(_bytes), _size); - const auto r = Reader(); - return Parser::read(r, root); + return read(root); } /// Parses an object from flexbuf using reflection. diff --git a/tests/flexbuffers/test_box.hpp b/tests/flexbuffers/test_box.hpp new file mode 100644 index 00000000..2de1b372 --- /dev/null +++ b/tests/flexbuffers/test_box.hpp @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_box() { + std::cout << "test_box" << std::endl; + + struct DecisionTree { + struct Leaf { + rfl::Field<"type", rfl::Literal<"Leaf">> type = rfl::default_value; + rfl::Field<"value", double> value; + }; + + struct Node { + rfl::Field<"type", rfl::Literal<"Node">> type = rfl::default_value; + rfl::Field<"criticalValue", double> critical_value; + rfl::Field<"left", rfl::Box> lesser; + rfl::Field<"right", rfl::Box> greater; + }; + + using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; + + rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; + }; + + auto leaf1 = DecisionTree::Leaf{.value = 3.0}; + + auto leaf2 = DecisionTree::Leaf{.value = 5.0}; + + auto node = DecisionTree::Node{ + .critical_value = 10.0, + .lesser = rfl::make_box(DecisionTree{leaf1}), + .greater = rfl::make_box(DecisionTree{leaf2})}; + + const DecisionTree tree{.leaf_or_node = std::move(node)}; + + write_and_read(tree); +} diff --git a/tests/flexbuffers/test_custom_constructor.hpp b/tests/flexbuffers/test_custom_constructor.hpp new file mode 100644 index 00000000..08b36c84 --- /dev/null +++ b/tests/flexbuffers/test_custom_constructor.hpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +// Makes sure that the example in the README works as expected. +// It would be embarrassing if it didn't... +void test_custom_constructor() { + std::cout << "test_custom_constructor" << std::endl; + + struct Person { + static rfl::Result from_flexbuf( + typename rfl::flexbuf::Reader::InputVarType _obj) { + /// This only exists for the purpose of the test. + const auto change_first_name = [](auto&& _person) { + return rfl::replace(std::move(_person), + rfl::Field<"firstName", std::string>("Bart")); + }; + const auto from_nt = [](auto&& _nt) { + return rfl::from_named_tuple(std::move(_nt)); + }; + return rfl::flexbuf::read>(_obj) + .transform(from_nt) + .transform(change_first_name); + } + + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name; + rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday; + }; + + const auto person1 = Person{ + .first_name = "Homer", .last_name = "Simpson", .birthday = "1987-04-19"}; + + const auto bytes = rfl::flexbuf::write(person1); + + const auto res = rfl::flexbuf::read(bytes); + + if (!res) { + std::cout << "Test failed on read. Error: " << res.error().value().what() + << std::endl; + return; + } + + if (res.value().first_name() != "Bart") { + std::cout << "Expected 'Bart', got '" << res.value().first_name() << "'" + << std::endl + << std::endl; + } else { + std::cout << "OK" << std::endl << std::endl; + } +} diff --git a/tests/flexbuffers/test_ref.hpp b/tests/flexbuffers/test_ref.hpp new file mode 100644 index 00000000..c9f7e2cf --- /dev/null +++ b/tests/flexbuffers/test_ref.hpp @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_ref() { + std::cout << "test_ref" << std::endl; + + struct DecisionTree { + struct Leaf { + rfl::Field<"type", rfl::Literal<"Leaf">> type = rfl::default_value; + rfl::Field<"value", double> value; + }; + + struct Node { + rfl::Field<"type", rfl::Literal<"Node">> type = rfl::default_value; + rfl::Field<"criticalValue", double> critical_value; + rfl::Field<"left", rfl::Ref> lesser; + rfl::Field<"right", rfl::Ref> greater; + }; + + using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; + + rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; + }; + + const auto leaf1 = DecisionTree::Leaf{.value = 3.0}; + + const auto leaf2 = DecisionTree::Leaf{.value = 5.0}; + + auto node = DecisionTree::Node{ + .critical_value = 10.0, + .lesser = rfl::make_ref(DecisionTree{leaf1}), + .greater = rfl::make_ref(DecisionTree{leaf2})}; + + const DecisionTree tree{.leaf_or_node = std::move(node)}; + + write_and_read(tree); +} diff --git a/tests/flexbuffers/tests.cpp b/tests/flexbuffers/tests.cpp index ffcbbb11..88396bd9 100644 --- a/tests/flexbuffers/tests.cpp +++ b/tests/flexbuffers/tests.cpp @@ -1,8 +1,10 @@ #include "test_all_of.hpp" #include "test_anonymous_fields.hpp" +#include "test_box.hpp" #include "test_custom_class1.hpp" #include "test_custom_class2.hpp" #include "test_custom_class3.hpp" +#include "test_custom_constructor.hpp" #include "test_deque.hpp" #include "test_field_variant.hpp" #include "test_forward_list.hpp" @@ -13,6 +15,7 @@ #include "test_multiset.hpp" #include "test_optional_fields.hpp" #include "test_readme_example.hpp" +#include "test_ref.hpp" #include "test_set.hpp" #include "test_string_map.hpp" #include "test_string_unordered_map.hpp" @@ -38,6 +41,8 @@ int main() { test_custom_class1(); test_custom_class2(); test_custom_class3(); + test_box(); + test_ref(); test_deque(); test_forward_list(); @@ -53,6 +58,8 @@ int main() { test_unordered_multiset(); test_unordered_set(); + test_custom_constructor(); + test_all_of(); return 0;