From 5a34c9bea87af7cbace2a171f81e2a69afb6650c Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Fri, 19 Jan 2024 16:09:26 +0100 Subject: [PATCH] Added BSON support; #22 --- .github/workflows/test.yaml | 18 +- .gitignore | 1 + CMakeLists.txt | 8 +- README.md | 6 +- docs/README.md | 8 +- docs/bson.md | 100 +++++++++ include/rfl/bson.hpp | 12 ++ include/rfl/bson/Parser.hpp | 17 ++ include/rfl/bson/Reader.hpp | 228 ++++++++++++++++++++ include/rfl/bson/Writer.hpp | 221 +++++++++++++++++++ include/rfl/bson/load.hpp | 20 ++ include/rfl/bson/read.hpp | 57 +++++ include/rfl/bson/save.hpp | 26 +++ include/rfl/bson/write.hpp | 58 +++++ include/rfl/flexbuf.hpp | 4 +- tests/CMakeLists.txt | 4 + tests/bson/CMakeLists.txt | 8 + tests/bson/test_array.cpp | 40 ++++ tests/bson/test_array.hpp | 4 + tests/bson/test_box.cpp | 49 +++++ tests/bson/test_box.hpp | 4 + tests/bson/test_custom_class1.cpp | 41 ++++ tests/bson/test_custom_class1.hpp | 4 + tests/bson/test_custom_class3.cpp | 67 ++++++ tests/bson/test_custom_class3.hpp | 4 + tests/bson/test_custom_class4.cpp | 68 ++++++ tests/bson/test_custom_class4.hpp | 4 + tests/bson/test_default_values.cpp | 32 +++ tests/bson/test_default_values.hpp | 4 + tests/bson/test_deque.cpp | 33 +++ tests/bson/test_deque.hpp | 4 + tests/bson/test_enum.cpp | 29 +++ tests/bson/test_enum.hpp | 4 + tests/bson/test_field_variant.cpp | 39 ++++ tests/bson/test_field_variant.hpp | 4 + tests/bson/test_flag_enum.cpp | 39 ++++ tests/bson/test_flag_enum.hpp | 4 + tests/bson/test_flag_enum_with_int.cpp | 38 ++++ tests/bson/test_flag_enum_with_int.hpp | 4 + tests/bson/test_flatten.cpp | 38 ++++ tests/bson/test_flatten.hpp | 4 + tests/bson/test_flatten_anonymous.cpp | 39 ++++ tests/bson/test_flatten_anonymous.hpp | 4 + tests/bson/test_forward_list.cpp | 33 +++ tests/bson/test_forward_list.hpp | 4 + tests/bson/test_literal.cpp | 30 +++ tests/bson/test_literal.hpp | 4 + tests/bson/test_literal_map.cpp | 28 +++ tests/bson/test_literal_map.hpp | 4 + tests/bson/test_map.cpp | 32 +++ tests/bson/test_map.hpp | 4 + tests/bson/test_map_with_key_validation.cpp | 33 +++ tests/bson/test_map_with_key_validation.hpp | 4 + tests/bson/test_monster_example.cpp | 64 ++++++ tests/bson/test_monster_example.hpp | 4 + tests/bson/test_readme_example.cpp | 51 +++++ tests/bson/test_readme_example.hpp | 4 + tests/bson/test_readme_example2.cpp | 26 +++ tests/bson/test_readme_example2.hpp | 4 + tests/bson/test_ref.cpp | 49 +++++ tests/bson/test_ref.hpp | 4 + tests/bson/test_save_load.cpp | 70 ++++++ tests/bson/test_save_load.hpp | 4 + tests/bson/test_set.cpp | 30 +++ tests/bson/test_set.hpp | 4 + tests/bson/test_size.cpp | 42 ++++ tests/bson/test_size.hpp | 4 + tests/bson/test_string_map.cpp | 24 +++ tests/bson/test_string_map.hpp | 4 + tests/bson/test_tagged_union.cpp | 36 ++++ tests/bson/test_tagged_union.hpp | 4 + tests/bson/test_timestamp.cpp | 36 ++++ tests/bson/test_timestamp.hpp | 4 + tests/bson/test_unique_ptr.cpp | 33 +++ tests/bson/test_unique_ptr.hpp | 4 + tests/bson/test_unique_ptr2.cpp | 48 +++++ tests/bson/test_unique_ptr2.hpp | 4 + tests/bson/test_variant.cpp | 36 ++++ tests/bson/test_variant.hpp | 4 + tests/bson/tests.cpp | 65 ++++++ tests/bson/write_and_read.hpp | 39 ++++ vcpkg.json | 4 + 82 files changed, 2267 insertions(+), 14 deletions(-) create mode 100644 docs/bson.md create mode 100644 include/rfl/bson.hpp create mode 100644 include/rfl/bson/Parser.hpp create mode 100644 include/rfl/bson/Reader.hpp create mode 100644 include/rfl/bson/Writer.hpp create mode 100644 include/rfl/bson/load.hpp create mode 100644 include/rfl/bson/read.hpp create mode 100644 include/rfl/bson/save.hpp create mode 100644 include/rfl/bson/write.hpp create mode 100644 tests/bson/CMakeLists.txt create mode 100644 tests/bson/test_array.cpp create mode 100644 tests/bson/test_array.hpp create mode 100644 tests/bson/test_box.cpp create mode 100644 tests/bson/test_box.hpp create mode 100644 tests/bson/test_custom_class1.cpp create mode 100644 tests/bson/test_custom_class1.hpp create mode 100644 tests/bson/test_custom_class3.cpp create mode 100644 tests/bson/test_custom_class3.hpp create mode 100644 tests/bson/test_custom_class4.cpp create mode 100644 tests/bson/test_custom_class4.hpp create mode 100644 tests/bson/test_default_values.cpp create mode 100644 tests/bson/test_default_values.hpp create mode 100644 tests/bson/test_deque.cpp create mode 100644 tests/bson/test_deque.hpp create mode 100644 tests/bson/test_enum.cpp create mode 100644 tests/bson/test_enum.hpp create mode 100644 tests/bson/test_field_variant.cpp create mode 100644 tests/bson/test_field_variant.hpp create mode 100644 tests/bson/test_flag_enum.cpp create mode 100644 tests/bson/test_flag_enum.hpp create mode 100644 tests/bson/test_flag_enum_with_int.cpp create mode 100644 tests/bson/test_flag_enum_with_int.hpp create mode 100644 tests/bson/test_flatten.cpp create mode 100644 tests/bson/test_flatten.hpp create mode 100644 tests/bson/test_flatten_anonymous.cpp create mode 100644 tests/bson/test_flatten_anonymous.hpp create mode 100644 tests/bson/test_forward_list.cpp create mode 100644 tests/bson/test_forward_list.hpp create mode 100644 tests/bson/test_literal.cpp create mode 100644 tests/bson/test_literal.hpp create mode 100644 tests/bson/test_literal_map.cpp create mode 100644 tests/bson/test_literal_map.hpp create mode 100644 tests/bson/test_map.cpp create mode 100644 tests/bson/test_map.hpp create mode 100644 tests/bson/test_map_with_key_validation.cpp create mode 100644 tests/bson/test_map_with_key_validation.hpp create mode 100644 tests/bson/test_monster_example.cpp create mode 100644 tests/bson/test_monster_example.hpp create mode 100644 tests/bson/test_readme_example.cpp create mode 100644 tests/bson/test_readme_example.hpp create mode 100644 tests/bson/test_readme_example2.cpp create mode 100644 tests/bson/test_readme_example2.hpp create mode 100644 tests/bson/test_ref.cpp create mode 100644 tests/bson/test_ref.hpp create mode 100644 tests/bson/test_save_load.cpp create mode 100644 tests/bson/test_save_load.hpp create mode 100644 tests/bson/test_set.cpp create mode 100644 tests/bson/test_set.hpp create mode 100644 tests/bson/test_size.cpp create mode 100644 tests/bson/test_size.hpp create mode 100644 tests/bson/test_string_map.cpp create mode 100644 tests/bson/test_string_map.hpp create mode 100644 tests/bson/test_tagged_union.cpp create mode 100644 tests/bson/test_tagged_union.hpp create mode 100644 tests/bson/test_timestamp.cpp create mode 100644 tests/bson/test_timestamp.hpp create mode 100644 tests/bson/test_unique_ptr.cpp create mode 100644 tests/bson/test_unique_ptr.hpp create mode 100644 tests/bson/test_unique_ptr2.cpp create mode 100644 tests/bson/test_unique_ptr2.hpp create mode 100644 tests/bson/test_variant.cpp create mode 100644 tests/bson/test_variant.hpp create mode 100644 tests/bson/tests.cpp create mode 100644 tests/bson/write_and_read.hpp diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index e31d9fbb..4d468cfb 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -6,9 +6,11 @@ jobs: linux-gcc: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - name: Checkout + uses: actions/checkout@v3 with: - submodules: true + submodules: recursive + fetch-depth: 0 - name: Run vcpkg uses: lukka/run-vcpkg@v11 - name: install dependencies @@ -18,8 +20,9 @@ jobs: - name: Run test run: | g++ --version - cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release + cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release cmake --build build -j 4 + ./build/tests/bson/reflect-cpp-bson-tests ./build/tests/flexbuffers/reflect-cpp-flexbuffers-tests ./build/tests/json/reflect-cpp-json-tests ./build/tests/xml/reflect-cpp-xml-tests @@ -28,9 +31,11 @@ jobs: linux-clang: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - name: Checkout + uses: actions/checkout@v3 with: - submodules: true + submodules: recursive + fetch-depth: 0 - name: Run vcpkg uses: lukka/run-vcpkg@v11 - uses: KyleMayes/install-llvm-action@v1 @@ -47,8 +52,9 @@ jobs: CXX: clang++ run: | clang --version - cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release + cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release cmake --build build -j 4 + ./build/tests/bson/reflect-cpp-bson-tests ./build/tests/flexbuffers/reflect-cpp-flexbuffers-tests ./build/tests/json/reflect-cpp-json-tests ./build/tests/xml/reflect-cpp-xml-tests diff --git a/.gitignore b/.gitignore index e5c9e168..1a789efc 100644 --- a/.gitignore +++ b/.gitignore @@ -41,6 +41,7 @@ **/build_clang/** # Output files +*.bson *.json *.fb *.xml diff --git a/CMakeLists.txt b/CMakeLists.txt index b9d7c640..9b799590 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,7 @@ cmake_minimum_required(VERSION 3.15) option(REFLECTCPP_BUILD_SHARED "Build shared library" OFF) +option(REFLECTCPP_BSON "Enable BSON support" OFF) option(REFLECTCPP_FLEXBUFFERS "Enable flexbuffers support" OFF) option(REFLECTCPP_XML "Enable XML support" OFF) option(REFLECTCPP_YAML "Enable YAML support" OFF) @@ -8,7 +9,7 @@ option(REFLECTCPP_YAML "Enable YAML support" OFF) option(REFLECTCPP_BUILD_TESTS "Build tests" OFF) # enable vcpkg if require features other than JSON -if (REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_XML OR REFLECTCPP_YAML) +if (REFLECTCPP_BSON OR REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_XML OR REFLECTCPP_YAML) set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake CACHE STRING "Vcpkg toolchain file") endif () @@ -24,6 +25,11 @@ endif () target_include_directories(reflectcpp PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) +if (REFLECTCPP_BSON) + find_package(bson-1.0 CONFIG REQUIRED) + target_link_libraries(reflectcpp PRIVATE $,mongo::bson_static,mongo::bson_shared>) +endif () + if (REFLECTCPP_FLEXBUFFERS) find_package(flatbuffers CONFIG REQUIRED) target_link_libraries(reflectcpp INTERFACE flatbuffers::flatbuffers) diff --git a/README.md b/README.md index cbdd9763..cbc93407 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,8 @@ The following table lists the serialization formats currently supported by refle | Format | Library | Version | License | Remarks | |--------------|------------------------------------------------------|--------------|------------| -----------------------------------------------------| -| JSON | [YYJSON](https://github.com/ibireme/yyjson) | >= 0.8.0 | MIT | out-of-the-box support, included in this repository | +| JSON | [yyjson](https://github.com/ibireme/yyjson) | >= 0.8.0 | MIT | out-of-the-box support, included in this repository | +| BSON | [libbson](https://github.com/mongodb/libbson) | >= 1.25.1 | Apache 2.0 | | | flexbuffers | [flatbuffers](https://github.com/google/flatbuffers) | >= 23.5.26 | Apache 2.0 | | | XML | [pugixml](https://github.com/zeux/pugixml) | >= 1.14 | MIT | | | YAML | [yaml-cpp](https://github.com/jbeder/yaml-cpp) | >= 0.8.0 | MIT | | @@ -448,6 +449,7 @@ To use reflect-cpp in your project: ```cmake add_subdirectory(reflect-cpp) # Add this project as a subdirectory +set(REFLECTCPP_BSON ON) # Optional set(REFLECTCPP_FLEXBUFFERS ON) # Optional set(REFLECTCPP_XML ON) # Optional set(REFLECTCPP_YAML ON) # Optional @@ -494,7 +496,7 @@ git submodule update --init ./vcpkg/bootstrap-vcpkg.bat # Windows # You may be prompted to install additional dependencies. -cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release +cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release cmake --build build -j 4 # gcc, clang cmake --build build --config Release -j 4 # MSVC ``` diff --git a/docs/README.md b/docs/README.md index 96a40c68..59b524a1 100644 --- a/docs/README.md +++ b/docs/README.md @@ -56,11 +56,13 @@ 5.1) [JSON](https://github.com/getml/reflect-cpp/blob/main/docs/json.md) -5.2) [flexbuffers](https://github.com/getml/reflect-cpp/blob/main/docs/flexbuffers.md) +5.2) [BSON](https://github.com/getml/reflect-cpp/blob/main/docs/bson.md) -5.3) [XML](https://github.com/getml/reflect-cpp/blob/main/docs/xml.md) +5.3) [flexbuffers](https://github.com/getml/reflect-cpp/blob/main/docs/flexbuffers.md) -5.4) [YAML](https://github.com/getml/reflect-cpp/blob/main/docs/yaml.md) +5.4) [XML](https://github.com/getml/reflect-cpp/blob/main/docs/xml.md) + +5.5) [YAML](https://github.com/getml/reflect-cpp/blob/main/docs/yaml.md) ## 6) Advanced topics diff --git a/docs/bson.md b/docs/bson.md new file mode 100644 index 00000000..bd20bd36 --- /dev/null +++ b/docs/bson.md @@ -0,0 +1,100 @@ +# BSON + +For BSON support, you must also include the header `` and link to the libbson library (https://github.com/mongodb/libbson). + +Like the name implies, BSON is a JSON-like binary format. It is most notably used by MongoDB. + +## Reading and writing + +Suppose you have a struct like this: + +```cpp +struct Person { + std::string first_name; + std::string last_name; + rfl::Timestamp<"%Y-%m-%d"> birthday; + std::vector children; +}; +``` + +A `person` can be turned into a bytes vector like this: + +```cpp +const auto person = Person{...}; +const auto bytes = rfl::bson::write(person); +``` + +You can parse bytes like this: + +```cpp +const rfl::Result result = rfl::bson::read(bytes); +``` + +## Loading and saving + +You can also load and save to disc using a very similar syntax: + +```cpp +const rfl::Result result = rfl::bson::load("/path/to/file.bson"); + +const auto person = Person{...}; +rfl::bson::save("/path/to/file.bson", person); +``` + +## Reading from and writing into streams + +You can also read from and write into any `std::istream` and `std::ostream` respectively. + +```cpp +const rfl::Result result = rfl::bson::read(my_istream); + +const auto person = Person{...}; +rfl::bson::write(person, my_ostream); +``` + +Note that `std::cout` is also an ostream, so this works as well: + +```cpp +rfl::bson::write(person, std::cout) << std::endl; +``` + +(Since BSON is a binary format, the readability of this will be limited, but it might be useful for debugging). + +## Custom constructors + +One of the great things about C++ is that it gives you control over +when and how you code is compiled. + +For large and complex systems of structs, it is often a good idea to split up +your code into smaller compilation units. You can do so using custom constructors. + +For the BSON format, these must be a static function on your struct or class called +`from_bson` that take a `rfl::bson::Reader::InputVarType` as input and return +the class or the class wrapped in `rfl::Result`. + +In your header file you can write something like this: + +```cpp +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + rfl::Timestamp<"%Y-%m-%d"> birthday; + + using InputVarType = typename rfl::bson::Reader::InputVarType; + static rfl::Result from_bson(const InputVarType& _obj); +}; +``` + +And in your source file, you implement `from_bson` as follows: + +```cpp +rfl::Result Person::from_bson(const InputVarType& _obj) { + const auto from_nt = [](auto&& _nt) { + return rfl::from_named_tuple(std::move(_nt)); + }; + return rfl::bson::read>(_obj) + .transform(from_nt); +} +``` + +This will force the compiler to only compile the BSON parsing when the source file is compiled. diff --git a/include/rfl/bson.hpp b/include/rfl/bson.hpp new file mode 100644 index 00000000..f0b11d65 --- /dev/null +++ b/include/rfl/bson.hpp @@ -0,0 +1,12 @@ +#ifndef RFL_BSON_HPP_ +#define RFL_BSON_HPP_ + +#include "bson/Parser.hpp" +#include "bson/Reader.hpp" +#include "bson/Writer.hpp" +#include "bson/load.hpp" +#include "bson/read.hpp" +#include "bson/save.hpp" +#include "bson/write.hpp" + +#endif diff --git a/include/rfl/bson/Parser.hpp b/include/rfl/bson/Parser.hpp new file mode 100644 index 00000000..fc8deb31 --- /dev/null +++ b/include/rfl/bson/Parser.hpp @@ -0,0 +1,17 @@ +#ifndef RFL_BSON_PARSER_HPP_ +#define RFL_BSON_PARSER_HPP_ + +#include "../parsing/Parser.hpp" +#include "Reader.hpp" +#include "Writer.hpp" + +namespace rfl { +namespace bson { + +template +using Parser = parsing::Parser; + +} +} // namespace rfl + +#endif diff --git a/include/rfl/bson/Reader.hpp b/include/rfl/bson/Reader.hpp new file mode 100644 index 00000000..7be62c08 --- /dev/null +++ b/include/rfl/bson/Reader.hpp @@ -0,0 +1,228 @@ +#ifndef RFL_BSON_READER_HPP_ +#define RFL_BSON_READER_HPP_ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../Box.hpp" +#include "../Result.hpp" +#include "../always_false.hpp" + +namespace rfl { +namespace bson { + +/// Please refer to https://mongoc.org/libbson/current/api.html +struct Reader { + struct BSONValue { + bson_value_t val_; + }; + + struct BSONInputArray { + BSONValue* val_; + }; + + struct BSONInputObject { + BSONValue* val_; + }; + + struct BSONInputVar { + BSONValue* val_; + }; + + using InputArrayType = BSONInputArray; + using InputObjectType = BSONInputObject; + using InputVarType = BSONInputVar; + + template + static constexpr bool has_custom_constructor = (requires(InputVarType var) { + T::from_bson_obj(var); + }); + + rfl::Result get_field( + const std::string& _name, const InputObjectType& _obj) const noexcept { + bson_t b; + bson_iter_t iter; + const auto doc = _obj.val_->val_.value.v_doc; + if (bson_init_static(&b, doc.data, doc.data_len)) { + if (bson_iter_init(&iter, &b)) { + while (bson_iter_next(&iter)) { + auto key = std::string(bson_iter_key(&iter)); + if (key == _name) { + return to_input_var(&iter); + } + } + } + } + return Error("No field named '" + _name + "' was found."); + } + + bool is_empty(const InputVarType& _var) const noexcept { + return _var.val_->val_.value_type == BSON_TYPE_NULL; + } + + template + rfl::Result to_basic_type(const InputVarType& _var) const noexcept { + const auto btype = _var.val_->val_.value_type; + const auto value = _var.val_->val_.value; + if constexpr (std::is_same, std::string>()) { + switch (btype) { + case BSON_TYPE_UTF8: + return std::string(value.v_utf8.str, value.v_utf8.len); + + case BSON_TYPE_SYMBOL: + return std::string(value.v_symbol.symbol, value.v_symbol.len); + + default: + return rfl::Error( + "Could not cast to string. The type must be UTF8 or symbol."); + } + } else if constexpr (std::is_same, bool>()) { + if (btype != BSON_TYPE_BOOL) { + return rfl::Error("Could not cast to boolean."); + } + return value.v_bool; + } else if constexpr (std::is_floating_point>() || + std::is_integral>()) { + switch (btype) { + case BSON_TYPE_DOUBLE: + return static_cast(value.v_double); + + case BSON_TYPE_INT32: + return static_cast(value.v_int32); + + case BSON_TYPE_INT64: + return static_cast(value.v_int64); + + case BSON_TYPE_DATE_TIME: + return static_cast(value.v_datetime); + + default: + return rfl::Error( + "Could not cast to numeric value. The type must be double, " + "int32, int64 or date_time."); + } + } else { + static_assert(rfl::always_false_v, "Unsupported type."); + } + } + + rfl::Result to_array( + const InputVarType& _var) const noexcept { + const auto btype = _var.val_->val_.value_type; + if (btype != BSON_TYPE_ARRAY && btype != BSON_TYPE_DOCUMENT) { + return Error("Could not cast to an array."); + } + return InputArrayType{_var.val_}; + } + + template + std::array, size> to_fields_array( + const FunctionType& _fct, const InputObjectType& _obj) const noexcept { + std::array, size> f_arr; + bson_t b; + bson_iter_t iter; + const auto doc = _obj.val_->val_.value.v_doc; + if (bson_init_static(&b, doc.data, doc.data_len)) { + if (bson_iter_init(&iter, &b)) { + while (bson_iter_next(&iter)) { + const char* k = bson_iter_key(&iter); + const auto ix = _fct(std::string_view(k)); + if (ix != -1) { + f_arr[ix] = to_input_var(&iter); + } + } + } + } + return f_arr; + } + + rfl::Result>> to_map( + const InputObjectType& _obj) const noexcept { + std::vector> map; + bson_t b; + bson_iter_t iter; + const auto doc = _obj.val_->val_.value.v_doc; + if (bson_init_static(&b, doc.data, doc.data_len)) { + if (bson_iter_init(&iter, &b)) { + while (bson_iter_next(&iter)) { + auto key = std::string(bson_iter_key(&iter)); + map.emplace_back(std::make_pair(std::move(key), to_input_var(&iter))); + } + } + } + return map; + } + + rfl::Result to_object( + const InputVarType& _var) const noexcept { + const auto btype = _var.val_->val_.value_type; + if (btype != BSON_TYPE_DOCUMENT) { + return Error("Could not cast to a document."); + } + return InputObjectType{_var.val_}; + } + + std::vector to_vec(const InputArrayType& _arr) const noexcept { + std::vector vec; + bson_t b; + bson_iter_t iter; + const auto doc = _arr.val_->val_.value.v_doc; + if (bson_init_static(&b, doc.data, doc.data_len)) { + if (bson_iter_init(&iter, &b)) { + while (bson_iter_next(&iter)) { + vec.push_back(to_input_var(&iter)); + } + } + } + return vec; + } + + template + rfl::Result use_custom_constructor( + const InputVarType& _var) const noexcept { + try { + return T::from_bson_obj(_var); + } catch (std::exception& e) { + return rfl::Error(e.what()); + } + } + + private: + struct BSONValues { + std::vector> vec_; + ~BSONValues() { + for (auto& v : vec_) { + bson_value_destroy(&(v->val_)); + } + } + }; + + private: + InputVarType to_input_var(bson_iter_t* _iter) const noexcept { + values_->vec_.emplace_back(rfl::Box::make()); + auto* last_value = values_->vec_.back().get(); + bson_value_copy(bson_iter_value(_iter), &last_value->val_); + return InputVarType{last_value}; + } + + private: + /// Contains the values inside the object. + rfl::Ref values_; +}; + +} // namespace bson +} // namespace rfl + +#endif // JSON_PARSER_HPP_ diff --git a/include/rfl/bson/Writer.hpp b/include/rfl/bson/Writer.hpp new file mode 100644 index 00000000..92c78577 --- /dev/null +++ b/include/rfl/bson/Writer.hpp @@ -0,0 +1,221 @@ +#ifndef RFL_BSON_WRITER_HPP_ +#define RFL_BSON_WRITER_HPP_ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../Box.hpp" +#include "../Ref.hpp" +#include "../Result.hpp" +#include "../always_false.hpp" + +namespace rfl { +namespace bson { + +/// Please refer to https://mongoc.org/libbson/current/api.html +class Writer { + struct BSONType { + bson_t val_; + }; + + struct IsArray { + bson_array_builder_t* ptr_; + }; + + struct IsObject { + bson_t* ptr_; + }; + + struct IsRoot {}; + + using ParentType = std::variant; + + public: + struct BSONOutputArray { + BSONOutputArray(bson_array_builder_t* _val, ParentType _parent) + : parent_(_parent), val_(_val) {} + ParentType parent_; + bson_array_builder_t* val_; + }; + + struct BSONOutputObject { + BSONOutputObject(bson_t* _val, ParentType _parent) + : parent_(_parent), val_(_val) {} + ParentType parent_; + bson_t* val_; + }; + + struct BSONOutputVar {}; + + using OutputArrayType = BSONOutputArray; + using OutputObjectType = BSONOutputObject; + using OutputVarType = BSONOutputVar; + + Writer(bson_t* _doc) : doc_(_doc) {} + + ~Writer() = default; + + OutputArrayType array_as_root(const size_t _size) const noexcept { + bson_array_builder_t* val = bson_array_builder_new(); + return OutputArrayType(val, IsRoot{}); + } + + OutputObjectType object_as_root(const size_t _size) const noexcept { + return OutputObjectType(doc_, IsRoot{}); + } + + OutputVarType null_as_root() const noexcept { + // Appears to be unsupported by the BSON C API. + return OutputVarType{}; + } + + template + OutputVarType value_as_root(const T& _var) const noexcept { + static_assert(rfl::always_false_v, + "BSON only allows arrays or objects as its root."); + return OutputVarType{}; + } + + OutputArrayType add_array_to_array(const size_t _size, + OutputArrayType* _parent) const noexcept { + bson_array_builder_t* val; + bson_array_builder_append_array_builder_begin(_parent->val_, &val); + return OutputArrayType(val, IsArray{_parent->val_}); + } + + OutputArrayType add_array_to_object( + const std::string& _name, const size_t _size, + OutputObjectType* _parent) const noexcept { + bson_array_builder_t* val; + bson_append_array_builder_begin(_parent->val_, _name.c_str(), + static_cast(_name.size()), &val); + return OutputArrayType(val, IsObject{_parent->val_}); + } + + OutputObjectType add_object_to_array( + const size_t _size, OutputArrayType* _parent) const noexcept { + subdocs_->emplace_back(rfl::Box()); + bson_array_builder_append_document_begin(_parent->val_, + &(subdocs_->back()->val_)); + return OutputObjectType(&subdocs_->back()->val_, IsArray{_parent->val_}); + } + + OutputObjectType add_object_to_object( + const std::string& _name, const size_t _size, + OutputObjectType* _parent) const noexcept { + subdocs_->emplace_back(rfl::Box()); + bson_append_document_begin(_parent->val_, _name.c_str(), + static_cast(_name.size()), + &(subdocs_->back()->val_)); + return OutputObjectType(&subdocs_->back()->val_, IsObject{_parent->val_}); + } + + template + OutputVarType add_value_to_array(const T& _var, + OutputArrayType* _parent) const noexcept { + if constexpr (std::is_same, std::string>()) { + bson_array_builder_append_utf8(_parent->val_, _var.c_str(), + static_cast(_var.size())); + } else if constexpr (std::is_same, bool>()) { + bson_array_builder_append_bool(_parent->val_, _var); + } else if constexpr (std::is_floating_point>()) { + bson_array_builder_append_double(_parent->val_, + static_cast(_var)); + } else if constexpr (std::is_integral>()) { + bson_array_builder_append_int64(_parent->val_, + static_cast(_var)); + } else { + static_assert(rfl::always_false_v, "Unsupported type."); + } + return OutputVarType{}; + } + + template + OutputVarType add_value_to_object(const std::string& _name, const T& _var, + OutputObjectType* _parent) const noexcept { + if constexpr (std::is_same, std::string>()) { + bson_append_utf8(_parent->val_, _name.c_str(), + static_cast(_name.size()), _var.c_str(), + static_cast(_var.size())); + } else if constexpr (std::is_same, bool>()) { + bson_append_bool(_parent->val_, _name.c_str(), + static_cast(_name.size()), _var); + } else if constexpr (std::is_floating_point>()) { + bson_append_double(_parent->val_, _name.c_str(), + static_cast(_name.size()), + static_cast(_var)); + } else if constexpr (std::is_integral>()) { + bson_append_int64(_parent->val_, _name.c_str(), + static_cast(_name.size()), + static_cast(_var)); + } else { + static_assert(rfl::always_false_v, "Unsupported type."); + } + return OutputVarType{}; + } + + OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept { + bson_array_builder_append_null(_parent->val_); + return OutputVarType{}; + } + + OutputVarType add_null_to_object(const std::string& _name, + OutputObjectType* _parent) const noexcept { + bson_append_null(_parent->val_, _name.c_str(), + static_cast(_name.size())); + return OutputVarType{}; + } + + void end_array(OutputArrayType* _arr) const noexcept { + const auto handle = [&](const auto _parent) { + using Type = std::remove_cvref_t; + if constexpr (std::is_same()) { + bson_array_builder_append_array_builder_end(_parent.ptr_, _arr->val_); + } else if constexpr (std::is_same()) { + bson_append_array_builder_end(_parent.ptr_, _arr->val_); + } else if constexpr (std::is_same()) { + bson_array_builder_build(_arr->val_, doc_); + } else { + static_assert(rfl::always_false_v, "Unsupported type."); + } + }; + std::visit(handle, _arr->parent_); + } + + void end_object(OutputObjectType* _obj) const noexcept { + const auto handle = [&](const auto _parent) { + using Type = std::remove_cvref_t; + if constexpr (std::is_same()) { + bson_array_builder_append_document_end(_parent.ptr_, _obj->val_); + } else if constexpr (std::is_same()) { + bson_append_document_end(_parent.ptr_, _obj->val_); + } else if constexpr (std::is_same()) { + } else { + static_assert(rfl::always_false_v, "Unsupported type."); + } + }; + std::visit(handle, _obj->parent_); + } + + private: + /// Pointer to the main document. In BSON, documents are what are usually + /// called objects. + bson_t* const doc_; + + /// Contain all of the subdocuments. + const rfl::Ref>> subdocs_; +}; + +} // namespace bson +} // namespace rfl + +#endif // BSON_PARSER_HPP_ diff --git a/include/rfl/bson/load.hpp b/include/rfl/bson/load.hpp new file mode 100644 index 00000000..d13a390b --- /dev/null +++ b/include/rfl/bson/load.hpp @@ -0,0 +1,20 @@ +#ifndef RFL_BSON_LOAD_HPP_ +#define RFL_BSON_LOAD_HPP_ + +#include "../Result.hpp" +#include "../io/load_bytes.hpp" +#include "read.hpp" + +namespace rfl { +namespace bson { + +template +Result load(const std::string& _fname) { + const auto read_bytes = [](const auto& _bytes) { return read(_bytes); }; + return rfl::io::load_bytes(_fname).and_then(read_bytes); +} + +} // namespace bson +} // namespace rfl + +#endif diff --git a/include/rfl/bson/read.hpp b/include/rfl/bson/read.hpp new file mode 100644 index 00000000..efba5db1 --- /dev/null +++ b/include/rfl/bson/read.hpp @@ -0,0 +1,57 @@ +#ifndef RFL_BSON_READ_HPP_ +#define RFL_BSON_READ_HPP_ + +#include + +#include +#include + +#include "../internal/wrap_in_rfl_array_t.hpp" +#include "Parser.hpp" +#include "Reader.hpp" + +namespace rfl { +namespace bson { + +using InputObjectType = typename Reader::InputObjectType; +using InputVarType = typename Reader::InputVarType; + +/// Parses an object from a BSON var. +template +auto read(const InputVarType& _obj) { + const auto r = Reader(); + return Parser::read(r, _obj); +} + +/// Parses an object from flexbuf using reflection. +template +Result> read(char* _bytes, + const size_t _size) { + Reader::BSONValue value; + value.val_.value.v_doc.data_len = static_cast(_size); + value.val_.value.v_doc.data = reinterpret_cast(_bytes); + value.val_.value_type = BSON_TYPE_DOCUMENT; + auto doc = InputVarType{&value}; + auto result = read(doc); + return result; +} + +/// Parses an object from BSON using reflection. +template +auto read(const std::vector& _bytes) { + auto bytes = _bytes; + return read(bytes.data(), bytes.size()); +} + +/// Parses an object from a stream. +template +auto read(std::istream& _stream) { + std::istreambuf_iterator begin(_stream), end; + auto bytes = std::vector(begin, end); + return read(bytes.data(), bytes.size()); +} + +} // namespace bson +} // namespace rfl + +#endif diff --git a/include/rfl/bson/save.hpp b/include/rfl/bson/save.hpp new file mode 100644 index 00000000..a703bd01 --- /dev/null +++ b/include/rfl/bson/save.hpp @@ -0,0 +1,26 @@ +#ifndef RFL_BSON_SAVE_HPP_ +#define RFL_BSON_SAVE_HPP_ + +#include +#include +#include + +#include "../Result.hpp" +#include "../io/save_bytes.hpp" +#include "write.hpp" + +namespace rfl { +namespace bson { + +template +Result save(const std::string& _fname, const T& _obj) { + const auto write_func = [](const auto& _obj, auto& _stream) -> auto& { + return write(_obj, _stream); + }; + return rfl::io::save_bytes(_fname, _obj, write_func); +} + +} // namespace bson +} // namespace rfl + +#endif diff --git a/include/rfl/bson/write.hpp b/include/rfl/bson/write.hpp new file mode 100644 index 00000000..6bb82482 --- /dev/null +++ b/include/rfl/bson/write.hpp @@ -0,0 +1,58 @@ +#ifndef RFL_BSON_WRITE_HPP_ +#define RFL_BSON_WRITE_HPP_ + +#include + +#include +#include +#include +#include + +#include "../parsing/Parent.hpp" +#include "Parser.hpp" + +namespace rfl { +namespace bson { + +/// Returns BSON bytes. Careful: It is the responsibility of the caller to call +/// bson_free on the returned pointer. +template +std::pair to_buffer(const T& _obj) noexcept { + using ParentType = parsing::Parent; + bson_t* doc = nullptr; + uint8_t* buf = nullptr; + size_t buflen = 0; + bson_writer_t* bson_writer = + bson_writer_new(&buf, &buflen, 0, bson_realloc_ctx, NULL); + bson_writer_begin(bson_writer, &doc); + const auto rfl_writer = Writer(doc); + Parser::write(rfl_writer, _obj, typename ParentType::Root{}); + bson_writer_end(bson_writer); + const auto len = bson_writer_get_length(bson_writer); + bson_writer_destroy(bson_writer); + return std::make_pair(buf, len); +} + +/// Returns BSON bytes. +template +std::vector write(const T& _obj) noexcept { + auto [buf, len] = to_buffer(_obj); + const auto result = std::vector(reinterpret_cast(buf), + reinterpret_cast(buf) + len); + bson_free(buf); + return result; +} + +/// Writes a BSON into an ostream. +template +std::ostream& write(const T& _obj, std::ostream& _stream) noexcept { + auto [buf, len] = to_buffer(_obj); + _stream.write(reinterpret_cast(buf), len); + bson_free(buf); + return _stream; +} + +} // namespace bson +} // namespace rfl + +#endif // BSON_PARSER_HPP_ diff --git a/include/rfl/flexbuf.hpp b/include/rfl/flexbuf.hpp index fb493310..e8ad58a9 100644 --- a/include/rfl/flexbuf.hpp +++ b/include/rfl/flexbuf.hpp @@ -1,5 +1,5 @@ -#ifndef FLEXBUF_FLEXBUF_HPP_ -#define FLEXBUF_FLEXBUF_HPP_ +#ifndef RFL_FLEXBUF_HPP_ +#define RFL_FLEXBUF_HPP_ #include "flexbuf/Parser.hpp" #include "flexbuf/Reader.hpp" diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index cd8e0fb8..ff3edfbf 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -8,6 +8,10 @@ endif() add_subdirectory(json) +if (REFLECTCPP_BSON) + add_subdirectory(bson) +endif() + if (REFLECTCPP_FLEXBUFFERS) add_subdirectory(flexbuffers) endif() diff --git a/tests/bson/CMakeLists.txt b/tests/bson/CMakeLists.txt new file mode 100644 index 00000000..318ba7a0 --- /dev/null +++ b/tests/bson/CMakeLists.txt @@ -0,0 +1,8 @@ +project(reflect-cpp-bson-tests) + +file(GLOB_RECURSE SOURCES "*.cpp") + +add_executable(reflect-cpp-bson-tests ${SOURCES}) + +target_include_directories(reflect-cpp-bson-tests SYSTEM PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include") +target_link_libraries(reflect-cpp-bson-tests PRIVATE reflectcpp) diff --git a/tests/bson/test_array.cpp b/tests/bson/test_array.cpp new file mode 100644 index 00000000..970ffd11 --- /dev/null +++ b/tests/bson/test_array.cpp @@ -0,0 +1,40 @@ +#include "test_array.hpp" + +#include +#include +#include +#include +#include +#include + +// Make sure things still compile when +// rfl.hpp is included after rfl/bson.hpp. +#include + +#include "write_and_read.hpp" + +namespace test_array { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children = nullptr; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto bart = Person{.first_name = "Bart"}; + + auto lisa = Person{.first_name = "Lisa"}; + + auto maggie = Person{.first_name = "Maggie"}; + + const auto homer = Person{ + .first_name = "Homer", + .children = std::make_unique>(std::array{ + std::move(bart), std::move(lisa), std::move(maggie)})}; + + write_and_read(homer); +} +} // namespace test_array diff --git a/tests/bson/test_array.hpp b/tests/bson/test_array.hpp new file mode 100644 index 00000000..502c3388 --- /dev/null +++ b/tests/bson/test_array.hpp @@ -0,0 +1,4 @@ +namespace test_array{ + void test(); +} + \ No newline at end of file diff --git a/tests/bson/test_box.cpp b/tests/bson/test_box.cpp new file mode 100644 index 00000000..fdb3516a --- /dev/null +++ b/tests/bson/test_box.cpp @@ -0,0 +1,49 @@ +#include "test_box.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_box { + +struct DecisionTree { + struct Leaf { + using Tag = rfl::Literal<"Leaf">; + double value; + }; + + struct Node { + using Tag = rfl::Literal<"Node">; + rfl::Rename<"criticalValue", double> critical_value; + rfl::Box lesser; + rfl::Box greater; + }; + + using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; + + rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + 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); + +} +} // namespace test_box diff --git a/tests/bson/test_box.hpp b/tests/bson/test_box.hpp new file mode 100644 index 00000000..a564b9e1 --- /dev/null +++ b/tests/bson/test_box.hpp @@ -0,0 +1,4 @@ +namespace test_box{ + void test(); +} + \ No newline at end of file diff --git a/tests/bson/test_custom_class1.cpp b/tests/bson/test_custom_class1.cpp new file mode 100644 index 00000000..fe8d0ea0 --- /dev/null +++ b/tests/bson/test_custom_class1.cpp @@ -0,0 +1,41 @@ +#include "test_custom_class1.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_custom_class1 { + +struct Person { + struct PersonImpl { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::vector children; + }; + + using ReflectionType = PersonImpl; + + Person(const PersonImpl& _impl) : impl(_impl) {} + + Person(const std::string& _first_name) + : impl(PersonImpl{.first_name = _first_name}) {} + + const ReflectionType& reflection() const { return impl; }; + + private: + PersonImpl impl; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person("Bart"); + + write_and_read(bart); +} +} // namespace test_custom_class1 diff --git a/tests/bson/test_custom_class1.hpp b/tests/bson/test_custom_class1.hpp new file mode 100644 index 00000000..eafe6cd0 --- /dev/null +++ b/tests/bson/test_custom_class1.hpp @@ -0,0 +1,4 @@ +namespace test_custom_class1{ + void test(); +} + \ No newline at end of file diff --git a/tests/bson/test_custom_class3.cpp b/tests/bson/test_custom_class3.cpp new file mode 100644 index 00000000..598364b1 --- /dev/null +++ b/tests/bson/test_custom_class3.cpp @@ -0,0 +1,67 @@ +#include "test_custom_class3.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_custom_class3 { + +struct Person { + Person(const std::string& _first_name, const std::string& _last_name, + const int _age) + : first_name_(_first_name), last_name_(_last_name), age_(_age) {} + + const auto& first_name() const { return first_name_; } + + const auto& last_name() const { return last_name_; } + + auto age() const { return age_; } + + private: + std::string first_name_; + std::string last_name_; + int age_; +}; + +struct PersonImpl { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + int age; + + static PersonImpl from_class(const Person& _p) noexcept { + return PersonImpl{.first_name = _p.first_name(), + .last_name = _p.last_name(), + .age = _p.age()}; + } + + Person to_class() const { return Person(first_name(), last_name(), age); } +}; +} // namespace test_custom_class3 + +namespace rfl { +namespace parsing { + +template +struct Parser + : public CustomParser {}; + +} // namespace parsing +} // namespace rfl + +namespace test_custom_class3 { + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person("Bart", "Simpson", 10); + + write_and_read(bart); +} + +} // namespace test_custom_class3 diff --git a/tests/bson/test_custom_class3.hpp b/tests/bson/test_custom_class3.hpp new file mode 100644 index 00000000..9a6fdab4 --- /dev/null +++ b/tests/bson/test_custom_class3.hpp @@ -0,0 +1,4 @@ +namespace test_custom_class3{ + void test(); +} + \ No newline at end of file diff --git a/tests/bson/test_custom_class4.cpp b/tests/bson/test_custom_class4.cpp new file mode 100644 index 00000000..2b3b3bac --- /dev/null +++ b/tests/bson/test_custom_class4.cpp @@ -0,0 +1,68 @@ +#include "test_custom_class4.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_custom_class4 { + +struct Person { + Person(const std::string& _first_name, + const rfl::Box& _last_name, int _age) + : first_name_(_first_name), + last_name_(rfl::make_box(*_last_name)), + age_(_age) {} + + const auto& first_name() const { return first_name_; } + + const auto& last_name() const { return last_name_; } + + auto age() const { return age_; } + + private: + std::string first_name_; + rfl::Box last_name_; + int age_; +}; + +struct PersonImpl { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", rfl::Box> last_name; + rfl::Field<"age", int> age; + + static PersonImpl from_class(const Person& _p) noexcept { + return PersonImpl{.first_name = _p.first_name(), + .last_name = rfl::make_box(*_p.last_name()), + .age = _p.age()}; + } +}; + +} // namespace test_custom_class4 + +namespace rfl { +namespace parsing { + +template +struct Parser + : public CustomParser {}; + +} // namespace parsing +} // namespace rfl + +namespace test_custom_class4 { + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = test_custom_class4::Person( + "Bart", rfl::make_box("Simpson"), 10); + + write_and_read(bart); +} +} // namespace test_custom_class4 diff --git a/tests/bson/test_custom_class4.hpp b/tests/bson/test_custom_class4.hpp new file mode 100644 index 00000000..2d3b151a --- /dev/null +++ b/tests/bson/test_custom_class4.hpp @@ -0,0 +1,4 @@ +namespace test_custom_class4{ + void test(); +} + \ No newline at end of file diff --git a/tests/bson/test_default_values.cpp b/tests/bson/test_default_values.cpp new file mode 100644 index 00000000..f6af2352 --- /dev/null +++ b/tests/bson/test_default_values.cpp @@ -0,0 +1,32 @@ +#include "test_default_values.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_default_values { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::vector children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + 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({bart, lisa, maggie})}; + + write_and_read(homer); +} +} // namespace test_default_values diff --git a/tests/bson/test_default_values.hpp b/tests/bson/test_default_values.hpp new file mode 100644 index 00000000..c8f8360e --- /dev/null +++ b/tests/bson/test_default_values.hpp @@ -0,0 +1,4 @@ +namespace test_default_values{ + void test(); +} + \ No newline at end of file diff --git a/tests/bson/test_deque.cpp b/tests/bson/test_deque.cpp new file mode 100644 index 00000000..6dfa5803 --- /dev/null +++ b/tests/bson/test_deque.cpp @@ -0,0 +1,33 @@ +#include "test_deque.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_deque { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>(); + children->emplace_back(Person{.first_name = "Bart"}); + children->emplace_back(Person{.first_name = "Lisa"}); + children->emplace_back(Person{.first_name = "Maggie"}); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read(homer); + +} +} // namespace test_deque diff --git a/tests/bson/test_deque.hpp b/tests/bson/test_deque.hpp new file mode 100644 index 00000000..6781e880 --- /dev/null +++ b/tests/bson/test_deque.hpp @@ -0,0 +1,4 @@ +namespace test_deque{ + void test(); +} + \ No newline at end of file diff --git a/tests/bson/test_enum.cpp b/tests/bson/test_enum.cpp new file mode 100644 index 00000000..d4d56a8b --- /dev/null +++ b/tests/bson/test_enum.cpp @@ -0,0 +1,29 @@ +#include "test_enum.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_enum { + +enum class Color { red, green, blue, yellow }; + +struct Circle { + float radius; + Color color; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto circle = Circle{.radius = 2.0, .color = Color::green}; + + write_and_read(circle); +} + +} // namespace test_enum diff --git a/tests/bson/test_enum.hpp b/tests/bson/test_enum.hpp new file mode 100644 index 00000000..2e2e0b3d --- /dev/null +++ b/tests/bson/test_enum.hpp @@ -0,0 +1,4 @@ +namespace test_enum { +void test(); +} + diff --git a/tests/bson/test_field_variant.cpp b/tests/bson/test_field_variant.cpp new file mode 100644 index 00000000..8b17665f --- /dev/null +++ b/tests/bson/test_field_variant.cpp @@ -0,0 +1,39 @@ +#include "test_field_variant.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_field_variant { + +struct Circle { + double radius; +}; + +struct Rectangle { + double height; + double width; +}; + +struct Square { + double width; +}; + +using Shapes = rfl::Variant, + rfl::Field<"rectangle", Rectangle>, + rfl::Field<"square", rfl::Box>>; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const Shapes r = + rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5}); + + write_and_read(r); +} +} // namespace test_field_variant diff --git a/tests/bson/test_field_variant.hpp b/tests/bson/test_field_variant.hpp new file mode 100644 index 00000000..ba93e732 --- /dev/null +++ b/tests/bson/test_field_variant.hpp @@ -0,0 +1,4 @@ +namespace test_field_variant{ + void test(); +} + \ No newline at end of file diff --git a/tests/bson/test_flag_enum.cpp b/tests/bson/test_flag_enum.cpp new file mode 100644 index 00000000..1d3f76af --- /dev/null +++ b/tests/bson/test_flag_enum.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include + +#include "test_enum.hpp" +#include "write_and_read.hpp" + +namespace test_flag_enum { + +enum class Color { + red = 256, + green = 512, + blue = 1024, + yellow = 2048, + orange = (256 | 2048) // red + yellow = orange +}; + +inline Color operator|(Color c1, Color c2) { + return static_cast(static_cast(c1) | static_cast(c2)); +} + +struct Circle { + float radius; + Color color; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto circle = + Circle{.radius = 2.0, .color = Color::blue | Color::orange}; + + write_and_read(circle); +} + +} // namespace test_flag_enum diff --git a/tests/bson/test_flag_enum.hpp b/tests/bson/test_flag_enum.hpp new file mode 100644 index 00000000..2f4dc7a0 --- /dev/null +++ b/tests/bson/test_flag_enum.hpp @@ -0,0 +1,4 @@ +namespace test_flag_enum { +void test(); +} + diff --git a/tests/bson/test_flag_enum_with_int.cpp b/tests/bson/test_flag_enum_with_int.cpp new file mode 100644 index 00000000..4c7deefe --- /dev/null +++ b/tests/bson/test_flag_enum_with_int.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +#include + +#include "test_enum.hpp" +#include "write_and_read.hpp" + +namespace test_flag_enum_with_int { + +enum class Color { + red = 256, + green = 512, + blue = 1024, + yellow = 2048, + orange = (256 | 2048) // red + yellow = orange +}; + +inline Color operator|(Color c1, Color c2) { + return static_cast(static_cast(c1) | static_cast(c2)); +} + +struct Circle { + float radius; + Color color; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto circle = Circle{.radius = 2.0, .color = static_cast(10000)}; + + write_and_read(circle); +} + +} // namespace test_flag_enum_with_int diff --git a/tests/bson/test_flag_enum_with_int.hpp b/tests/bson/test_flag_enum_with_int.hpp new file mode 100644 index 00000000..a7512b60 --- /dev/null +++ b/tests/bson/test_flag_enum_with_int.hpp @@ -0,0 +1,4 @@ +namespace test_flag_enum_with_int { +void test(); +} + diff --git a/tests/bson/test_flatten.cpp b/tests/bson/test_flatten.cpp new file mode 100644 index 00000000..c7f1d9be --- /dev/null +++ b/tests/bson/test_flatten.cpp @@ -0,0 +1,38 @@ +#include "test_flatten.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_flatten { + +struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", rfl::Box> last_name; + rfl::Field<"age", int> age; +}; + +struct Employee { + rfl::Flatten person; + rfl::Field<"employer", rfl::Box> employer; + rfl::Field<"salary", float> salary; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto employee = Employee{ + .person = Person{.first_name = "Homer", + .last_name = rfl::make_box("Simpson"), + .age = 45}, + .employer = rfl::make_box("Mr. Burns"), + .salary = 60000.0}; + + write_and_read(employee); +} +} // namespace test_flatten diff --git a/tests/bson/test_flatten.hpp b/tests/bson/test_flatten.hpp new file mode 100644 index 00000000..24d60e11 --- /dev/null +++ b/tests/bson/test_flatten.hpp @@ -0,0 +1,4 @@ +namespace test_flatten{ + void test(); +} + \ No newline at end of file diff --git a/tests/bson/test_flatten_anonymous.cpp b/tests/bson/test_flatten_anonymous.cpp new file mode 100644 index 00000000..05e61464 --- /dev/null +++ b/tests/bson/test_flatten_anonymous.cpp @@ -0,0 +1,39 @@ +#include "test_flatten_anonymous.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_flatten_anonymous { + +struct Person { + std::string first_name; + rfl::Box last_name; + int age; +}; + +struct Employee { + rfl::Flatten person; + rfl::Box employer; + float salary; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto employee = Employee{ + .person = Person{.first_name = "Homer", + .last_name = rfl::make_box("Simpson"), + .age = 45}, + .employer = rfl::make_box("Mr. Burns"), + .salary = 60000.0}; + + write_and_read(employee); +} + +} // namespace test_flatten_anonymous diff --git a/tests/bson/test_flatten_anonymous.hpp b/tests/bson/test_flatten_anonymous.hpp new file mode 100644 index 00000000..7ffa2785 --- /dev/null +++ b/tests/bson/test_flatten_anonymous.hpp @@ -0,0 +1,4 @@ +namespace test_flatten_anonymous{ + void test(); +} + \ No newline at end of file diff --git a/tests/bson/test_forward_list.cpp b/tests/bson/test_forward_list.cpp new file mode 100644 index 00000000..dd8d9e56 --- /dev/null +++ b/tests/bson/test_forward_list.cpp @@ -0,0 +1,33 @@ +#include "test_forward_list.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_forward_list { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>(); + children->emplace_front(Person{.first_name = "Maggie"}); + children->emplace_front(Person{.first_name = "Lisa"}); + children->emplace_front(Person{.first_name = "Bart"}); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read(homer); + +} +} // namespace test_forward_list diff --git a/tests/bson/test_forward_list.hpp b/tests/bson/test_forward_list.hpp new file mode 100644 index 00000000..9784a0c4 --- /dev/null +++ b/tests/bson/test_forward_list.hpp @@ -0,0 +1,4 @@ +namespace test_forward_list{ + void test(); +} + \ No newline at end of file diff --git a/tests/bson/test_literal.cpp b/tests/bson/test_literal.cpp new file mode 100644 index 00000000..00578178 --- /dev/null +++ b/tests/bson/test_literal.cpp @@ -0,0 +1,30 @@ +#include "test_literal.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_literal { + +using FirstName = rfl::Literal<"Homer", "Marge", "Bart", "Lisa", "Maggie">; +using LastName = rfl::Literal<"Simpson">; + +struct Person { + rfl::Rename<"firstName", FirstName> first_name; + rfl::Rename<"lastName", LastName> last_name; + std::vector children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{.first_name = FirstName::make<"Bart">()}; + + write_and_read(bart); +} +} // namespace test_literal diff --git a/tests/bson/test_literal.hpp b/tests/bson/test_literal.hpp new file mode 100644 index 00000000..ccd500ef --- /dev/null +++ b/tests/bson/test_literal.hpp @@ -0,0 +1,4 @@ +namespace test_literal{ + void test(); +} + \ No newline at end of file diff --git a/tests/bson/test_literal_map.cpp b/tests/bson/test_literal_map.cpp new file mode 100644 index 00000000..9bbebafb --- /dev/null +++ b/tests/bson/test_literal_map.cpp @@ -0,0 +1,28 @@ +#include "test_literal_map.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_literal_map { + +using FieldName = rfl::Literal<"firstName", "lastName">; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + std::map> homer; + homer.insert(std::make_pair(FieldName::make<"firstName">(), + std::make_unique("Homer"))); + homer.insert(std::make_pair(FieldName::make<"lastName">(), + std::make_unique("Simpson"))); + + write_and_read(homer); +} +} // namespace test_literal_map diff --git a/tests/bson/test_literal_map.hpp b/tests/bson/test_literal_map.hpp new file mode 100644 index 00000000..cc05d0c8 --- /dev/null +++ b/tests/bson/test_literal_map.hpp @@ -0,0 +1,4 @@ +namespace test_literal_map{ + void test(); +} + \ No newline at end of file diff --git a/tests/bson/test_map.cpp b/tests/bson/test_map.cpp new file mode 100644 index 00000000..e37ce564 --- /dev/null +++ b/tests/bson/test_map.cpp @@ -0,0 +1,32 @@ +#include "test_map.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_map { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::map children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::map(); + children.insert(std::make_pair("child1", Person{.first_name = "Bart"})); + children.insert(std::make_pair("child2", Person{.first_name = "Lisa"})); + children.insert(std::make_pair("child3", Person{.first_name = "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read(homer); +} +} // namespace test_map diff --git a/tests/bson/test_map.hpp b/tests/bson/test_map.hpp new file mode 100644 index 00000000..9ae49728 --- /dev/null +++ b/tests/bson/test_map.hpp @@ -0,0 +1,4 @@ +namespace test_map{ + void test(); +} + \ No newline at end of file diff --git a/tests/bson/test_map_with_key_validation.cpp b/tests/bson/test_map_with_key_validation.cpp new file mode 100644 index 00000000..e758219d --- /dev/null +++ b/tests/bson/test_map_with_key_validation.cpp @@ -0,0 +1,33 @@ +#include "test_map_with_key_validation.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_map_with_key_validation { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>(); + + children->insert(std::make_pair("Bart", Person{.first_name = "Bart"})); + children->insert(std::make_pair("Lisa", Person{.first_name = "Lisa"})); + children->insert(std::make_pair("Maggie", Person{.first_name = "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read(homer); +} +} // namespace test_map_with_key_validation diff --git a/tests/bson/test_map_with_key_validation.hpp b/tests/bson/test_map_with_key_validation.hpp new file mode 100644 index 00000000..1372f926 --- /dev/null +++ b/tests/bson/test_map_with_key_validation.hpp @@ -0,0 +1,4 @@ +namespace test_map_with_key_validation{ + void test(); +} + \ No newline at end of file diff --git a/tests/bson/test_monster_example.cpp b/tests/bson/test_monster_example.cpp new file mode 100644 index 00000000..052d8390 --- /dev/null +++ b/tests/bson/test_monster_example.cpp @@ -0,0 +1,64 @@ +#include "test_monster_example.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_monster_example { + +using Color = rfl::Literal<"Red", "Green", "Blue">; + +struct Weapon { + std::string name; + short damage; +}; + +using Equipment = rfl::Variant>; + +struct Vec3 { + float x; + float y; + float z; +}; + +struct Monster { + Vec3 pos; + short mana = 150; + short hp = 100; + std::string name; + bool friendly = false; + std::vector inventory; + Color color = Color::make<"Blue">(); + std::vector weapons; + Equipment equipped; + std::vector path; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto sword = Weapon{.name = "Sword", .damage = 3}; + const auto axe = Weapon{.name = "Axe", .damage = 5}; + + const auto weapons = std::vector({sword, axe}); + + const auto position = Vec3{1.0f, 2.0f, 3.0f}; + + const auto inventory = std::vector({0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); + + const auto orc = Monster{.pos = position, + .mana = 150, + .hp = 80, + .name = "MyMonster", + .inventory = inventory, + .color = Color::make<"Red">(), + .weapons = weapons, + .equipped = rfl::make_field<"weapon">(axe)}; + + write_and_read(orc); +} +} // namespace test_monster_example diff --git a/tests/bson/test_monster_example.hpp b/tests/bson/test_monster_example.hpp new file mode 100644 index 00000000..f2d959fc --- /dev/null +++ b/tests/bson/test_monster_example.hpp @@ -0,0 +1,4 @@ +namespace test_monster_example{ + void test(); +} + \ No newline at end of file diff --git a/tests/bson/test_readme_example.cpp b/tests/bson/test_readme_example.cpp new file mode 100644 index 00000000..4e6f8ee0 --- /dev/null +++ b/tests/bson/test_readme_example.cpp @@ -0,0 +1,51 @@ +#include "test_readme_example.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_readme_example { + +using Age = rfl::Validator, rfl::Maximum<130>>; + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::string town = "Springfield"; + rfl::Timestamp<"%Y-%m-%d"> birthday; + Age age; + rfl::Email email; + std::vector child; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{.first_name = "Bart", + .birthday = "1987-04-19", + .age = 10, + .email = "bart@simpson.com"}; + + const auto lisa = Person{.first_name = "Lisa", + .birthday = "1987-04-19", + .age = 8, + .email = "lisa@simpson.com"}; + + const auto maggie = Person{.first_name = "Maggie", + .birthday = "1987-04-19", + .age = 0, + .email = "maggie@simpson.com"}; + + const auto homer = Person{.first_name = "Homer", + .birthday = "1987-04-19", + .age = 45, + .email = "homer@simpson.com", + .child = std::vector({bart, lisa, maggie})}; + + write_and_read(homer); +} +} // namespace test_readme_example diff --git a/tests/bson/test_readme_example.hpp b/tests/bson/test_readme_example.hpp new file mode 100644 index 00000000..68c6cf81 --- /dev/null +++ b/tests/bson/test_readme_example.hpp @@ -0,0 +1,4 @@ +namespace test_readme_example{ + void test(); +} + \ No newline at end of file diff --git a/tests/bson/test_readme_example2.cpp b/tests/bson/test_readme_example2.cpp new file mode 100644 index 00000000..a2d212a4 --- /dev/null +++ b/tests/bson/test_readme_example2.cpp @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include + +#include "test_readme_example.hpp" +#include "write_and_read.hpp" + +namespace test_readme_example2 { + +struct Person { + std::string first_name; + std::string last_name; + int age; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto homer = + Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}; + + write_and_read(homer); +} +} // namespace test_readme_example2 diff --git a/tests/bson/test_readme_example2.hpp b/tests/bson/test_readme_example2.hpp new file mode 100644 index 00000000..5c6b011c --- /dev/null +++ b/tests/bson/test_readme_example2.hpp @@ -0,0 +1,4 @@ +namespace test_readme_example2 { +void test(); +} + diff --git a/tests/bson/test_ref.cpp b/tests/bson/test_ref.cpp new file mode 100644 index 00000000..32a32e6c --- /dev/null +++ b/tests/bson/test_ref.cpp @@ -0,0 +1,49 @@ +#include "test_ref.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_ref { + +struct DecisionTree { + struct Leaf { + using Tag = rfl::Literal<"Leaf">; + double value; + }; + + struct Node { + using Tag = rfl::Literal<"Node">; + rfl::Rename<"criticalValue", double> critical_value; + rfl::Ref lesser; + rfl::Ref greater; + }; + + using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; + + rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + 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); + +} +} // namespace test_ref diff --git a/tests/bson/test_ref.hpp b/tests/bson/test_ref.hpp new file mode 100644 index 00000000..d289ba09 --- /dev/null +++ b/tests/bson/test_ref.hpp @@ -0,0 +1,4 @@ +namespace test_ref{ + void test(); +} + \ No newline at end of file diff --git a/tests/bson/test_save_load.cpp b/tests/bson/test_save_load.cpp new file mode 100644 index 00000000..d51c34c8 --- /dev/null +++ b/tests/bson/test_save_load.cpp @@ -0,0 +1,70 @@ +#include "test_save_load.hpp" + +#include +#include +#include +#include +#include +#include +#include + +namespace test_save_load { + +using Age = rfl::Validator, rfl::Maximum<130>>>; + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + rfl::Timestamp<"%Y-%m-%d"> birthday; + Age age; + rfl::Email email; + std::vector children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{.first_name = "Bart", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 10, + .email = "bart@simpson.com", + .children = std::vector()}; + + const auto lisa = Person{.first_name = "Lisa", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 8, + .email = "lisa@simpson.com"}; + + const auto maggie = Person{.first_name = "Maggie", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 0, + .email = "maggie@simpson.com"}; + + const auto homer1 = + Person{.first_name = "Homer", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 45, + .email = "homer@simpson.com", + .children = std::vector({bart, lisa, maggie})}; + + rfl::bson::save("homer.bson", homer1); + + const auto homer2 = rfl::bson::load("homer.bson").value(); + + const auto string1 = rfl::bson::write(homer1); + const auto string2 = rfl::bson::write(homer2); + + if (string1 != string2) { + std::cout << "Test failed. Content was not identical." << std::endl + << std::endl; + return; + } + + std::cout << "OK" << std::endl << std::endl; +} +} // namespace test_save_load diff --git a/tests/bson/test_save_load.hpp b/tests/bson/test_save_load.hpp new file mode 100644 index 00000000..7bf10359 --- /dev/null +++ b/tests/bson/test_save_load.hpp @@ -0,0 +1,4 @@ +namespace test_save_load { +void test(); +} + diff --git a/tests/bson/test_set.cpp b/tests/bson/test_set.cpp new file mode 100644 index 00000000..9f2516ce --- /dev/null +++ b/tests/bson/test_set.cpp @@ -0,0 +1,30 @@ +#include "test_set.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_set { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>( + std::set({"Bart", "Lisa", "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read(homer); +} +} // namespace test_set diff --git a/tests/bson/test_set.hpp b/tests/bson/test_set.hpp new file mode 100644 index 00000000..142a663b --- /dev/null +++ b/tests/bson/test_set.hpp @@ -0,0 +1,4 @@ +namespace test_set{ + void test(); +} + \ No newline at end of file diff --git a/tests/bson/test_size.cpp b/tests/bson/test_size.cpp new file mode 100644 index 00000000..c37b9bd8 --- /dev/null +++ b/tests/bson/test_size.cpp @@ -0,0 +1,42 @@ +#include "test_size.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_size { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + rfl::Timestamp<"%Y-%m-%d"> birthday; + rfl::Validator, + rfl::Size, rfl::EqualTo<3>>>> + children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{ + .first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19"}; + + const auto lisa = Person{ + .first_name = "Lisa", .last_name = "Simpson", .birthday = "1987-04-19"}; + + const auto maggie = Person{ + .first_name = "Maggie", .last_name = "Simpson", .birthday = "1987-04-19"}; + + const auto homer = + Person{.first_name = "Homer", + .last_name = "Simpson", + .birthday = "1987-04-19", + .children = std::vector({bart, lisa, maggie})}; + + write_and_read(homer); +} +} // namespace test_size diff --git a/tests/bson/test_size.hpp b/tests/bson/test_size.hpp new file mode 100644 index 00000000..be330df0 --- /dev/null +++ b/tests/bson/test_size.hpp @@ -0,0 +1,4 @@ +namespace test_size{ + void test(); +} + \ No newline at end of file diff --git a/tests/bson/test_string_map.cpp b/tests/bson/test_string_map.cpp new file mode 100644 index 00000000..cb4fb903 --- /dev/null +++ b/tests/bson/test_string_map.cpp @@ -0,0 +1,24 @@ +#include "test_string_map.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_string_map { +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + std::map> homer; + homer.insert( + std::make_pair("firstName", std::make_unique("Homer"))); + homer.insert( + std::make_pair("lastName", std::make_unique("Simpson"))); + + write_and_read(homer); +} +} // namespace test_string_map diff --git a/tests/bson/test_string_map.hpp b/tests/bson/test_string_map.hpp new file mode 100644 index 00000000..94cb975a --- /dev/null +++ b/tests/bson/test_string_map.hpp @@ -0,0 +1,4 @@ +namespace test_string_map{ + void test(); +} + \ No newline at end of file diff --git a/tests/bson/test_tagged_union.cpp b/tests/bson/test_tagged_union.cpp new file mode 100644 index 00000000..a9dd1abc --- /dev/null +++ b/tests/bson/test_tagged_union.cpp @@ -0,0 +1,36 @@ +#include "test_tagged_union.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_tagged_union { + +struct Circle { + double radius; +}; + +struct Rectangle { + double height; + double width; +}; + +struct Square { + double width; +}; + +using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const Shapes r = Rectangle{.height = 10, .width = 5}; + + write_and_read(r); +} +} // namespace test_tagged_union diff --git a/tests/bson/test_tagged_union.hpp b/tests/bson/test_tagged_union.hpp new file mode 100644 index 00000000..5d522ff9 --- /dev/null +++ b/tests/bson/test_tagged_union.hpp @@ -0,0 +1,4 @@ +namespace test_tagged_union{ + void test(); +} + \ No newline at end of file diff --git a/tests/bson/test_timestamp.cpp b/tests/bson/test_timestamp.cpp new file mode 100644 index 00000000..8af261d1 --- /dev/null +++ b/tests/bson/test_timestamp.cpp @@ -0,0 +1,36 @@ +#include "test_timestamp.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_timestamp { + +using TS = rfl::Timestamp<"%Y-%m-%d">; + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + TS birthday; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto result = TS::from_string("nonsense"); + + if (result) { + std::cout << "Failed: Expected an error, but got none." << std::endl; + return; + } + + const auto bart = Person{.first_name = "Bart", .birthday = "1987-04-19"}; + + write_and_read(bart); +} +} // namespace test_timestamp diff --git a/tests/bson/test_timestamp.hpp b/tests/bson/test_timestamp.hpp new file mode 100644 index 00000000..891d89b9 --- /dev/null +++ b/tests/bson/test_timestamp.hpp @@ -0,0 +1,4 @@ +namespace test_timestamp{ + void test(); +} + \ No newline at end of file diff --git a/tests/bson/test_unique_ptr.cpp b/tests/bson/test_unique_ptr.cpp new file mode 100644 index 00000000..b1833533 --- /dev/null +++ b/tests/bson/test_unique_ptr.cpp @@ -0,0 +1,33 @@ +#include "test_unique_ptr.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_unique_ptr { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>(); + children->emplace_back(Person{.first_name = "Bart"}); + children->emplace_back(Person{.first_name = "Lisa"}); + children->emplace_back(Person{.first_name = "Maggie"}); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read(homer); +} +} // namespace test_unique_ptr diff --git a/tests/bson/test_unique_ptr.hpp b/tests/bson/test_unique_ptr.hpp new file mode 100644 index 00000000..428ea2a2 --- /dev/null +++ b/tests/bson/test_unique_ptr.hpp @@ -0,0 +1,4 @@ +namespace test_unique_ptr{ + void test(); +} + \ No newline at end of file diff --git a/tests/bson/test_unique_ptr2.cpp b/tests/bson/test_unique_ptr2.cpp new file mode 100644 index 00000000..e3007267 --- /dev/null +++ b/tests/bson/test_unique_ptr2.cpp @@ -0,0 +1,48 @@ +#include "test_unique_ptr2.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_unique_ptr2 { + +struct DecisionTree { + struct Leaf { + using Tag = rfl::Literal<"Leaf">; + double value; + }; + + struct Node { + using Tag = rfl::Literal<"Node">; + rfl::Rename<"criticalValue", double> critical_value; + std::unique_ptr lesser; + std::unique_ptr greater; + }; + + using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; + + rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto leaf1 = DecisionTree::Leaf{.value = 3.0}; + + auto leaf2 = DecisionTree::Leaf{.value = 5.0}; + + auto node = DecisionTree::Node{ + .critical_value = 10.0, + .lesser = std::make_unique(DecisionTree{leaf1}), + .greater = std::make_unique(DecisionTree{leaf2})}; + + const DecisionTree tree{.leaf_or_node = std::move(node)}; + + write_and_read(tree); +} +} // namespace test_unique_ptr2 diff --git a/tests/bson/test_unique_ptr2.hpp b/tests/bson/test_unique_ptr2.hpp new file mode 100644 index 00000000..74adc170 --- /dev/null +++ b/tests/bson/test_unique_ptr2.hpp @@ -0,0 +1,4 @@ +namespace test_unique_ptr2{ + void test(); +} + \ No newline at end of file diff --git a/tests/bson/test_variant.cpp b/tests/bson/test_variant.cpp new file mode 100644 index 00000000..cc3d6cbe --- /dev/null +++ b/tests/bson/test_variant.cpp @@ -0,0 +1,36 @@ +#include "test_variant.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_variant { + +struct Circle { + double radius; +}; + +struct Rectangle { + double height; + double width; +}; + +struct Square { + double width; +}; + +using Shapes = std::variant>; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const Shapes r = Rectangle{.height = 10, .width = 5}; + + write_and_read(r); +} +} // namespace test_variant diff --git a/tests/bson/test_variant.hpp b/tests/bson/test_variant.hpp new file mode 100644 index 00000000..0e58ce71 --- /dev/null +++ b/tests/bson/test_variant.hpp @@ -0,0 +1,4 @@ +namespace test_variant{ + void test(); +} + \ No newline at end of file diff --git a/tests/bson/tests.cpp b/tests/bson/tests.cpp new file mode 100644 index 00000000..07067f88 --- /dev/null +++ b/tests/bson/tests.cpp @@ -0,0 +1,65 @@ +#include "test_array.hpp" +#include "test_box.hpp" +#include "test_custom_class1.hpp" +#include "test_custom_class3.hpp" +#include "test_custom_class4.hpp" +#include "test_default_values.hpp" +#include "test_deque.hpp" +#include "test_enum.hpp" +#include "test_field_variant.hpp" +#include "test_flag_enum.hpp" +#include "test_flag_enum_with_int.hpp" +#include "test_flatten.hpp" +#include "test_flatten_anonymous.hpp" +#include "test_forward_list.hpp" +#include "test_literal.hpp" +#include "test_literal_map.hpp" +#include "test_map.hpp" +#include "test_map_with_key_validation.hpp" +#include "test_monster_example.hpp" +#include "test_readme_example.hpp" +#include "test_readme_example2.hpp" +#include "test_ref.hpp" +#include "test_save_load.hpp" +#include "test_set.hpp" +#include "test_size.hpp" +#include "test_tagged_union.hpp" +#include "test_timestamp.hpp" +#include "test_unique_ptr.hpp" +#include "test_unique_ptr2.hpp" +#include "test_variant.hpp" + +int main() { + test_readme_example::test(); + test_readme_example2::test(); + test_flatten::test(); + test_flatten_anonymous::test(); + test_enum::test(); + test_flag_enum::test(); + test_flag_enum_with_int::test(); + test_map::test(); + test_map_with_key_validation::test(); + test_variant::test(); + test_field_variant::test(); + test_tagged_union::test(); + test_deque::test(); + test_forward_list::test(); + test_literal_map::test(); + test_literal::test(); + test_monster_example::test(); + test_ref::test(); + test_set::test(); + test_size::test(); + test_timestamp::test(); + test_unique_ptr::test(); + test_unique_ptr2::test(); + test_array::test(); + test_box::test(); + test_custom_class1::test(); + test_custom_class3::test(); + test_custom_class4::test(); + test_default_values::test(); + test_save_load::test(); + + return 0; +} diff --git a/tests/bson/write_and_read.hpp b/tests/bson/write_and_read.hpp new file mode 100644 index 00000000..f009be61 --- /dev/null +++ b/tests/bson/write_and_read.hpp @@ -0,0 +1,39 @@ +#ifndef WRITE_AND_READ_ +#define WRITE_AND_READ_ + +#include +#include +#include + +template +void write_and_read(const T& _struct) { + const auto bytes1 = rfl::bson::write(_struct); + + const auto res = rfl::bson::read(bytes1); + + if (!res) { + std::cout << "Test failed on read. Error: " << res.error().value().what() + << std::endl + << std::endl; + return; + } + + const auto bytes2 = rfl::bson::write(res.value()); + + if (bytes1.size() != bytes2.size()) { + std::cout << "Test failed on write. Number of bytes was different." + << std::endl + << std::endl; + return; + } + + if (bytes1 != bytes2) { + std::cout << "Test failed on write. Content was not identical." << std::endl + << std::endl; + return; + } + + std::cout << "OK" << std::endl << std::endl; +} + +#endif diff --git a/vcpkg.json b/vcpkg.json index cd003f0e..db73fc39 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -7,6 +7,10 @@ "name": "flatbuffers", "version>=": "23.5.26#1" }, + { + "name": "libbson", + "version>=": "1.25.1" + }, { "name": "pugixml", "version>=": "1.14"